pub struct Offsets { /* private fields */ }Expand description
Converts offsets into one text between UTF-8 bytes, the unit of the Rust crates, and UTF-16 code units, the unit of JavaScript strings and browser editors.
Build it once per text, then convert as many offsets as needed, in either direction:
use jlens_core::Offsets;
// `é` takes 2 bytes and 1 UTF-16 unit; `𝄞` takes 4 bytes and 2 units (a surrogate pair).
let offsets = Offsets::new("é𝄞x");
assert_eq!(offsets.to_utf16(6), 3); // the byte offset of `x`
assert_eq!(offsets.to_byte(3), 6);Each direction maps an offset that falls inside a character to the start of that character (a byte inside a multi-byte character, or a unit between the halves of a surrogate pair), and an offset past the end of the text to the end. For ASCII text, both units are the same and no table is built.
Implementations§
Source§impl Offsets
impl Offsets
Sourcepub fn new(text: &str) -> Self
pub fn new(text: &str) -> Self
Prepares the conversion of offsets into text. Outside ASCII, this builds a table of
four bytes per byte of text, so each conversion takes constant or logarithmic time.
Trait Implementations§
Auto Trait Implementations§
impl Forget for Offsets
impl Freeze for Offsets
impl Move for Offsets
impl RefUnwindSafe for Offsets
impl Send for Offsets
impl Sync for Offsets
impl Unpin for Offsets
impl UnsafeUnpin for Offsetswhere
*const u8 is !null: UnsafeUnpin,
PhantomData<u32>: UnsafeUnpin,
*const str is !null: UnsafeUnpin,
PhantomData<str>: UnsafeUnpin,
impl UnwindSafe for Offsets
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more