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:
Claude
2026-09-24 09:16:04 -07:00
committed by Kyle Isom
parent a33c1759e7
commit e782d24e32
14 changed files with 670 additions and 107 deletions
+24
View File
@@ -15,6 +15,7 @@ set(KTE_FONT_SIZE "18.0" CACHE STRING "Default font size for GUI")
option(KTE_UNDO_DEBUG "Enable undo instrumentation logs" OFF)
option(KTE_ENABLE_TREESITTER "Enable optional Tree-sitter highlighter adapter" OFF)
option(KTE_STATIC_LINK "Enable static linking on Linux" OFF)
option(KTE_USE_PCRE2 "Use PCRE2 (libpcre2-8) for regex search when found; std::regex otherwise" ON)
# Optionally enable AddressSanitizer (ASan)
option(ENABLE_ASAN "Enable AddressSanitizer for builds" OFF)
@@ -66,6 +67,26 @@ endif ()
message(STATUS "Build system: ${CMAKE_HOST_SYSTEM_NAME}")
# Regex engine (see RegexEngine.h). Linked by name (-lpcre2-8) so that a
# static build picks up the static library.
if (KTE_USE_PCRE2)
find_package(PkgConfig QUIET)
if (PKG_CONFIG_FOUND)
pkg_check_modules(PCRE2 QUIET libpcre2-8)
endif ()
if (PCRE2_FOUND)
message(STATUS "Regex engine: PCRE2 ${PCRE2_VERSION}")
add_compile_definitions(KTE_USE_PCRE2)
include_directories(${PCRE2_INCLUDE_DIRS})
link_directories(${PCRE2_LIBRARY_DIRS})
link_libraries(${PCRE2_LIBRARIES})
else ()
message(STATUS "Regex engine: std::regex (libpcre2-8 not found)")
endif ()
else ()
message(STATUS "Regex engine: std::regex")
endif ()
if (BUILD_GUI)
include(cmake/imgui.cmake)
endif ()
@@ -175,6 +196,7 @@ set(COMMON_SOURCES
SyscallWrappers.cc
ErrorRecovery.cc
RegexGuard.cc
RegexEngine.cc
TerminalInputHandler.cc
TerminalRenderer.cc
TerminalFrontend.cc
@@ -273,6 +295,7 @@ set(COMMON_HEADERS
TestRenderer.h
TestFrontend.h
TextSpan.h
RegexEngine.h
UndoNode.h
UndoTree.h
UndoSystem.h
@@ -389,6 +412,7 @@ if (BUILD_TESTS)
SyscallWrappers.cc
ErrorRecovery.cc
RegexGuard.cc
RegexEngine.cc
KKeymap.cc
SwapRecorder.h
OptimizedSearch.cc