michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef jsdtoa_h michael@0: #define jsdtoa_h michael@0: michael@0: /* michael@0: * Public interface to portable double-precision floating point to string michael@0: * and back conversion package. michael@0: */ michael@0: michael@0: #include michael@0: michael@0: struct DtoaState; michael@0: michael@0: DtoaState * michael@0: js_NewDtoaState(); michael@0: michael@0: void michael@0: js_DestroyDtoaState(DtoaState *state); michael@0: michael@0: /* michael@0: * js_strtod_harder() returns as a double-precision floating-point number the michael@0: * value represented by the character string pointed to by s00. The string is michael@0: * scanned up to the first unrecognized character. michael@0: * michael@0: * If se is not nullptr, *se receives a pointer to the character terminating michael@0: * the scan. If no number can be formed, *se receives a pointer to the first michael@0: * unparseable character in s00, and zero is returned. michael@0: * michael@0: * On overflow, this function returns infinity and does not indicate an error. michael@0: * michael@0: * *err is set to zero on success; it's set to JS_DTOA_ENOMEM on memory failure. michael@0: */ michael@0: #define JS_DTOA_ENOMEM 2 michael@0: double michael@0: js_strtod_harder(DtoaState *state, const char *s00, char **se, int *err); michael@0: michael@0: /* michael@0: * Modes for converting floating-point numbers to strings. michael@0: * michael@0: * Some of the modes can round-trip; this means that if the number is converted to michael@0: * a string using one of these mode and then converted back to a number, the result michael@0: * will be identical to the original number (except that, due to ECMA, -0 will get converted michael@0: * to +0). These round-trip modes return the minimum number of significand digits that michael@0: * permit the round trip. michael@0: * michael@0: * Some of the modes take an integer parameter . michael@0: */ michael@0: /* NB: Keep this in sync with number_constants[]. */ michael@0: typedef enum JSDToStrMode { michael@0: DTOSTR_STANDARD, /* Either fixed or exponential format; round-trip */ michael@0: DTOSTR_STANDARD_EXPONENTIAL, /* Always exponential format; round-trip */ michael@0: DTOSTR_FIXED, /* Round to digits after the decimal point; exponential if number is large */ michael@0: DTOSTR_EXPONENTIAL, /* Always exponential format; significant digits */ michael@0: DTOSTR_PRECISION /* Either fixed or exponential format; significant digits */ michael@0: } JSDToStrMode; michael@0: michael@0: michael@0: /* Maximum number of characters (including trailing null) that a DTOSTR_STANDARD or DTOSTR_STANDARD_EXPONENTIAL michael@0: * conversion can produce. This maximum is reached for a number like -0.0000012345678901234567. */ michael@0: #define DTOSTR_STANDARD_BUFFER_SIZE 26 michael@0: michael@0: /* Maximum number of characters (including trailing null) that one of the other conversions michael@0: * can produce. This maximum is reached for TO_FIXED, which can generate up to 21 digits before the decimal point. */ michael@0: #define DTOSTR_VARIABLE_BUFFER_SIZE(precision) ((precision)+24 > DTOSTR_STANDARD_BUFFER_SIZE ? (precision)+24 : DTOSTR_STANDARD_BUFFER_SIZE) michael@0: michael@0: /* michael@0: * DO NOT USE THIS FUNCTION IF YOU CAN AVOID IT. js::NumberToCString() is a michael@0: * better function to use. michael@0: * michael@0: * Convert dval according to the given mode and return a pointer to the michael@0: * resulting ASCII string. If mode == DTOSTR_STANDARD and precision == 0 it's michael@0: * equivalent to ToString() as specified by ECMA-262-5 section 9.8.1, but it michael@0: * doesn't handle integers specially so should be avoided in that case (that's michael@0: * why js::NumberToCString() is better). michael@0: * michael@0: * The result is held somewhere in buffer, but not necessarily at the michael@0: * beginning. The size of buffer is given in bufferSize, and must be at least michael@0: * as large as given by the above macros. michael@0: * michael@0: * Return nullptr if out of memory. michael@0: */ michael@0: char * michael@0: js_dtostr(DtoaState *state, char *buffer, size_t bufferSize, JSDToStrMode mode, int precision, michael@0: double dval); michael@0: michael@0: /* michael@0: * DO NOT USE THIS FUNCTION IF YOU CAN AVOID IT. js::NumberToCString() is a michael@0: * better function to use. michael@0: * michael@0: * Convert d to a string in the given base. The integral part of d will be michael@0: * printed exactly in that base, regardless of how large it is, because there michael@0: * is no exponential notation for non-base-ten numbers. The fractional part michael@0: * will be rounded to as few digits as possible while still preserving the michael@0: * round-trip property (analogous to that of printing decimal numbers). In michael@0: * other words, if one were to read the resulting string in via a hypothetical michael@0: * base-number-reading routine that rounds to the nearest IEEE double (and to michael@0: * an even significand if there are two equally near doubles), then the result michael@0: * would equal d (except for -0.0, which converts to "0", and NaN, which is michael@0: * not equal to itself). michael@0: * michael@0: * Return nullptr if out of memory. If the result is not nullptr, it must be michael@0: * released via js_free(). michael@0: */ michael@0: char * michael@0: js_dtobasestr(DtoaState *state, int base, double d); michael@0: michael@0: #endif /* jsdtoa_h */