quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

dtoa.h (3306B)


      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 //#define JS_DTOA_DUMP_STATS
     26 
     27 /* maximum number of digits for fixed and frac formats */
     28 #define JS_DTOA_MAX_DIGITS 101
     29 
     30 /* radix != 10 is only supported with flags = JS_DTOA_FORMAT_FREE */
     31 /* use as many digits as necessary */
     32 #define JS_DTOA_FORMAT_FREE  (0 << 0)
     33 /* use n_digits significant digits (1 <= n_digits <= JS_DTOA_MAX_DIGITS) */
     34 #define JS_DTOA_FORMAT_FIXED (1 << 0)
     35 /* force fractional format: [-]dd.dd with n_digits fractional digits.
     36    0 <= n_digits <= JS_DTOA_MAX_DIGITS */
     37 #define JS_DTOA_FORMAT_FRAC  (2 << 0)
     38 #define JS_DTOA_FORMAT_MASK  (3 << 0)
     39 
     40 /* select exponential notation either in fixed or free format */
     41 #define JS_DTOA_EXP_AUTO     (0 << 2)
     42 #define JS_DTOA_EXP_ENABLED  (1 << 2)
     43 #define JS_DTOA_EXP_DISABLED (2 << 2)
     44 #define JS_DTOA_EXP_MASK     (3 << 2)
     45 
     46 #define JS_DTOA_MINUS_ZERO   (1 << 4) /* show the minus sign for -0 */
     47 
     48 /* only accepts integers (no dot, no exponent) */
     49 #define JS_ATOD_INT_ONLY       (1 << 0)
     50 /* accept Oo and Ob prefixes in addition to 0x prefix if radix = 0 */
     51 #define JS_ATOD_ACCEPT_BIN_OCT (1 << 1)
     52 /* accept O prefix as octal if radix == 0 and properly formed (Annex B) */
     53 #define JS_ATOD_ACCEPT_LEGACY_OCTAL  (1 << 2)
     54 /* accept _ between digits as a digit separator */
     55 #define JS_ATOD_ACCEPT_UNDERSCORES  (1 << 3)
     56 
     57 typedef struct {
     58     uint64_t mem[37];
     59 } JSDTOATempMem;
     60 
     61 typedef struct {
     62     uint64_t mem[27];
     63 } JSATODTempMem;
     64 
     65 /* return a maximum bound of the string length */
     66 int js_dtoa_max_len(double d, int radix, int n_digits, int flags);
     67 /* return the string length */
     68 int js_dtoa(char *buf, double d, int radix, int n_digits, int flags,
     69             JSDTOATempMem *tmp_mem);
     70 double js_atod(const char *str, const char **pnext, int radix, int flags,
     71                JSATODTempMem *tmp_mem);
     72 
     73 #ifdef JS_DTOA_DUMP_STATS
     74 void js_dtoa_dump_stats(void);
     75 #endif
     76 
     77 /* additional exported functions */
     78 size_t u32toa(char *buf, uint32_t n);
     79 size_t i32toa(char *buf, int32_t n);
     80 size_t u64toa(char *buf, uint64_t n);
     81 size_t i64toa(char *buf, int64_t n);
     82 size_t u64toa_radix(char *buf, uint64_t n, unsigned int radix);
     83 size_t i64toa_radix(char *buf, int64_t n, unsigned int radix);