qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH v6 37/36] qapi: Support (subset of) \u escapes i


From: Markus Armbruster
Subject: Re: [Qemu-devel] [PATCH v6 37/36] qapi: Support (subset of) \u escapes in strings
Date: Tue, 28 Apr 2015 15:23:35 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Eric Blake <address@hidden> writes:

> The handling of \ inside QAPI strings was less than ideal, and
> really only worked JSON's \/, \\, \", and our extension of \'
> (an obvious extension, when you realize we use '' instead of ""
> for strings).  For other things, like '\n', it resulted in a
> literal 'n' instead of a newline.
>
> Of course, at the moment, we really have no use for escaped
> characters, as QAPI has to map to C identifiers, and we currently
> support ASCII only for that.  But down the road, we may add
> support for default values for string parameters to a command
> or struct; if that happens, it would be nice to correctly support
> all JSON escape sequences, such as \n or \uXXXX.  This gets us
> closer, by supporting Unicode escapes in the ASCII range.
>
> Since JSON does not require \OCTAL or \xXX escapes, I did not
> add it here, but it would be an easy addition if we desired it.
>
> Signed-off-by: Eric Blake <address@hidden>
> ---
>
> v6: new patch
>
> ---
>  scripts/qapi.py                          | 33 
> +++++++++++++++++++++++++++++++-
>  tests/Makefile                           |  1 +
>  tests/qapi-schema/escape-too-big.err     |  1 +
>  tests/qapi-schema/escape-too-big.exit    |  1 +
>  tests/qapi-schema/escape-too-big.json    |  3 +++
>  tests/qapi-schema/escape-too-big.out     |  0
>  tests/qapi-schema/escape-too-short.err   |  1 +
>  tests/qapi-schema/escape-too-short.exit  |  1 +
>  tests/qapi-schema/escape-too-short.json  |  3 +++
>  tests/qapi-schema/escape-too-short.out   |  0
>  tests/qapi-schema/ident-with-escape.err  |  1 -
>  tests/qapi-schema/ident-with-escape.exit |  2 +-
>  tests/qapi-schema/ident-with-escape.json |  2 +-
>  tests/qapi-schema/ident-with-escape.out  |  3 +++
>  tests/qapi-schema/unicode-str.err        |  1 +
>  tests/qapi-schema/unicode-str.exit       |  1 +
>  tests/qapi-schema/unicode-str.json       |  2 ++
>  tests/qapi-schema/unicode-str.out        |  0
>  18 files changed, 52 insertions(+), 4 deletions(-)
>  create mode 100644 tests/qapi-schema/escape-too-big.err
>  create mode 100644 tests/qapi-schema/escape-too-big.exit
>  create mode 100644 tests/qapi-schema/escape-too-big.json
>  create mode 100644 tests/qapi-schema/escape-too-big.out
>  create mode 100644 tests/qapi-schema/escape-too-short.err
>  create mode 100644 tests/qapi-schema/escape-too-short.exit
>  create mode 100644 tests/qapi-schema/escape-too-short.json
>  create mode 100644 tests/qapi-schema/escape-too-short.out
>  create mode 100644 tests/qapi-schema/unicode-str.err
>  create mode 100644 tests/qapi-schema/unicode-str.exit
>  create mode 100644 tests/qapi-schema/unicode-str.json
>  create mode 100644 tests/qapi-schema/unicode-str.out
>
> diff --git a/scripts/qapi.py b/scripts/qapi.py
> index 60ed34a..853f9a3 100644
> --- a/scripts/qapi.py
> +++ b/scripts/qapi.py
> @@ -173,7 +173,38 @@ class QAPISchema:
>                          raise QAPISchemaError(self,
>                                                'Missing terminating "\'"')
>                      if esc:
> -                        string += ch
> +                        if ch == 'b':
> +                            string += '\b'
> +                        elif ch == 'f':
> +                            string += '\f'
> +                        elif ch == 'n':
> +                            string += '\n'
> +                        elif ch == 'r':
> +                            string += '\r'
> +                        elif ch == 't':
> +                            string += '\t'
> +                        elif ch == 'u':
> +                            value = 0
> +                            for x in range(0, 4):
> +                                ch = self.src[self.cursor]
> +                                self.cursor += 1
> +                                if ch not in "0123456789abcdefABCDEF":
> +                                    raise QAPISchemaError(self,
> +                                                          '\\u escape needs 
> 4 '
> +                                                          'hex digits')
> +                                value = (value << 4) + int(ch, 16)
> +                            # If Python 2 and 3 didn't disagree so much on
> +                            # how to handle Unicode, then we could allow
> +                            # Unicode string defaults.  But most of QAPI is
> +                            # ASCII-only, so we aren't losing much for now.
> +                            if value > 0x7f:
> +                                raise QAPISchemaError(self,
> +                                                      'For now, \\u escape '
> +                                                      'only supports values '
> +                                                      'up to \\u007f')
> +                            string += chr(value)
> +                        else:
> +                            string += ch
>                          esc = False
>                      elif ch == "\\":
>                          esc = True

RFC 7159 accepts escapes ["\/bfnrtu], where u is followed by four
hexadecimal digits.

Our C JSON parser additionally accepts ', see json-lexer.c.

This code accepts any character.  I'd prefer to make it consistent with
our C JSON parser instead.

[...]



reply via email to

[Prev in Thread] Current Thread [Next in Thread]