Implements the REGEXP operator for SQLite. Called by SQLite when expr REGEXP pattern is evaluated.
14 {
15 if (argc != 2) {
16 const char* errorMsg = "REGEXP requires exactly two arguments.";
17 sqlite3_result_error(context, errorMsg, -1);
18 return;
19 }
20
21
22
23 const unsigned char* pattern_uch = sqlite3_value_text(argv[0]);
24 const unsigned char* text_uch = sqlite3_value_text(argv[1]);
25
26
27
28 if (!pattern_uch || !text_uch) {
29 sqlite3_result_int(context, 0);
30 return;
31 }
32
33 const char* pattern = reinterpret_cast<const char*>(pattern_uch);
34 const char* text = reinterpret_cast<const char*>(text_uch);
35
36 try {
37
38
39
40 std::regex regex_pattern(pattern);
41
42
43 bool match_found = std::regex_search(text, regex_pattern);
44
45
46 sqlite3_result_int(context, match_found ? 1 : 0);
47
48 } catch (const std::regex_error& e) {
49
50 std::string errorMsg = "REGEXP pattern error: "s + e.what();
51 sqlite3_result_error(context, errorMsg.c_str(), errorMsg.length());
52 } catch (const std::exception& e) {
53
54 std::string errorMsg = "REGEXP unexpected error: "s + e.what();
55 sqlite3_result_error(context, errorMsg.c_str(), errorMsg.length());
56 }
57}