[njs] Added support of binary literals.

Valentin Bartenev vbart at nginx.com
Sun Jul 1 09:20:14 UTC 2018


details:   http://hg.nginx.org/njs/rev/e210675daceb
branches:  
changeset: 545:e210675daceb
user:      Valentin Bartenev <vbart at nginx.com>
date:      Sun Jul 01 10:01:53 2018 +0300
description:
Added support of binary literals.

diffstat:

 njs/njs_lexer.c          |  21 +++++++++++++++++++++
 njs/njs_number.c         |  29 +++++++++++++++++++++++++++++
 njs/njs_number.h         |   1 +
 njs/test/njs_unit_test.c |  20 ++++++++++++++++++++
 4 files changed, 71 insertions(+), 0 deletions(-)

diffs (111 lines):

diff -r 1cdf9c1a377d -r e210675daceb njs/njs_lexer.c
--- a/njs/njs_lexer.c	Sun Jul 01 09:59:45 2018 +0300
+++ b/njs/njs_lexer.c	Sun Jul 01 10:01:53 2018 +0300
@@ -569,6 +569,27 @@ njs_lexer_number(njs_lexer_t *lexer)
             return NJS_TOKEN_NUMBER;
         }
 
+        /* Binary literal values. */
+
+        if (*p == 'b' || *p == 'B') {
+            p++;
+
+            if (p == lexer->end) {
+                return NJS_TOKEN_ILLEGAL;
+            }
+
+            lexer->start = p;
+            lexer->number = njs_number_bin_parse((const u_char **) &lexer->start,
+                                                 lexer->end);
+            p = lexer->start;
+
+            if (p < lexer->end && (*p >= '2' && *p <= '9')) {
+                return NJS_TOKEN_ILLEGAL;
+            }
+
+            return NJS_TOKEN_NUMBER;
+        }
+
         /* Legacy Octal literals are deprecated. */
 
         if (*p >= '0' && *p <= '9') {
diff -r 1cdf9c1a377d -r e210675daceb njs/njs_number.c
--- a/njs/njs_number.c	Sun Jul 01 09:59:45 2018 +0300
+++ b/njs/njs_number.c	Sun Jul 01 10:01:53 2018 +0300
@@ -184,6 +184,35 @@ njs_number_oct_parse(const u_char **star
 
 
 uint64_t
+njs_number_bin_parse(const u_char **start, const u_char *end)
+{
+    u_char        c;
+    uint64_t      num;
+    const u_char  *p;
+
+    p = *start;
+
+    num = 0;
+
+    while (p < end) {
+        /* Values less than '0' become >= 208. */
+        c = *p - '0';
+
+        if (nxt_slow_path(c > 1)) {
+            break;
+        }
+
+        num = num * 2 + c;
+        p++;
+    }
+
+    *start = p;
+
+    return num;
+}
+
+
+uint64_t
 njs_number_hex_parse(const u_char **start, const u_char *end)
 {
     u_char        c;
diff -r 1cdf9c1a377d -r e210675daceb njs/njs_number.h
--- a/njs/njs_number.h	Sun Jul 01 09:59:45 2018 +0300
+++ b/njs/njs_number.h	Sun Jul 01 10:01:53 2018 +0300
@@ -14,6 +14,7 @@
 uint32_t njs_value_to_index(const njs_value_t *value);
 double njs_number_dec_parse(const u_char **start, const u_char *end);
 uint64_t njs_number_oct_parse(const u_char **start, const u_char *end);
+uint64_t njs_number_bin_parse(const u_char **start, const u_char *end);
 uint64_t njs_number_hex_parse(const u_char **start, const u_char *end);
 int64_t njs_number_radix_parse(const u_char **start, const u_char *end,
     uint8_t radix);
diff -r 1cdf9c1a377d -r e210675daceb njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Sun Jul 01 09:59:45 2018 +0300
+++ b/njs/test/njs_unit_test.c	Sun Jul 01 10:01:53 2018 +0300
@@ -147,6 +147,26 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("0O778"),
       nxt_string("SyntaxError: Unexpected token \"\" in 1") },
 
+    /* Binary Numbers. */
+
+    { nxt_string("0b0"),
+      nxt_string("0") },
+
+    { nxt_string("0B10"),
+      nxt_string("2") },
+
+    { nxt_string("0b0101"),
+      nxt_string("5") },
+
+    { nxt_string("-0B11111111"),
+      nxt_string("-255") },
+
+    { nxt_string("0b"),
+      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+
+    { nxt_string("0B12"),
+      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+
     /* Hex Numbers. */
 
     { nxt_string("0x0"),


More information about the nginx-devel mailing list