Combine two non-conflicting edit scripts and produce merged content bytes.
(base: &[MergeToken], left_ops: &[EditOp], right_ops: &[EditOp])
| 317 | |
| 318 | /// Combine two non-conflicting edit scripts and produce merged content bytes. |
| 319 | fn merge_ops(base: &[MergeToken], left_ops: &[EditOp], right_ops: &[EditOp]) -> Vec<u8> { |
| 320 | // Build per-index action maps. |
| 321 | let left_action = build_action_map(left_ops); |
| 322 | let right_action = build_action_map(right_ops); |
| 323 | |
| 324 | let left_inserts = insert_positions(left_ops); |
| 325 | let right_inserts = insert_positions(right_ops); |
| 326 | |
| 327 | let mut result: Vec<u8> = Vec::new(); |
| 328 | |
| 329 | // Handle insertions before the first token. |
| 330 | emit_inserts(&left_inserts, usize::MAX, &mut result); |
| 331 | emit_inserts(&right_inserts, usize::MAX, &mut result); |
| 332 | |
| 333 | for (i, base_token) in base.iter().enumerate() { |
| 334 | // Determine what to emit for base token i. |
| 335 | let left_act = left_action.get(&i); |
| 336 | let right_act = right_action.get(&i); |
| 337 | |
| 338 | match (left_act, right_act) { |
| 339 | // Both sides kept (or neither touched) — emit base. |
| 340 | (None, None) => { |
| 341 | result.extend_from_slice(&base_token.content); |
| 342 | } |
| 343 | // Only left modified. |
| 344 | (Some(action), None) => { |
| 345 | emit_action(action, base_token, &mut result); |
| 346 | } |
| 347 | // Only right modified. |
| 348 | (None, Some(action)) => { |
| 349 | emit_action(action, base_token, &mut result); |
| 350 | } |
| 351 | // Both modified identically (already verified no conflict). |
| 352 | (Some(action), Some(_)) => { |
| 353 | emit_action(action, base_token, &mut result); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // Emit any insertions that follow base token i. |
| 358 | emit_inserts(&left_inserts, i, &mut result); |
| 359 | emit_inserts(&right_inserts, i, &mut result); |
| 360 | } |
| 361 | |
| 362 | result |
| 363 | } |
| 364 | |
| 365 | /// Action on a single base token: either replace or delete. |
| 366 | #[derive(Debug, Clone)] |
no test coverage detected