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
This commit is contained in:
@@ -6,31 +6,13 @@
|
||||
// stack and crashes the editor.
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
namespace kte {
|
||||
// Used where std::regex is still called directly (the Qt frontend's match
|
||||
// highlighting; search and the other renderers use RegexEngine.h, whose
|
||||
// Regex::IncrementalLineLimit() applies the same limits for std::regex).
|
||||
// Regex matches are highlighted only on lines up to this many bytes:
|
||||
// std::regex can take time quadratic in line length even for ordinary
|
||||
// patterns (".*q" on a 20 KB line: 12 s), with no way to interrupt it, and
|
||||
// these run on every frame or keystroke.
|
||||
constexpr std::size_t kRegexRenderLineLimit = 2000;
|
||||
|
||||
// Run fn to completion on a thread with a large stack (address space is
|
||||
// reserved, pages are committed only as used), so regex work over long lines
|
||||
// cannot overflow. If such a thread cannot be created, fn runs on the calling
|
||||
// thread. Exceptions thrown by fn are rethrown to the caller.
|
||||
void RunWithLargeStack(const std::function<void()> &fn);
|
||||
|
||||
// Call on_match(position, length) for each match of rx in line. Lines short
|
||||
// enough to be safe on any stack are matched inline; longer ones on a large
|
||||
// stack (a 10,000-byte line already overflowed 8 MiB for some patterns).
|
||||
// Throws what std::regex throws.
|
||||
void ForEachRegexMatch(const std::string &line, const std::regex &rx,
|
||||
const std::function<void(std::size_t, std::size_t)> &on_match);
|
||||
} // namespace kte
|
||||
|
||||
Reference in New Issue
Block a user