Fix tonumber() to match standard Lua's behavior more closely.
This commit is contained in:
@@ -366,8 +366,16 @@ public class LString extends LValue {
|
|||||||
public LValue luaToNumber( int base ) {
|
public LValue luaToNumber( int base ) {
|
||||||
if ( base >= 2 && base <= 36 ) {
|
if ( base >= 2 && base <= 36 ) {
|
||||||
String str = toJavaString().trim();
|
String str = toJavaString().trim();
|
||||||
|
if ( ( base == 10 || base == 16 ) && ( str.startsWith("0x") || str.startsWith("0X") ) ) {
|
||||||
|
base = 16;
|
||||||
|
str = str.substring(2);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
return LInteger.valueOf( Integer.parseInt( str, base ) );
|
long x = Long.parseLong(str, base);
|
||||||
|
if (x < Integer.MIN_VALUE || x > Integer.MAX_VALUE)
|
||||||
|
return new LDouble((double) x);
|
||||||
|
else
|
||||||
|
return LInteger.valueOf((int) x);
|
||||||
} catch ( NumberFormatException nfe ) {
|
} catch ( NumberFormatException nfe ) {
|
||||||
if ( base == 10 ) {
|
if ( base == 10 ) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -207,6 +207,17 @@ print( 'pcall(tonumber,{"one","two",a="aa",b="bb"})', pcall(tonumber,{"one","two
|
|||||||
print( 'pcall(tonumber,"123.456")', pcall(tonumber,"123.456") )
|
print( 'pcall(tonumber,"123.456")', pcall(tonumber,"123.456") )
|
||||||
print( 'pcall(tonumber," 123.456")', pcall(tonumber," 123.456") )
|
print( 'pcall(tonumber," 123.456")', pcall(tonumber," 123.456") )
|
||||||
print( 'pcall(tonumber," 234qwer")', pcall(tonumber," 234qwer") )
|
print( 'pcall(tonumber," 234qwer")', pcall(tonumber," 234qwer") )
|
||||||
|
print( 'pcall(tonumber,"0x20")', pcall(tonumber,"0x20") )
|
||||||
|
print( 'pcall(tonumber," 0x20")', pcall(tonumber," 0x20") )
|
||||||
|
print( 'pcall(tonumber,"0x20 ")', pcall(tonumber,"0x20 ") )
|
||||||
|
print( 'pcall(tonumber," 0x20 ")', pcall(tonumber," 0x20 ") )
|
||||||
|
print( 'pcall(tonumber,"0X20")', pcall(tonumber,"0X20") )
|
||||||
|
print( 'pcall(tonumber," 0X20")', pcall(tonumber," 0X20") )
|
||||||
|
print( 'pcall(tonumber,"0X20 ")', pcall(tonumber,"0X20 ") )
|
||||||
|
print( 'pcall(tonumber," 0X20 ")', pcall(tonumber," 0X20 ") )
|
||||||
|
print( 'pcall(tonumber,"0x20",10)', pcall(tonumber,"0x20",10) )
|
||||||
|
print( 'pcall(tonumber,"0x20",16)', pcall(tonumber,"0x20",16) )
|
||||||
|
print( 'pcall(tonumber,"0x20",8)', pcall(tonumber,"0x20",8) )
|
||||||
|
|
||||||
-- tostring
|
-- tostring
|
||||||
print( 'pcall(tostring)', pcall(tostring) )
|
print( 'pcall(tostring)', pcall(tostring) )
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
version: 0.53
|
version: 0.54
|
||||||
|
|||||||
Reference in New Issue
Block a user