[njs] Added njs_uint32_to_string().

Dmitry Volyntsev xeioex at nginx.com
Thu Sep 13 17:19:45 UTC 2018


details:   http://hg.nginx.org/njs/rev/c4f9a4948697
branches:  
changeset: 604:c4f9a4948697
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Sep 13 18:46:02 2018 +0300
description:
Added njs_uint32_to_string().

diffstat:

 njs/njs_event.c  |   6 +-----
 njs/njs_json.c   |  16 +++-------------
 njs/njs_number.h |  12 ++++++++++++
 njs/njs_object.c |   6 +-----
 4 files changed, 17 insertions(+), 23 deletions(-)

diffs (153 lines):

diff -r a92ae4986bc0 -r c4f9a4948697 njs/njs_event.c
--- a/njs/njs_event.c	Thu Sep 13 15:52:17 2018 +0300
+++ b/njs/njs_event.c	Thu Sep 13 18:46:02 2018 +0300
@@ -6,7 +6,6 @@
 
 #include <njs_core.h>
 #include <string.h>
-#include <stdio.h>
 
 
 static nxt_int_t njs_event_hash_test(nxt_lvlhsh_query_t *lhq, void *data);
@@ -44,13 +43,10 @@ njs_event_hash_test(nxt_lvlhsh_query_t *
 nxt_int_t
 njs_add_event(njs_vm_t *vm, njs_event_t *event)
 {
-    size_t              size;
     nxt_int_t           ret;
     nxt_lvlhsh_query_t  lhq;
 
-    size = snprintf((char *) njs_string_short_start(&event->id),
-                    NJS_STRING_SHORT, "%u", vm->event_id++);
-    njs_string_short_set(&event->id, size, size);
+    njs_uint32_to_string(&event->id, vm->event_id++);
 
     njs_string_get(&event->id, &lhq.key);
     lhq.key_hash = nxt_djb_hash(lhq.key.start, lhq.key.length);
diff -r a92ae4986bc0 -r c4f9a4948697 njs/njs_json.c
--- a/njs/njs_json.c	Thu Sep 13 15:52:17 2018 +0300
+++ b/njs/njs_json.c	Thu Sep 13 18:46:02 2018 +0300
@@ -8,7 +8,6 @@
 #include <njs_json.h>
 #include <njs_date.h>
 #include <njs_regexp.h>
-#include <stdio.h>
 #include <string.h>
 
 
@@ -1036,7 +1035,6 @@ memory_error:
 static njs_ret_t
 njs_json_parse_continuation_apply(njs_vm_t *vm, njs_json_parse_t *parse)
 {
-    size_t            size;
     njs_value_t       arguments[3];
     njs_json_state_t  *state;
 
@@ -1053,9 +1051,7 @@ njs_json_parse_continuation_apply(njs_vm
         break;
 
     case NJS_JSON_ARRAY_START:
-        size = snprintf((char *) njs_string_short_start(&arguments[1]),
-                        NJS_STRING_SHORT, "%u", state->index);
-        njs_string_short_set(&arguments[1], size, size);
+        njs_uint32_to_string(&arguments[1], state->index);
         arguments[2] = state->value.data.u.array->start[state->index];
 
         state->type = NJS_JSON_ARRAY_REPLACED;
@@ -1455,7 +1451,6 @@ static njs_ret_t
 njs_json_stringify_to_json(njs_vm_t *vm, njs_json_stringify_t* stringify,
     njs_function_t *function, njs_value_t *key, njs_value_t *value)
 {
-    size_t            size;
     njs_value_t       arguments[2];
     njs_json_state_t  *state;
 
@@ -1482,9 +1477,7 @@ njs_json_stringify_to_json(njs_vm_t *vm,
 
     case NJS_JSON_ARRAY_START:
     case NJS_JSON_ARRAY_CONTINUE:
-        size = snprintf((char *) njs_string_short_start(&arguments[1]),
-                        NJS_STRING_SHORT, "%u", state->index - 1);
-        njs_string_short_set(&arguments[1], size, size);
+        njs_uint32_to_string(&arguments[1], state->index - 1);
 
         state->type = NJS_JSON_ARRAY_TO_JSON_REPLACED;
         break;
@@ -1504,7 +1497,6 @@ static njs_ret_t
 njs_json_stringify_replacer(njs_vm_t *vm, njs_json_stringify_t* stringify,
     njs_value_t *key, njs_value_t *value)
 {
-    size_t            size;
     njs_value_t       arguments[3];
     njs_json_state_t  *state;
 
@@ -1526,9 +1518,7 @@ njs_json_stringify_replacer(njs_vm_t *vm
     case NJS_JSON_ARRAY_START:
     case NJS_JSON_ARRAY_CONTINUE:
     case NJS_JSON_ARRAY_TO_JSON_REPLACED:
-        size = snprintf((char *) njs_string_short_start(&arguments[1]),
-                        NJS_STRING_SHORT, "%u", state->index - 1);
-        njs_string_short_set(&arguments[1], size, size);
+        njs_uint32_to_string(&arguments[1], state->index - 1);
         arguments[2] = *value;
 
         state->type = NJS_JSON_ARRAY_REPLACED;
diff -r a92ae4986bc0 -r c4f9a4948697 njs/njs_number.h
--- a/njs/njs_number.h	Thu Sep 13 15:52:17 2018 +0300
+++ b/njs/njs_number.h	Thu Sep 13 18:46:02 2018 +0300
@@ -9,6 +9,7 @@
 
 
 #include <math.h>
+#include <stdio.h>
 
 
 uint32_t njs_value_to_index(const njs_value_t *value);
@@ -56,6 +57,17 @@ njs_char_to_hex(u_char c)
 }
 
 
+nxt_inline void
+njs_uint32_to_string(njs_value_t *value, uint32_t u32)
+{
+    size_t  size;
+
+    size = snprintf((char *) njs_string_short_start(value),
+                    NJS_STRING_SHORT, "%u", u32);
+    njs_string_short_set(value, size, size);
+}
+
+
 extern const njs_object_init_t  njs_number_constructor_init;
 extern const njs_object_init_t  njs_number_prototype_init;
 
diff -r a92ae4986bc0 -r c4f9a4948697 njs/njs_object.c
--- a/njs/njs_object.c	Thu Sep 13 15:52:17 2018 +0300
+++ b/njs/njs_object.c	Thu Sep 13 18:46:02 2018 +0300
@@ -5,7 +5,6 @@
  */
 
 #include <njs_core.h>
-#include <stdio.h>
 #include <string.h>
 
 
@@ -651,7 +650,6 @@ njs_object_keys(njs_vm_t *vm, njs_value_
 njs_array_t *
 njs_object_keys_array(njs_vm_t *vm, const njs_value_t *object)
 {
-    size_t             size;
     uint32_t           i, n, keys_length, array_length;
     njs_value_t        *value;
     njs_array_t        *keys, *array;
@@ -704,9 +702,7 @@ njs_object_keys_array(njs_vm_t *vm, cons
              * The maximum array index is 4294967294, so
              * it can be stored as a short string inside value.
              */
-            size = snprintf((char *) njs_string_short_start(value),
-                            NJS_STRING_SHORT, "%u", i);
-            njs_string_short_set(value, size, size);
+            njs_uint32_to_string(value, i);
         }
     }
 


More information about the nginx-devel mailing list