ruby-changes:23553
From: shugo <ko1@a...>
Date: Wed, 9 May 2012 16:03:31 +0900 (JST)
Subject: [ruby-changes:23553] shugo:r35604 (trunk): * lib/net/imap.rb (decode_utf7, encode_utf7): refactored by
shugo 2012-05-09 16:03:19 +0900 (Wed, 09 May 2012) New Revision: 35604 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35604 Log: * lib/net/imap.rb (decode_utf7, encode_utf7): refactored by Nobuyoshi Nakada, to use String#encode. Modified files: trunk/ChangeLog trunk/lib/net/imap.rb trunk/test/net/imap/test_imap.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 35603) +++ ChangeLog (revision 35604) @@ -1,3 +1,8 @@ +Wed May 9 16:01:38 2012 Shugo Maeda <shugo@r...> + + * lib/net/imap.rb (decode_utf7, encode_utf7): refactored by + Nobuyoshi Nakada, to use String#encode. + Wed May 9 13:26:25 2012 Nobuyoshi Nakada <nobu@r...> * test/rubygems/test_gem_remote_fetcher.rb: skip OpenSSL dependent Index: lib/net/imap.rb =================================================================== --- lib/net/imap.rb (revision 35603) +++ lib/net/imap.rb (revision 35604) @@ -955,27 +955,22 @@ # Net::IMAP does _not_ automatically encode and decode # mailbox names to and from utf7. def self.decode_utf7(s) - return s.gsub(/&(.*?)-/n) { - if $1.empty? - "&" + return s.gsub(/&([^-]+)?-/n) { + if $1 + ($1.tr(",", "/") + "===").unpack("m")[0].encode(Encoding::UTF_8, Encoding::UTF_16BE) else - base64 = $1.tr(",", "/") - x = base64.length % 4 - if x > 0 - base64.concat("=" * (4 - x)) - end - base64.unpack("m")[0].unpack("n*").pack("U*") + "&" end - }.force_encoding("UTF-8") + } end # Encode a string from UTF-8 format to modified UTF-7. def self.encode_utf7(s) - return s.gsub(/(&)|([^\x20-\x7e]+)/u) { + return s.gsub(/(&)|[^\x20-\x7e]+/) { if $1 "&-" else - base64 = [$&.unpack("U*").pack("n*")].pack("m") + base64 = [$&.encode(Encoding::UTF_16BE)].pack("m") "&" + base64.delete("=\n").tr("/", ",") + "-" end }.force_encoding("ASCII-8BIT") Index: test/net/imap/test_imap.rb =================================================================== --- test/net/imap/test_imap.rb (revision 35603) +++ test/net/imap/test_imap.rb (revision 35604) @@ -18,16 +18,26 @@ end def test_encode_utf7 + assert_equal("foo", Net::IMAP.encode_utf7("foo")) + assert_equal("&-", Net::IMAP.encode_utf7("&")) + utf8 = "\357\274\241\357\274\242\357\274\243".force_encoding("UTF-8") s = Net::IMAP.encode_utf7(utf8) - assert_equal("&,yH,Iv8j-".force_encoding("UTF-8"), s) + assert_equal("&,yH,Iv8j-", s) + s = Net::IMAP.encode_utf7("foo&#{utf8}-bar".encode("EUC-JP")) + assert_equal("foo&-&,yH,Iv8j--bar", s) utf8 = "\343\201\202&".force_encoding("UTF-8") s = Net::IMAP.encode_utf7(utf8) - assert_equal("&MEI-&-".force_encoding("UTF-8"), s) + assert_equal("&MEI-&-", s) + s = Net::IMAP.encode_utf7(utf8.encode("EUC-JP")) + assert_equal("&MEI-&-", s) end def test_decode_utf7 + assert_equal("&", Net::IMAP.decode_utf7("&-")) + assert_equal("&-", Net::IMAP.decode_utf7("&--")) + s = Net::IMAP.decode_utf7("&,yH,Iv8j-") utf8 = "\357\274\241\357\274\242\357\274\243".force_encoding("UTF-8") assert_equal(utf8, s) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/