pub struct ByteSearch { /* private fields */ }Expand description
A deterministic search for the first differing byte within a differing line.
Supply both copies’ byte lengths, including any LF, to Self::new. An
absent line has length zero. Drive the search with Self::current_step and
Self::record_result; the caller keeps track of the selected line, and the
search itself performs no file I/O.
Counts and comparison results must describe the same unchanged pair of lines throughout the search. See the file stability requirements.
Implementations§
Source§impl ByteSearch
impl ByteSearch
Sourcepub fn new(local_byte_len: u64, other_byte_len: u64) -> Result<Self, Error>
pub fn new(local_byte_len: u64, other_byte_len: u64) -> Result<Self, Error>
Creates a byte search using both copies’ counts.
Construct only after the line search has established that the selected line differs and all preceding lines match. Supply each copy’s byte length for that line, including any CR/LF; use zero for an absent line. Both files must stay unchanged, and both instances must use accurate lengths and the same comparison answers.
Equal lengths bound the search at that length. Unequal lengths bound it at one past the shorter line: the first difference is either in a shared byte or at the first absent byte. Comparisons never exceed the shorter length. The constructor does not read either file or verify the mismatch.
§Errors
Returns Error::EmptyLinesCannotDiffer when both counts are zero.
§Examples
use paircomp_core::{ByteSearch, ByteSearchStep, Comparison};
// The differing lines are UTF-8 "café" and "cafè", each 5 bytes long.
let mut search = ByteSearch::new(5, 5)?;
assert_eq!(search.current_step(), ByteSearchStep::CompareThroughByte { byte: 3 });
search.record_result(Comparison::Match)?; // "caf" matches.
assert_eq!(search.current_step(), ByteSearchStep::CompareThroughByte { byte: 4 });
search.record_result(Comparison::Match)?; // The first byte of the final code point matches.
assert_eq!(search.current_step(), ByteSearchStep::DifferenceAtByte { byte: 5 });Sourcepub fn current_step(&self) -> ByteSearchStep
pub fn current_step(&self) -> ByteSearchStep
Returns the next prefix comparison or the completed byte result.
Repeated calls leave the state unchanged. Positions are 1-based; a final
result can refer to a byte absent from the shorter copy. When only one
candidate remains, returns ByteSearchStep::DifferenceAtByte without
requesting another comparison.
Sourcepub fn record_result(&mut self, comparison: Comparison) -> Result<(), Error>
pub fn record_result(&mut self, comparison: Comparison) -> Result<(), Error>
Applies the answer to the comparison returned by Self::current_step.
Pass Comparison::Match when the two fingerprints from
crate::fingerprint_line_prefix match, or Comparison::Differ when
they differ. A match excludes the prefix through the requested byte; a
mismatch retains that position as a candidate. The caller must supply
the answer for the current comparison on both copies.
§Errors
Returns Error::SearchAlreadyComplete if the result is already known.
An error leaves the search unchanged.