Skip to content

Inserting an unmatched letter between two pattern letters raises the score: "sc" ranks src_config (12) above sc_config (8), and "abc" scores aXb_bc 22 vs ab_bc 13 #144

Open

Description

@matt-edmondson

What's wrong

Fuzzy.Contains(subject, pattern, out int score) at 862888d:

pattern subject score subject with one extra letter score
sc sc_config 8 src_config 12
abc ab_bc 13 aXb_bc 22
db db_backup 8 dumb_backup 11
ui ui_input 9 uxi_input 13
ab abaB 13 axbaB 17

Under the README's scoring table an unmatched letter costs at least 1, so the left column should never score below the right one. The test suite states the same rule: Score_InsertingJunkBeforeABoundaryCopy_DoesNotRaiseTheScore asserts "Inserting a junk character should not raise the score", but checks only one doubled-letter case (baaA).

Cause

This is the #91/#109 guard at FuzzySearch/Fuzzy.cs:282-284:

bool displacesRun = rematch && !nextMatch && bestLetterContinuesRun;
...
if (newScore >= bestLetterScore && !displacesRun)

When the current best letter continues a run with the previous match, a later word-start copy of it (after a separator, or at a camelCase capital) can never replace it. Put one unmatched letter between the two, and the run is broken. The guard no longer applies, so the word-start copy takes the match with its full separator/camelCase bonus. The extra letter ends up worth +4 to +9 rather than costing 1.

Evidence the guard is the cause, from an exhaustive check over alphabet abcABC_x1, subjects of length 1-6 and 11 patterns (no adjacent duplicate letters, so #101 is not involved):

Why it matters

The subject that holds the pattern letters contiguously, typically the one the user meant, ranks below one that is strictly worse. For example, typing sc lists src_config above sc_config, and typing db lists dumb_backup above db_backup.

This is not #122: its proposed fix, capping the bonus when the gap is only separators, leaves this case alone, because a gap such as Xb_ is not only separators. It is not #101 either, since no letter is doubled. #117, #131 and #142 were the guard misfiring. Here it fires exactly as designed, but the design isn't monotone.

Suggested fix / acceptance criteria

Activity

  1. added
    bug
    on Nov 12, 2026
  2. matt-edmondson commented on Nov 12, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Suggested next step: land the monotonicity property test first, seeded with this table, so that whichever of the two fix routes is chosen is checked against #91 and the sibling issues at once.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions