Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions ext/erb/escape/escape.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,24 +188,19 @@ find_next_neon(search_state *search)
return find_next_match_neon(search);
}

const uint8x16_t single_quote = vdupq_n_u8('\'');
const uint8x16_t double_quote = vdupq_n_u8('"');
const uint8x16_t ampersand = vdupq_n_u8('&');
const uint8x16_t lt = vdupq_n_u8('<');
const uint8x16_t gt = vdupq_n_u8('>');
static const uint8x16x4_t escape_table = {
.val = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,255,0,0,0,255,255,0,0,0,0,0,0,0,0}, // " (0x22) & (0x26) ' (0x27)
{0,0,0,0,0,0,0,0,0,0,0,0,255,0,255,0}, // < (0x3C) > (0x3E)
}
};

while ((size_t)(search->end - search->cstr) >= sizeof(uint8x16_t)) {
const uint8x16_t bytes = vld1q_u8(search->cstr);
const uint8x16_t match1 = vceqq_u8(bytes, single_quote);
const uint8x16_t match2 = vceqq_u8(bytes, double_quote);
const uint8x16_t match3 = vceqq_u8(bytes, ampersand);
const uint8x16_t match4 = vceqq_u8(bytes, lt);
const uint8x16_t match5 = vceqq_u8(bytes, gt);

const uint8x16_t mask1 = vorrq_u8(match1, match2);
const uint8x16_t mask2 = vorrq_u8(match3, match4);
const uint8x16_t mask3 = vorrq_u8(mask1, match5);
const uint8x16_t matches = vorrq_u8(mask2, mask3);

const uint8x16_t matches = vqtbl4q_u8(escape_table, bytes);

const uint8x8_t res = vshrn_n_u16(vreinterpretq_u16_u8(matches), 4);
const uint64_t bitmap = vget_lane_u64(vreinterpret_u64_u8(res), 0);
Expand Down
67 changes: 67 additions & 0 deletions test/erb/test_erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,73 @@ def test_with_location
assert_match(/\Atest filename:201\b/, e.backtrace[0])
end

def test_html_escape
assert_equal(" !&quot;\#$%&amp;&#39;()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
ERB::Util.html_escape(" !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"))

assert_equal("", ERB::Util.html_escape(""))
assert_equal("abc", ERB::Util.html_escape("abc"))
assert_equal("&lt;&lt;", ERB::Util.html_escape("<\<"))
assert_equal("&#39;&amp;&quot;&gt;&lt;" * 10, ERB::Util.html_escape("'&\"><" * 10))

assert_equal("", ERB::Util.html_escape(nil))
assert_equal("123", ERB::Util.html_escape(123))

assert_equal(65536+5, ERB::Util.html_escape("x"*65536 + "&").size)
assert_equal(65536+5, ERB::Util.html_escape("&" + "x"*65536).size)
end

def test_html_escape_simd_block_boundary
# Ensure we only escape the characters that need to be escaped.
(0...48).each do |pos|
s = "a" * 48
s[pos] = "<"
expected = "a" * pos + "&lt;" + "a" * (48 - pos - 1)
assert_equal(expected, ERB::Util.html_escape(s), "escape at position #{pos}")
end
end

HTML_ESCAPE_ENTITIES = {"'" => "&#39;", '"' => "&quot;", "&" => "&amp;", "<" => "&lt;", ">" => "&gt;"}

def test_html_escape_simd_multiple_matches_per_block
chars = ["'", '"', '&', '<', '>']
(0..15).each do |a|
(0..15).each do |b|
next if a == b
s = "a" * 32
s[a] = chars[a % chars.size]
s[b] = chars[b % chars.size]
expected = Array.new(32, "a")
expected[a] = HTML_ESCAPE_ENTITIES[chars[a % chars.size]]
expected[b] = HTML_ESCAPE_ENTITIES[chars[b % chars.size]]
assert_equal(expected.join, ERB::Util.html_escape(s), "positions #{a}, #{b}")
end
end
end

def test_html_escape_simd_tail_lengths
(1..40).each do |len|
(0...len).each do |pos|
s = "a" * len
s[pos] = ">"
expected = "a" * pos + "&gt;" + "a" * (len - pos - 1)
assert_equal(expected, ERB::Util.html_escape(s), "len=#{len} pos=#{pos}")
end
end
end

def test_html_escape_to_s
object = Object.new
def object.to_s
"object"
end
assert_equal("object", ERB::Util.html_escape(object))
end

def test_html_escape_extension
assert_nil(ERB::Util.method(:html_escape).source_location)
end if RUBY_ENGINE == 'ruby'

def test_concurrent_default_binding
# This test randomly fails with JRuby -- NameError: undefined local variable or method `template2'
pend if RUBY_ENGINE == 'jruby'
Expand Down