Regex: optional PCRE2 engine behind kte::Regex
RegexEngine.h/.cc wrap the regex engine for search, regex replace-all and the terminal and ImGui renderers' match highlighting: - With KTE_USE_PCRE2 (default ON; used when pkg-config finds libpcre2-8, linked as -lpcre2-8) patterns are compiled by PCRE2 with its JIT and matched under a match limit: catastrophic backtracking stops with a status note instead of hanging the editor, and matching does not recurse on the C stack (no large-stack thread; the per-keystroke and per-frame line limit is 1 MiB instead of 2,000 bytes). Otherwise std::regex is used as before. - Replacement strings follow std::regex_replace (ECMAScript as libstdc++ reads it) with either engine: the PCRE2 path expands $&, $n, $`, $', $$ itself and steps over empty matches the same way. - Regex search walks lines over the materialized text instead of calling GetLineView per row. The terminal renderer compiles the pattern once per frame (it compiled it for every row). - Only one checkpoint per journal is queued at a time: whole-file edits in a row on a large buffer each queued a full copy of the buffer. - default.nix and the Dockerfile add pcre2 and pkg-config. On 26 MB: regex keystroke 1.6 -> 0.14 ms, next 62 -> 6 ms, replace-all 929 -> 438 ms; a rarely matching regex over 105 MB: 1.55 s -> 0.10 s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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 RegexGuard.h). 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 reads it: $& (the match), $1..$99 (groups), $` (the
|
||||
* text since the previous match), $' (the rest of the line), $$ (a dollar).
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#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;
|
||||
|
||||
// "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
|
||||
Reference in New Issue
Block a user