MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / slice_lines

Function slice_lines

atomic-core/src/record/workflow/globalize/pipeline.rs:617–643  ·  view source on GitHub ↗

Return the byte slice of `content` covering `len` lines starting at line `start` (0-indexed), where lines are delimited by `\n` (the newline is included with the line it terminates). Used to extract the replacement slice for a Replace hunk so that `globalize_replace` only creates per-line vertices for the lines it actually replaces.

(content: &[u8], start: usize, len: usize)

Source from the content-addressed store, hash-verified

615/// slice for a Replace hunk so that `globalize_replace` only creates
616/// per-line vertices for the lines it actually replaces.
617fn slice_lines(content: &[u8], start: usize, len: usize) -> &[u8] {
618 if len == 0 {
619 return &[];
620 }
621 let mut line = 0usize;
622 let mut byte_start: Option<usize> = None;
623 let mut byte_end = content.len();
624 if start == 0 {
625 byte_start = Some(0);
626 }
627 for (i, &b) in content.iter().enumerate() {
628 if b == b'\n' {
629 line += 1;
630 if byte_start.is_none() && line == start {
631 byte_start = Some(i + 1);
632 }
633 if byte_start.is_some() && line == start + len {
634 byte_end = i + 1;
635 break;
636 }
637 }
638 }
639 match byte_start {
640 Some(s) if s <= byte_end => &content[s..byte_end],
641 _ => &[],
642 }
643}
644
645/// Enrich FileOps with content ranges for a FileAdd operation.
646///

Callers 1

globalize_recorded_fileFunction · 0.85

Calls 3

is_noneMethod · 0.80
lenMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected