1 /*
2  *  Number-to-string conversion.  The semantics of these is very tightly
3  *  bound with the ECMAScript semantics required for call sites.
4  */
5 
6 #if !defined(DUK_NUMCONV_H_INCLUDED)
7 #define DUK_NUMCONV_H_INCLUDED
8 
9 /* Output a specified number of digits instead of using the shortest
10  * form.  Used for toPrecision() and toFixed().
11  */
12 #define DUK_N2S_FLAG_FIXED_FORMAT         (1U << 0)
13 
14 /* Force exponential format.  Used for toExponential(). */
15 #define DUK_N2S_FLAG_FORCE_EXP            (1U << 1)
16 
17 /* If number would need zero padding (for whole number part), use
18  * exponential format instead.  E.g. if input number is 12300, 3
19  * digits are generated ("123"), output "1.23e+4" instead of "12300".
20  * Used for toPrecision().
21  */
22 #define DUK_N2S_FLAG_NO_ZERO_PAD          (1U << 2)
23 
24 /* Digit count indicates number of fractions (i.e. an absolute
25  * digit index instead of a relative one).  Used together with
26  * DUK_N2S_FLAG_FIXED_FORMAT for toFixed().
27  */
28 #define DUK_N2S_FLAG_FRACTION_DIGITS      (1U << 3)
29 
30 /*
31  *  String-to-number conversion
32  */
33 
34 /* Maximum exponent value when parsing numbers.  This is not strictly
35  * compliant as there should be no upper limit, but as we parse the
36  * exponent without a bigint, impose some limit.  The limit should be
37  * small enough that multiplying it (or limit-1 to be precise) won't
38  * overflow signed 32-bit integer range.  Exponent is only parsed with
39  * radix 10, but with maximum radix (36) a safe limit is:
40  * (10000000*36).toString(16) -> '15752a00'
41  */
42 #define DUK_S2N_MAX_EXPONENT              10000000L
43 
44 /* Trim white space (= allow leading and trailing whitespace) */
45 #define DUK_S2N_FLAG_TRIM_WHITE           (1U << 0)
46 
47 /* Allow exponent */
48 #define DUK_S2N_FLAG_ALLOW_EXP            (1U << 1)
49 
50 /* Allow trailing garbage (e.g. treat "123foo" as "123) */
51 #define DUK_S2N_FLAG_ALLOW_GARBAGE        (1U << 2)
52 
53 /* Allow leading plus sign */
54 #define DUK_S2N_FLAG_ALLOW_PLUS           (1U << 3)
55 
56 /* Allow leading minus sign */
57 #define DUK_S2N_FLAG_ALLOW_MINUS          (1U << 4)
58 
59 /* Allow 'Infinity' */
60 #define DUK_S2N_FLAG_ALLOW_INF            (1U << 5)
61 
62 /* Allow fraction part */
63 #define DUK_S2N_FLAG_ALLOW_FRAC           (1U << 6)
64 
65 /* Allow naked fraction (e.g. ".123") */
66 #define DUK_S2N_FLAG_ALLOW_NAKED_FRAC     (1U << 7)
67 
68 /* Allow empty fraction (e.g. "123.") */
69 #define DUK_S2N_FLAG_ALLOW_EMPTY_FRAC     (1U << 8)
70 
71 /* Allow empty string to be interpreted as 0 */
72 #define DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO  (1U << 9)
73 
74 /* Allow leading zeroes (e.g. "0123" -> "123") */
75 #define DUK_S2N_FLAG_ALLOW_LEADING_ZERO   (1U << 10)
76 
77 /* Allow automatic detection of hex base ("0x" or "0X" prefix),
78  * overrides radix argument and forces integer mode.
79  */
80 #define DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT   (1U << 11)
81 
82 /* Allow automatic detection of legacy octal base ("0n"),
83  * overrides radix argument and forces integer mode.
84  */
85 #define DUK_S2N_FLAG_ALLOW_AUTO_LEGACY_OCT_INT   (1U << 12)
86 
87 /* Allow automatic detection of ES2015 octal base ("0o123"),
88  * overrides radix argument and forces integer mode.
89  */
90 #define DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT   (1U << 13)
91 
92 /* Allow automatic detection of ES2015 binary base ("0b10001"),
93  * overrides radix argument and forces integer mode.
94  */
95 #define DUK_S2N_FLAG_ALLOW_AUTO_BIN_INT   (1U << 14)
96 
97 /*
98  *  Prototypes
99  */
100 
101 DUK_INTERNAL_DECL void duk_numconv_stringify(duk_hthread *thr, duk_small_int_t radix, duk_small_int_t digits, duk_small_uint_t flags);
102 DUK_INTERNAL_DECL void duk_numconv_parse(duk_hthread *thr, duk_small_int_t radix, duk_small_uint_t flags);
103 
104 #endif  /* DUK_NUMCONV_H_INCLUDED */
105