[njs] Added njs_vm_value_to_c_string().

Dmitry Volyntsev xeioex at nginx.com
Thu Feb 23 04:13:42 UTC 2023


details:   https://hg.nginx.org/njs/rev/2af586015b65
branches:  
changeset: 2048:2af586015b65
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Feb 17 22:38:25 2023 -0800
description:
Added njs_vm_value_to_c_string().

diffstat:

 src/njs.h        |   7 +++++++
 src/njs_string.c |  38 --------------------------------------
 src/njs_string.h |   1 -
 src/njs_vm.c     |  40 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 47 insertions(+), 39 deletions(-)

diffs (126 lines):

diff -r 29fbf8f85c09 -r 2af586015b65 src/njs.h
--- a/src/njs.h	Thu Feb 09 18:34:51 2023 -0800
+++ b/src/njs.h	Fri Feb 17 22:38:25 2023 -0800
@@ -447,6 +447,13 @@ NJS_EXPORT njs_int_t njs_vm_value_to_str
  */
 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 -r 29fbf8f85c09 -r 2af586015b65 src/njs_string.c
--- a/src/njs_string.c	Thu Feb 09 18:34:51 2023 -0800
+++ b/src/njs_string.c	Fri Feb 17 22:38:25 2023 -0800
@@ -4075,44 +4075,6 @@ njs_string_to_index(const njs_value_t *v
 }
 
 
-/*
- * 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 -r 29fbf8f85c09 -r 2af586015b65 src/njs_string.h
--- a/src/njs_string.h	Thu Feb 09 18:34:51 2023 -0800
+++ b/src/njs_string.h	Fri Feb 17 22:38:25 2023 -0800
@@ -248,7 +248,6 @@ const u_char *njs_string_offset(const u_
 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 -r 29fbf8f85c09 -r 2af586015b65 src/njs_vm.c
--- a/src/njs_vm.c	Thu Feb 09 18:34:51 2023 -0800
+++ b/src/njs_vm.c	Fri Feb 17 22:38:25 2023 -0800
@@ -1257,6 +1257,46 @@ njs_vm_value_to_string(njs_vm_t *vm, njs
 }
 
 
+/*
+ * 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)
 {


More information about the nginx-devel mailing list