From: Dmitry Volyntsev Date: Thu, 13 Sep 2018 15:46:02 +0000 (+0300) Subject: Added njs_uint32_to_string(). X-Git-Tag: 0.2.4~3 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=d834dbd58828c0290403332f69695eed041cacc6;p=njs.git Added njs_uint32_to_string(). --- diff --git a/njs/njs_event.c b/njs/njs_event.c index d2d4a57e..5444f76e 100644 --- a/njs/njs_event.c +++ b/njs/njs_event.c @@ -6,7 +6,6 @@ #include #include -#include 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 *lhq, void *data) 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 --git a/njs/njs_json.c b/njs/njs_json.c index f2f6fc4b..276ff438 100644 --- a/njs/njs_json.c +++ b/njs/njs_json.c @@ -8,7 +8,6 @@ #include #include #include -#include #include @@ -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_t *vm, njs_json_parse_t *parse) 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, njs_json_stringify_t* stringify, 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, njs_json_stringify_t* stringify, 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 --git a/njs/njs_number.h b/njs/njs_number.h index 33cef22b..a651b526 100644 --- a/njs/njs_number.h +++ b/njs/njs_number.h @@ -9,6 +9,7 @@ #include +#include 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 --git a/njs/njs_object.c b/njs/njs_object.c index 45118f01..372a6c53 100644 --- a/njs/njs_object.c +++ b/njs/njs_object.c @@ -5,7 +5,6 @@ */ #include -#include #include @@ -651,7 +650,6 @@ njs_object_keys(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, 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, const njs_value_t *object) * 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); } }