Implicitly convert &str to [u8] for compatibility with existing usage code
Thanks to stephaneyfx from the #rust IRC channel for showing me that this needs separate type parameters on the functions in order to compile. The only API change that can't be helped are the `line_junk` and `char_junk` function pointers in the Differ struct, which now take `&&str` and `&char` parameters, respectively. This is probably for the better, since `char_junk` now operates on Unicode scalar values rather than the single UTF-8 bytes disguised as `&str` that were previously retrieved via the unsafe `slice_unchecked()` function.
This commit is contained in:
@@ -65,7 +65,7 @@ fn main() {
|
||||
}
|
||||
|
||||
//SequenceMatcher examples
|
||||
let mut matcher = SequenceMatcher::new(b"one two three four", b"zero one tree four");
|
||||
let mut matcher = SequenceMatcher::new("one two three four", "zero one tree four");
|
||||
let m = matcher.find_longest_match(0, 18, 0, 18);
|
||||
println!("{:?}", m);
|
||||
let all_matches = matcher.get_matching_blocks();
|
||||
@@ -76,7 +76,7 @@ fn main() {
|
||||
println!("{:?}", grouped_opcodes);
|
||||
let ratio = matcher.ratio();
|
||||
println!("{:?}", ratio);
|
||||
matcher.set_seqs(b"aaaaa", b"aaaab");
|
||||
matcher.set_seqs("aaaaa", "aaaab");
|
||||
println!("{:?}", matcher.ratio());
|
||||
}
|
||||
```
|
||||
|
||||
+2
-2
@@ -47,7 +47,7 @@ fn main() {
|
||||
}
|
||||
|
||||
//SequenceMatcher examples
|
||||
let mut matcher = SequenceMatcher::new(b"one two three four", b"zero one tree four");
|
||||
let mut matcher = SequenceMatcher::new("one two three four", "zero one tree four");
|
||||
let m = matcher.find_longest_match(0, 18, 0, 18);
|
||||
println!("{:?}", m);
|
||||
let all_matches = matcher.get_matching_blocks();
|
||||
@@ -58,6 +58,6 @@ fn main() {
|
||||
println!("{:?}", grouped_opcodes);
|
||||
let ratio = matcher.ratio();
|
||||
println!("{:?}", ratio);
|
||||
matcher.set_seqs(b"aaaaa", b"aaaab");
|
||||
matcher.set_seqs("aaaaa", "aaaab");
|
||||
println!("{:?}", matcher.ratio());
|
||||
}
|
||||
|
||||
+2
-2
@@ -17,9 +17,9 @@ pub fn get_close_matches<'a>(
|
||||
panic!("Cutoff must be greater than 0.0 and lower than 1.0");
|
||||
}
|
||||
let mut res: Vec<(f32, &str)> = Vec::new();
|
||||
let mut matcher = SequenceMatcher::new(b"", word.as_bytes());
|
||||
let mut matcher = SequenceMatcher::new("", word);
|
||||
for i in &possibilities {
|
||||
matcher.set_first_seq(i.as_bytes());
|
||||
matcher.set_first_seq(i);
|
||||
let ratio = matcher.ratio();
|
||||
if ratio >= cutoff {
|
||||
res.push((ratio, i));
|
||||
|
||||
+23
-9
@@ -60,10 +60,13 @@ pub struct SequenceMatcher<'a, T: 'a + Sequence> {
|
||||
}
|
||||
|
||||
impl<'a, T: Sequence> SequenceMatcher<'a, T> {
|
||||
pub fn new(first_sequence: &'a [T], second_sequence: &'a [T]) -> SequenceMatcher<'a, T> {
|
||||
pub fn new<S>(first_sequence: &'a S, second_sequence: &'a S) -> SequenceMatcher<'a, T>
|
||||
where
|
||||
S: AsRef<[T]> + ?Sized,
|
||||
{
|
||||
let mut matcher = SequenceMatcher {
|
||||
first_sequence,
|
||||
second_sequence,
|
||||
first_sequence: first_sequence.as_ref(),
|
||||
second_sequence: second_sequence.as_ref(),
|
||||
matching_blocks: None,
|
||||
opcodes: None,
|
||||
is_junk: None,
|
||||
@@ -75,22 +78,33 @@ impl<'a, T: Sequence> SequenceMatcher<'a, T> {
|
||||
|
||||
pub fn set_is_junk(&mut self, is_junk: Option<fn(&T) -> bool>) {
|
||||
self.is_junk = is_junk;
|
||||
self.set_second_seq(self.second_sequence);
|
||||
self.matching_blocks = None;
|
||||
self.opcodes = None;
|
||||
self.chain_second_seq();
|
||||
}
|
||||
|
||||
pub fn set_seqs(&mut self, first_sequence: &'a [T], second_sequence: &'a [T]) {
|
||||
pub fn set_seqs<S>(&mut self, first_sequence: &'a S, second_sequence: &'a S)
|
||||
where
|
||||
S: AsRef<[T]> + ?Sized,
|
||||
{
|
||||
self.set_first_seq(first_sequence);
|
||||
self.set_second_seq(second_sequence);
|
||||
}
|
||||
|
||||
pub fn set_first_seq(&mut self, sequence: &'a [T]) {
|
||||
self.first_sequence = sequence;
|
||||
pub fn set_first_seq<S>(&mut self, sequence: &'a S)
|
||||
where
|
||||
S: AsRef<[T]> + ?Sized,
|
||||
{
|
||||
self.first_sequence = sequence.as_ref();
|
||||
self.matching_blocks = None;
|
||||
self.opcodes = None;
|
||||
}
|
||||
|
||||
pub fn set_second_seq(&mut self, sequence: &'a [T]) {
|
||||
self.second_sequence = sequence;
|
||||
pub fn set_second_seq<S>(&mut self, sequence: &'a S)
|
||||
where
|
||||
S: AsRef<[T]> + ?Sized,
|
||||
{
|
||||
self.second_sequence = sequence.as_ref();
|
||||
self.matching_blocks = None;
|
||||
self.opcodes = None;
|
||||
self.chain_second_seq();
|
||||
|
||||
+4
-4
@@ -5,7 +5,7 @@ use difflib::sequencematcher::{Match, Opcode, SequenceMatcher};
|
||||
|
||||
#[test]
|
||||
fn test_longest_match() {
|
||||
let matcher = SequenceMatcher::new(b" abcd", b"abcd abcd");
|
||||
let matcher = SequenceMatcher::new(" abcd", "abcd abcd");
|
||||
let m = matcher.find_longest_match(0, 5, 0, 9);
|
||||
assert_eq!(m.first_start, 0);
|
||||
assert_eq!(m.second_start, 4);
|
||||
@@ -14,7 +14,7 @@ fn test_longest_match() {
|
||||
|
||||
#[test]
|
||||
fn test_all_matches() {
|
||||
let mut matcher = SequenceMatcher::new(b"abxcd", b"abcd");
|
||||
let mut matcher = SequenceMatcher::new("abxcd", "abcd");
|
||||
let result = matcher.get_matching_blocks();
|
||||
let mut expected_result = Vec::new();
|
||||
expected_result.push(Match {
|
||||
@@ -37,7 +37,7 @@ fn test_all_matches() {
|
||||
|
||||
#[test]
|
||||
fn test_get_opcodes() {
|
||||
let mut matcher = SequenceMatcher::new(b"qabxcd", b"abycdf");
|
||||
let mut matcher = SequenceMatcher::new("qabxcd", "abycdf");
|
||||
let result = matcher.get_opcodes();
|
||||
let mut expected_result = Vec::new();
|
||||
expected_result.push(Opcode {
|
||||
@@ -80,7 +80,7 @@ fn test_get_opcodes() {
|
||||
|
||||
#[test]
|
||||
fn test_ratio() {
|
||||
let mut matcher = SequenceMatcher::new(b"abcd", b"bcde");
|
||||
let mut matcher = SequenceMatcher::new("abcd", "bcde");
|
||||
assert_eq!(matcher.ratio(), 0.75);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user