pub struct SourceText { /* private fields */ }Expand description
Exact source bytes plus an equal-width UTF-8 carrier for the scalar lexer.
Genuine U+E000..=U+E7FF source characters are stored without side-table
entries, and genuine ASCII DEL bytes have no invalid-byte entry, so both
remain distinguishable from carrier scalars.
Implementations§
Source§impl SourceText
impl SourceText
Sourcepub fn from_utf8(source: &str) -> Self
pub fn from_utf8(source: &str) -> Self
Wrap ordinary, well-formed UTF-8 source.
This mirrors the engine’s existing infallible &str source boundary;
dynamic ECMAScript Strings use Self::try_from_js_string instead.
Sourcepub fn try_from_raw_bytes(source: &[u8]) -> Result<Self, JsStringError>
pub fn try_from_raw_bytes(source: &[u8]) -> Result<Self, JsStringError>
Encode one explicitly sized QuickJS source buffer as equal-width scalar carrier text while retaining the exact original bytes.
Structurally valid Unicode scalars remain byte-identical. Three-byte surrogate encodings become marked PUA scalars, and every byte belonging to malformed, overlong, or out-of-range input becomes one marked DEL. The latter byte-by-byte representation keeps every raw byte offset addressable without allowing the marker to acquire source semantics.
§Errors
Returns JsStringError::TooLong when offsets cannot be represented,
or JsStringError::OutOfMemory when storage cannot be reserved.
Sourcepub fn try_from_js_string(source: &JsString) -> Result<Self, JsStringError>
pub fn try_from_js_string(source: &JsString) -> Result<Self, JsStringError>
Encode an ECMAScript String as reversible scalar-oriented source.
Valid surrogate pairs become their Unicode scalar. Each unpaired
surrogate becomes U+E000..=U+E7FF and receives a side-table entry.
§Errors
Returns JsStringError::OutOfMemory if carrier or marker storage
cannot be reserved.
Sourcepub fn try_from_utf16(
units: impl IntoIterator<Item = u16>,
) -> Result<Self, JsStringError>
pub fn try_from_utf16( units: impl IntoIterator<Item = u16>, ) -> Result<Self, JsStringError>
Build source directly from ECMAScript UTF-16 code units.
§Errors
Propagates the QuickJS String length limit and allocation errors.
pub fn carrier(&self) -> &str
pub fn raw_bytes(&self) -> &[u8] ⓘ
pub fn has_surrogate_markers(&self) -> bool
pub fn surrogate_markers(&self) -> &[SurrogateMarker]
Sourcepub fn surrogate_at(&self, byte_offset: usize) -> Option<u16>
pub fn surrogate_at(&self, byte_offset: usize) -> Option<u16>
Return the source surrogate represented at one carrier/raw byte offset.
Sourcepub fn invalid_byte_at(&self, byte_offset: usize) -> bool
pub fn invalid_byte_at(&self, byte_offset: usize) -> bool
Whether one carrier DEL represents a malformed raw source byte.
Sourcepub fn slice(&self, range: Range<usize>) -> Option<SourceTextSlice<'_>>
pub fn slice(&self, range: Range<usize>) -> Option<SourceTextSlice<'_>>
Select a half-open carrier byte range aligned to UTF-8 boundaries.
pub fn semantic_utf16_units(&self) -> Option<SourceTextUtf16Units<'_>>
Sourcepub fn try_to_raw_bytes(&self) -> Result<Vec<u8>, TryReserveError>
pub fn try_to_raw_bytes(&self) -> Result<Vec<u8>, TryReserveError>
Clone the exact explicitly sized source payload.
Sourcepub fn try_to_js_string(&self) -> Result<JsString, JsStringError>
pub fn try_to_js_string(&self) -> Result<JsString, JsStringError>
Reconstruct the original ECMAScript String.
Sourcepub fn try_range_to_js_string(
&self,
range: Range<usize>,
) -> Result<Option<JsString>, JsStringError>
pub fn try_range_to_js_string( &self, range: Range<usize>, ) -> Result<Option<JsString>, JsStringError>
Reconstruct one valid carrier byte range as an ECMAScript String.
Ok(None) means the range is out of bounds or splits a UTF-8 scalar.
Sourcepub fn try_range_to_raw_bytes(
&self,
range: Range<usize>,
) -> Result<Option<Vec<u8>>, TryReserveError>
pub fn try_range_to_raw_bytes( &self, range: Range<usize>, ) -> Result<Option<Vec<u8>>, TryReserveError>
Clone one valid carrier byte range as exact original source bytes.
Ok(None) means the range is out of bounds or splits a UTF-8 scalar.