Skip to content

Add Regex.Escape via cre2_quote_meta#36

Merged
corpo-iwillspeak merged 3 commits into
mainfrom
copilot/implement-regex-escape-method
May 26, 2026
Merged

Add Regex.Escape via cre2_quote_meta#36
corpo-iwillspeak merged 3 commits into
mainfrom
copilot/implement-regex-escape-method

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 28, 2026

IronRe2 lacked a way to escape regex metacharacters for literal matching — useful when building patterns programmatically or integrating with RE2-based systems (e.g. Prometheus).

Changes

  • Re2Ffi.cs — P/Invoke binding for cre2_quote_meta, which delegates to RE2::QuoteMeta
  • Regex.cs — Two static Regex.Escape overloads: string and ReadOnlySpan<byte>. The native function heap-allocates the result via malloc; freed with NativeMemory.Free for cross-platform correctness
  • RegexTests.cs — Tests for metacharacter escaping, plain strings, empty input, and a round-trip literal match

Usage

var escaped = Regex.Escape("1+1=2 (really!)");
// => "1\+1\=2 \(really\!\)"

using var re = new Regex(escaped);
re.IsMatch("1+1=2 (really!)"); // true
re.IsMatch("1+1=2 Xreally!Y"); // false

Agent-Logs-Url: https://github.com/crispthinking/IronRe2/sessions/8e5472b7-ecbc-4fe7-8565-9cac67d0b3ae

Co-authored-by: corpo-iwillspeak <265613520+corpo-iwillspeak@users.noreply.github.com>
@corpo-iwillspeak corpo-iwillspeak marked this pull request as ready for review April 28, 2026 10:54
Copilot AI changed the title [WIP] Add method similar to Regex.Escape for IronRe2 Add Regex.Escape via cre2_quote_meta Apr 28, 2026
Copilot AI requested a review from corpo-iwillspeak April 28, 2026 10:56
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an Escape API to IronRe2’s Regex class (backed by RE2’s “quote meta” functionality via cre2_quote_meta) so callers can safely turn literal text/bytes into a regex pattern that matches literally.

Changes:

  • Added cre2_quote_meta P/Invoke declaration in Re2Ffi.
  • Added Regex.Escape(string) and Regex.Escape(ReadOnlySpan<byte>) implementations.
  • Added xUnit tests covering basic escaping and a literal-match smoke test.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/IronRe2/Re2Ffi.cs Adds native interop signature for cre2_quote_meta.
src/IronRe2/Regex.cs Adds public Escape APIs and unmanaged interop/cleanup logic.
test/IronRe2.Tests/RegexTests.cs Adds test coverage for the new Regex.Escape APIs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/IronRe2/Regex.cs
Comment thread test/IronRe2.Tests/RegexTests.cs
@corpo-iwillspeak
Copy link
Copy Markdown
Contributor

@copilot apply changes based on the comments in this thread

…est cases

Agent-Logs-Url: https://github.com/crispthinking/IronRe2/sessions/c34a446d-62c5-4937-a8a6-e3ad138c90c7

Co-authored-by: corpo-iwillspeak <265613520+corpo-iwillspeak@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 28, 2026

@copilot apply changes based on the comments in this thread

Done in bde98bf:

  • Added an if (input.IsEmpty) return string.Empty; fast-path at the top of Escape(ReadOnlySpan<byte>) to avoid passing a null pointer to native code when the span is empty.
  • Extended EscapeData with additional cases covering \\ (backslash), {} (braces), ?, |, and \n (newline — RE2 escapes it as \ + literal newline).

@corpo-iwillspeak corpo-iwillspeak merged commit 409122c into main May 26, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement method like Regex.Escape from System.Text.RegularExpressions

3 participants