Files
kte/RegexEngine.h
T
Claude 6bc3a6e0af Share search-match highlighting across renderers; engine owns its stack policy
SearchHighlight compiles the search pattern once per frame and yields each
line's match ranges; the terminal, ImGui and Qt renderers had three copies
of this, and Qt compiled a std::regex for every visible line on every
frame. Qt now uses kte::Regex like the others.

kte::Regex::RunGuarded replaces Command.cc's run_regex_work, which chose
the large stack by comparing engine names. ForEachRegexMatch had no
callers left and is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB
2026-09-24 12:19:59 -07:00

80 lines
2.9 KiB
C++

/*
* RegexEngine.h - the regex engine behind search, replace and match
* highlighting.
*
* With KTE_USE_PCRE2 (the default when libpcre2-8 is found) patterns are
* compiled by PCRE2 with its JIT: typically one to two orders of magnitude
* faster than std::regex, no recursion on the C stack, and a match limit
* that stops catastrophic backtracking (e.g. "^(a+)+$") instead of hanging
* the editor. Otherwise std::regex (ECMAScript) is used, run on a large
* stack (see RunGuarded). The syntax both accept covers ordinary use;
* PCRE2 additionally has lookbehind, possessive quantifiers and the like.
*
* Replacement strings use ECMAScript syntax with either engine, as
* std::regex_replace (libstdc++) reads it: $& or $0 (the match), $1..$99
* (groups, two digits read greedily; a group the pattern lacks expands to
* nothing), $` (the text since the previous match), $' (the rest of the
* line), $$ (a dollar sign). As in ECMAScript, '.' matches neither CR nor
* LF and '$' matches only at the end of the line's text.
*/
#pragma once
#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
namespace kte {
class Regex {
public:
Regex();
~Regex();
Regex(Regex &&other) noexcept;
Regex &operator=(Regex &&other) noexcept;
Regex(const Regex &) = delete;
Regex &operator=(const Regex &) = delete;
// Compile; on failure err describes the problem and Valid() is false.
bool Compile(std::string_view pattern, std::string &err);
[[nodiscard]] bool Valid() const;
// The first match in `subject` starting at byte `from` or later; `from`
// may be inside the subject (anchors and lookbehind see what precedes
// it). False if there is none, or if the engine gave up (LimitHit()).
bool Search(std::string_view subject, std::size_t from, std::size_t &pos, std::size_t &len) const;
// Every non-overlapping match replaced by `fmt` (ECMAScript syntax, see
// above); an empty match is not repeated at the same position.
[[nodiscard]] std::string ReplaceAll(std::string_view subject, std::string_view fmt) const;
// Whether the last Search/ReplaceAll stopped at the engine's match limit.
[[nodiscard]] bool LimitHit() const;
// Run fn, which does regex work that may span long text, where the
// engine cannot overflow the stack: std::regex recurses once per matched
// character, so fn runs to completion on a thread with a large stack
// (RegexGuard.h); PCRE2 does not recurse on the C stack, so fn runs
// inline. Exceptions thrown by fn are rethrown.
static void RunGuarded(const std::function<void()> &fn);
// "PCRE2" or "std::regex".
static const char *EngineName();
// The longest line search-as-you-type and match highlighting run the
// regex over (per keystroke/frame); std::regex can be quadratic in line
// length with no way to interrupt it, PCRE2 is bounded by its limit.
static std::size_t IncrementalLineLimit();
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
} // namespace kte