From: Dmitry Volyntsev Date: Sat, 18 Feb 2023 06:38:25 +0000 (-0800) Subject: Added njs_vm_value_to_c_string(). X-Git-Tag: 0.7.11~21 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=ada8b8bfc92cad56c73f4775a36a5dbb319a909e;p=njs.git Added njs_vm_value_to_c_string(). --- diff --git a/src/njs.h b/src/njs.h index d8185618..079817be 100644 --- a/src/njs.h +++ b/src/njs.h @@ -447,6 +447,13 @@ NJS_EXPORT njs_int_t njs_vm_value_to_string(njs_vm_t *vm, njs_str_t *dst, */ NJS_EXPORT njs_int_t njs_vm_value_string(njs_vm_t *vm, njs_str_t *dst, njs_value_t *src); +/* + * If string value is null-terminated the corresponding C string + * is returned as is, otherwise the new copy is allocated with + * the terminating zero byte. + */ +NJS_EXPORT const char *njs_vm_value_to_c_string(njs_vm_t *vm, + njs_value_t *value); NJS_EXPORT njs_int_t njs_vm_retval_string(njs_vm_t *vm, njs_str_t *dst); NJS_EXPORT njs_int_t njs_vm_value_dump(njs_vm_t *vm, njs_str_t *dst, diff --git a/src/njs_string.c b/src/njs_string.c index 4a551447..84a48f1a 100644 --- a/src/njs_string.c +++ b/src/njs_string.c @@ -4075,44 +4075,6 @@ njs_string_to_index(const njs_value_t *value) } -/* - * If string value is null-terminated the corresponding C string - * is returned as is, otherwise the new copy is allocated with - * the terminating zero byte. - */ -const char * -njs_string_to_c_string(njs_vm_t *vm, njs_value_t *value) -{ - u_char *p, *data, *start; - size_t size; - - if (value->short_string.size != NJS_STRING_LONG) { - start = value->short_string.start; - size = value->short_string.size; - - if (size < NJS_STRING_SHORT) { - start[size] = '\0'; - return (const char *) start; - } - - } else { - start = value->long_string.data->start; - size = value->long_string.size; - } - - data = njs_mp_alloc(vm->mem_pool, size + 1); - if (njs_slow_path(data == NULL)) { - njs_memory_error(vm); - return NULL; - } - - p = njs_cpymem(data, start, size); - *p++ = '\0'; - - return (const char *) data; -} - - static const njs_object_prop_t njs_string_prototype_properties[] = { NJS_DECLARE_PROP_HANDLER("__proto__", njs_primitive_prototype_get_proto, diff --git a/src/njs_string.h b/src/njs_string.h index 2ca7e90d..e9629385 100644 --- a/src/njs_string.h +++ b/src/njs_string.h @@ -248,7 +248,6 @@ const u_char *njs_string_offset(const u_char *start, const u_char *end, uint32_t njs_string_index(njs_string_prop_t *string, uint32_t offset); void njs_string_offset_map_init(const u_char *start, size_t size); double njs_string_to_index(const njs_value_t *value); -const char *njs_string_to_c_string(njs_vm_t *vm, njs_value_t *value); njs_int_t njs_string_encode_uri(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t component); njs_int_t njs_string_decode_uri(njs_vm_t *vm, njs_value_t *args, diff --git a/src/njs_vm.c b/src/njs_vm.c index 923177e1..4cbd0387 100644 --- a/src/njs_vm.c +++ b/src/njs_vm.c @@ -1257,6 +1257,46 @@ njs_vm_value_to_string(njs_vm_t *vm, njs_str_t *dst, njs_value_t *src) } +/* + * If string value is null-terminated the corresponding C string + * is returned as is, otherwise the new copy is allocated with + * the terminating zero byte. + */ +const char * +njs_vm_value_to_c_string(njs_vm_t *vm, njs_value_t *value) +{ + u_char *p, *data, *start; + size_t size; + + njs_assert(njs_is_string(value)); + + if (value->short_string.size != NJS_STRING_LONG) { + start = value->short_string.start; + size = value->short_string.size; + + if (size < NJS_STRING_SHORT) { + start[size] = '\0'; + return (const char *) start; + } + + } else { + start = value->long_string.data->start; + size = value->long_string.size; + } + + data = njs_mp_alloc(vm->mem_pool, size + njs_length("\0")); + if (njs_slow_path(data == NULL)) { + njs_memory_error(vm); + return NULL; + } + + p = njs_cpymem(data, start, size); + *p++ = '\0'; + + return (const char *) data; +} + + njs_int_t njs_vm_value_to_bytes(njs_vm_t *vm, njs_str_t *dst, njs_value_t *src) {