pub struct JSStrBuilder<'a> { /* private fields */ }Expand description
Build a JSStr in an arena, preserving lone surrogates.
Appends concatenated UTF-16 values. A leading surrogate at the end of one append
pairs with a trailing surrogate at the start of the next, including across empty appends.
into_js_str returns canonical WTF-8 without copying the completed buffer.
use oxc_str::JSStrBuilder;
let mut builder = JSStrBuilder::new_in(&&allocator);
builder.push_code_unit(0xD800);
builder.push_str("");
builder.push_code_unit(0xDC00);
let s = builder.into_js_str();
assert_eq!(s.as_str(), Some("๐"));Implementationsยง
Sourceยงimpl<'a> JSStrBuilder<'a>
impl<'a> JSStrBuilder<'a>
Sourcepub fn new_in(allocator: &impl GetAllocator<'a>) -> Self
pub fn new_in(allocator: &impl GetAllocator<'a>) -> Self
Create an empty JSStrBuilder without allocating.
Sourcepub fn with_capacity_in(
capacity: usize,
allocator: &impl GetAllocator<'a>,
) -> Self
pub fn with_capacity_in( capacity: usize, allocator: &impl GetAllocator<'a>, ) -> Self
Create an empty JSStrBuilder with reserved capacity.
If capacity is 0, does not allocate.
ยงPanics
Panics if capacity exceeds u32::MAX or isize::MAX.
Sourcepub fn push_js_char(&mut self, value: JSChar)
pub fn push_js_char(&mut self, value: JSChar)
Append one JavaScript code point, pairing surrogates at the boundary.
Sourcepub fn push_code_unit(&mut self, unit: u16)
pub fn push_code_unit(&mut self, unit: u16)
Append one UTF-16 code unit.
Use this for input from a UTF-16 API.
Use push_js_char for an already decoded JavaScript code point.
Sourcepub fn push_utf16(&mut self, units: &[u16])
pub fn push_utf16(&mut self, units: &[u16])
Append potentially ill-formed UTF-16.
This accepts code-unit buffers from UTF-16 APIs without replacing lone surrogates. A pair may span consecutive calls, including empty buffers.
Sourcepub fn push_code_units(&mut self, units: impl IntoIterator<Item = u16>)
pub fn push_code_units(&mut self, units: impl IntoIterator<Item = u16>)
Append potentially ill-formed UTF-16 code units from an iterator, like push_utf16.
Sourcepub fn push_js_str(&mut self, value: JSStr<'_>)
pub fn push_js_str(&mut self, value: JSStr<'_>)
Append a JavaScript string, repairing a surrogate pair at the boundary.
UTF-8 inputs are copied without scanning. For inputs with lone surrogates, only the boundary encodings change; the interior is copied as bytes.
Sourcepub fn into_js_str(self) -> JSStr<'a>
pub fn into_js_str(self) -> JSStr<'a>
Consume the builder and return a string without copying or rescanning the buffer.