fn insert_internal(&mut self, char_idx: usize, ins_text: &str) {
        let mut ins_text = ins_text;
        let mut left_seam = false;
        let root_info = self.root.text_info();

        let (l_info, residual) = Arc::make_mut(&mut self.root).edit_chunk_at_char(
            char_idx,
            root_info,
            |idx, cur_info, leaf_text| {
                // First check if we have a left seam.
                if idx == 0 && char_idx > 0 && ins_text.as_bytes()[0] == 0x0A {
                    left_seam = true;
                    ins_text = &ins_text[1..];
                    // Early out if it was only an LF.
                    if ins_text.is_empty() {
                        return (cur_info, None);
                    }
                }

                // Find our byte index
                let byte_idx = char_to_byte_idx(leaf_text, idx);

                // No node splitting
                if (leaf_text.len() + ins_text.len()) <= MAX_BYTES {
                    // Calculate new info without doing a full re-scan of cur_text
                    let new_info = {
                        // Get summed info of current text and to-be-inserted text
                        let mut info = cur_info + TextInfo::from_str(ins_text);
                        // Check for CRLF pairs on the insertion seams, and
                        // adjust line break counts accordingly
                        if byte_idx > 0 {
                            if leaf_text.as_bytes()[byte_idx - 1] == 0x0D
                                && ins_text.as_bytes()[0] == 0x0A
                            {
                                info.line_breaks -= 1;
                            }
                            if byte_idx < leaf_text.len()
                                && leaf_text.as_bytes()[byte_idx - 1] == 0x0D
                                && leaf_text.as_bytes()[byte_idx] == 0x0A
                            {
                                info.line_breaks += 1;
                            }
                        }
                        if byte_idx < leaf_text.len()
                            && *ins_text.as_bytes().last().unwrap() == 0x0D
                            && leaf_text.as_bytes()[byte_idx] == 0x0A
                        {
                            info.line_breaks -= 1;
                        }
                        info
                    };
                    // Insert the text and return the new info
                    leaf_text.insert_str(byte_idx, ins_text);
                    (new_info, None)
                }
                // We're splitting the node
                else {
                    let r_text = leaf_text.insert_str_split(byte_idx, ins_text);
                    let l_text_info = TextInfo::from_str(&leaf_text);
                    if r_text.len() > 0 {
                        let r_text_info = TextInfo::from_str(&r_text);
                        (
                            l_text_info,
                            Some((r_text_info, Arc::new(Node::Leaf(r_text)))),
                        )
                    } else {
                        // Leaf couldn't be validly split, so leave it oversized
                        (l_text_info, None)
                    }
                }
            },
        );

        // Handle root splitting, if any.
        if let Some((r_info, r_node)) = residual {
            let mut l_node = Arc::new(Node::new());
            std::mem::swap(&mut l_node, &mut self.root);

            let mut children = NodeChildren::new();
            children.push((l_info, l_node));
            children.push((r_info, r_node));

            *Arc::make_mut(&mut self.root) = Node::Internal(children);
        }

        // Insert the LF to the left.
        // TODO: this code feels fairly redundant with above.  Can we DRY this
        // better?
        if left_seam {
            // Do the insertion
            let root_info = self.root.text_info();
            let (l_info, residual) = Arc::make_mut(&mut self.root).edit_chunk_at_char(
                char_idx - 1,
                root_info,
                |_, cur_info, leaf_text| {
                    let byte_idx = leaf_text.len();

                    // No node splitting
                    if (leaf_text.len() + ins_text.len()) <= MAX_BYTES {
                        // Calculate new info without doing a full re-scan of cur_text
                        let mut new_info = cur_info;
                        new_info.bytes += 1;
                        new_info.chars += 1;
                        if *leaf_text.as_bytes().last().unwrap() != 0x0D {
                            new_info.line_breaks += 1;
                        }
                        // Insert the text and return the new info
                        leaf_text.insert_str(byte_idx, "\n");
                        (new_info, None)
                    }
                    // We're splitting the node
                    else {
                        let r_text = leaf_text.insert_str_split(byte_idx, "\n");
                        let l_text_info = TextInfo::from_str(&leaf_text);
                        if r_text.len() > 0 {
                            let r_text_info = TextInfo::from_str(&r_text);
                            (
                                l_text_info,
                                Some((r_text_info, Arc::new(Node::Leaf(r_text)))),
                            )
                        } else {
                            // Leaf couldn't be validly split, so leave it oversized
                            (l_text_info, None)
                        }
                    }
                },
            );

            // Handle root splitting, if any.
            if let Some((r_info, r_node)) = residual {
                let mut l_node = Arc::new(Node::new());
                std::mem::swap(&mut l_node, &mut self.root);

                let mut children = NodeChildren::new();
                children.push((l_info, l_node));
                children.push((r_info, r_node));

                *Arc::make_mut(&mut self.root) = Node::Internal(children);
            }
        }
    }
/// Private internal-only method that does a single insertion of
/// sufficiently small text.
///
/// This only works correctly for insertion texts smaller than or equal to
/// `MAX_BYTES - 4`.
///
/// Note that a lot of the complexity in this method comes from avoiding
/// splitting CRLF pairs and, when possible, avoiding re-scanning text for
/// text info.  It is otherwise conceptually fairly straightforward.
Private internal-only method that does a single insertion of
sufficiently small text.

This only works correctly for insertion texts smaller than or equal to
`MAX_BYTES - 4`.

Note that a lot of the complexity in this method comes from avoiding
splitting CRLF pairs and, when possible, avoiding re-scanning text for
text info.  It is otherwise conceptually fairly straightforward.
Private internal - only method that does a single insertion of sufficiently small text . This only works correctly for insertion texts smaller than or equal to ` MAX_BYTES - 4 ` . Note that a lot of the complexity in this method comes from avoiding splitting CRLF pairs and when possible avoiding re - scanning text for text info . It is otherwise conceptually fairly straightforward .
fn insert_internal(&mut self, char_idx: usize, ins_text: &str) {
        let mut ins_text = ins_text;
        let mut left_seam = false;
        let root_info = self.root.text_info();
        let (l_info, residual) = Arc::make_mut(&mut self.root).edit_chunk_at_char(
            char_idx,
            root_info,
            |idx, cur_info, leaf_text| {
                // First check if we have a left seam.
                if idx == 0 && char_idx > 0 && ins_text.as_bytes()[0] == 0x0A {
                    left_seam = true;
                    ins_text = &ins_text[1..];
                    // Early out if it was only an LF.
                    if ins_text.is_empty() {
                        return (cur_info, None);
                    }
                }
                // Find our byte index
                let byte_idx = char_to_byte_idx(leaf_text, idx);
                // No node splitting
                if (leaf_text.len() + ins_text.len()) <= MAX_BYTES {
                    // Calculate new info without doing a full re-scan of cur_text
                    let new_info = {
                        // Get summed info of current text and to-be-inserted text
                        let mut info = cur_info + TextInfo::from_str(ins_text);
                        // Check for CRLF pairs on the insertion seams, and
                        // adjust line break counts accordingly
                        if byte_idx > 0 {
                            if leaf_text.as_bytes()[byte_idx - 1] == 0x0D
                                && ins_text.as_bytes()[0] == 0x0A
                            {
                                info.line_breaks -= 1;
                            }
                            if byte_idx < leaf_text.len()
                                && leaf_text.as_bytes()[byte_idx - 1] == 0x0D
                                && leaf_text.as_bytes()[byte_idx] == 0x0A
                            {
                                info.line_breaks += 1;
                            }
                        }
                        if byte_idx < leaf_text.len()
                            && *ins_text.as_bytes().last().unwrap() == 0x0D
                            && leaf_text.as_bytes()[byte_idx] == 0x0A
                        {
                            info.line_breaks -= 1;
                        }
                        info
                    };
                    // Insert the text and return the new info
                    leaf_text.insert_str(byte_idx, ins_text);
                    (new_info, None)
                }
                // We're splitting the node
                else {
                    let r_text = leaf_text.insert_str_split(byte_idx, ins_text);
                    let l_text_info = TextInfo::from_str(&leaf_text);
                    if r_text.len() > 0 {
                        let r_text_info = TextInfo::from_str(&r_text);
                        (
                            l_text_info,
                            Some((r_text_info, Arc::new(Node::Leaf(r_text)))),
                        )
                    } else {
                        // Leaf couldn't be validly split, so leave it oversized
                        (l_text_info, None)
                    }
                }
            },
        );
        // Handle root splitting, if any.
        if let Some((r_info, r_node)) = residual {
            let mut l_node = Arc::new(Node::new());
            std::mem::swap(&mut l_node, &mut self.root);
            let mut children = NodeChildren::new();
            children.push((l_info, l_node));
            children.push((r_info, r_node));
            *Arc::make_mut(&mut self.root) = Node::Internal(children);
        }
        // Insert the LF to the left.
        // TODO: this code feels fairly redundant with above.  Can we DRY this
        // better?
        if left_seam {
            // Do the insertion
            let root_info = self.root.text_info();
            let (l_info, residual) = Arc::make_mut(&mut self.root).edit_chunk_at_char(
                char_idx - 1,
                root_info,
                |_, cur_info, leaf_text| {
                    let byte_idx = leaf_text.len();
                    // No node splitting
                    if (leaf_text.len() + ins_text.len()) <= MAX_BYTES {
                        // Calculate new info without doing a full re-scan of cur_text
                        let mut new_info = cur_info;
                        new_info.bytes += 1;
                        new_info.chars += 1;
                        if *leaf_text.as_bytes().last().unwrap() != 0x0D {
                            new_info.line_breaks += 1;
                        }
                        // Insert the text and return the new info
                        leaf_text.insert_str(byte_idx, "\n");
                        (new_info, None)
                    }
                    // We're splitting the node
                    else {
                        let r_text = leaf_text.insert_str_split(byte_idx, "\n");
                        let l_text_info = TextInfo::from_str(&leaf_text);
                        if r_text.len() > 0 {
                            let r_text_info = TextInfo::from_str(&r_text);
                            (
                                l_text_info,
                                Some((r_text_info, Arc::new(Node::Leaf(r_text)))),
                            )
                        } else {
                            // Leaf couldn't be validly split, so leave it oversized
                            (l_text_info, None)
                        }
                    }
                },
            );
            // Handle root splitting, if any.
            if let Some((r_info, r_node)) = residual {
                let mut l_node = Arc::new(Node::new());
                std::mem::swap(&mut l_node, &mut self.root);
                let mut children = NodeChildren::new();
                children.push((l_info, l_node));
                children.push((r_info, r_node));
                *Arc::make_mut(&mut self.root) = Node::Internal(children);
            }
        }
    }
fn insert_internal ( & mut self , char_idx : usize , ins_text : & str ) { let mut ins_text = ins_text ; let mut left_seam = false ; let root_info = self . root . text_info ( ) ; let ( l_info , residual ) = Arc :: make_mut ( & mut self . root ) . edit_chunk_at_char ( char_idx , root_info , | idx , cur_info , leaf_text | { // First check if we have a left seam. if idx == 0 && char_idx > 0 && ins_text . as_bytes ( ) [ 0 ] == 0x0A { left_seam = true ; ins_text = & ins_text [ 1 .. ] ; // Early out if it was only an LF. if ins_text . is_empty ( ) { return ( cur_info , None ) ; } } // Find our byte index let byte_idx = char_to_byte_idx ( leaf_text , idx ) ; // No node splitting if ( leaf_text . len ( ) + ins_text . len ( ) ) <= MAX_BYTES { // Calculate new info without doing a full re-scan of cur_text let new_info = { // Get summed info of current text and to-be-inserted text let mut info = cur_info + TextInfo :: from_str ( ins_text ) ; // Check for CRLF pairs on the insertion seams, and // adjust line break counts accordingly if byte_idx > 0 { if leaf_text . as_bytes ( ) [ byte_idx - 1 ] == 0x0D && ins_text . as_bytes ( ) [ 0 ] == 0x0A { info . line_breaks -= 1 ; } if byte_idx < leaf_text . len ( ) && leaf_text . as_bytes ( ) [ byte_idx - 1 ] == 0x0D && leaf_text . as_bytes ( ) [ byte_idx ] == 0x0A { info . line_breaks += 1 ; } } if byte_idx < leaf_text . len ( ) && * ins_text . as_bytes ( ) . last ( ) . unwrap ( ) == 0x0D && leaf_text . as_bytes ( ) [ byte_idx ] == 0x0A { info . line_breaks -= 1 ; } info } ; // Insert the text and return the new info leaf_text . insert_str ( byte_idx , ins_text ) ; ( new_info , None ) } // We're splitting the node else { let r_text = leaf_text . insert_str_split ( byte_idx , ins_text ) ; let l_text_info = TextInfo :: from_str ( & leaf_text ) ; if r_text . len ( ) > 0 { let r_text_info = TextInfo :: from_str ( & r_text ) ; ( l_text_info , Some ( ( r_text_info , Arc :: new ( Node :: Leaf ( r_text ) ) ) ) , ) } else { // Leaf couldn't be validly split, so leave it oversized ( l_text_info , None ) } } } , ) ; // Handle root splitting, if any. if let Some ( ( r_info , r_node ) ) = residual { let mut l_node = Arc :: new ( Node :: new ( ) ) ; std :: mem :: swap ( & mut l_node , & mut self . root ) ; let mut children = NodeChildren :: new ( ) ; children . push ( ( l_info , l_node ) ) ; children . push ( ( r_info , r_node ) ) ; * Arc :: make_mut ( & mut self . root ) = Node :: Internal ( children ) ; } // Insert the LF to the left. // TODO: this code feels fairly redundant with above.  Can we DRY this // better? if left_seam { // Do the insertion let root_info = self . root . text_info ( ) ; let ( l_info , residual ) = Arc :: make_mut ( & mut self . root ) . edit_chunk_at_char ( char_idx - 1 , root_info , | _ , cur_info , leaf_text | { let byte_idx = leaf_text . len ( ) ; // No node splitting if ( leaf_text . len ( ) + ins_text . len ( ) ) <= MAX_BYTES { // Calculate new info without doing a full re-scan of cur_text let mut new_info = cur_info ; new_info . bytes += 1 ; new_info . chars += 1 ; if * leaf_text . as_bytes ( ) . last ( ) . unwrap ( ) != 0x0D { new_info . line_breaks += 1 ; } // Insert the text and return the new info leaf_text . insert_str ( byte_idx , " \n " ) ; ( new_info , None ) } // We're splitting the node else { let r_text = leaf_text . insert_str_split ( byte_idx , " \n " ) ; let l_text_info = TextInfo :: from_str ( & leaf_text ) ; if r_text . len ( ) > 0 { let r_text_info = TextInfo :: from_str ( & r_text ) ; ( l_text_info , Some ( ( r_text_info , Arc :: new ( Node :: Leaf ( r_text ) ) ) ) , ) } else { // Leaf couldn't be validly split, so leave it oversized ( l_text_info , None ) } } } , ) ; // Handle root splitting, if any. if let Some ( ( r_info , r_node ) ) = residual { let mut l_node = Arc :: new ( Node :: new ( ) ) ; std :: mem :: swap ( & mut l_node , & mut self . root ) ; let mut children = NodeChildren :: new ( ) ; children . push ( ( l_info , l_node ) ) ; children . push ( ( r_info , r_node ) ) ; * Arc :: make_mut ( & mut self . root ) = Node :: Internal ( children ) ; } } }
Private internal-only method that does a single insertion of
sufficiently small text.
Private internal - only method that does a single insertion of sufficiently small text .
&mut self
char_idx
usize
ins_text
&str