Consistent ordering

This commit is contained in:
Andras Schmelczer 2025-03-16 18:19:36 +00:00
parent 7be1454460
commit 74c007be25
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
4 changed files with 15 additions and 38 deletions

View file

@ -160,7 +160,7 @@ mod test {
"hi ",
"hi there you ",
"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");

View file

@ -211,7 +211,16 @@ where
usize::from(matches!(operation.operation, Operation::Delete { .. })),
// Make sure that the ordering is deterministic regardless which text
// 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 left = "Hello world! I'm Andras.";
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_2 = EditedText::from_strings(original, right);

View file

@ -1,8 +1,5 @@
use core::fmt::{Debug, Display};
use std::{
hash::{DefaultHasher, Hash, Hasher},
ops::Range,
};
use std::ops::Range;
#[cfg(feature = "serde")]
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>
where
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>

View file

@ -25,9 +25,9 @@ fn test_base64_to_bytes_error() {
#[wasm_bindgen_test(unsupported = test)]
fn merge_text() {
let left = b"hello ";
let right = b"world ";
let right = b"world";
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)]