Skip to content

-i matches multi-character sequences via Unicode case folding where GNU matches one #32

Closed

Description

@sylvestre

Under -i, uu_grep lets a single-character pattern match a multi-character sequence when that sequence is a Unicode case-folding of one character — e.g. st (folds from the ligatures ſt/st), ss (from ß), or ff/fi/ffi (from ff/fi/ffi). GNU under LC_ALL=C folds case 1:1 and matches a single character. The input here is plain ASCII, so this is not the locale/encoding limitation — the extra matching comes from the case folder, not from byte-vs-codepoint handling.

Found by the differential fuzzer (fuzz_grep).

Rust (incorrect)

$ printf 'st\n' | ./target/release/grep -o -i '[[:alpha:]]'
st
# one match spanning two characters

GNU (correct)

$ printf 'st\n' | LC_ALL=C /usr/bin/grep -o -i '[[:alpha:]]'
s
t
# two separate single-character matches

More cases (Rust → GNU): ss → ss vs s/s; ff → ff vs f/f; ffi → ffi vs f/f/i. It is order-sensitive (st merges but ts does not) and changes match counts, not just -o output.

Activity

  1. lhecker commented on Jun 4, 2026

    @lhecker
    Collaborator

    This is a shortcoming of us not handling LC_* at all just yet. It always operates as if it was en_US.UTF-8 and I believe in that case grep folds it as per Unicode, right?

  2. wondr-wclabs commented on Jun 5, 2026

    @wondr-wclabs
    Contributor

    Good point that LC_* is the larger missing seam, but I do not think GNU grep's UTF-8 behavior is full Unicode case folding in the sense Oniguruma is applying here.

    GNU's own manual calls out this exact class of case: ß normally capitalizes to the two-character string SS, but under grep -i it does not match SS (and even matching capital ẞ is documented as locale/implementation-sensitive): https://www.gnu.org/software/grep/manual/html_node/Matching-Control.html

    That makes the issue a little different from just “we behave as en_US.UTF-8 until locale support exists.” The current behavior lets one regex atom match a multi-character input sequence ([[:alpha:]] matching st, ss, ffi, etc.), which changes -o output and counts. GNU appears to avoid those multi-character fold expansions even outside the C-locale discussion.

    So I think the eventual fix should probably be: keep broader LC_* support as the long-term locale seam, but make the current ignore-case matcher avoid multi-codepoint/full-fold expansions for GNU compatibility. I have not opened a PR yet because I want to check whether Oniguruma exposes a clean option/syntax flag for “simple folds only”; if it does not, a hand-rolled workaround around character classes and -o boundaries could get brittle quickly.

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

    good first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions