summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorNickolay Ponomarev <asqueella@gmail.com>2019-07-14 22:27:24 +0300
committerNickolay Ponomarev <asqueella@gmail.com>2019-07-14 22:30:50 +0300
commit4bdfd63d3ea86af5dfb979642ceb7f88e02d430c (patch)
treeb2fbff07e2ff74c8e6ed455819c9c010b5ab38e7 /src/lib.rs
parent6cf3e46f0e1ceb5aee7b8c228824a5b4eae9382b (diff)
Make it clearer why the previous fix was correct
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs27
1 files changed, 14 insertions, 13 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 3084b99..72b1b79 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -233,7 +233,7 @@ fn apply_hunk_to_tree<'repo>(
// first, write the lines from the old content that are above the
// hunk
let old_content = {
- let (pre, post) = old_content.split_at(skip_past_nth(b'\n', old_content, old_start));
+ let (pre, post) = split_lines_after(old_content, old_start);
blobwriter.write_all(pre)?;
post
};
@@ -243,7 +243,7 @@ fn apply_hunk_to_tree<'repo>(
}
// if this hunk removed lines from the old content, those must be
// skipped
- let old_content = &old_content[skip_past_nth(b'\n', old_content, hunk.removed.lines.len())..];
+ let (_, old_content) = split_lines_after(old_content, hunk.removed.lines.len());
// finally, write the remaining lines of the old content
blobwriter.write_all(old_content)?;
@@ -251,15 +251,16 @@ fn apply_hunk_to_tree<'repo>(
Ok(repo.find_tree(treebuilder.write()?)?)
}
-fn skip_past_nth(needle: u8, haystack: &[u8], n: usize) -> usize {
- if n == 0 {
- return 0;
- }
-
- // TODO: is fuse necessary here?
- memchr::Memchr::new(needle, haystack)
- .fuse()
- .nth(n - 1)
- .map(|x| x + 1)
- .unwrap_or_else(|| haystack.len())
+/// Return slices for lines [1..n] and [n+1; ...]
+fn split_lines_after(content: &[u8], n: usize) -> (&[u8], &[u8]) {
+ let split_index = if n > 0 {
+ memchr::Memchr::new(b'\n', content)
+ .fuse() // TODO: is fuse necessary here?
+ .nth(n - 1) // the position of '\n' ending the `n`-th line
+ .map(|x| x + 1)
+ .unwrap_or_else(|| content.len())
+ } else {
+ 0
+ };
+ content.split_at(split_index)
}