From f9463d7a2f4291c5f6d407c53485c6cc10ae29e0 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Sat, 28 Nov 2015 01:49:29 +0900 Subject: [PATCH] Skip Temple::Utils.escape_html method call --- ext/hamlit/hamlit.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/ext/hamlit/hamlit.c b/ext/hamlit/hamlit.c index 424527e4..4ecc6c9b 100644 --- a/ext/hamlit/hamlit.c +++ b/ext/hamlit/hamlit.c @@ -1,14 +1,30 @@ #include +#include +#include "houdini.h" VALUE mAttributeBuilder; -static ID id_temple, id_utils, id_escape_html; static ID id_flatten, id_join; static ID id_underscore; +static VALUE +escape_html(VALUE str) +{ + gh_buf buf = GH_BUF_INIT; + + Check_Type(str, T_STRING); + + if (houdini_escape_html0(&buf, (const uint8_t *)RSTRING_PTR(str), RSTRING_LEN(str), 0)) { + str = rb_enc_str_new(buf.ptr, buf.size, rb_utf8_encoding()); + gh_buf_free(&buf); + } + + return str; +} + static VALUE attr_build_id(VALUE escape_attrs, VALUE ids) { - VALUE truthy_ids, id, attr_value, mUtils; + VALUE truthy_ids, id, attr_value; int i, len; ids = rb_funcall(ids, id_flatten, 0); @@ -24,8 +40,7 @@ attr_build_id(VALUE escape_attrs, VALUE ids) attr_value = rb_funcall(truthy_ids, id_join, 1, rb_const_get(mAttributeBuilder, id_underscore)); if (RTEST(escape_attrs)) { - mUtils = rb_const_get(rb_const_get(rb_cObject, id_temple), id_utils); - attr_value = rb_funcall(mUtils, id_escape_html, 1, attr_value); + attr_value = escape_html(attr_value); } return attr_value; @@ -51,10 +66,6 @@ Init_hamlit(void) mAttributeBuilder = rb_define_module_under(mHamlit, "AttributeBuilder"); rb_define_singleton_method(mAttributeBuilder, "build_id", rb_attr_build_id, -1); - id_temple = rb_intern("Temple"); - id_utils = rb_intern("Utils"); - id_escape_html = rb_intern("escape_html"); - id_flatten = rb_intern("flatten"); id_join = rb_intern("join");