### Example: Slice and Collect Characters Source: https://docs.rs/jumprope/1.1.2/src/jumprope/iter.rs.html Demonstrates slicing a JumpRope by character range and collecting the result into a String. This example shows how to extract a specific part of the rope based on character indices. ```rust # use jumprope::* let rope = JumpRope::from("xxxGreetings!xxx"); assert_eq!("Greetings!", rope.slice_chars(3..rope.len_chars() - 3).collect::() ); ``` -------------------------------- ### Get Start Slice as String Source: https://docs.rs/jumprope/1.1.2/src/jumprope/gapbuffer.rs.html Returns a string slice representing the data before the gap. This is an unsafe operation as it relies on the internal byte offset. ```rust pub fn start_as_str(&self) -> &str { unsafe { slice_to_str(&self.data[0..self.gap_start_bytes as usize]) } } ``` -------------------------------- ### Node.js String Length Example Source: https://docs.rs/jumprope/1.1.2/index.html Demonstrates how JavaScript's Node.js reports the length of the Unicode string '🐻‍❄️' as 5, highlighting potential discrepancies with other length metrics. ```javascript $ node Welcome to Node.js v16.6.1. > "🐻‍❄️".length 5 ``` -------------------------------- ### JumpRope Node Iterator Initialization Source: https://docs.rs/jumprope/1.1.2/src/jumprope/iter.rs.html Provides a method to get a NodeIter starting from the head of the JumpRope. Use this to begin iterating through the rope's internal nodes. ```rust impl JumpRope { pub(crate) fn node_iter_at_start(&self) -> NodeIter { NodeIter(Some(&self.head)) } /// Iterate over the rope, visiting each substring in [`str`] chunks. Whenever possible, this is /// the best way for a program to read back the contents of a rope, because it avoids allocating ``` -------------------------------- ### Node String Slice Access (Start) Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Provides read-only access to the string content at the beginning of the Node's GapBuffer. ```rust fn as_str_1(&self) -> &str { self.str.start_as_str() } ``` -------------------------------- ### MutCursor Initialization at Start Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Creates a mutable cursor positioned at the beginning of the skip list. Initializes the cursor's internal skip entries. ```rust fn mut_cursor_at_start(&mut self) -> MutCursor<'_> { MutCursor { inner: [SkipEntry { node: &mut self.head, skip_chars: 0, #[cfg(feature = "wchar_conversion")] skip_pairs: 0 }; MAX_HEIGHT+1], rng: &mut self.rng, num_bytes: &mut self.num_bytes, phantom: PhantomData, } } ``` -------------------------------- ### Comparing String Length Representations Source: https://docs.rs/jumprope/1.1.2/src/jumprope/lib.rs.html Illustrates the differences between byte length, character count, and grapheme clusters using a complex emoji. This example highlights why counting Unicode characters is often preferred for consistency. ```rust # use jumprope::*; assert_eq!("🐻‍❄️".len(), 13); assert_eq!("🐻‍❄️".chars().count(), 4); let rope = JumpRope::from("🐻‍❄️"); // One grapheme cluster assert_eq!(rope.len_bytes(), 13); // 13 UTF8 bytes assert_eq!(rope.len_chars(), 4); // 4 unicode characters ``` -------------------------------- ### Buffered Insert Operation Logic Source: https://docs.rs/jumprope/1.1.2/src/jumprope/buffered.rs.html Handles local inserts, clearing content if the start position matches the rope's start. Otherwise, it trims content based on character and byte offsets. ```Rust if start == self.range.start { // Discard our local insert. self.ins_content.clear(); self.range.end = self.range.start; Ok(()) } else { // Trim from the end. let char_offset = start - self.range.start; let byte_offset = if self.range.len() == self.ins_content.len() { // If its all ascii, char offset == byte offset. char_offset } else { // TODO: Come up with a better way to calculate this. char_to_byte_idx(self.ins_content.as_str(), char_offset) }; self.range.end = start; self.ins_content.truncate(byte_offset); Ok(()) } ``` -------------------------------- ### start_as_str Source: https://docs.rs/jumprope/1.1.2/src/jumprope/gapbuffer.rs.html Returns a string slice representing the content before the gap. ```APIDOC ## start_as_str ### Description Returns a string slice representing the content before the gap. ### Method `pub fn start_as_str(&self) -> &str` ``` -------------------------------- ### Basic JumpRope Operations Source: https://docs.rs/jumprope/1.1.2/index.html Demonstrates basic insertion, replacement, and conversion to string using JumpRope. Ensure JumpRope is imported before use. ```rust use jumprope::JumpRope; let mut rope = JumpRope::from("Some large text document"); rope.insert(5, "really "); // "Some really large text document" rope.replace(0..4, "My rad"); // "My rad really large text document" assert_eq!(rope, "My rad really large text document"); // Extract to a string let s: String = rope.to_string(); assert_eq!(s, "My rad really large text document"); ``` -------------------------------- ### Get JumpRope Length in Unicode Characters Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Returns the length of the rope in Unicode characters in constant time (O(1)). This is not the byte length or grapheme cluster count. For example, '↯' has a length of 1 character, and '☃️' has a length of 2 characters. ```rust assert_eq!("↯".len(), 3); let rope = JumpRope::from("↯"); assert_eq!(rope.len_chars(), 1); // The unicode snowman grapheme cluster needs 2 unicode characters. let snowman = JumpRope::from("☃️"); assert_eq!(snowman.len_chars(), 2); ``` -------------------------------- ### Default::default Source: https://docs.rs/jumprope/1.1.2/src/jumprope/buffered.rs.html Creates a new, empty JumpRopeBuf. ```APIDOC ## Default::default ### Description Creates a new, empty `JumpRopeBuf` instance. This is the default constructor for the type. ### Method `default() -> Self` ### Parameters None ### Returns `Self` - A new, empty `JumpRopeBuf`. ``` -------------------------------- ### Any Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of Any trait. ```APIDOC ## impl Any for T where T: 'static + ?Sized, ### Description Provides access to type information. #### fn type_id(&self) -> TypeId ##### Description Gets the `TypeId` of `self`. ``` -------------------------------- ### Get Length in Bytes Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Returns the number of bytes used for the UTF8 representation of the rope. ```rust pub fn len_bytes(&self) -> usize ``` -------------------------------- ### Node Creation with Height and Content Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Constructs a new Node with a specified height and initial string content. It initializes the `nexts` field with default SkipEntry values. ```rust fn new_with_height(height: u8, content: &str) -> Self { Self { str: GapBuffer::new_from_str(content), height, nexts: [SkipEntry::default(); MAX_HEIGHT+1] } } ``` -------------------------------- ### len_bytes Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Gets the number of bytes used for the UTF8 representation of the rope. This is equivalent to the `.len()` of a String. ```APIDOC ## len_bytes ### Description Returns the number of bytes used for the UTF8 representation of the rope. This value is equivalent to the `.len()` of a `String`. This method is useful for scenarios like preparing a byte buffer for saving or network transmission. For general use, `len_chars` is often preferred. ### Method `len_bytes(&self) -> usize` ### Return Value - `usize`: The total number of bytes in the UTF8 representation of the rope. ### Example ```rust # use jumprope::* let str = "κόσμε"; // "Cosmos" in ancient greek assert_eq!(str.len(), 11); // 11 bytes over the wire let rope = JumpRope::from(str); assert_eq!(rope.len_bytes(), str.len()); ``` ``` -------------------------------- ### JumpRope Creation Methods Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Methods for creating new JumpRope instances with different initialization strategies. ```APIDOC ## pub fn new() ### Description Creates and returns a new, empty rope. In release mode this method is an alias for `new_from_entropy`. But when compiled for testing (or in debug mode), we use a fixed seed in order to keep tests fully deterministic. Note using this method in wasm significantly increases bundle size. Use `new_with_seed` instead. ### Method `new()` ### Returns `Self` - A new, empty JumpRope instance. ``` ```APIDOC ## pub fn new_from_entropy() ### Description Creates a new, empty rope seeded from an entropy source. ### Method `new_from_entropy()` ### Returns `Self` - A new, empty JumpRope instance seeded from entropy. ``` ```APIDOC ## pub fn new_from_seed(seed: u64) ### Description Creates a new, empty rope using an RNG seeded from the passed u64 parameter. The performance of this library with any particular data set will vary by a few percent within a range based on the seed provided. It may be useful to fix the seed within tests or benchmarks in order to make the program entirely deterministic, though bear in mind: * Jumprope will always use a fixed seed ### Method `new_from_seed(seed: u64)` ### Parameters #### Path Parameters - **seed** (u64) - Required - The seed for the random number generator. ``` -------------------------------- ### JumpRopeBuf Initialization Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Methods for creating new instances of JumpRopeBuf, either from an existing JumpRope, an empty rope, or a string slice. ```APIDOC ## JumpRopeBuf::with_rope ### Description Creates a new JumpRopeBuf with the provided JumpRope. ### Signature ```rust pub fn with_rope(rope: JumpRope) -> Self ``` ## JumpRopeBuf::new ### Description Creates a new, empty JumpRopeBuf. ### Signature ```rust pub fn new() -> Self ``` ## JumpRopeBuf::new_from_str ### Description Creates a new JumpRopeBuf initialized with the content of a string slice. ### Signature ```rust pub fn new_from_str(s: &str) -> Self ``` ``` -------------------------------- ### Get Length in Unicode Characters Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Returns the length of the rope in Unicode characters in constant time (O(1)). ```rust pub fn len_chars(&self) -> usize ``` -------------------------------- ### Sync for JumpRope Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of Sync for JumpRope. ```APIDOC ## impl Sync for JumpRope ### Description Indicates that JumpRope can be safely shared between threads. JumpRope is Send and Sync, because the only way to (safely) mutate the rope is via a &mut reference. ``` -------------------------------- ### Get Global Character Position (JumpRope) Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Retrieves the global character position within the JumpRope. This is calculated based on the height of the rope. ```rust pub(crate) fn global_char_pos(&self) -> usize { self.inner[self.head_height() - 1].skip_chars } ``` -------------------------------- ### Display Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of Display for JumpRope. ```APIDOC ## impl Display for JumpRope ### Description Allows JumpRope instances to be formatted for display. #### fn fmt(&self, f: &mut Formatter<'_>) -> Result ##### Description Formats the value using the given formatter. ``` -------------------------------- ### Get Wide Character Position (JumpRope) Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Calculates the position in wide characters (pairs) within the JumpRope. Requires the 'wchar_conversion' feature to be enabled. ```rust #[cfg(feature = "wchar_conversion")] pub(crate) fn wchar_pos(&self) -> usize { let entry = &self.inner[self.head_height() - 1]; entry.skip_chars + entry.skip_pairs } ``` -------------------------------- ### PartialEq for JumpRopeBuf Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of PartialEq for comparing JumpRopeBuf with JumpRope. ```APIDOC ## impl PartialEq for JumpRopeBuf ### Description Allows comparison between JumpRopeBuf and JumpRope. #### fn eq(&self, other: &JumpRope) -> bool ##### Description Tests for equality between self and other. #### fn ne(&self, other: &Rhs) -> bool ##### Description Tests for inequality between self and other. ``` -------------------------------- ### Get Rope Length in Unicode Characters Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Returns the length of the rope in Unicode characters. This is a constant-time operation. Note that this is not the same as the number of bytes or grapheme clusters. ```rust /// Return the length of the rope in unicode characters. Note this is not the same as either /// the number of bytes the characters take, or the number of grapheme clusters in the string. /// /// This method returns the length in constant-time (*O(1)*). /// /// # Example /// /// ``` /// # use jumprope::* /// assert_eq!("↯".len(), 3); /// /// let rope = JumpRope::from("↯"); /// assert_eq!(rope.len_chars(), 1); /// /// // The unicode snowman grapheme cluster needs 2 unicode characters. /// let snowman = JumpRope::from("☃️"); /// assert_eq!(snowman.len_chars(), 2); /// ``` pub fn len_chars(&self) -> usize { self.head.nexts[self.head.height as usize - 1].skip_chars } ``` -------------------------------- ### Any Trait Implementations Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Provides access to type information. ```APIDOC ## fn type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Method `type_id` ``` -------------------------------- ### Get Local Character Position (JumpRope) Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Returns the local character position within the current node of the JumpRope. This is the offset from the beginning of the current node. ```rust pub(crate) fn local_char_pos(&self) -> usize { self.inner[0].skip_chars } ``` -------------------------------- ### From Source: https://docs.rs/jumprope/1.1.2/src/jumprope/buffered.rs.html Creates a JumpRopeBuf from a string slice or String. ```APIDOC ## From ### Description Creates a new `JumpRopeBuf` initialized with the content of a given string slice (`&str`) or `String`. The provided string is directly used to initialize the underlying `JumpRope`. ### Method `from(str: S) -> Self` where `S: AsRef` ### Parameters - **str** (`S`) - The string slice or `String` to initialize the `JumpRopeBuf` with. ### Returns `Self` - A new `JumpRopeBuf` containing the provided string data. ``` -------------------------------- ### Default Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of Default for JumpRope. ```APIDOC ## impl Default for JumpRope ### Description Provides a default value for JumpRope. #### fn default() -> Self ##### Description Returns the default value for JumpRope. ``` -------------------------------- ### Get Current Node Pointer (JumpRope) Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Returns a mutable pointer to the current node in the JumpRope. This is an internal method used for direct node manipulation. ```rust pub(crate) fn here_mut_ptr(&mut self) -> *mut Node { self.inner[0].node } ``` -------------------------------- ### Delete Text from Rope Cursor Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Deletes a specified number of characters starting from the current cursor position. Handles cases where deletion spans multiple nodes. ```rust fn del_at_cursor(cursor: &mut MutCursor, mut length: usize) { if length == 0 { return; } let mut offset_chars = cursor.local_char_pos(); let mut node = cursor.here_ptr(); unsafe { while length > 0 { { ``` -------------------------------- ### JumpRopeBuf::new Source: https://docs.rs/jumprope/1.1.2/src/jumprope/buffered.rs.html Creates a new, empty buffered rope. ```APIDOC ## JumpRopeBuf::new ### Description Creates a new, empty buffered rope. ### Method `JumpRopeBuf::new()` ### Returns A new `JumpRopeBuf` instance. ### Example ```rust let buffered_rope = JumpRopeBuf::new(); ``` ``` -------------------------------- ### Flush changes and get mutable reference to JumpRope Source: https://docs.rs/jumprope/1.1.2/src/jumprope/buffered.rs.html Flushes any buffered operations and returns a mutable reference to the underlying JumpRope. This is part of the `AsMut` implementation. ```rust impl AsMut for JumpRopeBuf { /// Flush changes into the rope and mutably borrow the rope. fn as_mut(&mut self) -> &mut JumpRope { let inner = self.0.get_mut(); Self::flush_mut(inner); &mut inner.0 } } ``` -------------------------------- ### Implement PartialEq for &JumpRope Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Enables comparison of a reference to a JumpRope with a String. ```rust impl<'a> PartialEq for &JumpRope { fn eq(&self, other: &String) -> bool { self.eq_str(other.as_str()) } } ``` -------------------------------- ### Get End Slice as String Source: https://docs.rs/jumprope/1.1.2/src/jumprope/gapbuffer.rs.html Returns a string slice representing the data after the gap. This is an unsafe operation as it relies on the internal byte offset and gap length. ```rust pub fn end_as_str(&self) -> &str { unsafe { slice_to_str(&self.data[(self.gap_start_bytes +self.gap_len) as usize..LEN]) } } ``` -------------------------------- ### Implement PartialEq for JumpRopeBuf Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Compares a JumpRopeBuf with a JumpRope for equality. ```rust fn eq(&self, other: &JumpRope) -> bool ``` ```rust fn ne(&self, other: &Rhs) -> bool ``` -------------------------------- ### CloneToUninit Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of CloneToUninit trait. ```APIDOC ## impl CloneToUninit for T where T: Clone, ### Description Allows cloning into an uninitialized memory. ``` -------------------------------- ### remove Source: https://docs.rs/jumprope/1.1.2/src/jumprope/gapbuffer.rs.html Removes a specified number of bytes starting from a given position. The gap is moved to the specified position before the removal occurs. Returns the number of bytes actually removed. ```APIDOC ## remove ### Description Removes a specified number of bytes starting from a given position. The gap is moved to the specified position before the removal occurs. Returns the number of bytes actually removed. ### Signature `pub fn remove(&mut self, pos: usize, del_len: usize) -> usize` ### Parameters * `pos` (*usize*) - The starting byte position for removal. * `del_len` (*usize*) - The maximum number of bytes to remove. ### Returns * The number of bytes actually removed. ``` -------------------------------- ### PartialEq for &JumpRope Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of PartialEq for comparing &JumpRope with String. ```APIDOC ## impl PartialEq for &JumpRope ### Description Allows comparison between a reference to JumpRope and a String. #### fn eq(&self, other: &String) -> bool ##### Description Tests for equality between self and other. #### fn ne(&self, other: &Rhs) -> bool ##### Description Tests for inequality between self and other. ``` -------------------------------- ### Remove Bytes at Gap Position in Rust Source: https://docs.rs/jumprope/1.1.2/src/jumprope/gapbuffer.rs.html Removes a specified number of bytes starting from the current gap position. This is a helper for `remove_chars` and adjusts gap properties accordingly. ```rust pub fn remove(&mut self, pos: usize, del_len: usize) -> usize { let len = self.len_bytes(); if pos >= len { return 0; } let del_len = del_len.min(len - pos); self.move_gap(pos); self.remove_after_gap(del_len); del_len } ``` -------------------------------- ### Remove Characters from Gap Buffer in Rust Source: https://docs.rs/jumprope/1.1.2/src/jumprope/gapbuffer.rs.html Removes a specified number of characters starting from a given position. It handles moving the gap and adjusting character/byte counts, including Unicode considerations. ```rust pub fn remove_chars(&mut self, pos: usize, mut del_len: usize) -> usize { // This function is longer than it needs to be; but having it be a bit longer makes the // code faster. I think the trade-off is worth it. // self.move_gap(self.count_bytes(pos)); // let removed_bytes = str_get_byte_offset(s.end_as_str(), del_len); // self.remove_at_gap(removed_bytes); // removed_bytes if del_len == 0 { return 0; } debug_assert!(del_len <= self.len_bytes() - pos); let mut rm_start_bytes = 0; let gap_chars = self.gap_start_chars as usize; #[cfg(any(feature = "wchar_conversion", feature = "line_conversion"))] let gap_start_bytes = self.gap_start_bytes as usize; if pos <= gap_chars && pos+del_len >= gap_chars { if pos < gap_chars { // Delete the bit from pos..gap. // TODO: It would be better to count backwards here. // let pos_bytes = str_get_byte_offset(self.start_as_str(), pos) as u16; // rm_start_bytes = self.gap_start_bytes - pos_bytes; rm_start_bytes = self.int_chars_to_bytes_backwards(self.start_as_str(), gap_chars - pos); #[cfg(feature = "wchar_conversion")] if !self.all_ascii { self.gap_start_surrogate_pairs -= unsafe { count_utf16_surrogates_in_bytes(&self.data[gap_start_bytes - rm_start_bytes..gap_start_bytes]) as u16 } } #[cfg(feature = "line_conversion")] { unsafe { let s = std::str::from_utf8_unchecked(&self.data[gap_start_bytes - rm_start_bytes..gap_start_bytes]); self.gap_start_lines -= count_lines(s) as u16; } } del_len -= self.gap_start_chars as usize - pos; let rm_start_bytes = rm_start_bytes as u16; self.gap_len += rm_start_bytes; self.gap_start_chars = pos as u16; self.gap_start_bytes -= rm_start_bytes; // self.gap_start_bytes = pos_bytes; if del_len == 0 { return rm_start_bytes as usize; } } debug_assert!(del_len > 0); debug_assert!(pos >= self.gap_start_chars as usize); } else { ``` -------------------------------- ### GapBuffer Initialization Source: https://docs.rs/jumprope/1.1.2/src/jumprope/gapbuffer.rs.html Provides methods to create a new, empty GapBuffer or initialize one from an existing string slice. ```rust pub fn new() -> Self { Self { data: [0; LEN], gap_start_bytes: 0, gap_start_chars: 0, #[cfg(feature = "wchar_conversion")] gap_start_surrogate_pairs: 0, #[cfg(feature = "line_conversion")] gap_start_lines: 0, gap_len: LEN as u16, all_ascii: true, } } pub fn new_from_str(s: &str) -> Self { let mut val = Self::new(); val.try_insert(0, s).unwrap(); val } ``` -------------------------------- ### Clone Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of Clone for JumpRope. ```APIDOC ## impl Clone for JumpRope ### Description Allows creating a duplicate of a JumpRope. #### fn clone(&self) -> Self ##### Description Returns a duplicate of the value. ##### Returns A new JumpRope instance that is a duplicate of the original. #### fn clone_from(&mut self, source: &Self) ##### Description Performs copy-assignment from `source`. ``` -------------------------------- ### Get Byte Length of JumpRope Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Retrieve the total number of bytes used by the UTF8 representation of the rope using `len_bytes()`. This is equivalent to the `.len()` of a `String` and is useful for serialization or network transmission. ```rust # use jumprope::* let str = "κόσμε"; // "Cosmos" in ancient greek assert_eq!(str.len(), 11); // 11 bytes over the wire let rope = JumpRope::from(str); assert_eq!(rope.len_bytes(), str.len()); ``` -------------------------------- ### Replace Wide Characters in Rope Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Replaces a specified range of wide characters with new content. This method first removes the existing content in the range and then inserts the new content at the start of the range. ```rust pub fn replace_at_wchar(&mut self, range: Range, content: &str) { // TODO: Optimize this. This method should work similarly to replace(), where we create // a single cursor and use it in both contexts. if !range.is_empty() { self.remove_at_wchar(range.clone()); } if !content.is_empty() { self.insert_at_wchar(range.start, content); } } ``` -------------------------------- ### Create JumpRopeBuf with Existing Rope Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Initializes a JumpRopeBuf with an existing JumpRope instance. ```rust pub fn with_rope(rope: JumpRope) -> Self ``` -------------------------------- ### From for JumpRopeBuf Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of From for converting JumpRope to JumpRopeBuf. ```APIDOC ## impl From for JumpRopeBuf ### Description Allows conversion from JumpRope to JumpRopeBuf. #### fn from(rope: JumpRope) -> Self ##### Description Converts to JumpRopeBuf from a JumpRope instance. ``` -------------------------------- ### Get Rope Length in Wide Characters Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Calculates the length of the rope in wide characters, which is equivalent to the byte length when encoded to UTF16 divided by two. This feature requires the `wchar_conversion` feature to be enabled. ```rust /// String length in wide characters (as would be reported by javascript / C# / etc). /// /// The byte length of this string when encoded to UTF16 will be exactly /// `rope.len_wchars() * 2`. #[cfg(feature = "wchar_conversion")] pub fn len_wchars(&self) -> usize { let SkipEntry { skip_chars, skip_pairs, .. // other fields ignored } = self.head.nexts[self.head.height as usize - 1]; skip_pairs + skip_chars } ``` -------------------------------- ### clone_to_uninit Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Performs copy-assignment from `self` to `dest`. This is a nightly-only experimental API. ```APIDOC ## unsafe fn clone_to_uninit(&self, dest: *mut u8) ### Description Performs copy-assignment from `self` to `dest`. ### Method `unsafe fn` ### Parameters #### Path Parameters - `dest` (*mut u8) - Required - The destination pointer. ### Note This is a nightly-only experimental API. ``` -------------------------------- ### Get JumpRope Length in Wide Characters Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Returns the string length in wide characters, equivalent to what JavaScript, Java, or C# would report. The byte length when encoded to UTF16 is twice this value. ```rust pub fn len_wchars(&self) -> usize ``` -------------------------------- ### Create JumpRopeBuf from String Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Initializes a JumpRopeBuf with content from a string slice. ```rust pub fn new_from_str(s: &str) -> Self ``` -------------------------------- ### Implement PartialEq for &JumpRopeBuf Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Compares a borrowed JumpRopeBuf with a String for equality. ```rust fn eq(&self, other: &String) -> bool ``` ```rust fn ne(&self, other: &Rhs) -> bool ``` -------------------------------- ### Get Mutable Cursor at Character Position Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Returns a mutable cursor at a specified character position. This method is internal and used for modifying the rope. It initializes a `MutCursor` struct, which holds internal state for mutation operations. ```rust pub(super) fn mut_cursor_at_char(&mut self, char_pos: usize, stick_end: bool) -> MutCursor<'_> { assert!(char_pos <= self.len_chars()); let mut e: *mut Node = &mut self.head; let head_height = self.head.height as usize; let mut height = head_height - 1; let mut offset = char_pos; // How many more chars to skip #[cfg(feature = "wchar_conversion")] let mut surrogate_pairs = 0; // Current wchar pos from the start of the rope // It would be nice to pop this into a function, but miri gets confused if we pass the node // pointer out of this method. So I'm keeping this inline. let mut cursor = MutCursor { inner: [SkipEntry { node: e, skip_chars: 0, #[cfg(feature = "wchar_conversion")] skip_pairs: 0 }; MAX_HEIGHT+1], rng: &mut self.rng, num_bytes: &mut self.num_bytes, phantom: PhantomData, }; // The rest of the logic to position the cursor would go here, similar to read_cursor_at_char // but operating on mutable pointers and potentially updating the cursor struct. // This part is omitted as it's not fully present in the provided snippet. cursor } ``` -------------------------------- ### JumpRopeBuf::new Constructor Source: https://docs.rs/jumprope/1.1.2/src/jumprope/buffered.rs.html Creates an empty JumpRopeBuf instance. ```Rust pub fn new() -> Self { Self::with_rope(JumpRope::new()) } ``` -------------------------------- ### Get JumpRope Length in Bytes (UTF8) Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Returns the number of bytes used by the UTF8 representation of the rope, matching a `String`'s `.len()`. Useful for byte buffer operations like saving or network transmission. ```rust let str = "κόσμε"; // "Cosmos" in ancient greek assert_eq!(str.len(), 11); // 11 bytes over the wire let rope = JumpRope::from(str); assert_eq!(rope.len_bytes(), str.len()); ``` -------------------------------- ### Debug Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of Debug for JumpRope. ```APIDOC ## impl Debug for JumpRope ### Description Allows JumpRope instances to be formatted for debugging. #### fn fmt(&self, f: &mut Formatter<'_>) -> Result ##### Description Formats the value using the given formatter. ``` -------------------------------- ### remove_chars Source: https://docs.rs/jumprope/1.1.2/src/jumprope/gapbuffer.rs.html Removes a specified number of characters starting from a given position. This method handles multi-byte characters and updates internal counts for bytes, characters, surrogate pairs, and lines accordingly. It returns the number of bytes removed. ```APIDOC ## remove_chars ### Description Removes a specified number of characters starting from a given position. This method handles multi-byte characters and updates internal counts for bytes, characters, surrogate pairs, and lines accordingly. It returns the number of bytes removed. ### Signature `pub fn remove_chars(&mut self, pos: usize, del_len: usize) -> usize` ### Parameters * `pos` (*usize*) - The starting character position for removal. * `del_len` (*usize*) - The number of characters to remove. ### Returns * The number of bytes removed. ``` -------------------------------- ### Get Read Cursor at Character Position Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Returns a read cursor at a specified character position. The `stick_end` parameter influences behavior when the position is at the end of a node. This method is internal and requires careful handling of node pointers. ```rust pub(crate) fn read_cursor_at_char(&self, char_pos: usize, stick_end: bool) -> ReadCursor<'_> { assert!(char_pos <= self.len_chars()); let mut e: *const Node = &self.head; let mut height = self.head.height as usize - 1; let mut offset_chars = char_pos; // How many more chars to skip #[cfg(feature = "wchar_conversion")] let mut global_pairs = 0; // Current wchar pos from the start of the rope loop { // while height >= 0 let en = unsafe { &*e }; let next = en.nexts[height]; let skip = next.skip_chars; if offset_chars > skip || (!stick_end && offset_chars == skip && !next.node.is_null()) { // Go right. // debug_assert!(e == &self.head || !en.str.is_empty()); offset_chars -= skip; #[cfg(feature = "wchar_conversion")] { global_pairs += next.skip_pairs; } e = next.node; assert!(!e.is_null(), "Internal constraint violation: Reached rope end prematurely"); } else { // Go down. if height != 0 { height -= 1; } else { #[cfg(feature = "wchar_conversion")] let offset_pairs = en.str.count_surrogate_pairs(offset_chars); #[cfg(feature = "wchar_conversion")] { global_pairs += offset_pairs; } return ReadCursor { node: unsafe { &*e }, offset_chars, // #[cfg(feature = "wchar_conversion")] // offset_pairs, phantom: PhantomData, #[cfg(feature = "wchar_conversion")] global_pairs, } } } }; } ``` -------------------------------- ### PartialEq for JumpRope Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of PartialEq for comparing two JumpRope instances. ```APIDOC ## impl PartialEq for JumpRope ### Description Allows comparison between two JumpRope instances. #### fn eq(&self, other: &JumpRope) -> bool ##### Description Tests for equality between self and other. #### fn ne(&self, other: &Rhs) -> bool ##### Description Tests for inequality between self and other. ``` -------------------------------- ### Get JumpRope Memory Size (Debugging) Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Calculates the total memory allocated by the rope. This method is for debugging, has O(n) complexity, and is not part of the stable API. It may double-count memory if the rope is nested within another structure. ```rust pub fn mem_size(&self) -> usize ``` -------------------------------- ### JumpRopeBuf::new_from_str Constructor Source: https://docs.rs/jumprope/1.1.2/src/jumprope/buffered.rs.html Creates a new JumpRopeBuf instance initialized with a string slice. ```Rust pub fn new_from_str(s: &str) -> Self { Self::with_rope(JumpRope::from(s)) } ``` -------------------------------- ### Default JumpRope Initialization Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Provides a default JumpRope instance by calling the `new()` constructor. This is the standard way to create an empty JumpRope. ```rust impl Default for JumpRope { fn default() -> Self { Self::new() } } ``` -------------------------------- ### PartialEq for JumpRope Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of PartialEq for comparing JumpRope with other types that implement AsRef. ```APIDOC ## impl> PartialEq for JumpRope ### Description Allows comparison between JumpRope and types that can be referenced as strings. #### fn eq(&self, other: &T) -> bool ##### Description Tests for equality between self and other. #### fn ne(&self, other: &Rhs) -> bool ##### Description Tests for inequality between self and other. ``` -------------------------------- ### Implement PartialEq for JumpRopeBuf Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Compares a JumpRopeBuf with a string slice for equality. ```rust fn eq(&self, other: &str) -> bool ``` -------------------------------- ### Insertion and removal methods Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Methods for modifying the JumpRope by inserting or removing content at specific wchar positions. ```APIDOC ## insert_at_wchar ### Description Insert the given utf8 string into the rope at the specified wchar position. This is compatible with NSString, Javascript, etc. Returns the insertion position in characters. **NOTE:** This method's behaviour is undefined if the wchar offset is invalid. Eg, given a rope with contents `𐆚` (a single character with wchar length 2), `insert_at_wchar(1, ...)` is undefined and may panic / change in future versions of diamond types. ### Method `pub fn insert_at_wchar(&mut self, pos_wchar: usize, contents: &str) -> usize` ### Parameters - **pos_wchar** (usize) - The wchar position at which to insert the content. - **contents** (&str) - The UTF-8 string to insert. ### Response #### Success Response (usize) - The character position of the insertion. ## remove_at_wchar ### Description Remove items from the rope, specified by the passed range. The indexes are interpreted as wchar offsets (like you'd get in javascript / C# / etc). **NOTE:** This method's behaviour is undefined if the wchar offset is invalid. Eg, given a rope with contents `𐆚` (a single character with wchar length 2), `remove_at_wchar(1..2)` is undefined and may panic / change in future versions of diamond types. ### Method `pub fn remove_at_wchar(&mut self, range: std::ops::Range) ### Parameters - **range** (std::ops::Range) - The range of wchar offsets to remove. ### Response None ``` -------------------------------- ### From for JumpRope Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of From for converting string slices to JumpRope. ```APIDOC ## impl> From for JumpRope ### Description Allows conversion from string slices to JumpRope. #### fn from(str: S) -> Self ##### Description Converts to JumpRope from a string slice. ``` -------------------------------- ### JumpRope Utility Methods Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Utility methods for checking the state of the rope. ```APIDOC ## pub fn check(&self) ### Description Performs an internal consistency check on the rope. ### Method `check()` ``` -------------------------------- ### GapBuffer::new Source: https://docs.rs/jumprope/1.1.2/src/jumprope/gapbuffer.rs.html Creates a new, empty GapBuffer with the specified capacity. ```APIDOC ## GapBuffer::new ### Description Creates a new, empty GapBuffer with the specified capacity. ### Signature ```rust pub fn new() -> Self ``` ### Returns A new instance of `GapBuffer`. ``` -------------------------------- ### AsMut Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of AsMut for JumpRope. ```APIDOC ## impl AsMut for JumpRopeBuf ### Description Provides mutable access to the JumpRope. #### fn as_mut(&mut self) -> &mut JumpRope ##### Description Flush changes into the rope and mutably borrow the rope. ##### Returns A mutable reference to the JumpRope. ``` -------------------------------- ### CloneToUninit Trait Implementation Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Provides experimental functionality for cloning to uninitialized memory. ```APIDOC ## unsafe fn clone_to_uninit(&self, dest: *mut u8) ### Description Performs copy-assignment from `self` to `dest`. ### Method `clone_to_uninit` ``` -------------------------------- ### Implement PartialEq for JumpRope Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Allows direct comparison of a JumpRope with a string slice. ```rust impl PartialEq for JumpRope { fn eq(&self, other: &str) -> bool { self.eq_str(other) } } ``` -------------------------------- ### PartialEq for JumpRope Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of PartialEq for comparing JumpRope with string slices. ```APIDOC ## impl PartialEq for JumpRope ### Description Allows comparison between JumpRope and string slices. #### fn eq(&self, other: &str) -> bool ##### Description Tests for equality between self and other. #### fn ne(&self, other: &Rhs) -> bool ##### Description Tests for inequality between self and other. ``` -------------------------------- ### Test char_to_byte_idx with ASCII and Hiragana ranges Source: https://docs.rs/jumprope/1.1.2/src/jumprope/fast_str_tools.rs.html Tests the `char_to_byte_idx` function across different character ranges, including ASCII and Hiragana, and beyond the string length. ```rust #[test] fn char_to_byte_idx_05() { // Ascii range for i in 0..88 { assert_eq!(i, char_to_byte_idx(TEXT_LINES, i)); } // Hiragana characters for i in 88..100 { assert_eq!(88 + ((i - 88) * 3), char_to_byte_idx(TEXT_LINES, i)); } // Past the end for i in 100..110 { assert_eq!(124, char_to_byte_idx(TEXT_LINES, i)); } } ``` -------------------------------- ### PartialEq for JumpRopeBuf Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Provides methods for comparing JumpRopeBuf instances for equality. ```APIDOC ## fn eq(&self, other: &JumpRopeBuf) -> bool ### Description Tests for `self` and `other` values to be equal, and is used by `==`. ### Method `eq` ``` ```APIDOC ## fn ne(&self, other: &Rhs) -> bool ### Description Tests for `!=`. The default implementation is almost always sufficient, and should not be overridden without very good reason. ### Method `ne` ``` -------------------------------- ### JumpRope wchar Conversion Methods (wchar_conversion feature) Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Methods for converting between character counts and wide character counts, available when the `wchar_conversion` feature is enabled. ```APIDOC ## pub fn chars_to_wchars(&self, chars: usize) -> usize ### Description Convert from a unicode character count to a wchar index, like what you’d use in Javascript, Java or C#. ### Method `chars_to_wchars(chars: usize) -> usize` ### Parameters #### Path Parameters - **chars** (usize) - Required - The number of unicode characters to convert. ### Returns `usize` - The equivalent number of wide characters. ``` -------------------------------- ### as_mut Source: https://docs.rs/jumprope/1.1.2/src/jumprope/buffered.rs.html Flushes changes into the rope and mutably borrows the rope. ```APIDOC ## as_mut ### Description Flushes any pending buffered operations (inserts and deletes) into the underlying `JumpRope` and returns a mutable reference (`&mut JumpRope`) to it. This allows direct modification of the `JumpRope` after ensuring all buffered changes are applied. ### Method `as_mut(&mut self) -> &mut JumpRope` ### Parameters None ### Returns `&mut JumpRope` - A mutable reference to the underlying `JumpRope` after flushing. ``` -------------------------------- ### PartialEq Source: https://docs.rs/jumprope/1.1.2/src/jumprope/buffered.rs.html Compares JumpRopeBuf with other string types. ```APIDOC ## PartialEq ### Description Implements equality comparison between a `JumpRopeBuf` and other string-like types (`&str`, `String`, `JumpRope`). The comparison first flushes any buffered operations and then compares the resulting `JumpRope` content with the provided type. ### Methods - `eq(&self, other: &T) -> bool` where `T: AsRef` - `eq(&self, other: &str) -> bool` - `eq(&self, other: &JumpRope) -> bool` ### Parameters - **self** (`&JumpRopeBuf` or `&mut JumpRopeBuf` depending on the specific implementation) - **other** (`&T`, `&str`, or `&JumpRope`) - The value to compare against. ### Returns `bool` - `true` if the contents are equal after flushing, `false` otherwise. ``` -------------------------------- ### Default JumpRopeBuf creation Source: https://docs.rs/jumprope/1.1.2/src/jumprope/buffered.rs.html Provides a default JumpRopeBuf instance, equivalent to calling `JumpRopeBuf::new()`. ```rust impl Default for JumpRopeBuf { fn default() -> Self { JumpRopeBuf::new() } } ``` -------------------------------- ### Eq for JumpRope Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Implementation of Eq for JumpRope. ```APIDOC ## impl Eq for JumpRope ### Description Marks JumpRope as supporting total equality. ``` -------------------------------- ### Implement Default for JumpRopeBuf Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Provides the default value for JumpRopeBuf, which is an empty instance. ```rust fn default() -> Self ``` -------------------------------- ### Implement PartialEq for JumpRope Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Enables comparison of JumpRope with any type that can be referenced as a string. ```rust impl> PartialEq for JumpRope { fn eq(&self, other: &T) -> bool { self.eq_str(other.as_ref()) } } ``` -------------------------------- ### Create Empty JumpRopeBuf Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Creates a new, empty JumpRopeBuf. ```rust pub fn new() -> Self ``` -------------------------------- ### Implement CloneFrom for JumpRopeBuf Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Provides a method for copy-assignment from another JumpRopeBuf. ```rust fn clone_from(&mut self, source: &Self) ``` -------------------------------- ### Create New JumpRope from Entropy Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Creates a new, empty JumpRope seeded from a system entropy source. This is suitable for general use where determinism is not required. ```rust pub fn new_from_entropy() -> Self { Self::new_with_rng(RopeRng::from_entropy()) } ``` -------------------------------- ### Create JumpRope from Entropy Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRope.html Creates a new, empty rope seeded from a system entropy source. ```rust pub fn new_from_entropy() -> Self ``` -------------------------------- ### byte_to_utf16_surrogate_idx Source: https://docs.rs/jumprope/1.1.2/src/jumprope/fast_str_tools.rs.html Calculates the UTF-16 surrogate index corresponding to a given byte index in a string slice. This is an `#[inline(always)]` `pub(crate)` function. ```APIDOC ## byte_to_utf16_surrogate_idx ### Description Calculates the UTF-16 surrogate index corresponding to a given byte index in a string slice. ### Parameters #### Path Parameters - **text** (str) - The input string slice. - **byte_idx** (usize) - The byte index. ### Returns - **usize** - The corresponding UTF-16 surrogate index. ``` -------------------------------- ### JumpRopeBuf::with_rope Constructor Source: https://docs.rs/jumprope/1.1.2/src/jumprope/buffered.rs.html Creates a new JumpRopeBuf instance initialized with an existing JumpRope object. ```Rust pub fn with_rope(rope: JumpRope) -> Self { Self(RefCell::new((rope, BufferedOp::new()))) } ``` -------------------------------- ### Default Implementation for SkipEntry Source: https://docs.rs/jumprope/1.1.2/src/jumprope/jumprope.rs.html Provides a default implementation for SkipEntry by calling its `new` method. This ensures a SkipEntry can be created with default values. ```rust impl Default for SkipEntry { fn default() -> Self { Self::new() } } ``` -------------------------------- ### Implement PartialEq> for JumpRopeBuf Source: https://docs.rs/jumprope/1.1.2/jumprope/struct.JumpRopeBuf.html Compares a JumpRopeBuf with a type that can be referenced as a string slice for equality. ```rust fn eq(&self, other: &T) -> bool ``` ```rust fn ne(&self, other: &Rhs) -> bool ``` -------------------------------- ### to_string Source: https://docs.rs/jumprope/1.1.2/src/jumprope/iter.rs.html Converts the JumpRope into a String. ```APIDOC ## to_string ### Description Converts the JumpRope into a String. This method is implemented from the `Display` trait but provides a more efficient way to get the string representation by pre-allocating capacity. ### Signature `pub fn to_string(&self) -> String` ### Example ```rust # use jumprope::JumpRope; let rope = JumpRope::from("Hello, world!"); assert_eq!(rope.to_string(), "Hello, world!"); ``` ```