fabricatio_diff.rust

Functions

apply_insert_after(→ str)

Applies an insert_after edit to content based on a hashline anchor.

apply_replace(→ str)

Applies a replace edit (text substitution) to content.

apply_replace_lines(→ str)

Applies a replace_lines edit to content between two anchors.

apply_set_line(→ str)

Applies a set_line edit to content based on a hashline anchor.

compute_hash(→ str)

Computes a hash for a single line using xxHash-based hashing.

format_hashes(→ str)

Formats content with LINE:HASH anchors for each line.

match_lines(→ Optional[str])

Searches for a sequence of lines in haystack that approximately matches needle.

parse_hashline_anchor(→ tuple[int, str])

Parses a hashline anchor in the format "LINE:HASH".

rate(→ float)

Calculates the similarity rate between two strings using the normalized Damerau-Levenshtein distance.

show_diff(→ str)

Generates a unified diff between two strings showing line-level changes.

Package Contents

fabricatio_diff.rust.apply_insert_after(content: str, anchor: str, text: str) str

Applies an insert_after edit to content based on a hashline anchor.

Parameters:
  • content – The original content to modify.

  • anchor – The anchor string in “LINE:HASH” format.

  • text – The text to insert after the anchored line.

Returns:

The modified content after applying the edit.

fabricatio_diff.rust.apply_replace(content: str, old_text: str, new_text: str, all: bool) str

Applies a replace edit (text substitution) to content.

Parameters:
  • content – The original content to modify.

  • old_text – The text to search for.

  • new_text – The replacement text.

  • all – Whether to replace all occurrences (default: false).

Returns:

The modified content after applying the edit.

fabricatio_diff.rust.apply_replace_lines(content: str, start_anchor: str, end_anchor: str, new_text: str) str

Applies a replace_lines edit to content between two anchors.

Parameters:
  • content – The original content to modify.

  • start_anchor – The start anchor in “LINE:HASH” format.

  • end_anchor – The end anchor in “LINE:HASH” format.

  • new_text – The replacement text for the lines range.

Returns:

The modified content after applying the edit.

fabricatio_diff.rust.apply_set_line(content: str, anchor: str, new_text: str) str

Applies a set_line edit to content based on a hashline anchor.

Parameters:
  • content – The original content to modify.

  • anchor – The anchor string in “LINE:HASH” format.

  • new_text – The replacement text.

Returns:

The modified content after applying the edit.

fabricatio_diff.rust.compute_hash(line: str) str

Computes a hash for a single line using xxHash-based hashing.

Parameters:

line – The line content to hash.

Returns:

A hex string representing the line’s hash.

fabricatio_diff.rust.format_hashes(content: str, start_line: int = 1) str

Formats content with LINE:HASH anchors for each line.

Parameters:
  • content – The text content to format.

  • start_line – The starting line number (default: 1).

Returns:

A string where each line is prefixed with its line number and hash.

fabricatio_diff.rust.match_lines(haystack: str, needle: str, match_precision: float = 0.9) str | None

Searches for a sequence of lines in haystack that approximately matches needle.

This function uses the normalized Damerau-Levenshtein distance to find a matching block of lines with similarity score equal to or greater than match_precision.

Parameters:
  • haystack – The full text to search within.

  • needle – The text pattern to find within the haystack.

  • match_precision – Threshold for similarity score between 0.0 and 1.0 (default: 0.9).

Returns:

An Option containing the first matching block of lines if found, or None if no match meets the precision threshold.

fabricatio_diff.rust.parse_hashline_anchor(anchor: str) tuple[int, str]

Parses a hashline anchor in the format “LINE:HASH”.

Parameters:

anchor – The anchor string to parse (e.g., “42:ab123def”).

Returns:

A tuple of (line_number, hash_string) if valid.

fabricatio_diff.rust.rate(a: str, b: str) float

Calculates the similarity rate between two strings using the normalized Damerau-Levenshtein distance.

This function returns a float value between 0.0 and 1.0, where: - 1.0 means the strings are identical - 0.0 means the strings are completely different

Parameters:
  • a – The first string to compare.

  • b – The second string to compare.

Returns:

A f64 value representing the similarity rate between the two input strings.

fabricatio_diff.rust.show_diff(a: str, b: str) str

Generates a unified diff between two strings showing line-level changes.

The diff output follows unified diff format conventions where: - Lines prefixed with - indicate deletions from a - Lines prefixed with + indicate additions from b - Unchanged lines are prefixed with a space

Parameters:
  • a – The original/old text content.

  • b – The modified/new text content.

Returns:

A String containing the diff output with each line prefixed by its change type.