[njs] Unicode code point parsing optimization.

Igor Sysoev igor at sysoev.ru
Mon May 29 05:08:26 UTC 2017


details:   http://hg.nginx.org/njs/rev/7f1f3dcb278f
branches:  
changeset: 344:7f1f3dcb278f
user:      Igor Sysoev <igor at sysoev.ru>
date:      Sat May 27 18:02:09 2017 +0300
description:
Unicode code point parsing optimization.

diffstat:

 njs/njs_parser.c |  45 ++++++++++++++++++++++++---------------------
 1 files changed, 24 insertions(+), 21 deletions(-)

diffs (93 lines):

diff -r 7156ba123eae -r 7f1f3dcb278f njs/njs_parser.c
--- a/njs/njs_parser.c	Fri May 26 20:10:22 2017 +0300
+++ b/njs/njs_parser.c	Sat May 27 18:02:09 2017 +0300
@@ -2303,7 +2303,7 @@ njs_parser_escape_string_create(njs_vm_t
     njs_value_t *value)
 {
     u_char   c, *p, *start, *dst, *src, *end, *hex_end;
-    size_t   size, length, hex_length, skip;
+    size_t   size, length, hex_length;
     int64_t  u;
 
     start = NULL;
@@ -2334,35 +2334,25 @@ njs_parser_escape_string_create(njs_vm_t
                 switch (c) {
 
                 case 'u':
-                    skip = 0;
                     hex_length = 4;
-
                     /*
                      * A character after "u" can be safely tested here
                      * because there is always a closing quote at the
                      * end of string: ...\u".
                      */
-                    if (*src == '{') {
-                        hex_length = 0;
-                        src++;
-
-                        for (p = src; p < end && *p != '}'; p++) {
-                            hex_length++;
-                        }
-
-                        if (hex_length == 0 || hex_length > 6) {
-                            goto invalid;
-                        }
-
-                        skip = 1;
+                    if (*src != '{') {
+                        goto hex_length_test;
                     }
 
+                    src++;
+                    hex_length = 0;
+                    hex_end = end;
+
                     goto hex;
 
                 case 'x':
-                    skip = 0;
                     hex_length = 2;
-                    goto hex;
+                    goto hex_length_test;
 
                 case '0':
                     c = '\0';
@@ -2421,7 +2411,7 @@ njs_parser_escape_string_create(njs_vm_t
 
             continue;
 
-        hex:
+        hex_length_test:
 
             hex_end = src + hex_length;
 
@@ -2429,13 +2419,26 @@ njs_parser_escape_string_create(njs_vm_t
                 goto invalid;
             }
 
+        hex:
+
+            p = src;
             u = njs_number_radix_parse(&src, hex_end, 16);
 
-            if (nxt_slow_path(src != hex_end)) {
+            if (nxt_slow_path(u < 0)) {
                 goto invalid;
             }
 
-            src += skip;
+            if (hex_length != 0) {
+                if (src != hex_end) {
+                    goto invalid;
+                }
+
+            } else {
+                if ((src - p) > 6 || src == end || *(++src) == '}') {
+                    goto invalid;
+                }
+            }
+
             size += nxt_utf8_size(u);
             length++;
 


More information about the nginx-devel mailing list