Skip to content

Commit

Permalink
Improvements to string formatting
Browse files Browse the repository at this point in the history
 - Handle %o and %g converters correctly
 - Use longs instead of integers
 - Ensure unsigned longs are formatted as such
 - Correctly handle alternate form
  • Loading branch information
SquidDev committed Jan 2, 2018
1 parent ef8624c commit 5d73551
Show file tree
Hide file tree
Showing 11 changed files with 76,629 additions and 221 deletions.
40 changes: 33 additions & 7 deletions src/main/java/org/squiddev/cobalt/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public Buffer(LuaString value) {
* @return value as a {@link LuaValue}, converting as necessary
*/
public LuaValue value() {
return value != null ? value : this.tostring();
return value != null ? value : this.toLuaString();
}

/**
Expand All @@ -129,7 +129,7 @@ public Buffer setvalue(LuaString value) {
*
* @return the value as a {@link LuaString}
*/
public final LuaString tostring() {
public final LuaString toLuaString() {
realloc(length, 0);
return LuaString.valueOf(bytes, offset, length);
}
Expand All @@ -151,11 +151,37 @@ public String toString() {
* @return {@code this} to allow call chaining
*/
public final Buffer append(byte b) {
makeroom(0, 1);
makeRoom(0, 1);
bytes[offset + length++] = b;
return this;
}

/**
* Append a single character to the buffer.
*
* @param c The byte to append
* @return {@code this} to allow call chaining
*/
public final Buffer append(char c) {
makeRoom(0, 1);
bytes[offset + length++] = c < 256 ? (byte) c : 63;
return this;
}

/**
* Append a character array to the buffer.
*
* @param chars The byte to append
* @return {@code this} to allow call chaining
*/
public final Buffer append(char[] chars) {
final int n = chars.length;
makeRoom(0, n);
LuaString.encode(chars, bytes, offset + length);
length += n;
return this;
}

/**
* Append a {@link LuaString} to the buffer.
*
Expand All @@ -164,7 +190,7 @@ public final Buffer append(byte b) {
*/
public final Buffer append(LuaString str) {
final int n = str.length;
makeroom(0, n);
makeRoom(0, n);
str.copyTo(0, bytes, offset + length, n);
length += n;
return this;
Expand All @@ -180,7 +206,7 @@ public final Buffer append(LuaString str) {
*/
public final Buffer append(String str) {
final int n = str.length();
makeroom(0, n);
makeRoom(0, n);
LuaString.encode(str, bytes, offset + length);
length += n;
return this;
Expand All @@ -194,7 +220,7 @@ public final Buffer append(String str) {
*/
public Buffer prepend(LuaString s) {
int n = s.length;
makeroom(n, 0);
makeRoom(n, 0);
System.arraycopy(s.bytes, s.offset, bytes, offset - n, n);
offset -= n;
length += n;
Expand All @@ -208,7 +234,7 @@ public Buffer prepend(LuaString s) {
* @param nbefore number of unused bytes which must precede the data after this completes
* @param nafter number of unused bytes which must follow the data after this completes
*/
public final void makeroom(int nbefore, int nafter) {
public final void makeRoom(int nbefore, int nafter) {
if (value != null) {
LuaString s = value;
value = null;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/squiddev/cobalt/LuaString.java
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,25 @@ public static void encode(String string, byte[] bytes, int off) {
}
}

/**
* Encode the given character array with characters limited to the 0-255 range,
* writing the result to bytes starting at offset.
*
* The string should be measured first with lengthAsUtf8
* to make sure the given byte array is large enough.
*
* @param string Array of characters to be encoded
* @param bytes byte array to hold the result
* @param off offset into the byte array to start writing
* @see #decode(byte[], int, int)
*/
public static void encode(char[] string, byte[] bytes, int off) {
for (int i = 0; i < string.length; i++) {
int c = string[i];
bytes[i + off] = (c < 256 ? (byte) c : 63);
}
}

//region UTF8

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/squiddev/cobalt/LuaTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public LuaValue concat(LuaString sep, int i, int j) throws LuaError {
sb.append(rawget(i).checkLuaString());
}
}
return sb.tostring();
return sb.toLuaString();
}

@Override
Expand Down
Loading

0 comments on commit 5d73551

Please sign in to comment.