Line | Count | Source |
1 | | /* |
2 | | * Tiny float64 printing and parsing library |
3 | | * |
4 | | * Copyright (c) 2024 Fabrice Bellard |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to deal |
8 | | * in the Software without restriction, including without limitation the rights |
9 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 | | * copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
19 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22 | | * THE SOFTWARE. |
23 | | */ |
24 | | |
25 | | #ifndef _NJS_DTOA_H_INCLUDED_ |
26 | | #define _NJS_DTOA_H_INCLUDED_ |
27 | | |
28 | | #include <njs_types.h> |
29 | | #include <njs_clang.h> |
30 | | |
31 | | //#define JS_DTOA_DUMP_STATS |
32 | | |
33 | | /* maximum number of digits for fixed and frac formats */ |
34 | | #define JS_DTOA_MAX_DIGITS 101 |
35 | | |
36 | | /* radix != 10 is only supported with flags = JS_DTOA_FORMAT_FREE */ |
37 | | /* use as many digits as necessary */ |
38 | 157M | #define JS_DTOA_FORMAT_FREE (0 << 0) |
39 | | /* use n_digits significant digits (1 <= n_digits <= JS_DTOA_MAX_DIGITS) */ |
40 | 14.2M | #define JS_DTOA_FORMAT_FIXED (1 << 0) |
41 | | /* force fractional format: [-]dd.dd with n_digits fractional digits. |
42 | | 0 <= n_digits <= JS_DTOA_MAX_DIGITS */ |
43 | 4.76k | #define JS_DTOA_FORMAT_FRAC (2 << 0) |
44 | 65.4M | #define JS_DTOA_FORMAT_MASK (3 << 0) |
45 | | |
46 | | /* select exponential notation either in fixed or free format */ |
47 | 28.4M | #define JS_DTOA_EXP_AUTO (0 << 2) |
48 | 67.3M | #define JS_DTOA_EXP_ENABLED (1 << 2) |
49 | 14.2k | #define JS_DTOA_EXP_DISABLED (2 << 2) |
50 | 67.3M | #define JS_DTOA_EXP_MASK (3 << 2) |
51 | | |
52 | 8.49k | #define JS_DTOA_MINUS_ZERO (1 << 4) /* show the minus sign for -0 */ |
53 | | |
54 | | /* only accepts integers (no dot, no exponent) */ |
55 | 35.2M | #define JS_ATOD_INT_ONLY (1 << 0) |
56 | | /* accept Oo and Ob prefixes in addition to 0x prefix if radix = 0 */ |
57 | 5.09M | #define JS_ATOD_ACCEPT_BIN_OCT (1 << 1) |
58 | | /* accept O prefix as octal if radix == 0 and properly formed (Annex B) */ |
59 | 62.0k | #define JS_ATOD_ACCEPT_LEGACY_OCTAL (1 << 2) |
60 | | /* accept _ between digits as a digit separator */ |
61 | 18.8M | #define JS_ATOD_ACCEPT_UNDERSCORES (1 << 3) |
62 | | /* accept radix prefix (0x, 0o, 0b) after sign (+/-) */ |
63 | 181k | #define JS_ATOD_ACCEPT_PREFIX_AFTER_SIGN (1 << 4) |
64 | | |
65 | | typedef struct { |
66 | | uint64_t mem[37]; |
67 | | } JSDTOATempMem; |
68 | | |
69 | | typedef struct { |
70 | | uint64_t mem[27]; |
71 | | } JSATODTempMem; |
72 | | |
73 | | /* return a maximum bound of the string length */ |
74 | | int njs_dtoa_max_len(double d, int radix, int n_digits, int flags); |
75 | | /* return the string length */ |
76 | | int njs_dtoa2(char *buf, double d, int radix, int n_digits, int flags, |
77 | | JSDTOATempMem *tmp_mem); |
78 | | double njs_atod2(const char *str, const char **pnext, int radix, int flags, |
79 | | JSATODTempMem *tmp_mem); |
80 | | |
81 | | #ifdef JS_DTOA_DUMP_STATS |
82 | | void njs_dtoa_dump_stats(void); |
83 | | #endif |
84 | | |
85 | | /* additional exported functions */ |
86 | | size_t njs_u32toa(char *buf, uint32_t n); |
87 | | size_t njs_i32toa(char *buf, int32_t n); |
88 | | size_t njs_u64toa(char *buf, uint64_t n); |
89 | | size_t njs_i64toa(char *buf, int64_t n); |
90 | | size_t njs_u64toa_radix(char *buf, uint64_t n, unsigned int radix); |
91 | | size_t njs_i64toa_radix(char *buf, int64_t n, unsigned int radix); |
92 | | |
93 | | |
94 | | njs_inline size_t |
95 | | njs_dtoa(double value, char *start) |
96 | 65.4M | { |
97 | 65.4M | JSDTOATempMem tmp_mem; |
98 | | |
99 | 65.4M | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); |
100 | 65.4M | } Unexecuted instantiation: njs_arr.c:njs_dtoa Unexecuted instantiation: njs_rbtree.c:njs_dtoa Unexecuted instantiation: njs_mp.c:njs_dtoa Unexecuted instantiation: njs_sprintf.c:njs_dtoa Line | Count | Source | 96 | 31.9M | { | 97 | 31.9M | JSDTOATempMem tmp_mem; | 98 | | | 99 | 31.9M | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); | 100 | 31.9M | } |
Unexecuted instantiation: njs_atom.c:njs_dtoa Unexecuted instantiation: njs_vm.c:njs_dtoa Line | Count | Source | 96 | 232k | { | 97 | 232k | JSDTOATempMem tmp_mem; | 98 | | | 99 | 232k | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); | 100 | 232k | } |
Unexecuted instantiation: njs_parser.c:njs_dtoa Unexecuted instantiation: njs_variable.c:njs_dtoa Unexecuted instantiation: njs_scope.c:njs_dtoa Unexecuted instantiation: njs_generator.c:njs_dtoa Unexecuted instantiation: njs_disassembler.c:njs_dtoa Line | Count | Source | 96 | 8.07k | { | 97 | 8.07k | JSDTOATempMem tmp_mem; | 98 | | | 99 | 8.07k | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); | 100 | 8.07k | } |
Unexecuted instantiation: njs_extern.c:njs_dtoa Line | Count | Source | 96 | 12.9M | { | 97 | 12.9M | JSDTOATempMem tmp_mem; | 98 | | | 99 | 12.9M | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); | 100 | 12.9M | } |
Unexecuted instantiation: njs_symbol.c:njs_dtoa Line | Count | Source | 96 | 15.6M | { | 97 | 15.6M | JSDTOATempMem tmp_mem; | 98 | | | 99 | 15.6M | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); | 100 | 15.6M | } |
Line | Count | Source | 96 | 4.62M | { | 97 | 4.62M | JSDTOATempMem tmp_mem; | 98 | | | 99 | 4.62M | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); | 100 | 4.62M | } |
njs_object_prop.c:njs_dtoa Line | Count | Source | 96 | 1.50k | { | 97 | 1.50k | JSDTOATempMem tmp_mem; | 98 | | | 99 | 1.50k | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); | 100 | 1.50k | } |
Unexecuted instantiation: njs_array.c:njs_dtoa Line | Count | Source | 96 | 9.78k | { | 97 | 9.78k | JSDTOATempMem tmp_mem; | 98 | | | 99 | 9.78k | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); | 100 | 9.78k | } |
Unexecuted instantiation: njs_function.c:njs_dtoa Line | Count | Source | 96 | 35.7k | { | 97 | 35.7k | JSDTOATempMem tmp_mem; | 98 | | | 99 | 35.7k | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); | 100 | 35.7k | } |
Line | Count | Source | 96 | 3.94k | { | 97 | 3.94k | JSDTOATempMem tmp_mem; | 98 | | | 99 | 3.94k | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); | 100 | 3.94k | } |
Unexecuted instantiation: njs_error.c:njs_dtoa Unexecuted instantiation: njs_array_buffer.c:njs_dtoa Unexecuted instantiation: njs_typed_array.c:njs_dtoa Unexecuted instantiation: njs_promise.c:njs_dtoa Unexecuted instantiation: njs_iterator.c:njs_dtoa Unexecuted instantiation: njs_async.c:njs_dtoa Unexecuted instantiation: njs_builtin.c:njs_dtoa Unexecuted instantiation: njs_regex.c:njs_dtoa Line | Count | Source | 96 | 4.94k | { | 97 | 4.94k | JSDTOATempMem tmp_mem; | 98 | | | 99 | 4.94k | return njs_dtoa2(start, value, 10, 0, JS_DTOA_FORMAT_FREE, &tmp_mem); | 100 | 4.94k | } |
Unexecuted instantiation: njs_modules.c:njs_dtoa Unexecuted instantiation: njs_dtoa.c:njs_dtoa Unexecuted instantiation: njs_djb_hash.c:njs_dtoa Unexecuted instantiation: njs_utf8.c:njs_dtoa Unexecuted instantiation: njs_utf16.c:njs_dtoa Unexecuted instantiation: njs_flathsh.c:njs_dtoa Unexecuted instantiation: njs_trace.c:njs_dtoa Unexecuted instantiation: njs_malloc.c:njs_dtoa Unexecuted instantiation: njs_utils.c:njs_dtoa Unexecuted instantiation: njs_chb.c:njs_dtoa Unexecuted instantiation: njs_lexer.c:njs_dtoa Unexecuted instantiation: njs_boolean.c:njs_dtoa Unexecuted instantiation: njs_math.c:njs_dtoa Unexecuted instantiation: njs_encoding.c:njs_dtoa Unexecuted instantiation: njs_str.c:njs_dtoa Unexecuted instantiation: njs_random.c:njs_dtoa |
101 | | |
102 | | /* Expects a null-terminated string. */ |
103 | | njs_inline double |
104 | | njs_atod(const char *str, const char **pnext, int radix, int flags) |
105 | 17.6M | { |
106 | 17.6M | JSATODTempMem tmp_mem; |
107 | | |
108 | 17.6M | return njs_atod2(str, pnext, radix, flags, &tmp_mem); |
109 | 17.6M | } Unexecuted instantiation: njs_arr.c:njs_atod Unexecuted instantiation: njs_rbtree.c:njs_atod Unexecuted instantiation: njs_mp.c:njs_atod Unexecuted instantiation: njs_sprintf.c:njs_atod Unexecuted instantiation: njs_value.c:njs_atod Unexecuted instantiation: njs_atom.c:njs_atod Unexecuted instantiation: njs_vm.c:njs_atod Unexecuted instantiation: njs_vmcode.c:njs_atod Unexecuted instantiation: njs_parser.c:njs_atod Unexecuted instantiation: njs_variable.c:njs_atod Unexecuted instantiation: njs_scope.c:njs_atod Unexecuted instantiation: njs_generator.c:njs_atod Unexecuted instantiation: njs_disassembler.c:njs_atod Unexecuted instantiation: njs_module.c:njs_atod Unexecuted instantiation: njs_extern.c:njs_atod Line | Count | Source | 105 | 24.3k | { | 106 | 24.3k | JSATODTempMem tmp_mem; | 107 | | | 108 | 24.3k | return njs_atod2(str, pnext, radix, flags, &tmp_mem); | 109 | 24.3k | } |
Unexecuted instantiation: njs_symbol.c:njs_atod Line | Count | Source | 105 | 16.3M | { | 106 | 16.3M | JSATODTempMem tmp_mem; | 107 | | | 108 | 16.3M | return njs_atod2(str, pnext, radix, flags, &tmp_mem); | 109 | 16.3M | } |
Unexecuted instantiation: njs_object.c:njs_atod Unexecuted instantiation: njs_object_prop.c:njs_atod Unexecuted instantiation: njs_array.c:njs_atod Line | Count | Source | 105 | 84.1k | { | 106 | 84.1k | JSATODTempMem tmp_mem; | 107 | | | 108 | 84.1k | return njs_atod2(str, pnext, radix, flags, &tmp_mem); | 109 | 84.1k | } |
Unexecuted instantiation: njs_function.c:njs_atod Unexecuted instantiation: njs_regexp.c:njs_atod Unexecuted instantiation: njs_date.c:njs_atod Unexecuted instantiation: njs_error.c:njs_atod Unexecuted instantiation: njs_array_buffer.c:njs_atod Unexecuted instantiation: njs_typed_array.c:njs_atod Unexecuted instantiation: njs_promise.c:njs_atod Unexecuted instantiation: njs_iterator.c:njs_atod Unexecuted instantiation: njs_async.c:njs_atod Unexecuted instantiation: njs_builtin.c:njs_atod Unexecuted instantiation: njs_regex.c:njs_atod Unexecuted instantiation: njs_buffer.c:njs_atod Unexecuted instantiation: njs_modules.c:njs_atod Unexecuted instantiation: njs_dtoa.c:njs_atod Unexecuted instantiation: njs_djb_hash.c:njs_atod Unexecuted instantiation: njs_utf8.c:njs_atod Unexecuted instantiation: njs_utf16.c:njs_atod Unexecuted instantiation: njs_flathsh.c:njs_atod Unexecuted instantiation: njs_trace.c:njs_atod Unexecuted instantiation: njs_malloc.c:njs_atod Unexecuted instantiation: njs_utils.c:njs_atod Unexecuted instantiation: njs_chb.c:njs_atod Line | Count | Source | 105 | 1.18M | { | 106 | 1.18M | JSATODTempMem tmp_mem; | 107 | | | 108 | 1.18M | return njs_atod2(str, pnext, radix, flags, &tmp_mem); | 109 | 1.18M | } |
Unexecuted instantiation: njs_boolean.c:njs_atod Unexecuted instantiation: njs_math.c:njs_atod Unexecuted instantiation: njs_encoding.c:njs_atod Unexecuted instantiation: njs_str.c:njs_atod Unexecuted instantiation: njs_random.c:njs_atod |
110 | | |
111 | | #endif /* _NJS_DTOA_H_INCLUDED_ */ |