[njs] Made njs_string.h includable independently from njs_main.h.

Dmitry Volyntsev xeioex at nginx.com
Wed May 3 04:13:22 UTC 2023


details:   https://hg.nginx.org/njs/rev/5d814dca2327
branches:  
changeset: 2096:5d814dca2327
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon May 01 20:45:32 2023 -0700
description:
Made njs_string.h includable independently from njs_main.h.

diffstat:

 src/njs.h        |   18 ++--
 src/njs_regexp.c |    9 ++-
 src/njs_string.c |   60 +++++++++++++---
 src/njs_string.h |  188 +++++++++++++++++++++---------------------------------
 src/njs_value.h  |    5 +-
 5 files changed, 139 insertions(+), 141 deletions(-)

diffs (402 lines):

diff -r 4fa5ddc91108 -r 5d814dca2327 src/njs.h
--- a/src/njs.h	Thu Apr 27 17:28:52 2023 -0700
+++ b/src/njs.h	Mon May 01 20:45:32 2023 -0700
@@ -27,14 +27,16 @@
 #include <njs_sprintf.h>
 
 
-typedef uintptr_t                   njs_index_t;
-typedef struct njs_vm_s             njs_vm_t;
-typedef struct njs_mod_s            njs_mod_t;
-typedef union  njs_value_s          njs_value_t;
-typedef struct njs_function_s       njs_function_t;
-typedef struct njs_vm_shared_s      njs_vm_shared_t;
-typedef struct njs_object_prop_s    njs_object_prop_t;
-typedef struct njs_external_s       njs_external_t;
+typedef uintptr_t                     njs_index_t;
+typedef struct njs_vm_s               njs_vm_t;
+typedef struct njs_mod_s              njs_mod_t;
+typedef union  njs_value_s            njs_value_t;
+typedef struct njs_function_s         njs_function_t;
+typedef struct njs_vm_shared_s        njs_vm_shared_t;
+typedef struct njs_object_init_s      njs_object_init_t;
+typedef struct njs_object_prop_s      njs_object_prop_t;
+typedef struct njs_object_type_init_s njs_object_type_init_t;
+typedef struct njs_external_s         njs_external_t;
 
 /*
  * njs_opaque_value_t is the external storage type for native njs_value_t type.
diff -r 4fa5ddc91108 -r 5d814dca2327 src/njs_regexp.c
--- a/src/njs_regexp.c	Thu Apr 27 17:28:52 2023 -0700
+++ b/src/njs_regexp.c	Mon May 01 20:45:32 2023 -0700
@@ -1480,6 +1480,7 @@ njs_regexp_prototype_symbol_split(njs_vm
 {
     u_char             *dst;
     int64_t            e, i, p, q, ncaptures, length;
+    ssize_t            len;
     uint32_t           limit;
     njs_int_t          ret;
     njs_bool_t         sticky;
@@ -1650,7 +1651,9 @@ njs_regexp_prototype_symbol_split(njs_vm
             end = &s.start[q];
         }
 
-        ret = njs_string_split_part_add(vm, array, utf8, start, end - start);
+        len = njs_string_calc_length(utf8, start, end - start);
+
+        ret = njs_array_string_add(vm, array, start, end - start, len);
         if (njs_slow_path(ret != NJS_OK)) {
             return ret;
         }
@@ -1698,7 +1701,9 @@ njs_regexp_prototype_symbol_split(njs_vm
         start = &s.start[p];
     }
 
-    ret = njs_string_split_part_add(vm, array, utf8, start, end - start);
+    len = njs_string_calc_length(utf8, start, end - start);
+
+    ret = njs_array_string_add(vm, array, start, end - start, len);
     if (njs_slow_path(ret != NJS_OK)) {
         return ret;
     }
diff -r 4fa5ddc91108 -r 5d814dca2327 src/njs_string.c
--- a/src/njs_string.c	Thu Apr 27 17:28:52 2023 -0700
+++ b/src/njs_string.c	Mon May 01 20:45:32 2023 -0700
@@ -256,6 +256,49 @@ njs_string_alloc(njs_vm_t *vm, njs_value
 }
 
 
+uint32_t
+njs_string_length(njs_value_t *string)
+{
+    uint32_t  length, size;
+
+    if (string->short_string.size != NJS_STRING_LONG) {
+        size = string->short_string.size;
+        length = string->short_string.length;
+
+    } else {
+        size = string->long_string.size;
+        length = string->long_string.data->length;
+    }
+
+    return (length == 0) ? size : length;
+}
+
+
+size_t
+njs_string_prop(njs_string_prop_t *string, const njs_value_t *value)
+{
+    size_t     size;
+    uintptr_t  length;
+
+    size = value->short_string.size;
+
+    if (size != NJS_STRING_LONG) {
+        string->start = (u_char *) value->short_string.start;
+        length = value->short_string.length;
+
+    } else {
+        string->start = (u_char *) value->long_string.data->start;
+        size = value->long_string.size;
+        length = value->long_string.data->length;
+    }
+
+    string->size = size;
+    string->length = length;
+
+    return (length == 0) ? size : length;
+}
+
+
 void
 njs_string_truncate(njs_value_t *value, uint32_t size, uint32_t length)
 {
@@ -3305,6 +3348,7 @@ njs_string_prototype_split(njs_vm_t *vm,
     njs_index_t unused, njs_value_t *retval)
 {
     size_t             size;
+    ssize_t            len;
     uint32_t           limit;
     njs_int_t          ret;
     njs_utf8_t         utf8;
@@ -3429,7 +3473,9 @@ found:
 
         size = p - start;
 
-        ret = njs_string_split_part_add(vm, array, utf8, start, size);
+        len = njs_string_calc_length(utf8, start, size);
+
+        ret = njs_array_string_add(vm, array, start, size, len);
         if (njs_slow_path(ret != NJS_OK)) {
             return ret;
         }
@@ -3459,18 +3505,6 @@ done:
 
 
 njs_int_t
-njs_string_split_part_add(njs_vm_t *vm, njs_array_t *array, njs_utf8_t utf8,
-    const u_char *start, size_t size)
-{
-    ssize_t  length;
-
-    length = njs_string_calc_length(utf8, start, size);
-
-    return njs_array_string_add(vm, array, start, size, length);
-}
-
-
-njs_int_t
 njs_string_get_substitution(njs_vm_t *vm, njs_value_t *matched,
     njs_value_t *string, int64_t pos, njs_value_t *captures, int64_t ncaptures,
     njs_value_t *groups, njs_value_t *replacement, njs_value_t *retval)
diff -r 4fa5ddc91108 -r 5d814dca2327 src/njs_string.h
--- a/src/njs_string.h	Thu Apr 27 17:28:52 2023 -0700
+++ b/src/njs_string.h	Mon May 01 20:45:32 2023 -0700
@@ -104,94 +104,6 @@ typedef enum {
 } njs_trim_t;
 
 
-njs_inline njs_bool_t
-njs_is_byte_string(njs_string_prop_t *string)
-{
-    return (string->length == 0 && string->size != 0);
-}
-
-
-njs_inline njs_bool_t
-njs_is_byte_or_ascii_string(njs_string_prop_t *string)
-{
-    return (string->length == 0 || string->length == string->size);
-}
-
-
-njs_inline uint32_t
-njs_string_calc_length(njs_utf8_t utf8, const u_char *start, size_t size)
-{
-    ssize_t  length;
-
-    switch (utf8) {
-
-    case NJS_STRING_BYTE:
-        return 0;
-
-    case NJS_STRING_ASCII:
-        return size;
-
-    case NJS_STRING_UTF8:
-    default:
-        length = njs_utf8_length(start, size);
-
-        return (length >= 0) ? length : 0;
-    }
-}
-
-
-njs_inline uint32_t
-njs_string_length(njs_value_t *string)
-{
-    uint32_t  length, size;
-
-    if (string->short_string.size != NJS_STRING_LONG) {
-        size = string->short_string.size;
-        length = string->short_string.length;
-
-    } else {
-        size = string->long_string.size;
-        length = string->long_string.data->length;
-    }
-
-    return (length == 0) ? size : length;
-}
-
-
-njs_inline njs_bool_t
-njs_need_escape(const uint32_t *escape, uint32_t byte)
-{
-    return ((escape[byte >> 5] & ((uint32_t) 1 << (byte & 0x1f))) != 0);
-}
-
-
-njs_inline u_char *
-njs_string_encode(const uint32_t *escape, size_t size, const u_char *src,
-    u_char *dst)
-{
-    uint8_t              byte;
-    static const u_char  hex[16] = "0123456789ABCDEF";
-
-    do {
-        byte = *src++;
-
-        if (njs_need_escape(escape, byte)) {
-            *dst++ = '%';
-            *dst++ = hex[byte >> 4];
-            *dst++ = hex[byte & 0xf];
-
-        } else {
-            *dst++ = byte;
-        }
-
-        size--;
-
-    } while (size != 0);
-
-    return dst;
-}
-
-
 njs_int_t njs_string_set(njs_vm_t *vm, njs_value_t *value, const u_char *start,
     uint32_t size);
 u_char *njs_string_alloc(njs_vm_t *vm, njs_value_t *value, uint64_t size,
@@ -203,6 +115,9 @@ njs_int_t njs_string_create(njs_vm_t *vm
 njs_int_t njs_string_create_chb(njs_vm_t *vm, njs_value_t *value,
     njs_chb_t *chain);
 
+uint32_t njs_string_length(njs_value_t *string);
+size_t njs_string_prop(njs_string_prop_t *string, const njs_value_t *value);
+
 void njs_encode_hex(njs_str_t *dst, const njs_str_t *src);
 size_t njs_encode_hex_length(const njs_str_t *src, size_t *out_size);
 void njs_encode_base64(njs_str_t *dst, const njs_str_t *src);
@@ -258,13 +173,81 @@ njs_int_t njs_string_atob(njs_vm_t *vm, 
 
 njs_int_t njs_string_prototype_concat(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
-njs_int_t njs_string_split_part_add(njs_vm_t *vm, njs_array_t *array,
-    njs_utf8_t utf8, const u_char *start, size_t size);
 njs_int_t njs_string_get_substitution(njs_vm_t *vm, njs_value_t *matched,
     njs_value_t *string, int64_t pos, njs_value_t *captures, int64_t ncaptures,
     njs_value_t *groups, njs_value_t *replacement, njs_value_t *retval);
 
 
+njs_inline njs_bool_t
+njs_is_byte_string(njs_string_prop_t *string)
+{
+    return (string->length == 0 && string->size != 0);
+}
+
+
+njs_inline njs_bool_t
+njs_is_byte_or_ascii_string(njs_string_prop_t *string)
+{
+    return (string->length == 0 || string->length == string->size);
+}
+
+
+njs_inline uint32_t
+njs_string_calc_length(njs_utf8_t utf8, const u_char *start, size_t size)
+{
+    ssize_t  length;
+
+    switch (utf8) {
+
+    case NJS_STRING_BYTE:
+        return 0;
+
+    case NJS_STRING_ASCII:
+        return size;
+
+    case NJS_STRING_UTF8:
+    default:
+        length = njs_utf8_length(start, size);
+
+        return (length >= 0) ? length : 0;
+    }
+}
+
+
+njs_inline njs_bool_t
+njs_need_escape(const uint32_t *escape, uint32_t byte)
+{
+    return ((escape[byte >> 5] & ((uint32_t) 1 << (byte & 0x1f))) != 0);
+}
+
+
+njs_inline u_char *
+njs_string_encode(const uint32_t *escape, size_t size, const u_char *src,
+    u_char *dst)
+{
+    uint8_t              byte;
+    static const u_char  hex[16] = "0123456789ABCDEF";
+
+    do {
+        byte = *src++;
+
+        if (njs_need_escape(escape, byte)) {
+            *dst++ = '%';
+            *dst++ = hex[byte >> 4];
+            *dst++ = hex[byte & 0xf];
+
+        } else {
+            *dst++ = byte;
+        }
+
+        size--;
+
+    } while (size != 0);
+
+    return dst;
+}
+
+
 njs_inline const u_char *
 njs_string_offset(njs_string_prop_t *string, int64_t index)
 {
@@ -279,31 +262,6 @@ njs_string_offset(njs_string_prop_t *str
 }
 
 
-njs_inline size_t
-njs_string_prop(njs_string_prop_t *string, const njs_value_t *value)
-{
-    size_t     size;
-    uintptr_t  length;
-
-    size = value->short_string.size;
-
-    if (size != NJS_STRING_LONG) {
-        string->start = (u_char *) value->short_string.start;
-        length = value->short_string.length;
-
-    } else {
-        string->start = (u_char *) value->long_string.data->start;
-        size = value->long_string.size;
-        length = value->long_string.data->length;
-    }
-
-    string->size = size;
-    string->length = length;
-
-    return (length == 0) ? size : length;
-}
-
-
 extern const njs_object_init_t  njs_string_instance_init;
 extern const njs_object_type_init_t  njs_string_type_init;
 
diff -r 4fa5ddc91108 -r 5d814dca2327 src/njs_value.h
--- a/src/njs_value.h	Thu Apr 27 17:28:52 2023 -0700
+++ b/src/njs_value.h	Mon May 01 20:45:32 2023 -0700
@@ -88,7 +88,6 @@ typedef struct njs_regexp_s           nj
 typedef struct njs_date_s             njs_date_t;
 typedef struct njs_object_value_s     njs_promise_t;
 typedef struct njs_property_next_s    njs_property_next_t;
-typedef struct njs_object_init_s      njs_object_init_t;
 
 
 union njs_value_s {
@@ -305,12 +304,12 @@ typedef union {
 } njs_object_prototype_t;
 
 
-typedef struct {
+struct njs_object_type_init_s {
     njs_function_t            constructor;
     const njs_object_init_t   *constructor_props;
     const njs_object_init_t   *prototype_props;
     njs_object_prototype_t    prototype_value;
-} njs_object_type_init_t;
+};
 
 
 typedef enum {


More information about the nginx-devel mailing list