]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Add support for the \uXXXX escape sequence 04422d716cf226b5e8a6c11306e07adbde26fd67
authorEvgeny Bobkin <evgen.ibqn@gmail.com>
Wed, 17 Jul 2013 15:16:06 +0000 (17:16 +0200)
committerEvgeny Bobkin <evgen.ibqn@gmail.com>
Tue, 23 Jul 2013 09:23:27 +0000 (11:23 +0200)
An additional checks to validate the escape sequences \xYY and \uYYYY
were added, where Y represents a hex digit.
https://bugzilla.gnome.org/show_bug.cgi?id=704709

tests/Makefile.am
tests/basic-types/escape-chars.vala [new file with mode: 0644]
vala/valascanner.vala

index 8a0df29c3d3c6808672fd9f6c4ec7073251884f2..b84ee0411a69fa1dff227d960e77ae7cdaf7b93f 100644 (file)
@@ -16,6 +16,7 @@ TESTS_ENVIRONMENT = EXEEXT=$(EXEEXT) CFLAGS='$(CFLAGS)' CPPFLAGS='$(CPPFLAGS)' L
 
 TESTS = \
        basic-types/integers.vala \
+       basic-types/escape-chars.vala \
        basic-types/floats.vala \
        basic-types/strings.vala \
        basic-types/arrays.vala \
diff --git a/tests/basic-types/escape-chars.vala b/tests/basic-types/escape-chars.vala
new file mode 100644 (file)
index 0000000..6ff1662
--- /dev/null
@@ -0,0 +1,17 @@
+void test_x_escape_chars () {
+       string s = "Copyright \xc2\xa9";
+
+       assert (s == "Copyright ©");
+}
+
+void test_u_escape_chars () {
+       string s = "Copyright \u00a9";
+
+       assert (s == "Copyright ©");
+}
+
+void main () {
+       // Test case for bug report 704709
+       test_x_escape_chars ();
+       test_u_escape_chars ();
+}
index ba4736966ced578c9a3bc56f2c916551d666299e..ab1628cc0cc808b6f2e0f37f63a4526eb91452c9 100644 (file)
@@ -212,14 +212,31 @@ public class Vala.Scanner {
                                                        current++;
                                                        token_length_in_chars++;
                                                        break;
+                                               case 'u':
+                                                       // u escape character has four hex digits
+                                                       current++;
+                                                       token_length_in_chars++;
+                                                       int digit_length;
+                                                       for (digit_length = 0; digit_length < 4 && current < end && current[0].isxdigit (); digit_length++) {
+                                                               current++;
+                                                               token_length_in_chars++;
+                                                       }
+                                                       if (digit_length != 4) {
+                                                               Report.error (get_source_reference (token_length_in_chars), "\\u requires four hex digits");
+                                                       }
+                                                       break;
                                                case 'x':
-                                                       // hexadecimal escape character
+                                                       // hexadecimal escape character requires two hex digits
                                                        current++;
                                                        token_length_in_chars++;
-                                                       while (current < end && current[0].isxdigit ()) {
+                                                       int digit_length;
+                                                       for (digit_length = 0; digit_length < 2 && current < end && current[0].isxdigit (); digit_length++) {
                                                                current++;
                                                                token_length_in_chars++;
                                                        }
+                                                       if (digit_length != 2) {
+                                                               Report.error (get_source_reference (token_length_in_chars), "\\x requires two hex digits");
+                                                       }
                                                        break;
                                                default:
                                                        Report.error (get_source_reference (token_length_in_chars), "invalid escape sequence");
@@ -693,14 +710,31 @@ public class Vala.Scanner {
                                                        current++;
                                                        token_length_in_chars++;
                                                        break;
+                                               case 'u':
+                                                       // u escape character has four hex digits
+                                                       current++;
+                                                       token_length_in_chars++;
+                                                       int digit_length;
+                                                       for (digit_length = 0; digit_length < 4 && current < end && current[0].isxdigit (); digit_length++) {
+                                                               current++;
+                                                               token_length_in_chars++;
+                                                       }
+                                                       if (digit_length != 4) {
+                                                               Report.error (get_source_reference (token_length_in_chars), "\\u requires four hex digits");
+                                                       }
+                                                       break;
                                                case 'x':
-                                                       // hexadecimal escape character
+                                                       // hexadecimal escape character requires two hex digits
                                                        current++;
                                                        token_length_in_chars++;
-                                                       while (current < end && current[0].isxdigit ()) {
+                                                       int digit_length;
+                                                       for (digit_length = 0; digit_length < 2 && current < end && current[0].isxdigit (); digit_length++) {
                                                                current++;
                                                                token_length_in_chars++;
                                                        }
+                                                       if (digit_length != 2) {
+                                                               Report.error (get_source_reference (token_length_in_chars), "\\x requires two hex digits");
+                                                       }
                                                        break;
                                                default:
                                                        Report.error (get_source_reference (token_length_in_chars), "invalid escape sequence");
@@ -1110,14 +1144,31 @@ public class Vala.Scanner {
                                                        current++;
                                                        token_length_in_chars++;
                                                        break;
+                                               case 'u':
+                                                       // u escape character has four hex digits
+                                                       current++;
+                                                       token_length_in_chars++;
+                                                       int digit_length;
+                                                       for (digit_length = 0; digit_length < 4 && current < end && current[0].isxdigit (); digit_length++) {
+                                                               current++;
+                                                               token_length_in_chars++;
+                                                       }
+                                                       if (digit_length != 4) {
+                                                               Report.error (get_source_reference (token_length_in_chars), "\\u requires four hex digits");
+                                                       }
+                                                       break;
                                                case 'x':
-                                                       // hexadecimal escape character
+                                                       // hexadecimal escape character requires two hex digits
                                                        current++;
                                                        token_length_in_chars++;
-                                                       while (current < end && current[0].isxdigit ()) {
+                                                       int digit_length;
+                                                       for (digit_length = 0; digit_length < 2 && current < end && current[0].isxdigit (); digit_length++) {
                                                                current++;
                                                                token_length_in_chars++;
                                                        }
+                                                       if (digit_length != 2) {
+                                                               Report.error (get_source_reference (token_length_in_chars), "\\x requires two hex digits");
+                                                       }
                                                        break;
                                                default:
                                                        Report.error (get_source_reference (token_length_in_chars), "invalid escape sequence");