Skip to content

Commit 26d0fcc

Browse files
author
ochafik
committed
const for all static regexes
1 parent 12db972 commit 26d0fcc

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

common/chat.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,9 @@ static common_chat_params common_chat_params_init_command_r7b(const common_chat_
879879
return data;
880880
}
881881
static common_chat_msg common_chat_parse_command_r7b(const std::string & input, bool extract_reasoning) {
882-
static std::regex thought_regex("(<\\|START_THINKING\\|>([\\s\\S]*?)<\\|END_THINKING\\|>)([\\s\\S]*)");
883-
static std::regex action_regex("<\\|START_ACTION\\|>([\\s\\S]*?)<\\|END_ACTION\\|>");
884-
static std::regex response_regex("(?:<\\|START_RESPONSE\\|>)?([\\s\\S]*?)<\\|END_RESPONSE\\|>");
882+
static const std::regex thought_regex("(<\\|START_THINKING\\|>([\\s\\S]*?)<\\|END_THINKING\\|>)([\\s\\S]*)");
883+
static const std::regex action_regex("<\\|START_ACTION\\|>([\\s\\S]*?)<\\|END_ACTION\\|>");
884+
static const std::regex response_regex("(?:<\\|START_RESPONSE\\|>)?([\\s\\S]*?)<\\|END_RESPONSE\\|>");
885885

886886
std::smatch match;
887887

@@ -1013,10 +1013,10 @@ static common_chat_params common_chat_params_init_llama_3_1_tool_calls(const com
10131013
}
10141014
static common_chat_msg common_chat_parse_llama_3_1(const std::string & input, bool with_builtin_tools = false) {
10151015
// TODO: tighten & simplify the parser, don't accept leading text context.
1016-
static std::regex function_regex(
1016+
static const std::regex function_regex(
10171017
"\\s*\\{\\s*(?:\"type\"\\s*:\\s*\"function\"\\s*,\\s*)?\"name\"\\s*:\\s*\"([^\"]+)\"\\s*,\\s*\"parameters\"\\s*: ");
1018-
static std::regex close_regex("\\}\\s*");
1019-
static std::regex builtin_call_regex("<\\|python_tag\\|>\\s*([^.(]+)\\s*\\.\\s*call\\s*\\(\\s*([\\w]+)\\s*=\\s*([\\s\\S]*?)\\)");
1018+
static const std::regex close_regex("\\}\\s*");
1019+
static const std::regex builtin_call_regex("<\\|python_tag\\|>\\s*([^.(]+)\\s*\\.\\s*call\\s*\\(\\s*([\\w]+)\\s*=\\s*([\\s\\S]*?)\\)");
10201020

10211021
if (with_builtin_tools) {
10221022
std::smatch match;
@@ -1108,7 +1108,7 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
11081108
}
11091109
static common_chat_msg handle_think_tag_prelude(const std::string & input, bool extract_reasoning, const std::function<common_chat_msg(const std::string &)> & rest_parser) {
11101110
std::smatch match;
1111-
static std::regex reasoning_content_regex("((?:<think>)?([\\s\\S\\r\\n]*?)</think>)?([\\s\\S\\r\\n]*)");
1111+
static const std::regex reasoning_content_regex("((?:<think>)?([\\s\\S\\r\\n]*?)</think>)?([\\s\\S\\r\\n]*)");
11121112
if (std::regex_match(input, match, reasoning_content_regex)) {
11131113
auto rest = match[3].str();
11141114
auto msg = rest_parser(rest);
@@ -1126,9 +1126,9 @@ static common_chat_msg handle_think_tag_prelude(const std::string & input, bool
11261126
}
11271127
static common_chat_msg common_chat_parse_deepseek_r1(const std::string & input, bool extract_reasoning) {
11281128
return handle_think_tag_prelude(input, extract_reasoning, [](const std::string & input) {
1129-
static std::regex function_regex("<|tool▁call▁begin|>function<|tool▁sep|>([^\n]+)\n```json\n");
1130-
static std::regex close_regex("```[\\s\\r\\n]*<|tool▁call▁end|>");
1131-
static std::regex tool_calls_regex("[\\s\\r\\n]*(?:<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>|<|tool\\\\_calls\\\\_begin|>)([\\s\\S\\r\\n]*?)<|tool▁calls▁end|>");
1129+
static const std::regex function_regex("<|tool▁call▁begin|>function<|tool▁sep|>([^\n]+)\n```json\n");
1130+
static const std::regex close_regex("```[\\s\\r\\n]*<|tool▁call▁end|>");
1131+
static const std::regex tool_calls_regex("[\\s\\r\\n]*(?:<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>|<|tool\\\\_calls\\\\_begin|>)([\\s\\S\\r\\n]*?)<|tool▁calls▁end|>");
11321132

11331133
common_chat_msg msg;
11341134
msg.role = "assistant";
@@ -1246,8 +1246,8 @@ static common_chat_params common_chat_params_init_functionary_v3_2(const common_
12461246
}
12471247

12481248
static common_chat_msg common_chat_parse_functionary_v3_2(const std::string & input) {
1249-
static std::regex function_regex(R"((?:>>>)?(?:assistant<|end_header_id|>\n)?(\w+)\n)");
1250-
static std::regex close_regex(R"($|(?=>>>))");
1249+
static const std::regex function_regex(R"((?:>>>)?(?:assistant<|end_header_id|>\n)?(\w+)\n)");
1250+
static const std::regex close_regex(R"($|(?=>>>))");
12511251

12521252
std::string content;
12531253
auto it = input.begin();
@@ -1336,7 +1336,7 @@ static common_chat_params common_chat_params_init_functionary_v3_1_llama_3_1(con
13361336
}
13371337
static common_chat_msg common_chat_parse_functionary_v3_1_llama_3_1(const std::string & input) {
13381338
// This version of Functionary still supports the llama 3.1 tool call format for the python tool.
1339-
static std::regex python_tag_regex(R"(<\|python_tag\|>([\s\S\n]*)$)");
1339+
static const std::regex python_tag_regex(R"(<\|python_tag\|>([\s\S\n]*)$)");
13401340
std::smatch match;
13411341
if (std::regex_search(input, match, python_tag_regex)) {
13421342
auto code = match[1].str();
@@ -1350,8 +1350,8 @@ static common_chat_msg common_chat_parse_functionary_v3_1_llama_3_1(const std::s
13501350
});
13511351
return msg;
13521352
}
1353-
static std::regex function_regex(R"(<function=(\w+)>)");
1354-
static std::regex close_regex(R"(</function>)");
1353+
static const std::regex function_regex(R"(<function=(\w+)>)");
1354+
static const std::regex close_regex(R"(</function>)");
13551355
// TODO: tighten & simplify.
13561356
return parse_json_tool_calls(input, std::nullopt, function_regex, close_regex);
13571357
}
@@ -1445,7 +1445,7 @@ static common_chat_params common_chat_params_init_hermes_2_pro(const common_chat
14451445
}
14461446
static common_chat_msg common_chat_parse_hermes_2_pro(const std::string& input, bool extract_reasoning) {
14471447
return handle_think_tag_prelude(input, extract_reasoning, [](const std::string & input) {
1448-
const static std::regex open_regex(
1448+
static const std::regex open_regex(
14491449
"(?:"
14501450
"(```(?:xml|json)?\\n\\s*)?" // match 1 (block_start)
14511451
"(<tool_call>" // match 2 (open_tag)

0 commit comments

Comments
 (0)