Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/bun_core/string/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,6 @@ pub fn contains_any(slice: &[u8], chars: &[u8]) -> bool {
index_of_any(slice, chars).is_some()
}

pub fn index_of_any16(self_: &[u16], chars: &[u16]) -> Option<usize> {
index_of_any_t(self_, chars)
}

pub fn index_of_any_t<T: crate::NoUninit + Eq>(str: &[T], chars: &[T]) -> Option<usize> {
if let (Lanes::U8(s), Lanes::U8(c)) = (lanes(str), lanes(chars)) {
return index_of_any(s, c);
Expand Down
36 changes: 1 addition & 35 deletions src/jsc/ConsoleObject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2700,19 +2700,6 @@ pub mod formatter {
pub(crate) fn write_string(&mut self, str: &bun_core::String) {
self.print(format_args!("{str}"));
}

#[inline]
pub(crate) fn write_16_bit(&mut self, input: &[u16]) {
// `format_utf16_type` requires `impl fmt::Write + Sized`; route through
// the `Display` adapter so we go via `bun_io::Write::write_fmt` instead.
self.print(format_args!(
"{}",
bun_core::fmt::FormatUTF16 {
buf: input,
path_fmt_opts: None
}
));
}
}

const INDENTATION_BUF: [u8; 64] = [b' '; 64];
Expand Down Expand Up @@ -2980,34 +2967,13 @@ pub mod formatter {
key,
pfmt!("<d>:<r> ", C),
));
} else if key.is_16bit() {
let mut utf16_slice = key.utf16_slice();

writer.add_for_new_line(utf16_slice.len() + 2);

if C {
writer.write_all(pfmt!("<r><green>", true).as_bytes());
}

writer.write_all(b"\"");

const QUOTE_U16: &[u16] = &[b'"' as u16];
while let Some(j) = strings::index_of_any16(utf16_slice, QUOTE_U16) {
writer.write_16_bit(&utf16_slice[0..j]);
writer.write_all(b"\"");
utf16_slice = &utf16_slice[j + 1..];
}

writer.write_16_bit(utf16_slice);

writer.print(format_args!("{}", pfmt!("\"<r><d>:<r> ", C)));
} else {
writer.add_for_new_line(key.len + 2);

writer.print(format_args!(
"{}{}{}",
pfmt!("<r><green>", C),
bun_core::fmt::format_json_string_latin1(key.slice()),
bun_core::fmt::format_json_string_utf8(&key.to_utf8(), Default::default()),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
pfmt!("<r><d>:<r> ", C),
));
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/cli/run_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3877,15 +3877,15 @@ impl BunXFastPath {

// Trigger quoting only on
// space/tab/quote — compare the FULL u16, not the truncated low byte.
let needs_quote = strings::index_of_any16(warg, bun_core::w!(" \t\"")).is_some();
let needs_quote = strings::index_of_any_t(warg, bun_core::w!(" \t\"")).is_some();

if !needs_quote {
buffer[..warg.len()].copy_from_slice(warg);
return warg.len();
}

// Fast path: no embedded `"`/`\` → simple wrap.
let has_quote_or_backslash = strings::index_of_any16(warg, bun_core::w!("\"\\")).is_some();
let has_quote_or_backslash = strings::index_of_any_t(warg, bun_core::w!("\"\\")).is_some();
if !has_quote_or_backslash {
buffer[0] = b'"' as u16;
buffer[1..1 + warg.len()].copy_from_slice(warg);
Expand Down
33 changes: 1 addition & 32 deletions src/runtime/test_runner/pretty_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,21 +707,6 @@ impl<'w, W: bun_io::Write> WrappedWriter<'w, W> {
pub(crate) fn write_string(&mut self, str: &bun_core::String) {
self.print(format_args!("{}", str));
}

#[inline]
pub(crate) fn write_16_bit(&mut self, input: &[u16]) {
// `format_utf16_type` writes through `fmt::Write`; buffer to a `String`
// and forward bytes (UTF-16 → UTF-8 conversion is the point, so the
// intermediate allocation is unavoidable without a `bun_io::Write` overload).
let mut buf = String::new();
if bun_fmt::format_utf16_type(input, &mut buf).is_err() {
self.failed = true;
return;
}
if self.ctx.write_all(buf.as_bytes()).is_err() {
self.failed = true;
}
}
}

use bun_io::AsFmt;
Expand Down Expand Up @@ -967,29 +952,13 @@ impl<'a, 'f, W: bun_io::Write, const ENABLE_ANSI_COLORS: bool>
pretty_fmt_const!(ENABLE_ANSI_COLORS, "<d>"),
pretty_fmt_const!(ENABLE_ANSI_COLORS, "<r>"),
));
} else if key.is_16bit() {
let utf16_slice = key.utf16_slice();

this.add_for_new_line(utf16_slice.len() + 2);

if ENABLE_ANSI_COLORS {
writer.write_all(pretty_fmt_const!(true, "<r><green>").as_bytes());
}

writer.write_all(b"\"");
writer.write_16_bit(utf16_slice);
writer.print(format_args!(
"\"{}:{} ",
pretty_fmt_const!(ENABLE_ANSI_COLORS, "<r><d>"),
pretty_fmt_const!(ENABLE_ANSI_COLORS, "<r>"),
));
} else {
this.add_for_new_line(key.len + 2);

writer.print(format_args!(
"{}{}{}:{} ",
pretty_fmt_const!(ENABLE_ANSI_COLORS, "<r><green>"),
bun_fmt::format_json_string_latin1(key.slice()),
bun_fmt::format_json_string_utf8(&key.to_utf8(), Default::default()),
pretty_fmt_const!(ENABLE_ANSI_COLORS, "<r><d>"),
pretty_fmt_const!(ENABLE_ANSI_COLORS, "<r>"),
));
Expand Down
61 changes: 61 additions & 0 deletions test/js/bun/test/snapshot-tests/bun-snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,67 @@ test("ArrayBuffer values are serialized like typed arrays", () => {
`);
});

test("object keys get the same escapes in Latin-1 and UTF-16 strings", () => {
// A string decoded from UTF-16 bytes keeps 16-bit storage, even when every character is ASCII.
const utf16 = (s: string) => Buffer.from(s, "utf16le").toString("utf16le");
const special = '"\\\n\x1b';
Comment thread
robobun marked this conversation as resolved.
Resolved
const value = {
[utf16(["ascii in utf16 ", special].join(""))]: 1,
["latin1 " + special]: 2,
// A Latin-1 string cannot hold these three characters.
["utf16 only \u2028\u2029\ufeff"]: 3,
["日本 " + special]: 4,
["😀 " + special]: 5,
};
expect(value).toMatchInlineSnapshot(`
{
"ascii in utf16 \\"\\\\\\n\\u001B": 1,
"latin1 \\"\\\\\\n\\u001B": 2,
"utf16 only \\u2028\\u2029\\uFEFF": 3,
"日本 \\"\\\\\\n\\u001B": 4,
"😀 \\"\\\\\\n\\u001B": 5,
}
`);

// A matcher failure prints the value too. toEqual uses the snapshot formatter. toBe uses the console formatter.
const failure = (fn: () => void) => {
try {
fn();
} catch (e) {
return Bun.stripANSI((e as Error).message);
}
};
expect(failure(() => expect(value).toEqual(0))).toMatchInlineSnapshot(`
"expect(received).toEqual(expected)

- 0
+ {
+ "ascii in utf16 \\"\\\\\\n\\u001B": 1,
+ "latin1 \\"\\\\\\n\\u001B": 2,
+ "utf16 only \\u2028\\u2029\\uFEFF": 3,
+ "日本 \\"\\\\\\n\\u001B": 4,
+ "😀 \\"\\\\\\n\\u001B": 5,
+ }

- Expected - 1
+ Received + 7
"
`);
expect(failure(() => expect(value).toBe(0))).toMatchInlineSnapshot(`
"expect(received).toBe(expected)

Expected: 0
Received: {
"ascii in utf16 \\"\\\\\\n\\u001B": 1,
"latin1 \\"\\\\\\n\\u001B": 2,
"utf16 only \\u2028\\u2029\\uFEFF": 3,
"日本 \\"\\\\\\n\\u001B": 4,
"😀 \\"\\\\\\n\\u001B": 5,
}
"
`);
});

describe("toMatchSnapshot errors", () => {
it("should throw if property matchers exist and received is not an object", () => {
expect(() => {
Expand Down
24 changes: 24 additions & 0 deletions test/js/bun/util/inspect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,30 @@ it("utf16 property name", () => {
expect(Bun.inspect(db.prepare("select '😀' as 笑").all())).toBe(output);
});

it("property keys get the same escapes in Latin-1 and UTF-16 strings", () => {
// A string decoded from UTF-16 bytes keeps 16-bit storage, even when every character is ASCII.
const utf16 = s => Buffer.from(s, "utf16le").toString("utf16le");
const special = '"\\\n\x1b';
expect(
Bun.inspect({
[utf16(["ascii in utf16 ", special].join(""))]: 1,
["latin1 " + special]: 2,
// A Latin-1 string cannot hold these three characters.
["utf16 only \u2028\u2029\ufeff"]: 3,
["日本 " + special]: 4,
["😀 " + special]: 5,
}),
).toMatchInlineSnapshot(`
"{
"ascii in utf16 \\"\\\\\\n\\u001B": 1,
"latin1 \\"\\\\\\n\\u001B": 2,
"utf16 only \\u2028\\u2029\\uFEFF": 3,
"日本 \\"\\\\\\n\\u001B": 4,
"😀 \\"\\\\\\n\\u001B": 5,
}"
`);
});

it("latin1", () => {
expect(Bun.inspect("English")).toBe('"English"');
expect(Bun.inspect("Français")).toBe('"Français"');
Expand Down
Loading