ext/re2/re2.cc in re2-0.1.0 vs ext/re2/re2.cc in re2-0.1.1
- old
+ new
@@ -17,10 +17,14 @@
#if !defined(RSTRING_LEN)
# define RSTRING_LEN(x) (RSTRING(x)->len)
#endif
+ #if !defined(RSTRING_PTR)
+ # define RSTRING_PTR(x) (RSTRING(x)->ptr)
+ #endif
+
typedef struct {
RE2 *pattern;
} re2_pattern;
typedef struct {
@@ -139,13 +143,13 @@
re2_matchdata *m;
Data_Get_Struct(self, re2_matchdata, m);
VALUE array = rb_ary_new2(m->number_of_matches);
for (i = 0; i < m->number_of_matches; i++) {
if (m->matches[i].empty()) {
- rb_ary_store(array, i, Qnil);
+ rb_ary_push(array, Qnil);
} else {
- rb_ary_store(array, i, rb_str_new2(m->matches[i].as_string().c_str()));
+ rb_ary_push(array, rb_str_new(m->matches[i].data(), m->matches[i].size()));
}
}
return array;
}
@@ -156,11 +160,11 @@
Data_Get_Struct(self, re2_matchdata, m);
if (nth >= m->number_of_matches || m->matches[nth].empty()) {
return Qnil;
} else {
- return rb_str_new2(m->matches[nth].as_string().c_str());
+ return rb_str_new(m->matches[nth].data(), m->matches[nth].size());
}
}
/*
* Retrieve zero, one or more matches by index.
@@ -234,31 +238,33 @@
re2_matchdata *m;
VALUE match, result;
Data_Get_Struct(self, re2_matchdata, m);
- result = rb_str_new2("#<RE2::MatchData");
+ result = rb_str_new("#<RE2::MatchData", 16);
for (i = 0; i < m->number_of_matches; i++) {
- rb_str_cat2(result, " ");
+ rb_str_cat(result, " ", 1);
if (i > 0) {
char buf[sizeof(i)*3+1];
snprintf(buf, sizeof(buf), "%d", i);
rb_str_cat2(result, buf);
- rb_str_cat2(result, ":");
+ rb_str_cat(result, ":", 1);
}
match = re2_matchdata_nth_match(i, self);
if (match == Qnil) {
- rb_str_cat2(result, "nil");
+ rb_str_cat(result, "nil", 3);
} else {
- rb_str_append(result, rb_str_inspect(match));
+ rb_str_cat(result, "\"", 1);
+ rb_str_cat(result, RSTRING_PTR(match), RSTRING_LEN(match));
+ rb_str_cat(result, "\"", 1);
}
}
- rb_str_cat2(result, ">");
+ rb_str_cat(result, ">", 1);
return result;
}
/*
@@ -407,15 +413,15 @@
*/
static VALUE
re2_regexp_inspect(VALUE self)
{
re2_pattern *p;
- VALUE result = rb_str_new2("#<RE2::Regexp /");
+ VALUE result = rb_str_new("#<RE2::Regexp /", 15);
Data_Get_Struct(self, re2_pattern, p);
- rb_str_cat2(result, p->pattern->pattern().c_str());
- rb_str_cat2(result, "/>");
+ rb_str_cat(result, p->pattern->pattern().data(), p->pattern->pattern().size());
+ rb_str_cat(result, "/>", 2);
return result;
}
/*
@@ -429,11 +435,11 @@
static VALUE
re2_regexp_to_s(VALUE self)
{
re2_pattern *p;
Data_Get_Struct(self, re2_pattern, p);
- return rb_str_new2(p->pattern->pattern().c_str());
+ return rb_str_new(p->pattern->pattern().data(), p->pattern->pattern().size());
}
/*
* Returns whether or not the regular expression +re2+
* was compiled successfully or not.
@@ -663,11 +669,11 @@
static VALUE
re2_regexp_error(VALUE self)
{
re2_pattern *p;
Data_Get_Struct(self, re2_pattern, p);
- return rb_str_new2(p->pattern->error().c_str());
+ return rb_str_new(p->pattern->error().data(), p->pattern->error().size());
}
/*
* If the RE2 could not be created properly, returns
* the offending portion of the regexp.
@@ -677,11 +683,11 @@
static VALUE
re2_regexp_error_arg(VALUE self)
{
re2_pattern *p;
Data_Get_Struct(self, re2_pattern, p);
- return rb_str_new2(p->pattern->error_arg().c_str());
+ return rb_str_new(p->pattern->error_arg().data(), p->pattern->error_arg().size());
}
/*
* Returns the program size, a very approximate measure
* of a regexp's "cost". Larger numbers are more expensive
@@ -744,11 +750,11 @@
rb_hash_aset(options, ID2SYM(id_one_line),
BOOL2RUBY(p->pattern->options().one_line()));
/* This is a read-only hash after all... */
- OBJ_FREEZE(options);
+ rb_obj_freeze(options);
return options;
}
/*
@@ -842,11 +848,12 @@
matchdata = rb_class_new_instance(0, 0, re2_cMatchData);
Data_Get_Struct(matchdata, re2_matchdata, m);
m->matches = new (std::nothrow) re2::StringPiece[n];
m->regexp = self;
- m->string = rb_str_dup_frozen(text);
+ m->string = rb_str_dup(text);
+ rb_str_freeze(m->string);
if (m->matches == 0) {
rb_raise(rb_eNoMemError, "not enough memory to allocate StringPieces for matches");
}
@@ -894,10 +901,14 @@
* text #=> "Good evening"
*/
static VALUE
re2_Replace(VALUE self, VALUE str, VALUE pattern, VALUE rewrite)
{
+
+ /* Look out for frozen strings. */
+ rb_check_frozen(str);
+
UNUSED(self);
VALUE repl;
re2_pattern *p;
/* Convert all the inputs to be pumped into RE2::Replace. */
@@ -914,11 +925,14 @@
/* Save the replacement as a VALUE. */
repl = rb_str_new(str_as_string.c_str(), str_as_string.length());
/* Replace the original string with the replacement. */
- rb_str_update(str, 0, RSTRING_LEN(str), repl);
+ if (RSTRING_LEN(str) != RSTRING_LEN(repl)) {
+ rb_str_resize(str, RSTRING_LEN(repl));
+ }
+ memcpy(RSTRING_PTR(str), RSTRING_PTR(repl), RSTRING_LEN(repl));
return str;
}
/*
@@ -937,10 +951,14 @@
* text #=> "Geeeed meerning"
*/
static VALUE
re2_GlobalReplace(VALUE self, VALUE str, VALUE pattern, VALUE rewrite)
{
+
+ /* Look out for frozen strings. */
+ rb_check_frozen(str);
+
UNUSED(self);
/* Convert all the inputs to be pumped into RE2::GlobalReplace. */
re2_pattern *p;
std::string str_as_string(StringValuePtr(str));
@@ -957,11 +975,14 @@
/* Save the replacement as a VALUE. */
repl = rb_str_new(str_as_string.c_str(), str_as_string.length());
/* Replace the original string with the replacement. */
- rb_str_update(str, 0, RSTRING_LEN(str), repl);
+ if (RSTRING_LEN(str) != RSTRING_LEN(repl)) {
+ rb_str_resize(str, RSTRING_LEN(repl));
+ }
+ memcpy(RSTRING_PTR(str), RSTRING_PTR(repl), RSTRING_LEN(repl));
return str;
}
/*
@@ -977,10 +998,11 @@
static VALUE
re2_QuoteMeta(VALUE self, VALUE unquoted)
{
UNUSED(self);
re2::StringPiece unquoted_as_string_piece(StringValuePtr(unquoted));
- return rb_str_new2(RE2::QuoteMeta(unquoted_as_string_piece).c_str());
+ std::string quoted_string = RE2::QuoteMeta(unquoted_as_string_piece);
+ return rb_str_new(quoted_string.data(), quoted_string.size());
}
void
Init_re2()
{