Now using READABLE_STRING_... type

This commit is contained in:
Jocelyn Fiat
2011-09-20 16:57:28 +02:00
parent c2f7c198e0
commit dff267cd58
2 changed files with 55 additions and 49 deletions

View File

@@ -6,7 +6,7 @@ note
revision: "$Revision$" revision: "$Revision$"
deferred class deferred class
ENCODER [U -> STRING_GENERAL, E -> STRING_GENERAL] --| U:unencoded type, E:encoded type ENCODER [U -> READABLE_STRING_GENERAL, E -> READABLE_STRING_GENERAL] --| U:unencoded type, E:encoded type
feature -- Access feature -- Access

View File

@@ -13,7 +13,7 @@ class
URL_ENCODER URL_ENCODER
inherit inherit
ENCODER [STRING_32, STRING_8] ENCODER [READABLE_STRING_32, READABLE_STRING_8]
PLATFORM PLATFORM
export export
@@ -30,48 +30,17 @@ feature -- Status report
feature -- Encoder feature -- Encoder
encoded_string (s: STRING_32): STRING_8 encoded_string (s: READABLE_STRING_32): READABLE_STRING_8
-- URL-encoded value of `s'. -- URL-encoded value of `s'.
local local
i, n: INTEGER i, n: INTEGER
uc: CHARACTER_32 uc: CHARACTER_32
c: CHARACTER_8 c: CHARACTER_8
s8: STRING_8
do do
has_error := False has_error := False
create Result.make (s.count + s.count // 10) create s8.make (s.count + s.count // 10)
n := s.count Result := s8
from i := 1 until i > n loop
uc := s.item (i)
if uc.is_character_8 then
c := uc.to_character_8
inspect c
when
'A' .. 'Z',
'a' .. 'z', '0' .. '9',
'.', '-', '~', '_'
then
Result.extend (c)
when ' ' then
Result.extend ('+')
else
Result.append (url_encoded_char (uc))
end
else
Result.append (url_encoded_char (uc))
end
i := i + 1
end
end
partial_encoded_string (s: STRING_32; a_ignore: ARRAY [CHARACTER]): STRING_8
-- URL-encoded value of `s'.
local
i, n: INTEGER
uc: CHARACTER_32
c: CHARACTER_8
do
has_error := False
create Result.make (s.count + s.count // 10)
n := s.count n := s.count
from i := 1 until i > n loop from i := 1 until i > n loop
uc := s.item (i) uc := s.item (i)
@@ -83,21 +52,56 @@ feature -- Encoder
'a' .. 'z', '0' .. '9', 'a' .. 'z', '0' .. '9',
'.', '-', '~', '_' '.', '-', '~', '_'
then then
Result.extend (c) s8.extend (c)
when ' ' then when ' ' then
Result.extend ('+') s8.extend ('+')
else
s8.append (url_encoded_char (uc))
end
else
s8.append (url_encoded_char (uc))
end
i := i + 1
end
end
partial_encoded_string (s: READABLE_STRING_32; a_ignore: ARRAY [CHARACTER]): READABLE_STRING_8
-- URL-encoded value of `s'.
local
i, n: INTEGER
uc: CHARACTER_32
c: CHARACTER_8
s8: STRING_8
do
has_error := False
create s8.make (s.count + s.count // 10)
Result := s8
n := s.count
from i := 1 until i > n loop
uc := s.item (i)
if uc.is_character_8 then
c := uc.to_character_8
inspect c
when
'A' .. 'Z',
'a' .. 'z', '0' .. '9',
'.', '-', '~', '_'
then
s8.extend (c)
when ' ' then
s8.extend ('+')
else else
if a_ignore.has (c) then if a_ignore.has (c) then
Result.extend (c) s8.extend (c)
else else
Result.append (url_encoded_char (uc)) s8.append (url_encoded_char (uc))
end end
end end
else else
if a_ignore.has (c) then if a_ignore.has (c) then
Result.extend (c) s8.extend (c)
else else
Result.append (url_encoded_char (uc)) s8.append (url_encoded_char (uc))
end end
end end
i := i + 1 i := i + 1
@@ -127,33 +131,35 @@ feature {NONE} -- encoder character
feature -- Decoder feature -- Decoder
decoded_string (v: STRING_8): STRING_32 decoded_string (v: READABLE_STRING_8): READABLE_STRING_32
-- The URL-encoded equivalent of the given string -- The URL-encoded equivalent of the given string
local local
i, n: INTEGER i, n: INTEGER
c: CHARACTER c: CHARACTER
pr: CELL [INTEGER] pr: CELL [INTEGER]
s32: STRING_32
do do
n := v.count n := v.count
create Result.make (n) create s32.make (n)
Result := s32
from i := 1 from i := 1
until i > n until i > n
loop loop
c := v.item (i) c := v.item (i)
inspect c inspect c
when '+' then when '+' then
Result.append_character ({CHARACTER_32}' ') s32.append_character ({CHARACTER_32}' ')
when '%%' then when '%%' then
-- An escaped character ? -- An escaped character ?
if i = n then if i = n then
Result.append_character (c.to_character_32) s32.append_character (c.to_character_32)
else else
create pr.put (i) create pr.put (i)
Result.append (url_decoded_char (v, pr)) s32.append (url_decoded_char (v, pr))
i := pr.item i := pr.item
end end
else else
Result.append_character (c.to_character_32) s32.append_character (c.to_character_32)
end end
i := i + 1 i := i + 1
end end