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.21.0"
Class Method Summary collapse
-
.GlobalReplace(str, pattern, rewrite) ⇒ String
Return a copy of
strwithpatternreplaced byrewriteusingGlobalReplace. -
.QuoteMeta(unquoted) ⇒ String
Returns a version of
strwith all potentially meaningful regexp characters escaped usingQuoteMeta. -
.Replace(str, pattern, rewrite) ⇒ String
Returns a copy of
strwith the first occurrencepatternreplaced withrewriteusingReplace.
Class Method Details
.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).
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 |
# File 'ext/re2/re2.cc', line 1723
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;
StringValue(str);
std::string str_as_string(RSTRING_PTR(str), RSTRING_LEN(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,
re2::StringPiece(RSTRING_PTR(rewrite), RSTRING_LEN(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,
re2::StringPiece(RSTRING_PTR(pattern), RSTRING_LEN(pattern)),
re2::StringPiece(RSTRING_PTR(rewrite), RSTRING_LEN(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 using
QuoteMeta. The returned string, used as a regular expression, will
exactly match the original string.
1768 1769 1770 1771 1772 1773 1774 1775 |
# File 'ext/re2/re2.cc', line 1768
static VALUE re2_QuoteMeta(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).
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 |
# File 'ext/re2/re2.cc', line 1670
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.
*/
StringValue(str);
std::string str_as_string(RSTRING_PTR(str), RSTRING_LEN(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,
re2::StringPiece(RSTRING_PTR(rewrite), RSTRING_LEN(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,
re2::StringPiece(RSTRING_PTR(pattern), RSTRING_LEN(pattern)),
re2::StringPiece(RSTRING_PTR(rewrite), RSTRING_LEN(rewrite)));
return encoded_str_new(str_as_string.data(), str_as_string.size(), RE2::Options::EncodingUTF8);
}
}
|