-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathregexp_facade.cc
149 lines (117 loc) · 4.88 KB
/
regexp_facade.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
#include "sql/regexp/regexp_facade.h"
#include <string>
#include "sql/mysqld.h" // make_unique_destroy_only
#include "sql/regexp/regexp_engine.h"
#include "sql_string.h"
#include "template_utils.h"
namespace regexp {
String *EvalExprToCharset(Item *expr, String *out) {
uint dummy_errors;
if (expr->collation.collation != regexp_lib_charset) {
// Character set conversion is called for.
StringBuffer<MAX_FIELD_WIDTH> pre_conversion_buffer;
String *s = expr->val_str(&pre_conversion_buffer);
if (s == nullptr) return nullptr;
if (out->copy(s->ptr(), s->length(), s->charset(), regexp_lib_charset,
&dummy_errors))
return nullptr;
return out;
}
String *result = expr->val_str(out);
if (result != nullptr && !is_aligned_to(result->ptr(), alignof(UChar))) {
if (out->copy(result->ptr(), result->length(), result->charset(),
regexp_lib_charset, &dummy_errors))
return nullptr;
DBUG_ASSERT(is_aligned_to(out->ptr(), alignof(UChar)));
return out;
}
return result;
}
bool Regexp_facade::SetPattern(Item *pattern_expr) {
/*
The pattern is NULL, but that's fine. Since it's the facade's job to
handle NULL values, we leverage the fact that any matching against NULL
will have the result NULL and don't involve the Regexp_engine class at
all.
*/
if (pattern_expr == nullptr) {
m_engine = nullptr;
return false;
}
if (!pattern_expr->const_item() || // Non-constant pattern, see above.
m_engine == nullptr) { // Called for the first time.
if (SetupEngine(pattern_expr, m_flags)) return true;
}
return false;
}
bool Regexp_facade::Reset(Item *subject_expr) {
DBUG_ENTER("Regexp_facade::Reset");
if (m_engine == nullptr) DBUG_RETURN(true);
String *subject = EvalExprToCharset(subject_expr, &m_current_subject);
if (subject == nullptr) DBUG_RETURN(true);
m_engine->Reset(subject);
DBUG_RETURN(false);
}
Mysql::Nullable<bool> Regexp_facade::Matches(Item *subject_expr, int start,
int occurrence) {
DBUG_ENTER("Regexp_facade::Find");
if (Reset(subject_expr)) DBUG_RETURN(Mysql::Nullable<bool>());
DBUG_RETURN(m_engine->Matches(start - 1, occurrence));
}
Mysql::Nullable<int> Regexp_facade::Find(Item *subject_expr, int start,
int occurrence, bool after_match) {
Nullable<bool> match_found = Matches(subject_expr, start, occurrence);
if (!match_found.has_value()) return Mysql::Nullable<int>();
if (!match_found.value()) return 0;
return (after_match ? m_engine->EndOfMatch()
: m_engine->StartOfMatch()) + 1;
}
String *Regexp_facade::Replace(Item *subject_expr, Item *replacement_expr,
int64_t start, int occurrence, String *result) {
DBUG_ENTER("Regexp_facade::Replace");
String replacement_buf;
String *replacement =
EvalExprToCharset(replacement_expr, &replacement_buf);
if (replacement == nullptr) DBUG_RETURN(nullptr);
if (Reset(subject_expr)) DBUG_RETURN(nullptr);
DBUG_RETURN(m_engine->Replace(replacement->ptr(), replacement->length(),
start - 1, occurrence, result));
}
String *Regexp_facade::Substr(Item *subject_expr, int start,
int occurrence, String *result) {
if (Reset(subject_expr) || !m_engine->Matches(start - 1, occurrence)) {
m_engine->CheckError();
return nullptr;
}
String *res = m_engine->MatchedSubstring(result);
if (m_engine->CheckError()) return nullptr;
return res;
}
bool Regexp_facade::SetupEngine(Item *pattern_expr, uint flags) {
DBUG_ENTER("Regexp_facade::SetupEngine");
String pattern_buffer;
String *pattern = EvalExprToCharset(pattern_expr, &pattern_buffer);
if (pattern == nullptr) {
m_engine = nullptr;
DBUG_RETURN(false);
}
DBUG_ASSERT(is_aligned_to(pattern->ptr(), alignof(UChar)));
// Actually compile the regular expression.
m_engine = make_unique_destroy_only<Regexp_engine>(
*THR_MALLOC, pattern, flags, opt_regexp_stack_limit,
opt_regexp_time_limit);
// If something went wrong, an error was raised.
DBUG_RETURN(m_engine->IsError());
}
} // namespace regexp