For the String[] transformer, encode null elements as "\" followed by

the terminator (we encode the terminator as a different character).
This is somewhat nicer when inspecting an encoded String, and allows
for a future in which the decoder easily counts the number of terminator
characters and pre-allocates the storage, if we so desired.
This commit is contained in:
Ray Greenwell
2010-07-06 20:54:48 +00:00
parent 95f2fc6938
commit 9d83cc96a5
@@ -137,7 +137,7 @@ public class Transformers
StringBuilder buf = new StringBuilder();
for (String s : value) {
if (s == null) {
buf.append("\\0"); // encode nulls as "\0" (with no terminator)
buf.append("\\\n"); // encode nulls as slash followed by the terminator
} else {
s = s.replace("\\", "\\\\"); // turn \ into \\
s = s.replace("\n", "\\n"); // turn a newline in a String to "\n"
@@ -163,7 +163,7 @@ public class Transformers
Preconditions.checkArgument(++ii < nn, "Invalid encoded string");
char slashed = encoded.charAt(ii);
switch (slashed) {
case '0': // turn \0 into a null element
case '\n': // turn back into a null element
Preconditions.checkArgument(buf.length() == 0, "Invalid encoded string");
value.add(null);
break;