robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
ad22d0ace9
master
1/* fpconv - Floating point conversion routines 2* 3* Copyright (c) 2011-2012 Mark Pulford <mark@kyne.com.au> 4* 5* Permission is hereby granted, free of charge, to any person obtaining 6* a copy of this software and associated documentation files (the 7* "Software"), to deal in the Software without restriction, including 8* without limitation the rights to use, copy, modify, merge, publish, 9* distribute, sublicense, and/or sell copies of the Software, and to 10* permit persons to whom the Software is furnished to do so, subject to 11* the following conditions: 12* 13* The above copyright notice and this permission notice shall be 14* included in all copies or substantial portions of the Software. 15* 16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23*/ 24 25/* JSON uses a '.' decimal separator. strtod() / sprintf() under C libraries 26* with locale support will break when the decimal separator is a comma. 27* 28* fpconv_* will around these issues with a translation buffer if required. 29*/ 30 31#include <stdio.h> 32#include <stdlib.h> 33#include <assert.h> 34#include <string.h> 35 36#include "fpconv.h" 37 38/* Workaround for MSVC */ 39#ifdef _MSC_VER 40#define inline __inline 41#define snprintf sprintf_s 42#endif 43 44/* Lua CJSON assumes the locale is the same for all threads within a 45* process and doesn't change after initialisation. 46* 47* This avoids the need for per thread storage or expensive checks 48* for call. */ 49static char locale_decimal_point = '.' ; 50 51/* In theory multibyte decimal_points are possible, but 52* Lua CJSON only supports UTF-8 and known locales only have 53* single byte decimal points ([.,]). 54* 55* localconv() may not be thread safe (=>crash), and nl_langinfo() is 56* not supported on some platforms. Use sprintf() instead - if the 57* locale does change, at least Lua CJSON won't crash. */ 58static void fpconv_update_locale (void ) 59{ 60char buf [8 ]; 61 62snprintf (buf ,sizeof (buf ),"%g" ,0.5 ); 63 64/* Failing this test might imply the platform has a buggy dtoa 65* implementation or wide characters */ 66if (buf [0 ]!= '0' || buf [2 ]!= '5' || buf [3 ]!= 0 ) { 67fprintf (stderr ,"Error: wide characters found or printf() bug." ); 68abort (); 69 } 70 71locale_decimal_point = buf [1 ]; 72} 73 74/* Check for a valid number character: [-+0-9a-yA-Y.] 75* Eg: -0.6e+5, infinity, 0xF0.F0pF0 76* 77* Used to find the probable end of a number. It doesn't matter if 78* invalid characters are counted - strtod() will find the valid 79* number if it exists. The risk is that slightly more memory might 80* be allocated before a parse error occurs. */ 81static inline int valid_number_character (char ch ) 82{ 83char lower_ch ; 84 85if ('0' <=ch && ch <='9' ) 86return 1 ; 87if (ch == '-' || ch == '+' || ch == '.' ) 88return 1 ; 89 90/* Hex digits, exponent (e), base (p), "infinity",.. */ 91lower_ch = ch |0x20 ; 92if ('a' <=lower_ch && lower_ch <='y' ) 93return 1 ; 94 95return 0 ; 96} 97 98/* Calculate the size of the buffer required for a strtod locale 99* conversion. */ 100static int strtod_buffer_size (const char * s ) 101{ 102const char * p = s ; 103 104while (valid_number_character (* p )) 105p ++ ; 106 107return p - s ; 108} 109 110/* Similar to strtod(), but must be passed the current locale's decimal point 111* character. Guaranteed to be called at the start of any valid number in a string */ 112double fpconv_strtod (const char * nptr ,char ** endptr ) 113{ 114char localbuf [FPCONV_G_FMT_BUFSIZE ]; 115char * buf ,* endbuf ,* dp ; 116int buflen ; 117double value ; 118 119/* System strtod() is fine when decimal point is '.' */ 120if (locale_decimal_point == '.' ) 121return strtod (nptr ,endptr ); 122 123buflen = strtod_buffer_size (nptr ); 124if (!buflen ) { 125/* No valid characters found, standard strtod() return */ 126* endptr = (char * )nptr ; 127return 0 ; 128 } 129 130/* Duplicate number into buffer */ 131if (buflen >=FPCONV_G_FMT_BUFSIZE ) { 132/* Handle unusually large numbers */ 133buf = (char * )malloc (buflen + 1 ); 134if (!buf ) { 135fprintf (stderr ,"Out of memory" ); 136abort (); 137 } 138 }else { 139/* This is the common case.. */ 140buf = localbuf ; 141 } 142memcpy (buf ,nptr ,buflen ); 143buf [buflen ]= 0 ; 144 145/* Update decimal point character if found */ 146dp = strchr (buf ,'.' ); 147if (dp ) 148* dp = locale_decimal_point ; 149 150value = strtod (buf ,& endbuf ); 151* endptr = (char * )& nptr [endbuf - buf ]; 152if (buflen >=FPCONV_G_FMT_BUFSIZE ) 153free (buf ); 154 155return value ; 156} 157 158/* "fmt" must point to a buffer of at least 6 characters */ 159static void set_number_format (char * fmt ,int precision ) 160{ 161int d1 ,d2 ,i ; 162 163assert (1 <=precision && precision <=16 ); 164 165/* Create printf format (%.14g) from precision */ 166d1 = precision /10 ; 167d2 = precision %10 ; 168fmt [0 ]= '%' ; 169fmt [1 ]= '.' ; 170i = 2 ; 171if (d1 ) { 172fmt [i ++ ]= '0' + d1 ; 173 } 174fmt [i ++ ]= '0' + d2 ; 175fmt [i ++ ]= 'g' ; 176fmt [i ]= 0 ; 177} 178 179/* Assumes there is always at least 32 characters available in the target buffer */ 180int fpconv_g_fmt (char * str ,double num ,int precision ) 181{ 182char buf [FPCONV_G_FMT_BUFSIZE ]; 183char fmt [6 ]; 184int len ; 185char * b ; 186 187set_number_format (fmt ,precision ); 188 189/* Pass through when decimal point character is dot. */ 190if (locale_decimal_point == '.' ) 191return snprintf (str ,FPCONV_G_FMT_BUFSIZE ,fmt ,num ); 192 193/* snprintf() to a buffer then translate for other decimal point characters */ 194len = snprintf (buf ,FPCONV_G_FMT_BUFSIZE ,fmt ,num ); 195 196/* Copy into target location. Translate decimal point if required */ 197b = buf ; 198do { 199* str ++ = (* b == locale_decimal_point ?'.' :* b ); 200 }while (* b ++ ); 201 202return len ; 203} 204 205void fpconv_init (void ) 206{ 207fpconv_update_locale (); 208} 209 210/* vi:ai et sw=4 ts=4: 211*/