Make REG_LITERAL match the empty pattern - #146
Open
kevinushey wants to merge 2 commits into
Open
Conversation
With REG_LITERAL, compiling an empty pattern succeeds but the
resulting regex never matches anything:
tre_regcomp(&re, "", REG_EXTENDED | REG_LITERAL);
tre_regexec(&re, "abc", 1, m, 0); /* REG_NOMATCH; expected match */
The PARSE_ATOM empty-expression branch is skipped in literal mode, so
an empty pattern falls through without producing an atom. Emit an
EMPTY literal node in that case, matching the behavior of the
non-literal parser (and of an empty pattern in other regex engines).
A variant of this fix has been carried in GNU R's vendored copy of TRE
(where it backs fixed = TRUE matching) since 2009.
The suite had no REG_LITERAL coverage; these cases cover the empty-pattern fix in this branch and literal handling of metacharacters.
kevinushey
force-pushed
the
fix-literal-empty
branch
from
August 9, 2026 19:57
f4d007b to
0f5673d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
These pull requests were generated with Claude Code, but were reviewed by me (@kevinushey) before posting. I'll respond personally to any feedback; please let me know if this is okay.
This makes
REG_LITERALmatch the empty pattern.Problem. With
REG_LITERAL, compiling an empty pattern succeeds, but the resulting regex never matches anything — including the empty string:Expected: an empty pattern matches the empty string at offset 0 in any subject, as it does without
REG_LITERALand in other regex engines.Cause. The
PARSE_ATOMempty-expression branch is explicitly skipped in literal mode (!(ctx->cflags & REG_LITERAL) && ...), so an empty pattern falls through without ever producing an atom.Fix. In literal mode at end of pattern, emit an
EMPTYliteral node, mirroring the non-literal branch. The guard usesctx->re >= ctx->re_endso it also coverstre_regncomp(&re, "", 0, ...).Testing (Windows x64, mingw-w64 gcc 14):
Provenance. A variant of this fix has been carried in GNU R's vendored copy of TRE since 2009, where
REG_LITERALbacks R'sfixed = TRUEmatching.