Improve string compatibility with lua 5.2.

This commit is contained in:
James Roseborough
2012-09-19 14:00:26 +00:00
parent a8aeddfc60
commit 406068190b
4 changed files with 23 additions and 14 deletions

View File

@@ -230,8 +230,7 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
for ( int i=1, n=args.narg(); i<=n; i++ ) {
if ( i>1 ) globals.STDOUT.write( '\t' );
LuaString s = tostring.call( args.arg(i) ).strvalue();
int z = s.indexOf((byte)0, 0);
globals.STDOUT.write( s.m_bytes, s.m_offset, z>=0? z: s.m_length );
globals.STDOUT.write( s.m_bytes, s.m_offset, s.m_length );
}
globals.STDOUT.println();
return NONE;

View File

@@ -303,15 +303,20 @@ public class StringLib extends OneArgFunction {
buf.append( (byte)'\\' );
buf.append( (byte)c );
break;
case '\r':
buf.append( "\\r" );
break;
case '\0':
buf.append( "\\000" );
break;
default:
buf.append( (byte) c );
break;
if (c <= 0x1F || c == 0x7F) {
buf.append( (byte) '\\' );
if (i+1 == n || s.luaByte(i+1) < '0' || s.luaByte(i+1) > '9') {
buf.append(Integer.toString(c));
} else {
buf.append( (byte) '0' );
buf.append( (byte) (char) ('0' + c / 10) );
buf.append( (byte) (char) ('0' + c % 10) );
}
} else {
buf.append((byte) c);
}
break;
}
}
buf.append( (byte) '"' );

Binary file not shown.

View File

@@ -111,18 +111,23 @@ print(string.format("simple%ssimple", " simple "))
local testformat = function(message,fmt,...)
local s,e = pcall( string.format, fmt, ... )
if s then
if string.find(fmt, 'q') then
print(message, e)
end
print( message, string.byte(e,1,#e) )
else
print( message, 'error', e )
end
end
specials = "\"specials\": %% \000 \r \n"
testformat('plain %', "%%")
testformat("specials (%s)", "---%s---", specials)
testformat("specials (%q)", "---%q---", specials)
testformat("specials (%s)", "---%s---", " %% \000 \r \n ")
testformat("specials (%q)", "---%q---", " %% \000 \r \n ")
testformat("specials (%q)", "---%q---", "0%%0\0000\r0\n0")
testformat("controls (%q)", "---%q---", ' \a \b \f \t \v \\ ')
testformat("controls (%q)", "---%q---", '0\a0\b0\f0\t0\v0\\0')
testformat("extended (%q)", "---%q---", ' \222 \223 \224 ')
testformat("extended (%q)", "---%q---", '0\2220\2230\2240')
testformat("embedded newlines", "%s\r%s\n%s", '===', '===', '===')
-- format long string
@@ -144,7 +149,7 @@ strtests('lower', string.lower, s )
strtests('upper', string.upper, s )
strtests('reverse', string.reverse, s )
strtests('char', string.char, 92, 60, 61, 93 )
strtests('dump', string.dump, load('print("hello, world")', 'sample') )
print( 'string.dump test:', load(string.dump(function(x) return 'foo->'..x end),'bar')('bat') )
-- floating point formats (not supported yet)