From 1d25d54f54618c6307c7a28fcc7223e86ce94d8c Mon Sep 17 00:00:00 2001 From: Wang Renxin Date: Wed, 25 Jul 2018 15:15:37 +0800 Subject: [PATCH] *fixed a real number formatting bug with different locales. --- HISTORY | 17 ++++++++++------- core/my_basic.c | 22 +++++++++++++++++++--- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/HISTORY b/HISTORY index c73fa94..b8f5023 100755 --- a/HISTORY +++ b/HISTORY @@ -1,3 +1,6 @@ +Jul. 25 2018 +Fixed a real number formatting bug with different locales + Jul. 14 2018 Fixed a multiple disposing bug with upvalues @@ -310,7 +313,7 @@ Jul. 16 2016 Added class format support with the STR statement Jul. 14 2016 -Fixed a boolean operation bug with the NOT statement on float number +Fixed a boolean operation bug with the NOT statement on real number Improved boolean operation Jul. 13 2016 @@ -374,7 +377,7 @@ Jun. 14 2016 Fixed some bugs with meta method calling Jun. 7 2016 -Simplified double precision float number redefinition +Simplified double precision real number redefinition May. 24 2016 Refactored error raising of string manipulation @@ -892,7 +895,7 @@ Added recursive sub routine support Fixed a wrong argument detection bug in mb_has_arg Sep. 18 2015 -Fixed a float number parsing bug, thanks to Cybermonkey342 for pointing it out +Fixed a real number parsing bug, thanks to Cybermonkey342 for pointing it out Added directly expression evaluation shell command Sep. 17 2015 @@ -902,7 +905,7 @@ Fixed a repeated disposing bug when using sub routine Sep. 16 2015 Added Nil type handling, including assignment, boolean operation, serialization, etc. -Added an MB_CONVERT_TO_INT_LEVEL macro, would convert float to integer as much as possible if this macro was enabled +Added an MB_CONVERT_TO_INT_LEVEL macro, would convert real to integer as much as possible if this macro was enabled Sep. 11 2015 Added a duplicate sub routine error handling @@ -973,7 +976,7 @@ Apr. 15 2015 Added mb_pop_usertype, mb_push_usertype to support user defined type Apr. 13 2015 -Added mixed integer/float array support +Added mixed integer/real array support Added warning prompt when passing strings to maths functions Fixed a memory leak when storing strings in an array Improved the interpreter commands @@ -986,7 +989,7 @@ Disposed parsing context at runtime to reduce memory occupation Apr. 10 2015 Improved compatibility with PellesC -Fixed a double precision float parsing bug on all 32bit systems, thanks to Pito for pointing it out +Fixed a double precision real parsing bug on all 32bit systems, thanks to Pito for pointing it out Fixed an exponential number parsing bug, thanks to Pito for pointing it out Fixed a crash bug when a script begins with meaningless negtive number @@ -1089,7 +1092,7 @@ Fixed some memory leaks Jul. 3 2012 Modified/added math functions: FLOOR, CEIL, FIX Fixed an INPUT type issue -Changed float number formatting from using "%f" to "%g" in PRINT, thanks to Bruce Kendall for pointing above three issues out +Changed real number formatting from using "%f" to "%g" in PRINT, thanks to Bruce Kendall for pointing above three issues out Refactored the way to load a script file Done several small improvements diff --git a/core/my_basic.c b/core/my_basic.c index da1ff4e..2930480 100755 --- a/core/my_basic.c +++ b/core/my_basic.c @@ -1739,6 +1739,10 @@ static char* _extract_string(_object_t* obj); #ifdef MB_MANUAL_REAL_FORMATTING static void _real_to_str(real_t r, char* str, size_t size, size_t afterpoint); #endif /* MB_MANUAL_REAL_FORMATTING */ +static void _real_to_str_std(real_t r, char* str, size_t size); +#ifndef mb_realtostr +# define mb_realtostr(__r, __s, __z) _real_to_str_std((__r), (__s), (__z)) +#endif /* mb_realtostr */ #ifdef _HAS_REF_OBJ_LOCK static bool_t _lock_ref_object(_lock_t* lk, _ref_t* ref, void* obj); @@ -6204,6 +6208,20 @@ static void _real_to_str(real_t r, char* str, size_t size, size_t afterpoint) { } #endif /* MB_MANUAL_REAL_FORMATTING */ +/* Convert a real number to string the standard way */ +static void _real_to_str_std(real_t r, char* str, size_t size) { + if((size_t)sprintf(str, MB_REAL_FMT, r) >= size) { + mb_assert(0 && "Buffer overflow."); + } + for(size_t i = 0; i < size; ++i) { + if(str[i] == ',') { + str[i] = '.'; + + break; + } + } +} + #ifdef _HAS_REF_OBJ_LOCK /* Lock a referenced object */ static bool_t _lock_ref_object(_lock_t* lk, _ref_t* ref, void* obj) { @@ -17668,9 +17686,7 @@ static int _std_str(mb_interpreter_t* s, void** l) { #ifdef MB_MANUAL_REAL_FORMATTING _real_to_str(arg.value.float_point, _CHAR_BUF_PTR(buf), lbuf, 5); #else /* MB_MANUAL_REAL_FORMATTING */ - if((size_t)sprintf(_CHAR_BUF_PTR(buf), MB_REAL_FMT, arg.value.float_point) >= lbuf) { - mb_assert(0 && "Buffer overflow."); - } + mb_realtostr(arg.value.float_point, _CHAR_BUF_PTR(buf), lbuf); #endif /* MB_MANUAL_REAL_FORMATTING */ break;