1
0
mirror of https://github.com/e621ng/dtext_rb.git synced 2025-03-04 03:03:03 -05:00

Optimize append_segment_uri_escaped

Eliminate two unnecessary string allocations in append_segment_uri_escaped.
As a side effect, Unicode characters in URL parameters are now percent-encoded.

36031e0ff5

Co-Authored-By: evazion <noizave@gmail.com>
This commit is contained in:
Earlopain 2023-03-27 21:07:31 +02:00
parent 86f7d2308a
commit 3af76a4703
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
2 changed files with 16 additions and 15 deletions

View File

@ -906,28 +906,18 @@ static inline void append_segment(StateMachine * sm, const char * a, const char
}
static inline void append_segment_uri_escaped(StateMachine * sm, const char * a, const char * b) {
g_autofree char * segment1 = NULL;
g_autofree char * segment2 = NULL;
g_autoptr(GString) segment_string = g_string_new_len(a, b - a + 1);
segment1 = g_uri_escape_string(segment_string->str, NULL, TRUE);
segment2 = g_markup_escape_text(segment1, -1);
sm->output = g_string_append(sm->output, segment2);
g_autofree char* escaped = g_uri_escape_bytes((const guint8 *)a, b - a + 1, NULL);
g_string_append(sm->output, escaped);
}
static inline void append_segment_uri_possible_fragment_escaped(StateMachine * sm, const char * a, const char * b) {
g_autofree char * segment1 = NULL;
g_autofree char * segment2 = NULL;
g_autoptr(GString) segment_string = g_string_new_len(a, b - a + 1);
segment1 = g_uri_escape_string(segment_string->str, "#", TRUE);
segment2 = g_markup_escape_text(segment1, -1);
sm->output = g_string_append(sm->output, segment2);
g_autofree char* escaped = g_uri_escape_bytes((const guint8 *)a, b - a + 1, "#");
g_string_append(sm->output, escaped);
}
static inline void append_segment_html_escaped(StateMachine * sm, const char * a, const char * b) {
g_autofree gchar * segment = g_markup_escape_text(a, b - a + 1);
sm->output = g_string_append(sm->output, segment);
g_string_append(sm->output, segment);
}
static inline void append_url(StateMachine * sm, const char* url) {
@ -987,6 +977,7 @@ static inline void append_wiki_link(StateMachine * sm, const char * tag, const s
g_autofree gchar* lowercased_tag = g_utf8_strdown(tag, tag_len);
g_autoptr(GString) normalized_tag = g_string_new(g_strdelimit(lowercased_tag, " ", '_'));
// FIXME: Take the anchor as an argument here
if (tag[0] == '#') {
append(sm, "<a rel=\"nofollow\" class=\"dtext-link dtext-wiki-link\" href=\"#");
append_segment_uri_escaped(sm, lowercased_tag+1, lowercased_tag + tag_len - 1);

View File

@ -102,6 +102,10 @@ test2[/ltable]
assert_parse("<p><a rel=\"nofollow\" class=\"dtext-link dtext-wiki-link\" href=\"/wiki_pages/show_or_new?title=wiki_page\">Some Text</a></p>", "[[wiki page|Some Text]]")
end
def test_wiki_links_utf8
assert_parse("<p><a rel=\"nofollow\" class=\"dtext-link dtext-wiki-link\" href=\"/wiki_pages/show_or_new?title=pok%C3%A9mon\">pokémon</a></p>", "[[pokémon]]")
end
def test_wiki_links_edge
assert_parse("<p>[[|_|]]</p>", "[[|_|]]")
assert_parse("<p>[[||_||]]</p>", "[[||_||]]")
@ -263,6 +267,10 @@ test2[/ltable]
assert_parse('<p>a <a rel="nofollow" class="dtext-link" href="http://test.com/home.html#toc">http://test.com/home.html#toc</a> b</p>', 'a http://test.com/home.html#toc b')
end
def test_urls_with_params
assert_parse('<p><a rel="nofollow" class="dtext-link" href="https://test.com/?a=b&amp;c=d#abc">https://test.com/?a=b&amp;c=d#abc</a></p>', "https://test.com/?a=b&c=d#abc")
end
def test_auto_urls
assert_parse('<p>a <a rel="nofollow" class="dtext-link" href="http://test.com">http://test.com</a>. b</p>', 'a http://test.com. b')
end
@ -360,6 +368,8 @@ test2[/ltable]
def test_inline_tags_special_entities
assert_parse('<p><a rel="nofollow" class="dtext-link dtext-post-search-link" href="/posts?tags=%3C3">&lt;3</a></p>', "{{<3}}")
assert_parse('<p><a rel="nofollow" class="dtext-link dtext-post-search-link" href="/posts?tags=%20%22%23%26%2B%3C%3E%3F"> &quot;#&amp;+&lt;&gt;?</a></p>', '{{ "#&+<>?}}')
assert_parse('<p><a rel="nofollow" class="dtext-link dtext-post-search-link" href="/posts?tags=%E6%9D%B1%E6%96%B9">東方</a></p>', "{{東方}}")
end
def test_inline_tags_aliased