From 27ecaed76eec467163c30cc5029a682fc46fd2c7 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sun, 25 Jan 2026 16:12:45 +0100 Subject: [PATCH] Use regex in source search. --- profiler/src/llm/tools.json | 2 +- profiler/src/profiler/TracyLlmTools.cpp | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/profiler/src/llm/tools.json b/profiler/src/llm/tools.json index 7cf0c16d..841b7ac5 100644 --- a/profiler/src/llm/tools.json +++ b/profiler/src/llm/tools.json @@ -148,7 +148,7 @@ "properties": { "query": { "type": "string", - "description": "Keyword to search for." + "description": "ECMAScript regex to search for." }, "case_insensitive": { "type": "boolean", diff --git a/profiler/src/profiler/TracyLlmTools.cpp b/profiler/src/profiler/TracyLlmTools.cpp index 45be4a26..f4325f43 100644 --- a/profiler/src/profiler/TracyLlmTools.cpp +++ b/profiler/src/profiler/TracyLlmTools.cpp @@ -857,6 +857,15 @@ std::string TracyLlmTools::SourceSearch( std::string query, bool caseInsensitive nlohmann::json json = {}; if( caseInsensitive ) std::ranges::transform( query, query.begin(), []( char c ) { return std::tolower( c ); } ); + std::regex rx; + try + { + rx = std::regex( query ); + } + catch( const std::regex_error& e ) + { + return "Error: Invalid regex: " + std::string( e.what() ); + } size_t total = 0; for( auto& item : cache ) @@ -877,23 +886,17 @@ std::string TracyLlmTools::SourceSearch( std::string query, bool caseInsensitive end = tmp + mem.len; } - if( std::search( start, end, query.begin(), query.end() ) == end ) - { - delete[] tmp; - continue; - } - std::vector res; auto lines = SplitLines( start, mem.len ); for( size_t idx = 0; idx < lines.size(); idx++ ) { - if( lines[idx].find( query ) != std::string::npos ) + if( std::regex_search( lines[idx], rx ) ) { res.emplace_back( idx ); total++; } } - assert( !res.empty() ); + if( res.empty() ) continue; auto r = nlohmann::json::array(); if( caseInsensitive )