module RE2

Public Class Methods

GlobalReplace(p1, p2, p3) click to toggle source

Return a copy of str with pattern replaced by rewrite.

@param [String] str the string to modify @param [String, RE2::Regexp] pattern a regexp matching text to be replaced @param [String] rewrite the string to replace with @return [String] the resulting string @example

re2 = RE2.new("oo?")
RE2.GlobalReplace("whoops-doops", re2, "e")  #=> "wheps-deps"
RE2.GlobalReplace("hello there", "e", "i")   #=> "hillo thiri"
static VALUE re2_GlobalReplace(VALUE self, VALUE str, VALUE pattern,
                               VALUE rewrite) {
  UNUSED(self);

  /* Convert all the inputs to be pumped into RE2::GlobalReplace. */
  re2_pattern *p;
  string str_as_string(StringValuePtr(str));

  /* Do the replacement. */
  if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
    Data_Get_Struct(pattern, re2_pattern, p);
    RE2::GlobalReplace(&str_as_string, *p->pattern, StringValuePtr(rewrite));

    return ENCODED_STR_NEW(str_as_string.data(), str_as_string.size(),
        p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
  } else {
    RE2::GlobalReplace(&str_as_string, StringValuePtr(pattern),
                       StringValuePtr(rewrite));

    return ENCODED_STR_NEW2(str_as_string.data(), str_as_string.size(),
        pattern);
  }
}
QuoteMeta(p1) click to toggle source

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.

@param [String] unquoted the unquoted string @return [String] the escaped string @example

RE2::Regexp.escape("1.5-2.0?")    #=> "1\.5\-2\.0\?"
static VALUE re2_QuoteMeta(VALUE self, VALUE unquoted) {
  UNUSED(self);
  string quoted_string = RE2::QuoteMeta(StringValuePtr(unquoted));
  return rb_str_new(quoted_string.data(), quoted_string.size());
}
Replace(p1, p2, p3) click to toggle source

Returns a copy of str with the first occurrence pattern replaced with rewrite.

@param [String] str the string to modify @param [String, RE2::Regexp] pattern a regexp matching text to be replaced @param [String] rewrite the string to replace with @return [String] the resulting string @example

RE2.Replace("hello there", "hello", "howdy") #=> "howdy there"
re2 = RE2.new("hel+o")
RE2.Replace("hello there", re2, "yo")        #=> "yo there"
static VALUE re2_Replace(VALUE self, VALUE str, VALUE pattern,
    VALUE rewrite) {
  UNUSED(self);
  re2_pattern *p;

  /* Convert all the inputs to be pumped into RE2::Replace. */
  string str_as_string(StringValuePtr(str));

  /* Do the replacement. */
  if (rb_obj_is_kind_of(pattern, re2_cRegexp)) {
    Data_Get_Struct(pattern, re2_pattern, p);
    RE2::Replace(&str_as_string, *p->pattern, StringValuePtr(rewrite));

    return ENCODED_STR_NEW(str_as_string.data(), str_as_string.size(),
        p->pattern->options().utf8() ? "UTF-8" : "ISO-8859-1");
  } else {
    RE2::Replace(&str_as_string, StringValuePtr(pattern),
        StringValuePtr(rewrite));

    return ENCODED_STR_NEW2(str_as_string.data(), str_as_string.size(),
        pattern);
  }

}