Skip to content

Commit d845aa4

Browse files
daladimJérôme Froissart
authored andcommitted
[minor] Renamed internal functions
This will be useful to disambiguiate when adding support for 3-way merging of arbitrary types
1 parent b36b981 commit d845aa4

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

src/diff/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ impl DiffOptions {
9696
/// Produce a Patch between two texts based on the configured options
9797
pub fn create_patch<'a>(&self, original: &'a str, modified: &'a str) -> Patch<'a, str> {
9898
let mut classifier = Classifier::default();
99-
let (old_lines, old_ids) = classifier.classify_lines(original);
100-
let (new_lines, new_ids) = classifier.classify_lines(modified);
99+
let (old_lines, old_ids) = classifier.classify_text(original);
100+
let (new_lines, new_ids) = classifier.classify_text(modified);
101101

102102
let solution = self.diff_slice(&old_ids, &new_ids);
103103

@@ -112,8 +112,8 @@ impl DiffOptions {
112112
modified: &'a [u8],
113113
) -> Patch<'a, [u8]> {
114114
let mut classifier = Classifier::default();
115-
let (old_lines, old_ids) = classifier.classify_lines(original);
116-
let (new_lines, new_ids) = classifier.classify_lines(modified);
115+
let (old_lines, old_ids) = classifier.classify_text(original);
116+
let (new_lines, new_ids) = classifier.classify_text(modified);
117117

118118
let solution = self.diff_slice(&old_ids, &new_ids);
119119

src/merge/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ impl MergeOptions {
152152
theirs: &'a str,
153153
) -> Result<String, String> {
154154
let mut classifier = Classifier::default();
155-
let (ancestor_lines, ancestor_ids) = classifier.classify_lines(ancestor);
156-
let (our_lines, our_ids) = classifier.classify_lines(ours);
157-
let (their_lines, their_ids) = classifier.classify_lines(theirs);
155+
let (ancestor_lines, ancestor_ids) = classifier.classify_text(ancestor);
156+
let (our_lines, our_ids) = classifier.classify_text(ours);
157+
let (their_lines, their_ids) = classifier.classify_text(theirs);
158158

159159
let opts = DiffOptions::default();
160160
let our_solution = opts.diff_slice(&ancestor_ids, &our_ids);
@@ -183,9 +183,9 @@ impl MergeOptions {
183183
theirs: &'a [u8],
184184
) -> Result<Vec<u8>, Vec<u8>> {
185185
let mut classifier = Classifier::default();
186-
let (ancestor_lines, ancestor_ids) = classifier.classify_lines(ancestor);
187-
let (our_lines, our_ids) = classifier.classify_lines(ours);
188-
let (their_lines, their_ids) = classifier.classify_lines(theirs);
186+
let (ancestor_lines, ancestor_ids) = classifier.classify_text(ancestor);
187+
let (our_lines, our_ids) = classifier.classify_text(ours);
188+
let (their_lines, their_ids) = classifier.classify_text(theirs);
189189

190190
let opts = DiffOptions::default();
191191
let our_solution = opts.diff_slice(&ancestor_ids, &our_ids);

src/utils.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Classifier<'a, T: ?Sized> {
1212
}
1313

1414
impl<'a, T: ?Sized + Eq + Hash> Classifier<'a, T> {
15-
fn classify(&mut self, record: &'a T) -> u64 {
15+
fn classify_item(&mut self, record: &'a T) -> u64 {
1616
match self.unique_ids.entry(record) {
1717
Entry::Occupied(o) => *o.get(),
1818
Entry::Vacant(v) => {
@@ -25,9 +25,9 @@ impl<'a, T: ?Sized + Eq + Hash> Classifier<'a, T> {
2525
}
2626

2727
impl<'a, T: ?Sized + Text> Classifier<'a, T> {
28-
pub fn classify_lines(&mut self, text: &'a T) -> (Vec<&'a T>, Vec<u64>) {
28+
pub fn classify_text(&mut self, text: &'a T) -> (Vec<&'a T>, Vec<u64>) {
2929
LineIter::new(text)
30-
.map(|line| (line, self.classify(line)))
30+
.map(|line| (line, self.classify_item(line)))
3131
.unzip()
3232
}
3333
}
@@ -227,3 +227,16 @@ fn find_bytes(haystack: &[u8], needle: &[u8]) -> Option<usize> {
227227
fn find_byte(haystack: &[u8], byte: u8) -> Option<usize> {
228228
haystack.iter().position(|&b| b == byte)
229229
}
230+
231+
#[cfg(test)]
232+
mod test {
233+
use super::Classifier;
234+
235+
#[test]
236+
fn classify_string() {
237+
let input = "abc\ndef";
238+
let mut classifier = Classifier::default();
239+
let (lines, _ids) = classifier.classify_text(input);
240+
assert_eq!(lines, vec!["abc\n", "def"]);
241+
}
242+
}

0 commit comments

Comments
 (0)