Improve concat operation.

This commit is contained in:
James Roseborough
2010-08-24 19:05:04 +00:00
parent bb7a3390cd
commit 7e2c2db59c
2 changed files with 8 additions and 6 deletions

View File

@@ -109,11 +109,11 @@ public final class Buffer {
} }
public Buffer concatTo(LuaString lhs) { public Buffer concatTo(LuaString lhs) {
return value!=null? setvalue(lhs.concat(value)): prepend(lhs); return value!=null&&!value.isstring()? setvalue(lhs.concat(value)): prepend(lhs);
} }
public Buffer concatTo(LuaNumber lhs) { public Buffer concatTo(LuaNumber lhs) {
return value!=null? setvalue(lhs.concat(value)): prepend(lhs.strvalue()); return value!=null&&!value.isstring()? setvalue(lhs.concat(value)): prepend(lhs.strvalue());
} }
public Buffer prepend(LuaString s) { public Buffer prepend(LuaString s) {
@@ -130,12 +130,14 @@ public final class Buffer {
if ( value != null ) { if ( value != null ) {
LuaString s = value.strvalue(); LuaString s = value.strvalue();
value = null; value = null;
bytes = new byte[nbefore+s.m_length+nafter];
length = s.m_length; length = s.m_length;
offset = nbefore; offset = nbefore;
bytes = new byte[nbefore+length+nafter];
System.arraycopy(s.m_bytes, s.m_offset, bytes, offset, length); System.arraycopy(s.m_bytes, s.m_offset, bytes, offset, length);
} else if ( offset+length+nafter > bytes.length || offset<nbefore ) { } else if ( offset+length+nafter > bytes.length || offset<nbefore ) {
realloc( Math.max(nbefore+length+nafter,length*2), nbefore ); int n = nbefore+length+nafter;
int m = n<32? 32: n<length*2? length*2: n;
realloc( m, nbefore==0? 0: m-length-nafter );
} }
} }

View File

@@ -345,7 +345,7 @@ public class LuaValue extends Varargs {
public int strcmp( LuaString rhs ) { error("attempt to compare "+typename()); return 0; } public int strcmp( LuaString rhs ) { error("attempt to compare "+typename()); return 0; }
// concatenation // concatenation
public LuaValue concat(LuaValue rhs) { return rhs.concatTo(this); } public LuaValue concat(LuaValue rhs) { return this.concatmt(rhs); }
public LuaValue concatTo(LuaValue lhs) { return lhs.concatmt(this); } public LuaValue concatTo(LuaValue lhs) { return lhs.concatmt(this); }
public LuaValue concatTo(LuaNumber lhs) { return lhs.concatmt(this); } public LuaValue concatTo(LuaNumber lhs) { return lhs.concatmt(this); }
public LuaValue concatTo(LuaString lhs) { return lhs.concatmt(this); } public LuaValue concatTo(LuaString lhs) { return lhs.concatmt(this); }