diff --git a/icu4j/src/com/ibm/icu/dev/test/util/UtilityTest.java b/icu4j/src/com/ibm/icu/dev/test/util/UtilityTest.java index 4040b0174c8..04d5943c2ca 100644 --- a/icu4j/src/com/ibm/icu/dev/test/util/UtilityTest.java +++ b/icu4j/src/com/ibm/icu/dev/test/util/UtilityTest.java @@ -43,33 +43,29 @@ public class UtilityTest extends TestFmwk { // cause a newline to be inserted "testing space , quotations \"", "testing weird supplementary characters \ud800\udc00", - "testing control characters \u0001 and line breaking \n are we done yet?" + "testing control characters \u0001 and line breaking!! \n are we done yet?" }; String result[] = { " \"the quick brown fox jumps over the lazy dog\"", " \"testing space , quotations \\042\"", " \"testing weird supplementary characters \\uD800\\uDC00\"", - " \"testing control characters \\001 and line breaking \\012 are we done ye\"+" + " \"testing control characters \\001 and line breaking!! \\n are we done ye\"+" + Utility.LINE_SEPARATOR + " \"t?\"" }; String result1[] = { "\"the quick brown fox jumps over the lazy dog\"", "\"testing space , quotations \\042\"", "\"testing weird supplementary characters \\uD800\\uDC00\"", - "\"testing control characters \\001 and line breaking \\012 are we done yet?\"" + "\"testing control characters \\001 and line breaking!! \\n are we done yet?\"" }; for (int i = 0; i < data.length; i ++) { - if (!result[i].equals(Utility.formatForSource(data[i]))) { - errln("Fail: Utility.formatForSource(\"" - + Utility.unescape(data[i]) + "expected to be " + result[i]); - } + assertEquals("formatForSource(\"" + data[i] + "\")", + result[i], Utility.formatForSource(data[i])); } for (int i = 0; i < data.length; i ++) { - if (!result1[i].equals(Utility.format1ForSource (data[i]))) { - errln("Fail: Utility.formatForSource(\"" - + Utility.unescape(data[i]) + "expected to be " + result1[i]); - } + assertEquals("format1ForSource(\"" + data[i] + "\")", + result1[i], Utility.format1ForSource(data[i])); } } diff --git a/icu4j/src/com/ibm/icu/impl/Utility.java b/icu4j/src/com/ibm/icu/impl/Utility.java index 40fbb7bd861..7b1de87d142 100755 --- a/icu4j/src/com/ibm/icu/impl/Utility.java +++ b/icu4j/src/com/ibm/icu/impl/Utility.java @@ -609,38 +609,41 @@ public final class Utility { buffer.append(" \""); int count = 11; while (i> 6]); // HEX_DIGIT works for octal - buffer.append(HEX_DIGIT[(c & 0070) >> 3]); - buffer.append(HEX_DIGIT[(c & 0007)]); - count += 4; - } - } - else if (c <= '\u007E') { - buffer.append(c); - count += 1; - } - else { - buffer.append("\\u"); - buffer.append(HEX_DIGIT[(c & 0xF000) >> 12]); - buffer.append(HEX_DIGIT[(c & 0x0F00) >> 8]); - buffer.append(HEX_DIGIT[(c & 0x00F0) >> 4]); - buffer.append(HEX_DIGIT[(c & 0x000F)]); - count += 6; - } + char c = s.charAt(i++); + if (c < '\u0020' || c == '"' || c == '\\') { + if (c == '\n') { + buffer.append("\\n"); + count += 2; + } else if (c == '\t') { + buffer.append("\\t"); + count += 2; + } else if (c == '\r') { + buffer.append("\\r"); + count += 2; + } else { + // Represent control characters, backslash and double quote + // using octal notation; otherwise the string we form + // won't compile, since Unicode escape sequences are + // processed before tokenization. + buffer.append('\\'); + buffer.append(HEX_DIGIT[(c & 0700) >> 6]); // HEX_DIGIT works for octal + buffer.append(HEX_DIGIT[(c & 0070) >> 3]); + buffer.append(HEX_DIGIT[(c & 0007)]); + count += 4; + } + } + else if (c <= '\u007E') { + buffer.append(c); + count += 1; + } + else { + buffer.append("\\u"); + buffer.append(HEX_DIGIT[(c & 0xF000) >> 12]); + buffer.append(HEX_DIGIT[(c & 0x0F00) >> 8]); + buffer.append(HEX_DIGIT[(c & 0x00F0) >> 4]); + buffer.append(HEX_DIGIT[(c & 0x000F)]); + count += 6; + } } buffer.append('"'); } @@ -660,22 +663,22 @@ public final class Utility { for (int i=0; i> 6]); // HEX_DIGIT works for octal - buffer.append(HEX_DIGIT[(c & 0070) >> 3]); - buffer.append(HEX_DIGIT[(c & 0007)]); - } + if (c == '\n') { + buffer.append("\\n"); + } else if (c == '\t') { + buffer.append("\\t"); + } else if (c == '\r') { + buffer.append("\\r"); + } else { + // Represent control characters, backslash and double quote + // using octal notation; otherwise the string we form + // won't compile, since Unicode escape sequences are + // processed before tokenization. + buffer.append('\\'); + buffer.append(HEX_DIGIT[(c & 0700) >> 6]); // HEX_DIGIT works for octal + buffer.append(HEX_DIGIT[(c & 0070) >> 3]); + buffer.append(HEX_DIGIT[(c & 0007)]); + } } else if (c <= '\u007E') { buffer.append(c);