diff --git a/js/js.dart-ast/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index b0e3c33b992..1df3fea69af 100644 --- a/js/js.dart-ast/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/js/js.dart-ast/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -124,40 +124,21 @@ public class JsToStringGenerationVisitor extends JsVisitor { result.append((char) escape); } else { - /* - * Emit characters from 0 to 31 that don't have a single character - * escape sequence in octal where possible. This saves one or two - * characters compared to the hexadecimal format '\xXX'. - * - * These short octal sequences may only be used at the end of the string - * or where the following character is a non-digit. Otherwise, the - * following character would be incorrectly interpreted as belonging to - * the sequence. - */ - if (c < ' ' && (i == n - 1 || chars.charAt(i + 1) < '0' || chars.charAt(i + 1) > '9')) { - result.append('\\'); - if (c > 0x7) { - result.append((char) ('0' + (0x7 & (c >> 3)))); - } - result.append((char) ('0' + (0x7 & c))); + int hexSize; + if (c < 256) { + // 2-digit hex + result.append("\\x"); + hexSize = 2; } else { - int hexSize; - if (c < 256) { - // 2-digit hex - result.append("\\x"); - hexSize = 2; - } - else { - // Unicode. - result.append("\\u"); - hexSize = 4; - } - // append hexadecimal form of ch left-padded with 0 - for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) { - int digit = 0xf & (c >> shift); - result.append(HEX_DIGITS[digit]); - } + // Unicode. + result.append("\\u"); + hexSize = 4; + } + // append hexadecimal form of ch left-padded with 0 + for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) { + int digit = 0xf & (c >> shift); + result.append(HEX_DIGITS[digit]); } } } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/CharTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/CharTest.java index 238d6dc1203..2b32b18bb2a 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/CharTest.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/CharTest.java @@ -50,4 +50,8 @@ public final class CharTest extends SingleFileTranslationTest { public void testCharEquals() throws Exception { checkFooBoxIsOk(); } + + public void testCharConstantByUnicodeId() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.translator/testData/char/cases/charConstantByUnicodeId.kt b/js/js.translator/testData/char/cases/charConstantByUnicodeId.kt new file mode 100644 index 00000000000..485a07332fd --- /dev/null +++ b/js/js.translator/testData/char/cases/charConstantByUnicodeId.kt @@ -0,0 +1,20 @@ +package foo + +fun box(): String { + val chars = mapOf('\u0000' to 0x0000, + '\u0001' to 0x0001, + '\u0010' to 0x0010, + '\u001F' to 0x001F, + '\u0064' to 0x0064, + '\u00A0' to 0x00A0, + '\u00FF' to 0x00FF, + '\u0100' to 0x0100, + '\uF01F' to 0xF01F) + + for ((char, code) in chars) { + assertEquals(code, char.toInt()) + assertEquals(char, code.toChar()) + } + + return "OK" +}