[njs] Using njs_set_regexp() and njs_regexp() where approprite.

Dmitry Volyntsev xeioex at nginx.com
Mon Jul 8 14:52:36 UTC 2019


details:   https://hg.nginx.org/njs/rev/b946c1073968
branches:  
changeset: 1036:b946c1073968
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jul 08 17:51:58 2019 +0300
description:
Using njs_set_regexp() and njs_regexp() where approprite.

diffstat:

 njs/njs_regexp.c |  22 ++++++++++------------
 njs/njs_string.c |  12 ++++++------
 njs/njs_value.h  |  17 +++++++++++++++++
 njs/njs_vm.c     |   4 +---
 4 files changed, 34 insertions(+), 21 deletions(-)

diffs (200 lines):

diff -r 4c9047471ed0 -r b946c1073968 njs/njs_regexp.c
--- a/njs/njs_regexp.c	Mon Jul 08 17:51:35 2019 +0300
+++ b/njs/njs_regexp.c	Mon Jul 08 17:51:58 2019 +0300
@@ -80,7 +80,7 @@ njs_regexp_value_flags(njs_vm_t *vm, con
 
     flags = 0;
 
-    pattern = regexp->data.u.regexp->pattern;
+    pattern = njs_regexp_pattern(regexp);
 
     if (pattern->global) {
         flags |= NJS_REGEXP_GLOBAL;
@@ -195,9 +195,7 @@ njs_regexp_create(njs_vm_t *vm, njs_valu
     regexp = njs_regexp_alloc(vm, pattern);
 
     if (nxt_fast_path(regexp != NULL)) {
-        value->data.u.regexp = regexp;
-        value->type = NJS_REGEXP;
-        value->data.truth = 1;
+        njs_set_regexp(value, regexp);
 
         return NXT_OK;
     }
@@ -729,7 +727,7 @@ njs_regexp_prototype_last_index(njs_vm_t
 
     njs_release(vm, value);
 
-    regexp = value->data.u.regexp;
+    regexp = njs_regexp(value);
 
     (void) njs_string_prop(&string, &regexp->string);
 
@@ -746,7 +744,7 @@ njs_regexp_prototype_global(njs_vm_t *vm
 {
     njs_regexp_pattern_t  *pattern;
 
-    pattern = value->data.u.regexp->pattern;
+    pattern = njs_regexp_pattern(value);
     *retval = pattern->global ? njs_value_true : njs_value_false;
     njs_release(vm, value);
 
@@ -760,7 +758,7 @@ njs_regexp_prototype_ignore_case(njs_vm_
 {
     njs_regexp_pattern_t  *pattern;
 
-    pattern = value->data.u.regexp->pattern;
+    pattern = njs_regexp_pattern(value);
     *retval = pattern->ignore_case ? njs_value_true : njs_value_false;
     njs_release(vm, value);
 
@@ -774,7 +772,7 @@ njs_regexp_prototype_multiline(njs_vm_t 
 {
     njs_regexp_pattern_t  *pattern;
 
-    pattern = value->data.u.regexp->pattern;
+    pattern = njs_regexp_pattern(value);
     *retval = pattern->multiline ? njs_value_true : njs_value_false;
     njs_release(vm, value);
 
@@ -791,7 +789,7 @@ njs_regexp_prototype_source(njs_vm_t *vm
     uint32_t              size;
     njs_regexp_pattern_t  *pattern;
 
-    pattern = value->data.u.regexp->pattern;
+    pattern = njs_regexp_pattern(value);
     /* Skip starting "/". */
     source = pattern->source + 1;
 
@@ -825,7 +823,7 @@ njs_regexp_to_string(njs_vm_t *vm, njs_v
     uint32_t              size;
     njs_regexp_pattern_t  *pattern;
 
-    pattern = value->data.u.regexp->pattern;
+    pattern = njs_regexp_pattern(value);
     source = pattern->source;
 
     size = nxt_strlen(source);
@@ -861,7 +859,7 @@ njs_regexp_prototype_test(njs_vm_t *vm, 
 
     n = (string.length != 0);
 
-    pattern = args[0].data.u.regexp->pattern;
+    pattern = njs_regexp_pattern(&args[0]);
 
     if (nxt_regex_is_valid(&pattern->regex[n])) {
         ret = njs_regexp_match(vm, &pattern->regex[n], string.start,
@@ -903,7 +901,7 @@ njs_regexp_prototype_exec(njs_vm_t *vm, 
         value = &njs_string_undefined;
     }
 
-    regexp = args[0].data.u.regexp;
+    regexp = njs_regexp(&args[0]);
     regexp->string = *value;
 
     (void) njs_string_prop(&string, value);
diff -r 4c9047471ed0 -r b946c1073968 njs/njs_string.c
--- a/njs/njs_string.c	Mon Jul 08 17:51:35 2019 +0300
+++ b/njs/njs_string.c	Mon Jul 08 17:51:58 2019 +0300
@@ -2567,7 +2567,7 @@ njs_string_prototype_search(njs_vm_t *vm
         switch (args[1].type) {
 
         case NJS_REGEXP:
-            pattern = args[1].data.u.regexp->pattern;
+            pattern = njs_regexp_pattern(&args[1]);
             break;
 
         case NJS_STRING:
@@ -2638,7 +2638,7 @@ njs_string_prototype_match(njs_vm_t *vm,
     if (nargs > 1) {
 
         if (njs_is_regexp(&args[1])) {
-            pattern = args[1].data.u.regexp->pattern;
+            pattern = njs_regexp_pattern(&args[1]);
 
             if (pattern->global) {
                 return njs_string_match_multiple(vm, args, pattern);
@@ -2867,7 +2867,7 @@ found:
             goto done;
 
         case NJS_REGEXP:
-            pattern = args[1].data.u.regexp->pattern;
+            pattern = njs_regexp_pattern(&args[1]);
 
             if (!nxt_regex_is_valid(&pattern->regex[type])) {
                 goto single;
@@ -2986,7 +2986,7 @@ njs_string_prototype_replace(njs_vm_t *v
     }
 
     if (njs_is_regexp(&args[1])) {
-        regex = &args[1].data.u.regexp->pattern->regex[r->type];
+        regex = &njs_regexp_pattern(&args[1])->regex[r->type];
 
         if (!nxt_regex_is_valid(regex)) {
             goto original;
@@ -3076,7 +3076,7 @@ njs_string_replace_regexp(njs_vm_t *vm, 
     njs_regexp_pattern_t       *pattern;
     njs_string_replace_part_t  replace;
 
-    pattern = args[1].data.u.regexp->pattern;
+    pattern = njs_regexp_pattern(&args[1]);
     end = r->part[0].start + r->part[0].size;
 
     replace = r->part[1];
@@ -3266,7 +3266,7 @@ njs_string_replace_regexp_continuation(n
     if (njs_is_string(&r->retval)) {
         njs_string_replacement_copy(&r->part[r->empty ? 0 : 1], &r->retval);
 
-        if (args[1].data.u.regexp->pattern->global) {
+        if (njs_regexp_pattern(&args[1])->global) {
             r->part += 2;
 
             if (r->part[0].start > (string.start + string.size)) {
diff -r 4c9047471ed0 -r b946c1073968 njs/njs_value.h
--- a/njs/njs_value.h	Mon Jul 08 17:51:35 2019 +0300
+++ b/njs/njs_value.h	Mon Jul 08 17:51:58 2019 +0300
@@ -544,6 +544,14 @@ typedef enum {
     ((value)->data.u.date)
 
 
+#define njs_regexp(value)                                                     \
+    ((value)->data.u.regexp)
+
+
+#define njs_regexp_pattern(value)                                             \
+    ((value)->data.u.regexp->pattern)
+
+
 #define njs_object_value(_value)                                              \
     (&(_value)->data.u.object_value->value)
 
@@ -629,6 +637,15 @@ njs_set_date(njs_value_t *value, njs_dat
 
 
 nxt_inline void
+njs_set_regexp(njs_value_t *value, njs_regexp_t *regexp)
+{
+    value->data.u.regexp = regexp;
+    value->type = NJS_REGEXP;
+    value->data.truth = 1;
+}
+
+
+nxt_inline void
 njs_set_object_value(njs_value_t *value, njs_object_value_t *object_value)
 {
     value->data.u.object_value = object_value;
diff -r 4c9047471ed0 -r b946c1073968 njs/njs_vm.c
--- a/njs/njs_vm.c	Mon Jul 08 17:51:35 2019 +0300
+++ b/njs/njs_vm.c	Mon Jul 08 17:51:58 2019 +0300
@@ -354,9 +354,7 @@ njs_vmcode_regexp(njs_vm_t *vm, njs_valu
     regexp = njs_regexp_alloc(vm, code->pattern);
 
     if (nxt_fast_path(regexp != NULL)) {
-        vm->retval.data.u.regexp = regexp;
-        vm->retval.type = NJS_REGEXP;
-        vm->retval.data.truth = 1;
+        njs_set_regexp(&vm->retval, regexp);
 
         return sizeof(njs_vmcode_regexp_t);
     }


More information about the nginx-devel mailing list