Skip to content

Commit

Permalink
use rb_str_new_with_class instead of setting klass
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Somerville committed Aug 15, 2013
1 parent 838bb53 commit 962253d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 7 additions & 9 deletions ext/escape_utils/escape_utils.c
Expand Up @@ -62,6 +62,7 @@ static VALUE rb_eu_set_html_secure(VALUE self, VALUE val)
* html_safe_string_class instance variable
*/
static VALUE rb_html_safe_string_class;
static VALUE rb_html_safe_string_template_object;

static VALUE rb_eu_set_html_safe_string_class(VALUE self, VALUE val)
{
Expand All @@ -71,6 +72,8 @@ static VALUE rb_eu_set_html_safe_string_class(VALUE self, VALUE val)
rb_raise(rb_eArgError, "%s must be a descendent of String", rb_class2name(val));

rb_html_safe_string_class = val;
rb_html_safe_string_template_object = rb_class_new_instance(0, NULL, rb_html_safe_string_class);
OBJ_FREEZE(rb_html_safe_string_template_object);
rb_ivar_set(self, rb_intern("@html_safe_string_class"), val);
return val;
}
Expand Down Expand Up @@ -115,19 +118,14 @@ static VALUE rb_eu_escape_html_as_html_safe(VALUE self, VALUE str)
result = eu_new_str(buf.ptr, buf.size);
gh_buf_free(&buf);
} else {
#ifdef RBASIC
result = rb_str_dup(str);
#ifdef HAVE_RB_STR_NEW_WITH_CLASS
result = rb_str_new_with_class(rb_html_safe_string_template_object,
RSTRING_PTR(str), RSTRING_LEN(str));
#else
result = str;
result = rb_funcall(rb_html_safe_string_class, ID_new, 1, str);
#endif
}

#ifdef RBASIC
RBASIC(result)->klass = rb_html_safe_string_class;
#else
result = rb_funcall(rb_html_safe_string_class, ID_new, 1, result);
#endif

rb_ivar_set(result, ID_at_html_safe, Qtrue);

return result;
Expand Down
2 changes: 2 additions & 0 deletions ext/escape_utils/extconf.rb
Expand Up @@ -3,4 +3,6 @@
$CFLAGS << ' -Wall -funroll-loops -fvisibility=hidden'
$CFLAGS << ' -Wextra -O0 -ggdb3' if ENV['DEBUG']

have_func("rb_str_new_with_class")

create_makefile("escape_utils/escape_utils")

0 comments on commit 962253d

Please sign in to comment.