Changed my String[] Transformer:
- continue to terminate Strings with a newline - turn newlines inside a String into "\n" - turn null elements into "\0". This is one additional character when encoding nulls, but I think it may improve readability..
This commit is contained in:
@@ -134,11 +134,10 @@ public class Transformers
|
|||||||
StringBuilder buf = new StringBuilder();
|
StringBuilder buf = new StringBuilder();
|
||||||
for (String s : value) {
|
for (String s : value) {
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
buf.append('\uFFFC');
|
buf.append("\\0"); // encode nulls as "\0" (with no terminator)
|
||||||
} else {
|
} else {
|
||||||
s = s.replace("\\", "\\\\"); // turn \ into \\
|
s = s.replace("\\", "\\\\"); // turn \ into \\
|
||||||
s = s.replace("\n", "\\\n"); // preslash a newline
|
s = s.replace("\n", "\\n"); // turn a newline in a String to "\n"
|
||||||
s = s.replace("\uFFFC", "\\\uFFFC"); // preslash the nullmarker too
|
|
||||||
buf.append(s).append('\n');
|
buf.append(s).append('\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -155,21 +154,27 @@ public class Transformers
|
|||||||
for (int ii = 0, nn = encoded.length(); ii < nn; ii++) {
|
for (int ii = 0, nn = encoded.length(); ii < nn; ii++) {
|
||||||
char c = encoded.charAt(ii);
|
char c = encoded.charAt(ii);
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '\uFFFC':
|
|
||||||
Preconditions.checkArgument(buf.length() == 0, "Invalid encoded string");
|
|
||||||
value.add(null);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '\n':
|
case '\n':
|
||||||
value.add(buf.toString()); // TODO: intern?
|
value.add(buf.toString()); // TODO: intern?
|
||||||
buf.setLength(0);
|
buf.setLength(0);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '\\':
|
case '\\':
|
||||||
try {
|
Preconditions.checkArgument(++ii < nn, "Invalid encoded string");
|
||||||
buf.append(encoded.charAt(++ii)); // take the next char at face value
|
char slashed = encoded.charAt(ii);
|
||||||
} catch (IndexOutOfBoundsException e) {
|
switch (slashed) {
|
||||||
throw new IllegalArgumentException("Invalid encoded string", e);
|
case '0': // turn \0 into a null element
|
||||||
|
Preconditions.checkArgument(buf.length() == 0, "Invalid encoded string");
|
||||||
|
value.add(null);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'n': // turn \n back into a newline
|
||||||
|
buf.append('\n');
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: // this should only be a slash...
|
||||||
|
buf.append(slashed);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -178,7 +183,7 @@ public class Transformers
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// make sure there are no left-over characters
|
// make sure the last element was terminated
|
||||||
Preconditions.checkArgument(buf.length() == 0, "Invalid encoded string");
|
Preconditions.checkArgument(buf.length() == 0, "Invalid encoded string");
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user