[njs] Adding const qualifiers to njs_number_dec_parse() and friends.

Dmitry Volyntsev xeioex at nginx.com
Thu Apr 26 16:11:37 UTC 2018


details:   http://hg.nginx.org/njs/rev/29eee021e03e
branches:  
changeset: 503:29eee021e03e
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Apr 26 19:11:28 2018 +0300
description:
Adding const qualifiers to njs_number_dec_parse() and friends.

The functions which call njs_number_dec_parse() and friends are
changed accordingly.

diffstat:

 njs/njs_json.c   |  90 ++++++++++++++++++++++++++++++-------------------------
 njs/njs_lexer.c  |   9 +++-
 njs/njs_number.c |  37 ++++++++++++----------
 njs/njs_number.h |  11 +++---
 njs/njs_parser.c |   7 ++-
 njs/njs_string.c |  20 ++++++------
 njs/njs_string.h |   6 +-
 7 files changed, 98 insertions(+), 82 deletions(-)

diffs (424 lines):

diff -r 7f75ccba396e -r 29eee021e03e njs/njs_json.c
--- a/njs/njs_json.c	Fri Apr 20 16:42:12 2018 +0300
+++ b/njs/njs_json.c	Thu Apr 26 19:11:28 2018 +0300
@@ -32,8 +32,8 @@ typedef struct {
     njs_vm_t                   *vm;
     nxt_mem_cache_pool_t       *pool;
     nxt_uint_t                 depth;
-    u_char                     *start;
-    u_char                     *end;
+    const u_char               *start;
+    const u_char               *end;
 } njs_json_parse_ctx_t;
 
 
@@ -110,18 +110,19 @@ typedef struct {
 } njs_json_stringify_t;
 
 
-static u_char *njs_json_parse_value(njs_json_parse_ctx_t *ctx,
-    njs_value_t *value, u_char *p);
-static u_char *njs_json_parse_object(njs_json_parse_ctx_t *ctx,
-    njs_value_t *value, u_char *p);
-static u_char *njs_json_parse_array(njs_json_parse_ctx_t *ctx,
-    njs_value_t *value, u_char *p);
-static u_char *njs_json_parse_string(njs_json_parse_ctx_t *ctx,
-    njs_value_t *value, u_char *p);
-static u_char *njs_json_parse_number(njs_json_parse_ctx_t *ctx,
-    njs_value_t *value, u_char *p);
+static const u_char *njs_json_parse_value(njs_json_parse_ctx_t *ctx,
+    njs_value_t *value, const u_char *p);
+static const u_char *njs_json_parse_object(njs_json_parse_ctx_t *ctx,
+    njs_value_t *value, const u_char *p);
+static const u_char *njs_json_parse_array(njs_json_parse_ctx_t *ctx,
+    njs_value_t *value, const u_char *p);
+static const u_char *njs_json_parse_string(njs_json_parse_ctx_t *ctx,
+    njs_value_t *value, const u_char *p);
+static const u_char *njs_json_parse_number(njs_json_parse_ctx_t *ctx,
+    njs_value_t *value, const u_char *p);
 nxt_inline uint32_t njs_json_unicode(const u_char *p);
-static u_char *njs_json_skip_space(u_char *start, u_char *end);
+static const u_char *njs_json_skip_space(const u_char *start,
+    const u_char *end);
 
 static njs_ret_t njs_json_parse_continuation(njs_vm_t *vm,
     njs_value_t *args, nxt_uint_t nargs, njs_index_t unused);
@@ -131,7 +132,7 @@ static njs_json_state_t *njs_json_push_p
     njs_json_parse_t *parse, njs_value_t *value);
 static njs_json_state_t *njs_json_pop_parse_state(njs_json_parse_t *parse);
 static void njs_json_parse_exception(njs_json_parse_ctx_t *ctx,
-    const char* msg, u_char *pos);
+    const char* msg, const u_char *pos);
 
 static njs_ret_t njs_json_stringify_continuation(njs_vm_t *vm,
     njs_value_t *args, nxt_uint_t nargs, njs_index_t unused);
@@ -179,8 +180,8 @@ static njs_ret_t
 njs_json_parse(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t unused)
 {
-    u_char                *p, *end;
     njs_value_t           arg, *value, *wrapper;
+    const u_char          *p, *end;
     njs_json_parse_t      *parse;
     njs_string_prop_t     string;
     njs_json_parse_ctx_t  ctx;
@@ -348,8 +349,9 @@ memory_error:
 }
 
 
-static u_char *
-njs_json_parse_value(njs_json_parse_ctx_t *ctx, njs_value_t *value, u_char *p)
+static const u_char *
+njs_json_parse_value(njs_json_parse_ctx_t *ctx, njs_value_t *value,
+    const u_char *p)
 {
     switch (*p) {
     case '{':
@@ -401,8 +403,9 @@ error:
 }
 
 
-static u_char *
-njs_json_parse_object(njs_json_parse_ctx_t *ctx, njs_value_t *value, u_char *p)
+static const u_char *
+njs_json_parse_object(njs_json_parse_ctx_t *ctx, njs_value_t *value,
+    const u_char *p)
 {
     nxt_int_t           ret;
     njs_object_t        *object;
@@ -533,8 +536,9 @@ memory_error:
 }
 
 
-static u_char *
-njs_json_parse_array(njs_json_parse_ctx_t *ctx, njs_value_t *value, u_char *p)
+static const u_char *
+njs_json_parse_array(njs_json_parse_ctx_t *ctx, njs_value_t *value,
+    const u_char *p)
 {
     nxt_int_t    ret;
     njs_array_t  *array;
@@ -621,14 +625,16 @@ error_end:
 }
 
 
-static u_char *
-njs_json_parse_string(njs_json_parse_ctx_t *ctx, njs_value_t *value, u_char *p)
+static const u_char *
+njs_json_parse_string(njs_json_parse_ctx_t *ctx, njs_value_t *value,
+    const u_char *p)
 {
-    u_char      *s, ch, *last, *start;
-    size_t      size, surplus;
-    ssize_t     length;
-    uint32_t    utf, utf_low;
-    njs_ret_t   ret;
+    u_char        ch, *s, *dst;
+    size_t        size, surplus;
+    ssize_t       length;
+    uint32_t      utf, utf_low;
+    njs_ret_t     ret;
+    const u_char  *start, *last;
 
     enum {
         sw_usual = 0,
@@ -734,13 +740,13 @@ njs_json_parse_string(njs_json_parse_ctx
     if (surplus != 0) {
         p = start;
 
-        start = nxt_mem_cache_alloc(ctx->pool, size);
+        dst = nxt_mem_cache_alloc(ctx->pool, size);
         if (nxt_slow_path(start == NULL)) {
             njs_memory_error(ctx->vm);;
             return NULL;
         }
 
-        s = start;
+        s = dst;
 
         do {
             ch = *p++;
@@ -811,7 +817,8 @@ njs_json_parse_string(njs_json_parse_ctx
 
         } while (p != last);
 
-        size = s - start;
+        size = s - dst;
+        start = dst;
     }
 
     length = nxt_utf8_length(start, size);
@@ -819,7 +826,7 @@ njs_json_parse_string(njs_json_parse_ctx
         length = 0;
     }
 
-    ret = njs_string_create(ctx->vm, value, start, size, length);
+    ret = njs_string_create(ctx->vm, value, (u_char *) start, size, length);
     if (nxt_slow_path(ret != NXT_OK)) {
         njs_memory_error(ctx->vm);
         return NULL;
@@ -829,12 +836,13 @@ njs_json_parse_string(njs_json_parse_ctx
 }
 
 
-static u_char *
-njs_json_parse_number(njs_json_parse_ctx_t *ctx, njs_value_t *value, u_char *p)
+static const u_char *
+njs_json_parse_number(njs_json_parse_ctx_t *ctx, njs_value_t *value,
+    const u_char *p)
 {
-    u_char     *start;
-    double     num;
-    nxt_int_t  sign;
+    double        num;
+    nxt_int_t     sign;
+    const u_char  *start;
 
     sign = 1;
 
@@ -888,10 +896,10 @@ njs_json_unicode(const u_char *p)
 }
 
 
-static u_char *
-njs_json_skip_space(u_char *start, u_char *end)
+static const u_char *
+njs_json_skip_space(const u_char *start, const u_char *end)
 {
-    u_char  *p;
+    const u_char  *p;
 
     for (p = start; nxt_fast_path(p != end); p++) {
 
@@ -1130,7 +1138,7 @@ njs_json_pop_parse_state(njs_json_parse_
 
 static void
 njs_json_parse_exception(njs_json_parse_ctx_t *ctx, const char* msg,
-    u_char *pos)
+    const u_char *pos)
 {
     ssize_t  length;
 
diff -r 7f75ccba396e -r 29eee021e03e njs/njs_lexer.c
--- a/njs/njs_lexer.c	Fri Apr 20 16:42:12 2018 +0300
+++ b/njs/njs_lexer.c	Thu Apr 26 19:11:28 2018 +0300
@@ -556,7 +556,8 @@ njs_lexer_number(njs_lexer_t *lexer)
             }
 
             lexer->start = p;
-            lexer->number = njs_number_hex_parse(&lexer->start, lexer->end);
+            lexer->number = njs_number_hex_parse((const u_char **) &lexer->start,
+                                                 lexer->end);
 
             return NJS_TOKEN_NUMBER;
         }
@@ -571,7 +572,8 @@ njs_lexer_number(njs_lexer_t *lexer)
             }
 
             lexer->start = p;
-            lexer->number = njs_number_oct_parse(&lexer->start, lexer->end);
+            lexer->number = njs_number_oct_parse((const u_char **) &lexer->start,
+                                                 lexer->end);
             p = lexer->start;
 
             if (p < lexer->end && (*p == '8' || *p == '9')) {
@@ -589,7 +591,8 @@ njs_lexer_number(njs_lexer_t *lexer)
     }
 
     lexer->start = p - 1;
-    lexer->number = njs_number_dec_parse(&lexer->start, lexer->end);
+    lexer->number = njs_number_dec_parse((const u_char **) &lexer->start,
+                                         lexer->end);
 
     return NJS_TOKEN_NUMBER;
 }
diff -r 7f75ccba396e -r 29eee021e03e njs/njs_number.c
--- a/njs/njs_number.c	Fri Apr 20 16:42:12 2018 +0300
+++ b/njs/njs_number.c	Thu Apr 26 19:11:28 2018 +0300
@@ -39,7 +39,7 @@ static njs_ret_t njs_number_to_string_ra
 
 
 uint32_t
-njs_value_to_index(njs_value_t *value)
+njs_value_to_index(const njs_value_t *value)
 {
     double       num;
     njs_array_t  *array;
@@ -79,11 +79,12 @@ njs_value_to_index(njs_value_t *value)
 
 
 double
-njs_number_dec_parse(u_char **start, u_char *end)
+njs_number_dec_parse(const u_char **start, const u_char *end)
 {
-    u_char      c, *e, *p;
-    double      num, frac, scale, exponent;
-    nxt_bool_t  minus;
+    u_char        c;
+    double        num, frac, scale, exponent;
+    nxt_bool_t    minus;
+    const u_char  *e, *p;
 
     p = *start;
 
@@ -169,10 +170,11 @@ njs_number_dec_parse(u_char **start, u_c
 
 
 uint64_t
-njs_number_oct_parse(u_char **start, u_char *end)
+njs_number_oct_parse(const u_char **start, const u_char *end)
 {
-    u_char    c, *p;
-    uint64_t  num;
+    u_char        c;
+    uint64_t      num;
+    const u_char  *p;
 
     p = *start;
 
@@ -197,10 +199,11 @@ njs_number_oct_parse(u_char **start, u_c
 
 
 uint64_t
-njs_number_hex_parse(u_char **start, u_char *end)
+njs_number_hex_parse(const u_char **start, const u_char *end)
 {
-    u_char    c, *p;
-    uint64_t  num;
+    u_char        c;
+    uint64_t      num;
+    const u_char  *p;
 
     p = *start;
 
@@ -234,12 +237,12 @@ njs_number_hex_parse(u_char **start, u_c
 
 
 int64_t
-njs_number_radix_parse(u_char **start, u_char *end, uint8_t radix)
+njs_number_radix_parse(const u_char **start, const u_char *end, uint8_t radix)
 {
-    u_char    *p;
-    uint8_t   d;
-    int64_t   num;
-    uint64_t  n;
+    uint8_t       d;
+    int64_t       num;
+    uint64_t      n;
+    const u_char  *p;
 
     static const int8_t  digits[256]
         nxt_aligned(32) =
@@ -780,11 +783,11 @@ njs_number_parse_int(njs_vm_t *vm, njs_v
     njs_index_t unused)
 {
     double             num;
-    u_char             *p, *end;
     int64_t            n;
     uint8_t            radix;
     nxt_str_t          string;
     nxt_bool_t         minus, test_prefix;
+    const u_char       *p, *end;
 
     num = NAN;
 
diff -r 7f75ccba396e -r 29eee021e03e njs/njs_number.h
--- a/njs/njs_number.h	Fri Apr 20 16:42:12 2018 +0300
+++ b/njs/njs_number.h	Thu Apr 26 19:11:28 2018 +0300
@@ -11,11 +11,12 @@
 #include <math.h>
 
 
-uint32_t njs_value_to_index(njs_value_t *value);
-double njs_number_dec_parse(u_char **start, u_char *end);
-uint64_t njs_number_oct_parse(u_char **start, u_char *end);
-uint64_t njs_number_hex_parse(u_char **start, u_char *end);
-int64_t njs_number_radix_parse(u_char **start, u_char *end, uint8_t radix);
+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_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);
 njs_ret_t njs_number_to_string(njs_vm_t *vm, njs_value_t *string,
     const njs_value_t *number);
 size_t njs_num_to_buf(double num, u_char *buf, size_t size);
diff -r 7f75ccba396e -r 29eee021e03e njs/njs_parser.c
--- a/njs/njs_parser.c	Fri Apr 20 16:42:12 2018 +0300
+++ b/njs/njs_parser.c	Thu Apr 26 19:11:28 2018 +0300
@@ -2379,9 +2379,10 @@ static njs_token_t
 njs_parser_escape_string_create(njs_vm_t *vm, njs_parser_t *parser,
     njs_value_t *value)
 {
-    u_char    c, *p, *start, *dst, *src, *end, *hex_end;
-    size_t    size, length, hex_length;
-    uint64_t  u;
+    u_char        c, *start, *dst;
+    size_t        size,length, hex_length;
+    uint64_t      u;
+    const u_char  *p, *src, *end, *hex_end;
 
     start = NULL;
     dst = NULL;
diff -r 7f75ccba396e -r 29eee021e03e njs/njs_string.c
--- a/njs/njs_string.c	Fri Apr 20 16:42:12 2018 +0300
+++ b/njs/njs_string.c	Thu Apr 26 19:11:28 2018 +0300
@@ -3183,12 +3183,12 @@ njs_primitive_value_to_string(njs_vm_t *
 
 
 double
-njs_string_to_number(njs_value_t *value, nxt_bool_t parse_float)
+njs_string_to_number(const njs_value_t *value, nxt_bool_t parse_float)
 {
-    u_char      *p, *start, *end;
-    double      num;
-    size_t      size;
-    nxt_bool_t  minus;
+    double        num;
+    size_t        size;
+    nxt_bool_t    minus;
+    const u_char  *p, *start, *end;
 
     const size_t  infinity = sizeof("Infinity") - 1;
 
@@ -3265,11 +3265,11 @@ njs_string_to_number(njs_value_t *value,
 
 
 double
-njs_string_to_index(njs_value_t *value)
+njs_string_to_index(const njs_value_t *value)
 {
-    u_char  *p, *end;
-    double  num;
-    size_t  size;
+    double        num;
+    size_t        size;
+    const u_char  *p, *end;
 
     size = value->short_string.size;
 
@@ -3305,7 +3305,7 @@ njs_string_to_index(njs_value_t *value)
  * is returned as is, otherwise the new copy is allocated with
  * the terminating zero byte.
  */
-u_char *
+const u_char *
 njs_string_to_c_string(njs_vm_t *vm, njs_value_t *value)
 {
     u_char  *p, *data, *start;
diff -r 7f75ccba396e -r 29eee021e03e njs/njs_string.h
--- a/njs/njs_string.h	Fri Apr 20 16:42:12 2018 +0300
+++ b/njs/njs_string.h	Thu Apr 26 19:11:28 2018 +0300
@@ -147,9 +147,9 @@ nxt_noinline uint32_t njs_string_index(n
 void njs_string_offset_map_init(const u_char *start, size_t size);
 njs_ret_t njs_primitive_value_to_string(njs_vm_t *vm, njs_value_t *dst,
     const njs_value_t *src);
-double njs_string_to_index(njs_value_t *value);
-double njs_string_to_number(njs_value_t *value, nxt_bool_t parse_float);
-u_char *njs_string_to_c_string(njs_vm_t *vm, njs_value_t *value);
+double njs_string_to_index(const njs_value_t *value);
+double njs_string_to_number(const njs_value_t *value, nxt_bool_t parse_float);
+const u_char *njs_string_to_c_string(njs_vm_t *vm, njs_value_t *value);
 njs_ret_t njs_string_encode_uri(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused);
 njs_ret_t njs_string_encode_uri_component(njs_vm_t *vm, njs_value_t *args,


More information about the nginx-devel mailing list