Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some LuaJ bugs #371

Merged
merged 2 commits into from
Jul 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/main/java/org/luaj/vm3/LuaDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,19 @@ public static double dmod_d(double lhs, double rhs) {

// 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
long bits = Double.doubleToLongBits( v );
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() {
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/org/luaj/vm3/LuaString.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Arrays;

import org.luaj.vm3.lib.MathLib;
import org.luaj.vm3.lib.StringLib;
Expand Down Expand Up @@ -394,7 +395,7 @@ public LuaString strvalue() {
* 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() {
Expand Down Expand Up @@ -704,8 +705,8 @@ public double scannumber() {
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;
}

Expand All @@ -722,25 +723,25 @@ public double scannumber(int base) {
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
Expand Down