[njs] parseFloat() function.

Igor Sysoev igor at sysoev.ru
Wed Jun 1 12:50:33 UTC 2016


details:   http://hg.nginx.org/njs/rev/169958fd3f70
branches:  
changeset: 110:169958fd3f70
user:      Igor Sysoev <igor at sysoev.ru>
date:      Mon Apr 25 19:30:27 2016 +0300
description:
parseFloat() function.

diffstat:

 njs/njs_builtin.c        |   2 ++
 njs/njs_generator.c      |   1 +
 njs/njs_lexer_keyword.c  |   1 +
 njs/njs_number.c         |  20 +++++++++++++++++++-
 njs/njs_number.h         |   2 ++
 njs/njs_parser.c         |   1 +
 njs/njs_parser.h         |   1 +
 njs/njs_string.c         |  14 ++++++++------
 njs/njs_string.h         |   2 +-
 njs/njs_vm.c             |   4 ++--
 njs/njs_vm.h             |   3 ++-
 njs/test/njs_unit_test.c |   3 +++
 12 files changed, 43 insertions(+), 11 deletions(-)

diffs (200 lines):

diff -r ffe9a5658450 -r 169958fd3f70 njs/njs_builtin.c
--- a/njs/njs_builtin.c	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/njs_builtin.c	Mon Apr 25 19:30:27 2016 +0300
@@ -84,6 +84,7 @@ njs_builtin_objects_create(njs_vm_t *vm)
         NULL,
         NULL,
         NULL,
+        NULL,
     };
 
     static const njs_function_init_t  native_functions[] = {
@@ -94,6 +95,7 @@ njs_builtin_objects_create(njs_vm_t *vm)
         { njs_number_is_finite,            { NJS_SKIP_ARG, NJS_NUMBER_ARG } },
         { njs_number_parse_int,
           { NJS_SKIP_ARG, NJS_STRING_ARG, NJS_INTEGER_ARG } },
+        { njs_number_parse_float,          { NJS_SKIP_ARG, NJS_STRING_ARG } },
     };
 
     static const njs_object_prop_t    null_proto_property = {
diff -r ffe9a5658450 -r 169958fd3f70 njs/njs_generator.c
--- a/njs/njs_generator.c	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/njs_generator.c	Mon Apr 25 19:30:27 2016 +0300
@@ -298,6 +298,7 @@ njs_generator(njs_vm_t *vm, njs_parser_t
     case NJS_TOKEN_IS_NAN:
     case NJS_TOKEN_IS_FINITE:
     case NJS_TOKEN_PARSE_INT:
+    case NJS_TOKEN_PARSE_FLOAT:
         return njs_generate_builtin_object(vm, parser, node);
 
     case NJS_TOKEN_FUNCTION:
diff -r ffe9a5658450 -r 169958fd3f70 njs/njs_lexer_keyword.c
--- a/njs/njs_lexer_keyword.c	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/njs_lexer_keyword.c	Mon Apr 25 19:30:27 2016 +0300
@@ -92,6 +92,7 @@ static const njs_keyword_t  njs_keywords
     { nxt_string("isNaN"),         NJS_TOKEN_IS_NAN, 0 },
     { nxt_string("isFinite"),      NJS_TOKEN_IS_FINITE, 0 },
     { nxt_string("parseInt"),      NJS_TOKEN_PARSE_INT, 0 },
+    { nxt_string("parseFloat"),    NJS_TOKEN_PARSE_FLOAT, 0 },
 
     /* Reserved words. */
 
diff -r ffe9a5658450 -r 169958fd3f70 njs/njs_number.c
--- a/njs/njs_number.c	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/njs_number.c	Mon Apr 25 19:30:27 2016 +0300
@@ -36,7 +36,7 @@ njs_value_to_number(njs_value_t *value)
     }
 
     if (njs_is_string(value)) {
-        return njs_string_to_number(value);
+        return njs_string_to_number(value, 1);
     }
 
     if (njs_is_array(value)) {
@@ -549,3 +549,21 @@ done:
 
     return NXT_OK;
 }
+
+
+njs_ret_t
+njs_number_parse_float(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+    njs_index_t unused)
+{
+    double  num;
+
+    num = NJS_NAN;
+
+    if (nargs > 1) {
+        num = njs_string_to_number(&args[1], 0);
+    }
+
+    njs_number_set(&vm->retval, num);
+
+    return NXT_OK;
+}
diff -r ffe9a5658450 -r 169958fd3f70 njs/njs_number.h
--- a/njs/njs_number.h	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/njs_number.h	Mon Apr 25 19:30:27 2016 +0300
@@ -37,6 +37,8 @@ njs_ret_t njs_number_is_finite(njs_vm_t 
     nxt_uint_t nargs, njs_index_t unused);
 njs_ret_t njs_number_parse_int(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused);
+njs_ret_t njs_number_parse_float(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, njs_index_t unused);
 
 
 extern const njs_object_init_t  njs_number_constructor_init;
diff -r ffe9a5658450 -r 169958fd3f70 njs/njs_parser.c
--- a/njs/njs_parser.c	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/njs_parser.c	Mon Apr 25 19:30:27 2016 +0300
@@ -1648,6 +1648,7 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
     case NJS_TOKEN_IS_NAN:
     case NJS_TOKEN_IS_FINITE:
     case NJS_TOKEN_PARSE_INT:
+    case NJS_TOKEN_PARSE_FLOAT:
         return njs_parser_builtin_function(vm, parser, node);
 
     default:
diff -r ffe9a5658450 -r 169958fd3f70 njs/njs_parser.h
--- a/njs/njs_parser.h	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/njs_parser.h	Mon Apr 25 19:30:27 2016 +0300
@@ -179,6 +179,7 @@ typedef enum {
     NJS_TOKEN_IS_NAN,
     NJS_TOKEN_IS_FINITE,
     NJS_TOKEN_PARSE_INT,
+    NJS_TOKEN_PARSE_FLOAT,
 
     NJS_TOKEN_RESERVED,
 } njs_token_t;
diff -r ffe9a5658450 -r 169958fd3f70 njs/njs_string.c
--- a/njs/njs_string.c	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/njs_string.c	Mon Apr 25 19:30:27 2016 +0300
@@ -1678,7 +1678,7 @@ njs_primitive_value_to_string(njs_vm_t *
 
 
 double
-njs_string_to_number(njs_value_t *value)
+njs_string_to_number(njs_value_t *value, nxt_bool_t exact)
 {
     double        num;
     size_t        size;
@@ -1737,12 +1737,14 @@ njs_string_to_number(njs_value_t *value)
         p += infinity;
     }
 
-    while (p < end) {
-        if (*p != ' ' && *p != '\t') {
-            return NJS_NAN;
+    if (exact) {
+        while (p < end) {
+            if (*p != ' ' && *p != '\t') {
+                return NJS_NAN;
+            }
+
+            p++;
         }
-
-        p++;
     }
 
     return minus ? -num : num;
diff -r ffe9a5658450 -r 169958fd3f70 njs/njs_string.h
--- a/njs/njs_string.h	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/njs_string.h	Mon Apr 25 19:30:27 2016 +0300
@@ -103,7 +103,7 @@ nxt_noinline uint32_t njs_string_index(n
     uint32_t offset);
 njs_ret_t njs_primitive_value_to_string(njs_vm_t *vm, njs_value_t *dst,
     const njs_value_t *src);
-double njs_string_to_number(njs_value_t *value);
+double njs_string_to_number(njs_value_t *value, nxt_bool_t exact);
 
 njs_index_t njs_value_index(njs_vm_t *vm, njs_parser_t *parser,
     const njs_value_t *src);
diff -r ffe9a5658450 -r 169958fd3f70 njs/njs_vm.c
--- a/njs/njs_vm.c	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/njs_vm.c	Mon Apr 25 19:30:27 2016 +0300
@@ -2915,7 +2915,7 @@ njs_vmcode_number_primitive(njs_vm_t *vm
             num = NJS_NAN;
 
             if (njs_is_string(value)) {
-                num = njs_string_to_number(value);
+                num = njs_string_to_number(value, 1);
             }
 
             njs_number_set(value, num);
@@ -2968,7 +2968,7 @@ njs_vmcode_number_argument(njs_vm_t *vm,
             num = NJS_NAN;
 
             if (njs_is_string(value)) {
-                num = njs_string_to_number(value);
+                num = njs_string_to_number(value, 1);
             }
 
             njs_number_set(value, num);
diff -r ffe9a5658450 -r 169958fd3f70 njs/njs_vm.h
--- a/njs/njs_vm.h	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/njs_vm.h	Mon Apr 25 19:30:27 2016 +0300
@@ -705,7 +705,8 @@ enum njs_function_e {
     NJS_FUNCTION_IS_NAN =      2,
     NJS_FUNCTION_IS_FINITE =   3,
     NJS_FUNCTION_PARSE_INT =   4,
-#define NJS_FUNCTION_MAX       (NJS_FUNCTION_PARSE_INT + 1)
+    NJS_FUNCTION_PARSE_FLOAT = 5,
+#define NJS_FUNCTION_MAX       (NJS_FUNCTION_PARSE_FLOAT + 1)
 };
 
 
diff -r ffe9a5658450 -r 169958fd3f70 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Apr 25 17:28:34 2016 +0300
+++ b/njs/test/njs_unit_test.c	Mon Apr 25 19:30:27 2016 +0300
@@ -4449,6 +4449,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("parseInt('njscript', 36)"),
       nxt_string("1845449130881") },
 
+    { nxt_string("parseFloat('12345abc')"),
+      nxt_string("12345") },
+
     /* External interface. */
 
     { nxt_string("function f(req) { return req.uri }"),



More information about the nginx-devel mailing list