Fixed issue with URL_ENCODER encoding (and small optimization)

This commit is contained in:
2014-04-08 21:47:39 +02:00
parent a2cbcbbbc6
commit 1d0a2363d8
2 changed files with 35 additions and 17 deletions

View File

@@ -61,10 +61,10 @@ feature -- Encoder
then then
Result.extend (c) Result.extend (c)
else else
Result.append (url_encoded_char (l_code)) append_url_encoded_char (l_code, Result)
end end
else else
Result.append (url_encoded_char (l_code)) append_url_encoded_char (l_code, Result)
end end
i := i + 1 i := i + 1
end end
@@ -97,14 +97,14 @@ feature -- Encoder
if a_ignore.has (c) then if a_ignore.has (c) then
s8.extend (c) s8.extend (c)
else else
s8.append (url_encoded_char (l_code)) append_url_encoded_char (l_code, s8)
end end
end end
else else
if a_ignore.has (c) then if a_ignore.has (c) then
s8.extend (c) s8.extend (c)
else else
s8.append (url_encoded_char (l_code)) append_url_encoded_char (l_code, s8)
end end
end end
i := i + 1 i := i + 1
@@ -113,25 +113,42 @@ feature -- Encoder
feature {NONE} -- encoder character feature {NONE} -- encoder character
url_encoded_char (a_code: NATURAL_32): STRING_8 append_url_encoded_char (a_code: NATURAL_32; a_output: STRING_GENERAL)
local
c: INTEGER
do do
create Result.make (3)
if a_code.is_valid_character_8_code then if a_code.is_valid_character_8_code then
Result.extend ('%%') c := a_code.to_integer_32
Result.append (a_code.to_hex_string) a_output.append_code (37) -- 37 '%%'
from a_output.append_code (hex_digit [c |>> 4])
until a_output.append_code (hex_digit [c & 0xF])
Result.count < 2 or else Result[2] /= '0'
loop
Result.remove (2)
end
else else
has_error := True --| Non-ascii escape not currently supported has_error := True --| Non-ascii escape not currently supported
end end
ensure
exists: Result /= Void
end end
hex_digit: SPECIAL [NATURAL_32]
-- Hexadecimal digits.
once
create Result.make_filled (0, 16)
Result [0] := {NATURAL_32} 48 -- 48 '0'
Result [1] := {NATURAL_32} 49 -- 49 '1'
Result [2] := {NATURAL_32} 50 -- 50 '2'
Result [3] := {NATURAL_32} 51 -- 51 '3'
Result [4] := {NATURAL_32} 52 -- 52 '4'
Result [5] := {NATURAL_32} 53 -- 53 '5'
Result [6] := {NATURAL_32} 54 -- 54 '6'
Result [7] := {NATURAL_32} 55 -- 55 '7'
Result [8] := {NATURAL_32} 56 -- 56 '8'
Result [9] := {NATURAL_32} 57 -- 57 '9'
Result [10] := {NATURAL_32} 65 -- 65 'A'
Result [11] := {NATURAL_32} 66 -- 66 'B'
Result [12] := {NATURAL_32} 67 -- 67 'C'
Result [13] := {NATURAL_32} 68 -- 68 'D'
Result [14] := {NATURAL_32} 69 -- 69 'E'
Result [15] := {NATURAL_32} 70 -- 70 'F'
end
feature -- Decoder feature -- Decoder
decoded_string (v: READABLE_STRING_8): STRING_32 decoded_string (v: READABLE_STRING_8): STRING_32
@@ -365,7 +382,7 @@ feature {NONE} -- Hexadecimal and strings
end end
note note
copyright: "2011-2012, Eiffel Software and others" copyright: "2011-2014, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[ source: "[
Eiffel Software Eiffel Software

View File

@@ -22,6 +22,7 @@ feature -- Test routines
test_url_encoded_encoding ({STRING_32}"http://domain.tld/foo/bar/script.php?test='toto'&foo=bar&title=il <20>tait une fois") test_url_encoded_encoding ({STRING_32}"http://domain.tld/foo/bar/script.php?test='toto'&foo=bar&title=il <20>tait une fois")
test_url_encoded_encoding ({STRING_32}"<22>t<EFBFBD>") test_url_encoded_encoding ({STRING_32}"<22>t<EFBFBD>")
test_url_encoded_decoding ({STRING_8}"%%E9t%%E9", {STRING_32}"<22>t<EFBFBD>") test_url_encoded_decoding ({STRING_8}"%%E9t%%E9", {STRING_32}"<22>t<EFBFBD>")
test_url_encoded_decoding ({STRING_8}"Test%0A", {STRING_32}"Test%N")
test_utf8_url_encoded_decoding ({STRING_8}"%%C3%%A9t%%C3%%A9", {STRING_32}"<22>t<EFBFBD>") test_utf8_url_encoded_decoding ({STRING_8}"%%C3%%A9t%%C3%%A9", {STRING_32}"<22>t<EFBFBD>")
end end