From 2e63cd97c1a1acf7cd93976c7e7531c46901e1d2 Mon Sep 17 00:00:00 2001 From: shugo Date: Wed, 9 May 2012 07:03:19 +0000 Subject: * lib/net/imap.rb (decode_utf7, encode_utf7): refactored by Nobuyoshi Nakada, to use String#encode. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35604 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/net/imap.rb | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'lib/net') diff --git a/lib/net/imap.rb b/lib/net/imap.rb index 567cf66809..d4336f7ec3 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -955,27 +955,22 @@ module Net # 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") -- cgit v1.2.3