Module: RE2
- Defined in:
- ext/re2/re2.cc,
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.4.3"
Class Method Summary collapse
-
.GlobalReplace(str, pattern, rewrite) ⇒ String
Return a copy of
str
withpattern
replaced byrewrite
. -
.QuoteMeta(unquoted) ⇒ String
Returns a version of str with all potentially meaningful regexp characters escaped.
-
.Replace(str, pattern, rewrite) ⇒ String
Returns a copy of
str
with the first occurrencepattern
replaced withrewrite
.
Class Method Details
.GlobalReplace(str, pattern, rewrite) ⇒ String
Return a copy of str
with pattern
replaced by rewrite
.
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 RE2::Regexp is set to false (any other encoding’s behaviour is undefined).
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 |
# File 'ext/re2/re2.cc', line 1546
static VALUE re2_GlobalReplace(VALUE, VALUE str, VALUE pattern,
VALUE rewrite) {
/* Ensure rewrite is a string. */
StringValue(rewrite);
/* Take a copy of str so it can be modified in-place by
* RE2::GlobalReplace.
*/
re2_pattern *p;
std::string str_as_string(StringValuePtr(str));
/* Do the replacement. */
if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
TypedData_Get_Struct(pattern, re2_pattern, &re2_regexp_data_type, p);
RE2::GlobalReplace(&str_as_string, *p->pattern, RSTRING_PTR(rewrite));
return encoded_str_new(str_as_string.data(), str_as_string.size(),
p->pattern->options().encoding());
} else {
/* Ensure pattern is a string. */
StringValue(pattern);
RE2::GlobalReplace(&str_as_string, RSTRING_PTR(pattern),
RSTRING_PTR(rewrite));
return encoded_str_new(str_as_string.data(), str_as_string.size(), RE2::Options::EncodingUTF8);
}
}
|
.QuoteMeta(unquoted) ⇒ String
Returns a version of str with all potentially meaningful regexp characters escaped. The returned string, used as a regular expression, will exactly match the original string.
1585 1586 1587 1588 1589 1590 1591 |
# File 'ext/re2/re2.cc', line 1585
static VALUE re2_QuoteMeta(VALUE, VALUE unquoted) {
StringValue(unquoted);
std::string quoted_string = RE2::QuoteMeta(RSTRING_PTR(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
.
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 RE2::Regexp is set to false (any other encoding’s behaviour is undefined).
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 |
# File 'ext/re2/re2.cc', line 1501
static VALUE re2_Replace(VALUE, VALUE str, VALUE pattern,
VALUE rewrite) {
/* Ensure rewrite is a string. */
StringValue(rewrite);
re2_pattern *p;
/* Take a copy of str so it can be modified in-place by
* RE2::Replace.
*/
std::string str_as_string(StringValuePtr(str));
/* Do the replacement. */
if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
TypedData_Get_Struct(pattern, re2_pattern, &re2_regexp_data_type, p);
RE2::Replace(&str_as_string, *p->pattern, RSTRING_PTR(rewrite));
return encoded_str_new(str_as_string.data(), str_as_string.size(),
p->pattern->options().encoding());
} else {
/* Ensure pattern is a string. */
StringValue(pattern);
RE2::Replace(&str_as_string, RSTRING_PTR(pattern), RSTRING_PTR(rewrite));
return encoded_str_new(str_as_string.data(), str_as_string.size(), RE2::Options::EncodingUTF8);
}
}
|