[njs] Added function to get length of the string.

Alexander Borisov alexander.borisov at nginx.com
Thu Apr 18 16:10:17 UTC 2019


details:   https://hg.nginx.org/njs/rev/e4f3b3b97990
branches:  
changeset: 905:e4f3b3b97990
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Wed Apr 17 17:13:39 2019 +0300
description:
Added function to get length of the string.

Renamed function njs_string_length() to njs_string_eval_length().

diffstat:

 njs/njs_regexp.c |   2 +-
 njs/njs_string.c |   8 ++++----
 njs/njs_string.h |  20 +++++++++++++++++++-
 3 files changed, 24 insertions(+), 6 deletions(-)

diffs (88 lines):

diff -r 91a74d5ffc5d -r e4f3b3b97990 njs/njs_regexp.c
--- a/njs/njs_regexp.c	Mon Apr 08 19:01:56 2019 +0300
+++ b/njs/njs_regexp.c	Wed Apr 17 17:13:39 2019 +0300
@@ -797,7 +797,7 @@ njs_regexp_exec_result(njs_vm_t *vm, njs
             start = &string[captures[n]];
             size = captures[n + 1] - captures[n];
 
-            length = njs_string_length(utf8, start, size);
+            length = njs_string_calc_length(utf8, start, size);
 
             ret = njs_regexp_string_create(vm, &array->start[i], start, size,
                                            length);
diff -r 91a74d5ffc5d -r e4f3b3b97990 njs/njs_string.c
--- a/njs/njs_string.c	Mon Apr 08 19:01:56 2019 +0300
+++ b/njs/njs_string.c	Wed Apr 17 17:13:39 2019 +0300
@@ -2691,7 +2691,7 @@ njs_string_match_multiple(njs_vm_t *vm, 
 
                 size = captures[1] - captures[0];
 
-                length = njs_string_length(utf8, start, size);
+                length = njs_string_calc_length(utf8, start, size);
 
                 ret = njs_string_new(vm, &array->start[array->length],
                                      start, size, length);
@@ -2890,7 +2890,7 @@ njs_string_split_part_add(njs_vm_t *vm, 
 {
     ssize_t  length;
 
-    length = njs_string_length(utf8, start, size);
+    length = njs_string_calc_length(utf8, start, size);
 
     return njs_array_string_add(vm, array, start, size, length);
 }
@@ -3133,7 +3133,7 @@ njs_string_replace_regexp_function(njs_v
         size = captures[k + 1] - captures[k];
         k += 2;
 
-        length = njs_string_length(r->utf8, start, size);
+        length = njs_string_calc_length(r->utf8, start, size);
 
         ret = njs_string_new(vm, &arguments[i], start, size, length);
         if (nxt_slow_path(ret != NXT_OK)) {
@@ -3145,7 +3145,7 @@ njs_string_replace_regexp_function(njs_v
     njs_value_number_set(&arguments[n + 1], captures[0]);
 
     /* The whole string being examined. */
-    length = njs_string_length(r->utf8, r->part[0].start, r->part[0].size);
+    length = njs_string_calc_length(r->utf8, r->part[0].start, r->part[0].size);
 
     ret = njs_string_new(vm, &arguments[n + 2], r->part[0].start,
                          r->part[0].size, length);
diff -r 91a74d5ffc5d -r e4f3b3b97990 njs/njs_string.h
--- a/njs/njs_string.h	Mon Apr 08 19:01:56 2019 +0300
+++ b/njs/njs_string.h	Wed Apr 17 17:13:39 2019 +0300
@@ -100,7 +100,7 @@ typedef enum {
 
 
 nxt_inline uint32_t
-njs_string_length(njs_utf8_t utf8, const u_char *start, size_t size)
+njs_string_calc_length(njs_utf8_t utf8, const u_char *start, size_t size)
 {
     ssize_t  length;
 
@@ -121,6 +121,24 @@ njs_string_length(njs_utf8_t utf8, const
 }
 
 
+nxt_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_ret_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, uint32_t size,


More information about the nginx-devel mailing list