Skip to content

Commit

Permalink
Added lexical_cast for number->string conversions
Browse files Browse the repository at this point in the history
Fix #3436
  • Loading branch information
ashdnazg committed Oct 8, 2015
1 parent f046d4a commit 2e57d0d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions rts/lib/lua/include/luaconf.h
Expand Up @@ -527,9 +527,9 @@
@@ lua_str2number converts a string to a number.
*/
//SPRING#define LUA_NUMBER_SCAN "%lf"
#define LUA_NUMBER_SCAN "%f"
#define LUA_NUMBER_FMT "%.14g"
#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
#define LUA_NUMBER_SCAN "%f" // Format for reading from lua files
#define LUA_NUMBER_FMT "%.14g" // Format for printing into files/stdout
//SPRING#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) - changed into boost::lexical_cast in the file
#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
#define lua_str2number(s,p) strtod((s), (p))

Expand Down
22 changes: 12 additions & 10 deletions rts/lib/lua/src/lvm.cpp
Expand Up @@ -10,6 +10,7 @@
#include <string.h>

//SPRING
#include <boost/lexical_cast.hpp>
#include "streflop_cond.h"

#define lvm_c
Expand Down Expand Up @@ -51,29 +52,30 @@ int luaV_tostring (lua_State *L, StkId obj) {
if (!ttisnumber(obj))
return 0;
else {
char s[LUAI_MAXNUMBER2STR];
typedef std::array<char, LUAI_MAXNUMBER2STR> str_buf;
str_buf s;
lua_Number n = nvalue(obj);
// SPRING -- synced safety change
// -- need a custom number formatter?
// -- using lexical_cast for formatting to force sync
if (math::isfinite(n)) {
lua_number2str(s, n);
s = boost::lexical_cast<str_buf>(n);
}
else {
if (math::isnan(n)) {
strcpy(s, "nan");
strcpy(s.begin(), "nan");
}
else {
const int inf_type = math::isinf(n);
if (inf_type == 1) {
strcpy(s, "+inf");
strcpy(s.begin(), "+inf");
} else if (inf_type == -1) {
strcpy(s, "-inf");
strcpy(s.begin(), "-inf");
} else {
strcpy(s, "weird_number");
strcpy(s.begin(), "weird_number");
}
}
}
setsvalue2s(L, obj, luaS_new(L, s));
}
setsvalue2s(L, obj, luaS_new(L, s.begin()));
return 1;
}
}
Expand Down Expand Up @@ -147,7 +149,7 @@ void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
callTMres(L, val, tm, t, key);
return;
}
t = tm; /* else repeat with `tm' */
t = tm; /* else repeat with `tm' */
}
luaG_runerror(L, "loop in gettable");
}
Expand Down

0 comments on commit 2e57d0d

Please sign in to comment.