mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/net/imap.rb (decode_utf7): use pack("U*") to encode UTF-8.
* lib/net/imap.rb (encode_utf7): use unpack("U*") to decode UTF-8. * test/net/imap/test_imap.rb: added tests for Net::IMAP. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7800 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d3125e3e43
commit
90ad5e3699
3 changed files with 24 additions and 121 deletions
|
@ -1,3 +1,11 @@
|
|||
Fri Jan 21 17:09:44 2005 Shugo Maeda <shugo@ruby-lang.org>
|
||||
|
||||
* lib/net/imap.rb (decode_utf7): use pack("U*") to encode UTF-8.
|
||||
|
||||
* lib/net/imap.rb (encode_utf7): use unpack("U*") to decode UTF-8.
|
||||
|
||||
* test/net/imap/test_imap.rb: added tests for Net::IMAP.
|
||||
|
||||
Fri Jan 21 13:58:37 2005 Shugo Maeda <shugo@ruby-lang.org>
|
||||
|
||||
* lib/net/imap.rb (u8tou16): fixed typo. fixed: [ruby-lis:40546]
|
||||
|
|
123
lib/net/imap.rb
123
lib/net/imap.rb
|
@ -828,7 +828,7 @@ module Net
|
|||
if x > 0
|
||||
base64.concat("=" * (4 - x))
|
||||
end
|
||||
u16tou8(base64.unpack("m")[0])
|
||||
base64.unpack("m")[0].unpack("n*").pack("U*")
|
||||
end
|
||||
}
|
||||
end
|
||||
|
@ -839,7 +839,7 @@ module Net
|
|||
if $1
|
||||
"&-"
|
||||
else
|
||||
base64 = [u8tou16(x)].pack("m")
|
||||
base64 = [x.unpack("U*").pack("n*")].pack("m")
|
||||
"&" + base64.delete("=\n").tr("/", ",") + "-"
|
||||
end
|
||||
}
|
||||
|
@ -1203,125 +1203,6 @@ module Net
|
|||
end
|
||||
end
|
||||
|
||||
def self.u16tou8(s)
|
||||
len = s.length
|
||||
if len < 2
|
||||
return ""
|
||||
end
|
||||
buf = ""
|
||||
i = 0
|
||||
while i < len
|
||||
c = s[i] << 8 | s[i + 1]
|
||||
i += 2
|
||||
if c == 0xfeff
|
||||
next
|
||||
elsif c < 0x0080
|
||||
buf.concat(c)
|
||||
elsif c < 0x0800
|
||||
b2 = c & 0x003f
|
||||
b1 = c >> 6
|
||||
buf.concat(b1 | 0xc0)
|
||||
buf.concat(b2 | 0x80)
|
||||
elsif c >= 0xdc00 && c < 0xe000
|
||||
raise DataFormatError, "invalid surrogate detected"
|
||||
elsif c >= 0xd800 && c < 0xdc00
|
||||
if i + 2 > len
|
||||
raise DataFormatError, "invalid surrogate detected"
|
||||
end
|
||||
low = s[i] << 8 | s[i + 1]
|
||||
i += 2
|
||||
if low < 0xdc00 || low > 0xdfff
|
||||
raise DataFormatError, "invalid surrogate detected"
|
||||
end
|
||||
c = (((c & 0x03ff)) << 10 | (low & 0x03ff)) + 0x10000
|
||||
b4 = c & 0x003f
|
||||
b3 = (c >> 6) & 0x003f
|
||||
b2 = (c >> 12) & 0x003f
|
||||
b1 = c >> 18;
|
||||
buf.concat(b1 | 0xf0)
|
||||
buf.concat(b2 | 0x80)
|
||||
buf.concat(b3 | 0x80)
|
||||
buf.concat(b4 | 0x80)
|
||||
else # 0x0800-0xffff
|
||||
b3 = c & 0x003f
|
||||
b2 = (c >> 6) & 0x003f
|
||||
b1 = c >> 12
|
||||
buf.concat(b1 | 0xe0)
|
||||
buf.concat(b2 | 0x80)
|
||||
buf.concat(b3 | 0x80)
|
||||
end
|
||||
end
|
||||
return buf
|
||||
end
|
||||
private_class_method :u16tou8
|
||||
|
||||
def self.u8tou16(s)
|
||||
len = s.length
|
||||
buf = ""
|
||||
i = 0
|
||||
while i < len
|
||||
c = s[i]
|
||||
if (c & 0x80) == 0
|
||||
buf.concat(0x00)
|
||||
buf.concat(c)
|
||||
i += 1
|
||||
elsif (c & 0xe0) == 0xc0 &&
|
||||
len >= 2 &&
|
||||
(s[i + 1] & 0xc0) == 0x80
|
||||
if c == 0xc0 || c == 0xc1
|
||||
raise DataFormatError, format("non-shortest UTF-8 sequence (%02x)", c)
|
||||
end
|
||||
u = ((c & 0x1f) << 6) | (s[i + 1] & 0x3f)
|
||||
buf.concat(u >> 8)
|
||||
buf.concat(u & 0x00ff)
|
||||
i += 2
|
||||
elsif (c & 0xf0) == 0xe0 &&
|
||||
i + 2 < len &&
|
||||
(s[i + 1] & 0xc0) == 0x80 &&
|
||||
(s[i + 2] & 0xc0) == 0x80
|
||||
if c == 0xe0 && s[i + 1] < 0xa0
|
||||
raise DataFormatError, format("non-shortest UTF-8 sequence (%02x)", c)
|
||||
end
|
||||
u = ((c & 0x0f) << 12) | ((s[i + 1] & 0x3f) << 6) | (s[i + 2] & 0x3f)
|
||||
# surrogate chars
|
||||
if u >= 0xd800 && u <= 0xdfff
|
||||
raise DataFormatError, format("none-UTF-16 char detected (%04x)", u)
|
||||
end
|
||||
buf.concat(u >> 8)
|
||||
buf.concat(u & 0x00ff)
|
||||
i += 3
|
||||
elsif (c & 0xf8) == 0xf0 &&
|
||||
i + 3 < len &&
|
||||
(s[i + 1] & 0xc0) == 0x80 &&
|
||||
(s[i + 2] & 0xc0) == 0x80 &&
|
||||
(s[i + 3] & 0xc0) == 0x80
|
||||
if c == 0xf0 && s[i + 1] < 0x90
|
||||
raise DataFormatError, format("non-shortest UTF-8 sequence (%02x)", c)
|
||||
end
|
||||
u = ((c & 0x07) << 18) | ((s[i + 1] & 0x3f) << 12) |
|
||||
((s[i + 2] & 0x3f) << 6) | (s[i + 3] & 0x3f)
|
||||
if u < 0x10000
|
||||
buf.concat(u >> 8)
|
||||
buf.concat(u & 0x00ff)
|
||||
elsif u < 0x110000
|
||||
high = ((u - 0x10000) >> 10) | 0xd800
|
||||
low = (u & 0x03ff) | 0xdc00
|
||||
buf.concat(high >> 8)
|
||||
buf.concat(high & 0x00ff)
|
||||
buf.concat(low >> 8)
|
||||
buf.concat(low & 0x00ff)
|
||||
else
|
||||
raise DataFormatError, format("none-UTF-16 char detected (%04x)", u)
|
||||
end
|
||||
i += 4
|
||||
else
|
||||
raise DataFormatError, format("illegal UTF-8 sequence (%02x)", c)
|
||||
end
|
||||
end
|
||||
return buf
|
||||
end
|
||||
private_class_method :u8tou16
|
||||
|
||||
class RawData # :nodoc:
|
||||
def send_data(imap)
|
||||
imap.send(:put_string, @data)
|
||||
|
|
14
test/net/imap/test_imap.rb
Normal file
14
test/net/imap/test_imap.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
require "net/imap"
|
||||
require "test/unit"
|
||||
|
||||
class IMAPTest < Test::Unit::TestCase
|
||||
def test_encode_utf7
|
||||
s = Net::IMAP.encode_utf7("\357\274\241\357\274\242\357\274\243")
|
||||
assert_equal("&,yH,Iv8j-", s)
|
||||
end
|
||||
|
||||
def test_decode_utf7
|
||||
s = Net::IMAP.decode_utf7("&,yH,Iv8j-")
|
||||
assert_equal("\357\274\241\357\274\242\357\274\243", s)
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue