JS backend: don't use octal numbers when render non-latin symbols, because of it prohibited in strict mode

#KT-7921 Fixed
This commit is contained in:
Zalim Bashorov
2015-06-08 14:09:44 +03:00
parent 69ae976be3
commit 65adc3ac02
3 changed files with 37 additions and 32 deletions
@@ -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]);
}
}
}
@@ -50,4 +50,8 @@ public final class CharTest extends SingleFileTranslationTest {
public void testCharEquals() throws Exception {
checkFooBoxIsOk();
}
public void testCharConstantByUnicodeId() throws Exception {
checkFooBoxIsOk();
}
}
@@ -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"
}