summaryrefslogtreecommitdiff
path: root/lib/net/imap.rb
diff options
context:
space:
mode:
authorshugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-05-09 07:03:19 +0000
committershugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-05-09 07:03:19 +0000
commit2e63cd97c1a1acf7cd93976c7e7531c46901e1d2 (patch)
tree262f229733c0cb00e9152968cea8c0deebb1924d /lib/net/imap.rb
parente62c35fb8f7bc9fd9bc7e99a603da492fc89d21c (diff)
* 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
Diffstat (limited to 'lib/net/imap.rb')
-rw-r--r--lib/net/imap.rb19
1 files changed, 7 insertions, 12 deletions
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")