Skip to main content

Module matching

Module matching 

Source
Expand description

The one definition of “a match”.

Everything that asks “does this text contain that query” goes through here: the in-document find, find-and-replace, and (through the public API) a host app’s own project-wide search. Two implementations would drift, and the way a writer meets that drift is “the editor found it but the search panel didn’t”.

§The rule this module exists to enforce

An offset computed in folded text is not valid in the original text. Folding can change a string’s length'İ'.to_lowercase() is two chars, ß folds to ss, é folds to e — so a position found in a folded haystack lands somewhere else entirely when applied to the source.

That was not hypothetical. The literal search path used to lowercase the haystack, compute char offsets in the lowercased copy, and hand them back to a caller that applied them to the original. On "İİİ ipsum dolor", find_all("ipsum") reported position 7 instead of 4, and replace_text — running with the default, case-insensitive options — turned the prose into "İİİ ipsLOREMlor". It did not merely highlight the wrong word; it destroyed the writer’s text, and it would do so in any manuscript containing a Turkish capital İ.

So folding here always produces an index map alongside the folded string, and matches are always reported in ORIGINAL char offsets.

§And a match must not begin or end inside a letter

Because one source char can fold to several (Folded), a substring of the folded text is not necessarily a substring of anything the writer typed. Straße folds to strasse, so a naive scan finds s in the middle of the ß. There is no source range to report for half a letter, and replacing it would produce mojibake. Every match is therefore checked against the index map and rejected unless it begins and ends on a source-char boundary — see Folded::to_source_match.

Re-exports§

pub use crate::folding::FoldLocale;
pub use crate::folding::FoldSpec;

Structs§

FoldedText
A haystack folded once, ready to be searched many times.
Match
One occurrence, in char offsets into the original haystack — never into the folded copy.
MatchOptions
How to match.

Functions§

find_all
Every occurrence of needle in haystack, in original char offsets.
preserve_case
Dress replacement in the case of the text it is replacing.