5 #include "jansson_private.h"
8 #if JSON_HAVE_LOCALECONV
12 - This code assumes that the decimal separator is exactly one
15 - If setlocale() is called by another thread between the call to
16 localeconv() and the call to sprintf() or strtod(), the result may
17 be wrong. setlocale() is not thread-safe and should not be used
18 this way. Multi-threaded programs should use uselocale() instead.
21 static void to_locale(strbuffer_t *strbuffer)
26 point = localeconv()->decimal_point;
28 /* No conversion needed */
32 pos = strchr(strbuffer->value, '.');
37 static void from_locale(char *buffer)
42 point = localeconv()->decimal_point;
44 /* No conversion needed */
48 pos = strchr(buffer, *point);
54 int jsonp_strtod(strbuffer_t *strbuffer, double *out)
59 #if JSON_HAVE_LOCALECONV
64 value = strtod(strbuffer->value, &end);
65 assert(end == strbuffer->value + strbuffer->length);
67 if(errno == ERANGE && value != 0) {
76 int jsonp_dtostr(char *buffer, size_t size, double value)
82 ret = snprintf(buffer, size, "%.17g", value);
90 #if JSON_HAVE_LOCALECONV
94 /* Make sure there's a dot or 'e' in the output. Otherwise
95 a real is converted to an integer when decoding */
96 if(strchr(buffer, '.') == NULL &&
97 strchr(buffer, 'e') == NULL)
99 if(length + 3 >= size) {
100 /* No space to append ".0" */
103 buffer[length] = '.';
104 buffer[length + 1] = '0';
105 buffer[length + 2] = '\0';
109 /* Remove leading '+' from positive exponent. Also remove leading
110 zeros from exponents (added by some printf() implementations) */
111 start = strchr(buffer, 'e');
123 memmove(start, end, length - (size_t)(end - buffer));
124 length -= (size_t)(end - start);