This commit is contained in:
Andras Schmelczer 2025-11-16 15:40:28 +00:00
parent 9c23229627
commit 5fbeb79fdc
4 changed files with 27 additions and 6 deletions

View file

@ -8,6 +8,6 @@
"declaration": true,
"declarationDir": "./dist/types",
"skipLibCheck": true,
"inlineSourceMap": true,
},
"inlineSourceMap": true
}
}

View file

@ -107,6 +107,11 @@ where
/// from the same original text. The operations are merged using the
/// principles of Operational Transformation. The cursors are updated
/// accordingly to reflect the changes made by the merged operations.
///
/// # Panics
///
/// Panics if there's an integer overflow (in i64) when calculating new
/// cursor positions.
#[must_use]
#[allow(clippy::too_many_lines)]
pub fn merge(self, other: Self) -> Self {
@ -356,6 +361,10 @@ where
///
/// Inserts are represented as strings, deletes as negative integers,
/// and equal spans as positive integers.
///
/// # Panics
///
/// Panics if there's an integer overflow in i64.
#[must_use]
pub fn to_diff(&self) -> Vec<NumberOrString> {
let mut result: Vec<NumberOrString> = Vec::with_capacity(self.operations.len());
@ -414,6 +423,10 @@ where
}
/// Deserialize an `EditedText` from a change list and the original text.
///
/// # Panics
///
/// Panics if there's an integer overflow in i64.
#[must_use]
pub fn from_diff(
original_text: &'a str,

View file

@ -144,7 +144,8 @@ where
// By Lemma 1 in the paper, the optimal edit script length is odd or even as
// `delta` is odd or even.
let delta = isize::try_from(n).expect("n must fit in isize") - isize::try_from(m).expect("m must fit in isize");
let delta = isize::try_from(n).expect("n must fit in isize")
- isize::try_from(m).expect("m must fit in isize");
let odd = delta & 1 == 1;
// The initial point at (0, -1)
@ -165,7 +166,8 @@ where
} else {
vf[k - 1] + 1
};
let y = usize::try_from(isize::try_from(x).expect("x must fit in isize") - k).expect("x - k must be non-negative and fit in usize");
let y = usize::try_from(isize::try_from(x).expect("x must fit in isize") - k)
.expect("x - k must be non-negative and fit in usize");
// The coordinate of the start of a snake
let (x0, y0) = (x, y);
@ -203,7 +205,8 @@ where
} else {
vb[k - 1] + 1
};
let mut y = usize::try_from(isize::try_from(x).expect("x must fit in isize") - k).expect("x - k must be non-negative and fit in usize");
let mut y = usize::try_from(isize::try_from(x).expect("x must fit in isize") - k)
.expect("x - k must be non-negative and fit in usize");
// The coordinate of the start of a snake
if x < n && y < m {

View file

@ -95,6 +95,11 @@ pub fn diff(parent: &str, changed: &TextWithCursors, tokenizer: BuiltinTokenizer
}
/// Inverse of `diff`, applies a compact diff representation to a parent text
///
/// # Panics
///
/// Panics if the diff format is invalid or there's an integer overflow when
/// applying the diff.
#[wasm_bindgen(js_name = undiff)]
#[must_use]
pub fn undiff(parent: &str, diff: Vec<JsValue>, tokenizer: BuiltinTokenizer) -> String {
@ -103,7 +108,7 @@ pub fn undiff(parent: &str, diff: Vec<JsValue>, tokenizer: BuiltinTokenizer) ->
EditedText::from_diff(
parent,
diff.into_iter()
.map(|js_value| js_value.try_into())
.map(std::convert::TryInto::try_into)
.collect::<Result<_, _>>()
.expect("Invalid diff format"),
&*tokenizer,