Module: RE2
- Defined in:
- ext/re2/re2.cc,
lib/re2/regexp.rb,
lib/re2/string.rb,
lib/re2/scanner.rb,
lib/re2/version.rb
Defined Under Namespace
Modules: String Classes: MatchData, Regexp, Scanner, Set
Constant Summary collapse
- VERSION =
"2.28.0"
Class Method Summary collapse
-
.escape(unquoted) ⇒ String
Returns a version of
strwith all potentially meaningful regexp characters escaped using `QuoteMeta`. -
.extract(text, pattern, rewrite) ⇒ String?
If
patternmatchestext, returns a copy ofrewritewith substitutions using `Extract`. -
.global_replace(str, pattern, rewrite) ⇒ String
Return a copy of
strwithpatternreplaced byrewriteusing `GlobalReplace`. -
.GlobalReplace(str, pattern, rewrite) ⇒ String
Return a copy of
strwithpatternreplaced byrewriteusing `GlobalReplace`. -
.quote(unquoted) ⇒ String
Returns a version of
strwith all potentially meaningful regexp characters escaped using `QuoteMeta`. -
.QuoteMeta(unquoted) ⇒ String
Returns a version of
strwith all potentially meaningful regexp characters escaped using `QuoteMeta`. -
.replace(str, pattern, rewrite) ⇒ String
Returns a copy of
strwith the first occurrencepatternreplaced withrewriteusing `Replace`. -
.Replace(str, pattern, rewrite) ⇒ String
Returns a copy of
strwith the first occurrencepatternreplaced withrewriteusing `Replace`.
Class Method Details
.escape(unquoted) ⇒ String
Returns a version of str with all potentially meaningful regexp characters
escaped using
`QuoteMeta`. The returned string, used as a regular expression, will
exactly match the original string.
2359 2360 2361 2362 2363 2364 2365 2366 |
# File 'ext/re2/re2.cc', line 2359
static VALUE re2_escape(VALUE, VALUE unquoted) {
StringValue(unquoted);
std::string quoted_string = RE2::QuoteMeta(
re2::StringPiece(RSTRING_PTR(unquoted), RSTRING_LEN(unquoted)));
return rb_str_new(quoted_string.data(), quoted_string.size());
}
|
.extract(text, pattern, rewrite) ⇒ String?
If pattern matches text, returns a copy of rewrite with substitutions
using
`Extract`. Non-matching portions of text are ignored.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 |
# File 'ext/re2/re2.cc', line 2289
static VALUE re2_extract(VALUE, VALUE text, VALUE pattern,
VALUE rewrite) {
re2_pattern *p = nullptr;
/* Coerce and freeze all arguments before any C++ allocations so that any
* Ruby exceptions (via longjmp) cannot bypass C++ destructors and leak
* memory, and later coercions cannot mutate earlier strings.
*/
StringValue(text);
text = rb_str_new_frozen(text);
if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
p = unwrap_re2_regexp(pattern);
} else {
StringValue(pattern);
pattern = rb_str_new_frozen(pattern);
}
StringValue(rewrite);
rewrite = rb_str_new_frozen(rewrite);
std::string out;
nogvl_extract_arg arg;
arg.text = re2::StringPiece(RSTRING_PTR(text), RSTRING_LEN(text));
if (p) {
arg.pattern = p->pattern;
} else {
arg.pattern = nullptr;
arg.string_pattern = re2::StringPiece(
RSTRING_PTR(pattern), RSTRING_LEN(pattern));
}
arg.rewrite = re2::StringPiece(
RSTRING_PTR(rewrite), RSTRING_LEN(rewrite));
arg.out = &out;
arg.extracted = false;
#ifdef _WIN32
nogvl_extract(&arg);
#else
rb_thread_call_without_gvl(nogvl_extract, &arg, NULL, NULL);
#endif
RB_GC_GUARD(text);
RB_GC_GUARD(rewrite);
RB_GC_GUARD(pattern);
if (arg.extracted) {
return encoded_str_new(out.data(), out.size(),
p ? p->pattern->options().encoding()
: RE2::Options::EncodingUTF8);
} else {
return Qnil;
}
}
|
.global_replace(str, pattern, rewrite) ⇒ String
Return a copy of str with pattern replaced by rewrite using
`GlobalReplace`.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 |
# File 'ext/re2/re2.cc', line 2213
static VALUE re2_global_replace(VALUE, VALUE str, VALUE pattern,
VALUE rewrite) {
re2_pattern *p = nullptr;
/* Coerce and freeze all arguments before any C++ allocations so that any
* Ruby exceptions (via longjmp) cannot bypass C++ destructors and leak
* memory, and later coercions cannot mutate earlier strings.
*/
StringValue(str);
str = rb_str_new_frozen(str);
if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
p = unwrap_re2_regexp(pattern);
} else {
StringValue(pattern);
pattern = rb_str_new_frozen(pattern);
}
StringValue(rewrite);
rewrite = rb_str_new_frozen(rewrite);
/* Take a copy of str so it can be modified in-place by
* RE2::GlobalReplace.
*/
std::string str_as_string(RSTRING_PTR(str), RSTRING_LEN(str));
nogvl_replace_arg arg;
arg.str = &str_as_string;
if (p) {
arg.pattern = p->pattern;
} else {
arg.pattern = nullptr;
arg.string_pattern = re2::StringPiece(
RSTRING_PTR(pattern), RSTRING_LEN(pattern));
}
arg.rewrite = re2::StringPiece(
RSTRING_PTR(rewrite), RSTRING_LEN(rewrite));
#ifdef _WIN32
nogvl_global_replace(&arg);
#else
rb_thread_call_without_gvl(nogvl_global_replace, &arg, NULL, NULL);
#endif
RB_GC_GUARD(rewrite);
RB_GC_GUARD(pattern);
if (p) {
return encoded_str_new(str_as_string.data(), str_as_string.size(),
p->pattern->options().encoding());
} else {
return encoded_str_new(str_as_string.data(), str_as_string.size(),
RE2::Options::EncodingUTF8);
}
}
|
.GlobalReplace(str, pattern, rewrite) ⇒ String
Return a copy of str with pattern replaced by rewrite using
`GlobalReplace`.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 |
# File 'ext/re2/re2.cc', line 2213
static VALUE re2_global_replace(VALUE, VALUE str, VALUE pattern,
VALUE rewrite) {
re2_pattern *p = nullptr;
/* Coerce and freeze all arguments before any C++ allocations so that any
* Ruby exceptions (via longjmp) cannot bypass C++ destructors and leak
* memory, and later coercions cannot mutate earlier strings.
*/
StringValue(str);
str = rb_str_new_frozen(str);
if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
p = unwrap_re2_regexp(pattern);
} else {
StringValue(pattern);
pattern = rb_str_new_frozen(pattern);
}
StringValue(rewrite);
rewrite = rb_str_new_frozen(rewrite);
/* Take a copy of str so it can be modified in-place by
* RE2::GlobalReplace.
*/
std::string str_as_string(RSTRING_PTR(str), RSTRING_LEN(str));
nogvl_replace_arg arg;
arg.str = &str_as_string;
if (p) {
arg.pattern = p->pattern;
} else {
arg.pattern = nullptr;
arg.string_pattern = re2::StringPiece(
RSTRING_PTR(pattern), RSTRING_LEN(pattern));
}
arg.rewrite = re2::StringPiece(
RSTRING_PTR(rewrite), RSTRING_LEN(rewrite));
#ifdef _WIN32
nogvl_global_replace(&arg);
#else
rb_thread_call_without_gvl(nogvl_global_replace, &arg, NULL, NULL);
#endif
RB_GC_GUARD(rewrite);
RB_GC_GUARD(pattern);
if (p) {
return encoded_str_new(str_as_string.data(), str_as_string.size(),
p->pattern->options().encoding());
} else {
return encoded_str_new(str_as_string.data(), str_as_string.size(),
RE2::Options::EncodingUTF8);
}
}
|
.quote(unquoted) ⇒ String
Returns a version of str with all potentially meaningful regexp characters
escaped using
`QuoteMeta`. The returned string, used as a regular expression, will
exactly match the original string.
2359 2360 2361 2362 2363 2364 2365 2366 |
# File 'ext/re2/re2.cc', line 2359
static VALUE re2_escape(VALUE, VALUE unquoted) {
StringValue(unquoted);
std::string quoted_string = RE2::QuoteMeta(
re2::StringPiece(RSTRING_PTR(unquoted), RSTRING_LEN(unquoted)));
return rb_str_new(quoted_string.data(), quoted_string.size());
}
|
.QuoteMeta(unquoted) ⇒ String
Returns a version of str with all potentially meaningful regexp characters
escaped using
`QuoteMeta`. The returned string, used as a regular expression, will
exactly match the original string.
2359 2360 2361 2362 2363 2364 2365 2366 |
# File 'ext/re2/re2.cc', line 2359
static VALUE re2_escape(VALUE, VALUE unquoted) {
StringValue(unquoted);
std::string quoted_string = RE2::QuoteMeta(
re2::StringPiece(RSTRING_PTR(unquoted), RSTRING_LEN(unquoted)));
return rb_str_new(quoted_string.data(), quoted_string.size());
}
|
.replace(str, pattern, rewrite) ⇒ String
Returns a copy of str with the first occurrence pattern replaced with
rewrite using
`Replace`.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 |
# File 'ext/re2/re2.cc', line 2141
static VALUE re2_replace(VALUE, VALUE str, VALUE pattern,
VALUE rewrite) {
re2_pattern *p = nullptr;
/* Coerce and freeze all arguments before any C++ allocations so that any
* Ruby exceptions (via longjmp) cannot bypass C++ destructors and leak
* memory, and later coercions cannot mutate earlier strings.
*/
StringValue(str);
str = rb_str_new_frozen(str);
if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
p = unwrap_re2_regexp(pattern);
} else {
StringValue(pattern);
pattern = rb_str_new_frozen(pattern);
}
StringValue(rewrite);
rewrite = rb_str_new_frozen(rewrite);
/* Take a copy of str so it can be modified in-place by RE2::Replace. */
std::string str_as_string(RSTRING_PTR(str), RSTRING_LEN(str));
nogvl_replace_arg arg;
arg.str = &str_as_string;
if (p) {
arg.pattern = p->pattern;
} else {
arg.pattern = nullptr;
arg.string_pattern = re2::StringPiece(
RSTRING_PTR(pattern), RSTRING_LEN(pattern));
}
arg.rewrite = re2::StringPiece(
RSTRING_PTR(rewrite), RSTRING_LEN(rewrite));
#ifdef _WIN32
nogvl_replace(&arg);
#else
rb_thread_call_without_gvl(nogvl_replace, &arg, NULL, NULL);
#endif
RB_GC_GUARD(rewrite);
RB_GC_GUARD(pattern);
if (p) {
return encoded_str_new(str_as_string.data(), str_as_string.size(),
p->pattern->options().encoding());
} else {
return encoded_str_new(str_as_string.data(), str_as_string.size(),
RE2::Options::EncodingUTF8);
}
}
|
.Replace(str, pattern, rewrite) ⇒ String
Returns a copy of str with the first occurrence pattern replaced with
rewrite using
`Replace`.
Note RE2 only supports UTF-8 and ISO-8859-1 encoding so strings will be
returned in UTF-8 by default or ISO-8859-1 if the :utf8 option for the
Regexp is set to false (any other encoding's behaviour is undefined).
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 |
# File 'ext/re2/re2.cc', line 2141
static VALUE re2_replace(VALUE, VALUE str, VALUE pattern,
VALUE rewrite) {
re2_pattern *p = nullptr;
/* Coerce and freeze all arguments before any C++ allocations so that any
* Ruby exceptions (via longjmp) cannot bypass C++ destructors and leak
* memory, and later coercions cannot mutate earlier strings.
*/
StringValue(str);
str = rb_str_new_frozen(str);
if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
p = unwrap_re2_regexp(pattern);
} else {
StringValue(pattern);
pattern = rb_str_new_frozen(pattern);
}
StringValue(rewrite);
rewrite = rb_str_new_frozen(rewrite);
/* Take a copy of str so it can be modified in-place by RE2::Replace. */
std::string str_as_string(RSTRING_PTR(str), RSTRING_LEN(str));
nogvl_replace_arg arg;
arg.str = &str_as_string;
if (p) {
arg.pattern = p->pattern;
} else {
arg.pattern = nullptr;
arg.string_pattern = re2::StringPiece(
RSTRING_PTR(pattern), RSTRING_LEN(pattern));
}
arg.rewrite = re2::StringPiece(
RSTRING_PTR(rewrite), RSTRING_LEN(rewrite));
#ifdef _WIN32
nogvl_replace(&arg);
#else
rb_thread_call_without_gvl(nogvl_replace, &arg, NULL, NULL);
#endif
RB_GC_GUARD(rewrite);
RB_GC_GUARD(pattern);
if (p) {
return encoded_str_new(str_as_string.data(), str_as_string.size(),
p->pattern->options().encoding());
} else {
return encoded_str_new(str_as_string.data(), str_as_string.size(),
RE2::Options::EncodingUTF8);
}
}
|