Refactor @uri and @urid and fix multi-byte UTF-8 corruption (#3495)

This commit improves performance by refactoring `@uri` and `@urid`
to pre-alloacte buffers and build the `jv` string at the end.
Also, this fixes `@urid` to prevent multi-byte UTF-8 corruption,
and to preserve embedded null bytes.
This commit is contained in:
itchyny authored and GitHub committed 2026-03-05 08:28:09 +09:00
1 parent 36b6ae00b1
commit bfcf82fde1
2 files changed
+118 -54

No files matched your search

+67 -13
View File
@@ -1,38 +1,92 @@
# Tests are groups of three lines: program, input, expected output
# Blank lines and lines starting with # are ignored
# @uri
@uri
"<>&'\"\t"
"%3C%3E%26%27%22%09"
""
""
# decoding encoded output results in same text
(@uri|@urid)
"<>&'\"\t"
"<>&'\"\t"
@uri
"\b\t\n\f\r\"\\"
"%08%09%0A%0C%0D%22%5C"
@uri
",-./09:;<=>?@AZ[\\]^_`az{|}~\u007f"
"%2C-.%2F09%3A%3B%3C%3D%3E%3F%40AZ%5B%5C%5D%5E_%60az%7B%7C%7D~%7F"
# testing variable length unicode characters
@uri
"a \u03bc \u2230 \ud83d\ude0e"
"a%20%CE%BC%20%E2%88%B0%20%F0%9F%98%8E"
@uri
"a\u0000b\u0000c"
"a%00b%00c"
# @urid
@urid
""
""
@urid
"%08%09%0A%0C%0D%22%5C"
"\b\t\n\f\r\"\\"
@urid
"%2C-.%2F09%3A%3B%3C%3D%3E%3F%40AZ%5B%5C%5D%5E_%60az%7B%7C%7D~%7F"
",-./09:;<=>?@AZ[\\]^_`az{|}~\u007f"
@urid
"a%20%CE%BC%20%E2%88%B0%20%F0%9F%98%8E"
"a \u03bc \u2230 \ud83d\ude0e"
### invalid uri strings
@urid
"a%00b%00c"
"a\u0000b\u0000c"
# unicode character should be length 4 (not 3)
@urid
"hello world"
"hello world"
@urid
"Knäckebröd"
"Knäckebröd"
@urid
"%c3%a4b%c3%a7d%c3%ab"
"äbçdë"
@urid
"a\u0000b\u0000c"
"a\u0000b\u0000c"
# '%' at end of string
. | try @urid catch .
"%F0%93%81"
"string (\"%F0%93%81\") is not a valid uri encoding"
"abc%"
"string (\"abc%\") is not a valid uri encoding"
# '%' followed by only one hex digit
. | try @urid catch .
"abc%f"
"string (\"abc%f\") is not a valid uri encoding"
# '%' followed by non-hex character
. | try @urid catch .
"abc%g"
"string (\"abc%g\") is not a valid uri encoding"
# invalid hex value ('FX')
. | try @urid catch .
"%FX%9F%98%8E"
"string (\"%FX%9F%98%8E\") is not a valid uri encoding"
# trailing utf-8 octets must be formatted like 10xxxxxx
# 'C0' = 11000000 invalid
# incomplete 4-byte UTF-8 sequence (only 3 bytes)
. | try @urid catch .
"%F0%93%81"
"string (\"%F0%93%81\") is not a valid uri encoding"
# invalid continuation byte (0xC0 is not 10xxxxxx)
. | try @urid catch .
"%F0%C0%81%8E"
"string (\"%F0%C0%81%8E\") is not a valid uri encoding"