This commit is contained in:
Justin Whear
2023-02-17 14:48:10 -08:00
committed by GitHub
parent 79f7d29d03
commit d37daeb76a
2 changed files with 60 additions and 1 deletions

View File

@@ -2629,7 +2629,54 @@ pub fn NewPrinter(
p.print(" ");
}
p.print(e.value);
if (comptime is_bun_platform) {
// Translate any non-ASCII to unicode escape sequences
var ascii_start: usize = 0;
var is_ascii = false;
var iter = CodepointIterator.init(e.value);
var cursor = CodepointIterator.Cursor{};
while (iter.next(&cursor)) {
switch (cursor.c) {
first_ascii...last_ascii => {
if (!is_ascii) {
ascii_start = cursor.i;
is_ascii = true;
}
},
else => {
if (is_ascii) {
p.print(e.value[ascii_start..cursor.i]);
is_ascii = false;
}
switch (cursor.c) {
0...0xFFFF => {
p.print([_]u8{
'\\',
'u',
hex_chars[cursor.c >> 12],
hex_chars[(cursor.c >> 8) & 15],
hex_chars[(cursor.c >> 4) & 15],
hex_chars[cursor.c & 15],
});
},
else => {
p.print("\\u{");
std.fmt.formatInt(cursor.c, 16, .lower, .{}, p) catch unreachable;
p.print("}");
},
}
},
}
}
if (is_ascii) {
p.print(e.value[ascii_start..]);
}
} else {
// UTF8 sequence is fine
p.print(e.value);
}
// Need a space before the next identifier to avoid it turning into flags
p.prev_reg_exp_end = p.writer.written;

View File

@@ -0,0 +1,12 @@
import { it, expect } from "bun:test";
it("regex literal with non-Latin1 should work", () => {
const text = "这是一段要替换的文字";
//Correct results: 这是一段的文字
expect(text.replace(new RegExp("要替换"), "")).toBe("这是一段的文字");
//Incorrect result: 这是一段要替换的文字
expect(text.replace(/要替换/, "")).toBe("这是一段的文字");
});