[njs] Use njs_string_get() where appropriate.

Dmitry Volyntsev xeioex at nginx.com
Fri Jun 9 14:23:02 UTC 2017


details:   http://hg.nginx.org/njs/rev/065bf91227a1
branches:  
changeset: 358:065bf91227a1
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Jun 09 17:22:27 2017 +0300
description:
Use njs_string_get() where appropriate.

diffstat:

 njs/njs_date.c   |   8 ++++----
 njs/njs_number.c |   6 +++---
 njs/njs_object.c |  18 +++---------------
 njs/njs_regexp.c |  14 ++++++++------
 njs/njs_string.c |  44 ++++++++++++++++++++++----------------------
 5 files changed, 40 insertions(+), 50 deletions(-)

diffs (288 lines):

diff -r 692ad3557d58 -r 065bf91227a1 njs/njs_date.c
--- a/njs/njs_date.c	Thu Jun 08 14:18:37 2017 +0300
+++ b/njs/njs_date.c	Fri Jun 09 17:22:27 2017 +0300
@@ -306,15 +306,15 @@ njs_date_string_parse(njs_value_t *date)
 {
     int                ext, ms, ms_length, skipped;
     double             time;
+    nxt_str_t          string;
     struct tm          tm;
     nxt_bool_t         sign, week, utc;
     const u_char       *p, *next, *end;
-    njs_string_prop_t  string;
-
-    (void) njs_string_prop(&string, date);
+
+    njs_string_get(date, &string);
 
     p = string.start;
-    end = p + string.size;
+    end = p + string.length;
 
     if (nxt_slow_path(p >= end)) {
         return NAN;
diff -r 692ad3557d58 -r 065bf91227a1 njs/njs_number.c
--- a/njs/njs_number.c	Thu Jun 08 14:18:37 2017 +0300
+++ b/njs/njs_number.c	Fri Jun 09 17:22:27 2017 +0300
@@ -741,15 +741,15 @@ njs_number_parse_int(njs_vm_t *vm, njs_v
     u_char             *p, *end;
     int64_t            n;
     uint8_t            radix;
+    nxt_str_t          string;
     nxt_bool_t         minus, test_prefix;
-    njs_string_prop_t  string;
 
     num = NAN;
 
     if (nargs > 1) {
-        (void) njs_string_prop(&string, &args[1]);
+        njs_string_get(&args[1], &string);
 
-        end = string.start + string.size;
+        end = string.start + string.length;
 
         for (p = string.start; p < end; p++) {
             if (*p != ' ') {
diff -r 692ad3557d58 -r 065bf91227a1 njs/njs_object.c
--- a/njs/njs_object.c	Thu Jun 08 14:18:37 2017 +0300
+++ b/njs/njs_object.c	Fri Jun 09 17:22:27 2017 +0300
@@ -417,8 +417,6 @@ njs_object_define_property(njs_vm_t *vm,
     njs_index_t unused)
 {
     nxt_int_t           ret;
-    nxt_str_t           key;
-    njs_value_t         *name;
     njs_object_t        *object, *descriptor;
     njs_object_prop_t   *prop, *pr;
     nxt_lvlhsh_query_t  lhq, pq;
@@ -429,26 +427,16 @@ njs_object_define_property(njs_vm_t *vm,
     }
 
     object = args[1].data.u.object;
-    name = &args[2];
     descriptor = args[3].data.u.object;
 
-    if (name->short_string.size != NJS_STRING_LONG) {
-        key.start = name->short_string.start;
-        key.length = name->short_string.length;
-
-    } else {
-        key.start = name->data.u.string->start;
-        key.length = name->data.string_size;
-    }
-
-    lhq.key = key;
-    lhq.key_hash = nxt_djb_hash(key.start, key.length);
+    njs_string_get(&args[2], &lhq.key);
+    lhq.key_hash = nxt_djb_hash(lhq.key.start, lhq.key.length);
     lhq.proto = &njs_object_hash_proto;
 
     ret = nxt_lvlhsh_find(&object->hash, &lhq);
 
     if (ret != NXT_OK) {
-        prop = njs_object_prop_alloc(vm, name);
+        prop = njs_object_prop_alloc(vm, &args[2]);
 
         if (nxt_slow_path(prop == NULL)) {
             return NXT_ERROR;
diff -r 692ad3557d58 -r 065bf91227a1 njs/njs_regexp.c
--- a/njs/njs_regexp.c	Thu Jun 08 14:18:37 2017 +0300
+++ b/njs/njs_regexp.c	Fri Jun 09 17:22:27 2017 +0300
@@ -87,7 +87,7 @@ njs_ret_t
 njs_regexp_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t unused)
 {
-    njs_string_prop_t   string;
+    nxt_str_t           string;
     njs_regexp_flags_t  flags;
 
     flags = 0;
@@ -96,13 +96,14 @@ njs_regexp_constructor(njs_vm_t *vm, njs
 
     case 1:
         string.start = NULL;
-        string.size = 0;
+        string.length = 0;
         break;
 
     default:
-        (void) njs_string_prop(&string, &args[2]);
+        njs_string_get(&args[2], &string);
 
-        flags = njs_regexp_flags(&string.start, string.start + string.size, 1);
+        flags = njs_regexp_flags(&string.start, string.start + string.length,
+                                 1);
         if (nxt_slow_path(flags < 0)) {
             return NXT_ERROR;
         }
@@ -110,11 +111,12 @@ njs_regexp_constructor(njs_vm_t *vm, njs
         /* Fall through. */
 
     case 2:
-        (void) njs_string_prop(&string, &args[1]);
+        njs_string_get(&args[1], &string);
         break;
     }
 
-    return njs_regexp_create(vm, &vm->retval, string.start, string.size, flags);
+    return njs_regexp_create(vm, &vm->retval, string.start, string.length,
+                             flags);
 }
 
 
diff -r 692ad3557d58 -r 065bf91227a1 njs/njs_string.c
--- a/njs/njs_string.c	Thu Jun 08 14:18:37 2017 +0300
+++ b/njs/njs_string.c	Fri Jun 09 17:22:27 2017 +0300
@@ -1940,15 +1940,15 @@ static njs_ret_t
 njs_string_prototype_match(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t unused)
 {
+    nxt_str_t             string;
     njs_ret_t             ret;
     njs_value_t           arguments[2];
-    njs_string_prop_t     string;
     njs_regexp_pattern_t  *pattern;
 
     arguments[1] = args[0];
 
     string.start = NULL;
-    string.size = 0;
+    string.length = 0;
 
     if (nargs > 1) {
 
@@ -1970,13 +1970,13 @@ njs_string_prototype_match(njs_vm_t *vm,
 
         if (njs_is_string(&args[1])) {
             /* string1.match(string2) is the same as /string2/.exec(string1). */
-            (void) njs_string_prop(&string, &args[1]);
+            njs_string_get(&args[1], &string);
         }
 
         /* A void value. */
     }
 
-    ret = njs_regexp_create(vm, &arguments[0], string.start, string.size, 0);
+    ret = njs_regexp_create(vm, &arguments[0], string.start, string.length, 0);
     if (nxt_slow_path(ret != NXT_OK)) {
         return ret;
     }
@@ -2559,19 +2559,19 @@ njs_string_replace_search(njs_vm_t *vm, 
     u_char             *p, *end;
     size_t             size;
     njs_ret_t          ret;
-    njs_string_prop_t  search;
-
-    (void) njs_string_prop(&search, &args[1]);
+    nxt_str_t          search;
+
+    njs_string_get(&args[1], &search);
 
     p = r->part[0].start;
-    end = (p + r->part[0].size) - (search.size - 1);
+    end = (p + r->part[0].size) - (search.length - 1);
 
     do {
-        if (memcmp(p, search.start, search.size) == 0) {
+        if (memcmp(p, search.start, search.length) == 0) {
 
             if (r->substitutions != NULL) {
                 captures[0] = p - r->part[0].start;
-                captures[1] = captures[0] + search.size;
+                captures[1] = captures[0] + search.length;
 
                 ret = njs_string_replace_substitute(vm, r, captures);
                 if (nxt_slow_path(ret != NXT_OK)) {
@@ -2579,9 +2579,9 @@ njs_string_replace_search(njs_vm_t *vm, 
                 }
 
             } else {
-                r->part[2].start = p + search.size;
+                r->part[2].start = p + search.length;
                 size = p - r->part[0].start;
-                r->part[2].size = r->part[0].size - size - search.size;
+                r->part[2].size = r->part[0].size - size - search.length;
                 r->part[0].size = size;
                 njs_set_invalid(&r->part[2].value);
 
@@ -3344,17 +3344,17 @@ njs_string_encode(njs_vm_t *vm, njs_valu
 {
     u_char               byte, *src, *dst;
     size_t               n, size;
-    njs_string_prop_t    string;
+    nxt_str_t            string;
     static const u_char  hex[16] = "0123456789ABCDEF";
 
     nxt_prefetch(escape);
 
-    (void) njs_string_prop(&string, value);
+    njs_string_get(value, &string);
 
     src = string.start;
     n = 0;
 
-    for (size = string.size; size != 0; size--) {
+    for (size = string.length; size != 0; size--) {
         byte = *src++;
 
         if ((escape[byte >> 5] & ((uint32_t) 1 << (byte & 0x1f))) != 0) {
@@ -3368,14 +3368,14 @@ njs_string_encode(njs_vm_t *vm, njs_valu
         return NXT_OK;
     }
 
-    size = string.size + n;
+    size = string.length + n;
 
     dst = njs_string_alloc(vm, &vm->retval, size, size);
     if (nxt_slow_path(dst == NULL)) {
         return NXT_ERROR;
     }
 
-    size = string.size;
+    size = string.length;
     src = string.start;
 
     do {
@@ -3477,8 +3477,8 @@ njs_string_decode(njs_vm_t *vm, njs_valu
     u_char               byte, *start, *src, *dst;
     size_t               n;
     ssize_t              size, length;
+    nxt_str_t            string;
     nxt_bool_t           utf8;
-    njs_string_prop_t    string;
 
     static const int8_t  hex[256]
         nxt_aligned(32) =
@@ -3504,12 +3504,12 @@ njs_string_decode(njs_vm_t *vm, njs_valu
     nxt_prefetch(&hex['0']);
     nxt_prefetch(reserve);
 
-    (void) njs_string_prop(&string, value);
+    njs_string_get(value, &string);
 
     src = string.start;
     n = 0;
 
-    for (size = string.size; size != 0; size--) {
+    for (size = string.length; size != 0; size--) {
         byte = *src++;
 
         if (byte == '%') {
@@ -3543,7 +3543,7 @@ njs_string_decode(njs_vm_t *vm, njs_valu
         return NXT_OK;
     }
 
-    n = string.size - n;
+    n = string.length - n;
 
     start = njs_string_alloc(vm, &vm->retval, n, n);
     if (nxt_slow_path(start == NULL)) {
@@ -3552,7 +3552,7 @@ njs_string_decode(njs_vm_t *vm, njs_valu
 
     utf8 = 0;
     dst = start;
-    size = string.size;
+    size = string.length;
     src = string.start;
 
     do {


More information about the nginx-devel mailing list