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.14.0"
Class Method Summary collapse
-
.GlobalReplace(str, pattern, rewrite) ⇒ String
Return a copy of
str
withpattern
replaced byrewrite
usingGlobalReplace
. -
.QuoteMeta(unquoted) ⇒ String
Returns a version of
str
with all potentially meaningful regexp characters escaped usingQuoteMeta
. -
.Replace(str, pattern, rewrite) ⇒ String
Returns a copy of
str
with the first occurrencepattern
replaced withrewrite
usingReplace
.
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).
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 |
# File 'ext/re2/re2.cc', line 1738
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.
1783 1784 1785 1786 1787 1788 1789 1790 |
# File 'ext/re2/re2.cc', line 1783
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).
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 |
# File 'ext/re2/re2.cc', line 1685
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);
}
}
|