1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsdtoa.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,112 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef jsdtoa_h 1.11 +#define jsdtoa_h 1.12 + 1.13 +/* 1.14 + * Public interface to portable double-precision floating point to string 1.15 + * and back conversion package. 1.16 + */ 1.17 + 1.18 +#include <stddef.h> 1.19 + 1.20 +struct DtoaState; 1.21 + 1.22 +DtoaState * 1.23 +js_NewDtoaState(); 1.24 + 1.25 +void 1.26 +js_DestroyDtoaState(DtoaState *state); 1.27 + 1.28 +/* 1.29 + * js_strtod_harder() returns as a double-precision floating-point number the 1.30 + * value represented by the character string pointed to by s00. The string is 1.31 + * scanned up to the first unrecognized character. 1.32 + * 1.33 + * If se is not nullptr, *se receives a pointer to the character terminating 1.34 + * the scan. If no number can be formed, *se receives a pointer to the first 1.35 + * unparseable character in s00, and zero is returned. 1.36 + * 1.37 + * On overflow, this function returns infinity and does not indicate an error. 1.38 + * 1.39 + * *err is set to zero on success; it's set to JS_DTOA_ENOMEM on memory failure. 1.40 + */ 1.41 +#define JS_DTOA_ENOMEM 2 1.42 +double 1.43 +js_strtod_harder(DtoaState *state, const char *s00, char **se, int *err); 1.44 + 1.45 +/* 1.46 + * Modes for converting floating-point numbers to strings. 1.47 + * 1.48 + * Some of the modes can round-trip; this means that if the number is converted to 1.49 + * a string using one of these mode and then converted back to a number, the result 1.50 + * will be identical to the original number (except that, due to ECMA, -0 will get converted 1.51 + * to +0). These round-trip modes return the minimum number of significand digits that 1.52 + * permit the round trip. 1.53 + * 1.54 + * Some of the modes take an integer parameter <precision>. 1.55 + */ 1.56 +/* NB: Keep this in sync with number_constants[]. */ 1.57 +typedef enum JSDToStrMode { 1.58 + DTOSTR_STANDARD, /* Either fixed or exponential format; round-trip */ 1.59 + DTOSTR_STANDARD_EXPONENTIAL, /* Always exponential format; round-trip */ 1.60 + DTOSTR_FIXED, /* Round to <precision> digits after the decimal point; exponential if number is large */ 1.61 + DTOSTR_EXPONENTIAL, /* Always exponential format; <precision> significant digits */ 1.62 + DTOSTR_PRECISION /* Either fixed or exponential format; <precision> significant digits */ 1.63 +} JSDToStrMode; 1.64 + 1.65 + 1.66 +/* Maximum number of characters (including trailing null) that a DTOSTR_STANDARD or DTOSTR_STANDARD_EXPONENTIAL 1.67 + * conversion can produce. This maximum is reached for a number like -0.0000012345678901234567. */ 1.68 +#define DTOSTR_STANDARD_BUFFER_SIZE 26 1.69 + 1.70 +/* Maximum number of characters (including trailing null) that one of the other conversions 1.71 + * can produce. This maximum is reached for TO_FIXED, which can generate up to 21 digits before the decimal point. */ 1.72 +#define DTOSTR_VARIABLE_BUFFER_SIZE(precision) ((precision)+24 > DTOSTR_STANDARD_BUFFER_SIZE ? (precision)+24 : DTOSTR_STANDARD_BUFFER_SIZE) 1.73 + 1.74 +/* 1.75 + * DO NOT USE THIS FUNCTION IF YOU CAN AVOID IT. js::NumberToCString() is a 1.76 + * better function to use. 1.77 + * 1.78 + * Convert dval according to the given mode and return a pointer to the 1.79 + * resulting ASCII string. If mode == DTOSTR_STANDARD and precision == 0 it's 1.80 + * equivalent to ToString() as specified by ECMA-262-5 section 9.8.1, but it 1.81 + * doesn't handle integers specially so should be avoided in that case (that's 1.82 + * why js::NumberToCString() is better). 1.83 + * 1.84 + * The result is held somewhere in buffer, but not necessarily at the 1.85 + * beginning. The size of buffer is given in bufferSize, and must be at least 1.86 + * as large as given by the above macros. 1.87 + * 1.88 + * Return nullptr if out of memory. 1.89 + */ 1.90 +char * 1.91 +js_dtostr(DtoaState *state, char *buffer, size_t bufferSize, JSDToStrMode mode, int precision, 1.92 + double dval); 1.93 + 1.94 +/* 1.95 + * DO NOT USE THIS FUNCTION IF YOU CAN AVOID IT. js::NumberToCString() is a 1.96 + * better function to use. 1.97 + * 1.98 + * Convert d to a string in the given base. The integral part of d will be 1.99 + * printed exactly in that base, regardless of how large it is, because there 1.100 + * is no exponential notation for non-base-ten numbers. The fractional part 1.101 + * will be rounded to as few digits as possible while still preserving the 1.102 + * round-trip property (analogous to that of printing decimal numbers). In 1.103 + * other words, if one were to read the resulting string in via a hypothetical 1.104 + * base-number-reading routine that rounds to the nearest IEEE double (and to 1.105 + * an even significand if there are two equally near doubles), then the result 1.106 + * would equal d (except for -0.0, which converts to "0", and NaN, which is 1.107 + * not equal to itself). 1.108 + * 1.109 + * Return nullptr if out of memory. If the result is not nullptr, it must be 1.110 + * released via js_free(). 1.111 + */ 1.112 +char * 1.113 +js_dtobasestr(DtoaState *state, int base, double d); 1.114 + 1.115 +#endif /* jsdtoa_h */