Consistent ordering
This commit is contained in:
parent
7be1454460
commit
74c007be25
4 changed files with 15 additions and 38 deletions
|
|
@ -160,7 +160,7 @@ mod test {
|
||||||
"hi ",
|
"hi ",
|
||||||
"hi there you ",
|
"hi there you ",
|
||||||
"hi there my friend ",
|
"hi there my friend ",
|
||||||
"hi there you my friend ",
|
"hi there my friend you ",
|
||||||
);
|
);
|
||||||
|
|
||||||
test_merge_both_ways("a", "a b c", "a b c d", "a b c d");
|
test_merge_both_ways("a", "a b c", "a b c d", "a b c d");
|
||||||
|
|
|
||||||
|
|
@ -211,7 +211,16 @@ where
|
||||||
usize::from(matches!(operation.operation, Operation::Delete { .. })),
|
usize::from(matches!(operation.operation, Operation::Delete { .. })),
|
||||||
// Make sure that the ordering is deterministic regardless which text
|
// Make sure that the ordering is deterministic regardless which text
|
||||||
// is left or right.
|
// is left or right.
|
||||||
operation.operation.get_hash(),
|
match &operation.operation {
|
||||||
|
Operation::Insert { text, .. } => text
|
||||||
|
.iter()
|
||||||
|
.map(super::super::tokenizer::token::Token::original)
|
||||||
|
.collect::<String>(),
|
||||||
|
Operation::Delete {
|
||||||
|
deleted_character_count,
|
||||||
|
..
|
||||||
|
} => deleted_character_count.to_string(),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
@ -285,7 +294,7 @@ mod tests {
|
||||||
let original = "hello world! ...";
|
let original = "hello world! ...";
|
||||||
let left = "Hello world! I'm Andras.";
|
let left = "Hello world! I'm Andras.";
|
||||||
let right = "Hello world! How are you?";
|
let right = "Hello world! How are you?";
|
||||||
let expected = "Hello world! I'm Andras. How are you?";
|
let expected = "Hello world! How are you? I'm Andras.";
|
||||||
|
|
||||||
let operations_1 = EditedText::from_strings(original, left);
|
let operations_1 = EditedText::from_strings(original, left);
|
||||||
let operations_2 = EditedText::from_strings(original, right);
|
let operations_2 = EditedText::from_strings(original, right);
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
use core::fmt::{Debug, Display};
|
use core::fmt::{Debug, Display};
|
||||||
use std::{
|
use std::ops::Range;
|
||||||
hash::{DefaultHasher, Hash, Hasher},
|
|
||||||
ops::Range,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
@ -39,28 +36,6 @@ where
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Hash for Operation<T>
|
|
||||||
where
|
|
||||||
T: PartialEq + Clone + std::fmt::Debug,
|
|
||||||
{
|
|
||||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
||||||
match self {
|
|
||||||
Operation::Insert { index, text } => {
|
|
||||||
index.hash(state);
|
|
||||||
text.iter().for_each(|token| token.original().hash(state));
|
|
||||||
}
|
|
||||||
Operation::Delete {
|
|
||||||
index,
|
|
||||||
deleted_character_count,
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
index.hash(state);
|
|
||||||
deleted_character_count.hash(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Operation<T>
|
impl<T> Operation<T>
|
||||||
where
|
where
|
||||||
T: PartialEq + Clone + std::fmt::Debug,
|
T: PartialEq + Clone + std::fmt::Debug,
|
||||||
|
|
@ -315,13 +290,6 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the hash of the operation based on the indexes and original text.
|
|
||||||
pub fn get_hash(&self) -> u64 {
|
|
||||||
let mut hasher = DefaultHasher::new();
|
|
||||||
self.hash(&mut hasher);
|
|
||||||
hasher.finish()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Display for Operation<T>
|
impl<T> Display for Operation<T>
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ fn merge_text() {
|
||||||
let left = b"hello ";
|
let left = b"hello ";
|
||||||
let right = b"world";
|
let right = b"world";
|
||||||
let result = merge(b"", left, right);
|
let result = merge(b"", left, right);
|
||||||
assert!(result == b"hello world ".to_vec() || result == b"world hello ".to_vec());
|
assert_eq!(result, b"hello world");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test(unsupported = test)]
|
#[wasm_bindgen_test(unsupported = test)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue