[njs] Added macro for converting surrogate pair to code point.

Alexander Borisov alexander.borisov at nginx.com
Tue Apr 23 15:11:25 UTC 2019


details:   https://hg.nginx.org/njs/rev/1c1732696e98
branches:  
changeset: 922:1c1732696e98
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Tue Apr 23 18:10:37 2019 +0300
description:
Added macro for converting surrogate pair to code point.

diffstat:

 njs/njs_json.c            |  2 +-
 njs/njs_parser_terminal.c |  8 ++------
 njs/njs_string.h          |  4 ++++
 3 files changed, 7 insertions(+), 7 deletions(-)

diffs (57 lines):

diff -r 724c31e77d2a -r 1c1732696e98 njs/njs_json.c
--- a/njs/njs_json.c	Mon Apr 22 16:23:50 2019 +0300
+++ b/njs/njs_json.c	Tue Apr 23 18:10:37 2019 +0300
@@ -820,7 +820,7 @@ njs_json_parse_string(njs_json_parse_ctx
                     return NULL;
                 }
 
-                utf = 0x10000 + ((utf - 0xd800) << 10) + (utf_low - 0xdc00);
+                utf = njs_string_surrogate_pair(utf, utf_low);
             }
 
             s = nxt_utf8_encode(s, utf);
diff -r 724c31e77d2a -r 1c1732696e98 njs/njs_parser_terminal.c
--- a/njs/njs_parser_terminal.c	Mon Apr 22 16:23:50 2019 +0300
+++ b/njs/njs_parser_terminal.c	Tue Apr 23 18:10:37 2019 +0300
@@ -1048,10 +1048,8 @@ njs_parser_escape_string_create(njs_vm_t
             src++;
         }
 
-        /* Surrogate pair. */
-
         if (cp_pair != 0) {
-            cp = 0x10000 + ((cp_pair - 0xd800) << 10) + (cp - 0xdc00);
+            cp = njs_string_surrogate_pair(cp_pair, cp);
             cp_pair = 0;
 
         } else if (cp >= 0xd800 && cp <= 0xdfff) {
@@ -1184,14 +1182,12 @@ njs_parser_escape_string_calc_length(njs
             }
         }
 
-        /* Surrogate pair. */
-
         if (cp_pair != 0) {
             if (nxt_slow_path(cp < 0xdc00 || cp > 0xdfff)) {
                 goto invalid_pair;
             }
 
-            cp = 0x10000 + ((cp_pair - 0xd800) << 10) + (cp - 0xdc00);
+            cp = njs_string_surrogate_pair(cp_pair, cp);
             cp_pair = 0;
 
         } else if (cp >= 0xd800 && cp <= 0xdfff) {
diff -r 724c31e77d2a -r 1c1732696e98 njs/njs_string.h
--- a/njs/njs_string.h	Mon Apr 22 16:23:50 2019 +0300
+++ b/njs/njs_string.h	Tue Apr 23 18:10:37 2019 +0300
@@ -27,6 +27,10 @@
 /* The maximum signed int32_t. */
 #define NJS_STRING_MAX_LENGTH  0x7fffffff
 
+/* Converting surrogate pair to code point.  */
+#define njs_string_surrogate_pair(high, low)                                  \
+    (0x10000 + ((high - 0xd800) << 10) + (low - 0xdc00))
+
 /*
  * NJS_STRING_MAP_STRIDE should be power of two to use shift and binary
  * AND operations instead of division and remainder operations but no


More information about the nginx-devel mailing list