Fix some LuaJ bugs (Not tested for LuaJ 3)

LuaDouble:
Do not try Long.toString
Do not cast to float

LuaString:
Do not scan as a long
Add another check in scandouble (was scanlong)
Have substring create new bytes to work with
This commit is contained in:
gamax92 2014-07-04 17:10:25 -06:00
parent 029027fadb
commit 6164a9f769
2 changed files with 12 additions and 15 deletions

View File

@ -219,7 +219,7 @@ public class LuaDouble extends LuaNumber {
// string comparison
public int strcmp( LuaString rhs ) { typerror("attempt to compare number with string"); return 0; }
public String tojstring() {
/*
if ( v == 0.0 ) { // never occurs in J2me
@ -227,14 +227,11 @@ public class LuaDouble extends LuaNumber {
return ( bits >> 63 == 0 ) ? "0" : "-0";
}
*/
long l = (long) v;
if ( l == v )
return Long.toString(l);
if ( Double.isNaN(v) )
return JSTR_NAN;
if ( Double.isInfinite(v) )
if ( Double.isInfinite(v) )
return (v<0? JSTR_NEGINF: JSTR_POSINF);
return Float.toString((float)v);
return Double.toString(v);
}
public LuaString strvalue() {

View File

@ -394,7 +394,7 @@ public class LuaString extends LuaValue {
* beginIndex and extending for (endIndex - beginIndex ) characters.
*/
public LuaString substring( int beginIndex, int endIndex ) {
return valueOf( m_bytes, m_offset + beginIndex, endIndex - beginIndex );
return new LuaString(Arrays.copyOfRange(m_bytes, beginIndex, endIndex), 0, endIndex - beginIndex);
}
public int hashCode() {
@ -704,8 +704,8 @@ public class LuaString extends LuaValue {
if ( i>=j )
return Double.NaN;
if ( m_bytes[i]=='0' && i+1<j && (m_bytes[i+1]=='x'||m_bytes[i+1]=='X'))
return scanlong(16, i+2, j);
double l = scanlong(10, i, j);
return scandouble(16, i+2, j);
double l = scandouble(10, i, j);
return Double.isNaN(l)? scandouble(i,j): l;
}
@ -722,25 +722,25 @@ public class LuaString extends LuaValue {
while ( i<j && m_bytes[j-1]==' ' ) --j;
if ( i>=j )
return Double.NaN;
return scanlong( base, i, j );
return scandouble( base, i, j );
}
/**
* Scan and convert a long value, or return Double.NaN if not found.
* Scan and convert a double value, or return Double.NaN if not found.
* @param base the base to use, such as 10
* @param start the index to start searching from
* @param end the first index beyond the search range
* @return double value if conversion is valid,
* or Double.NaN if not
*/
private double scanlong( int base, int start, int end ) {
long x = 0;
private double scandouble( int base, int start, int end ) {
double x = 0;
boolean neg = (m_bytes[start] == '-');
for ( int i=(neg?start+1:start); i<end; i++ ) {
int digit = m_bytes[i] - (base<=10||(m_bytes[i]>='0'&&m_bytes[i]<='9')? '0':
m_bytes[i]>='A'&&m_bytes[i]<='Z'? ('A'-10): ('a'-10));
if ( digit < 0 || digit >= base )
return Double.NaN;
if ( digit < 0 || digit >= base || (m_bytes[i] >= '9' && digit < 10))
return Double.NaN;
x = x * base + digit;
if ( x < 0 )
return Double.NaN; // overflow