From: Brian Candler Date: 2003-04-06T17:39:13+09:00 Subject: Re: parsing/decoding mail/news headers On Sun, Apr 06, 2003 at 10:45:02AM +0900, Martin Pirker wrote: > [some hours later] > > my quick&dirty hack: > > def decode(str) > str = str.gsub(/=\?iso-8859-(1|15)\?q\?(.*?)\?=/i){|x| > x.gsub(/=\?iso-8859-(1|15)\?q\?/i,"") [0..-3].unpack("M*").to_s.tr("_"," ")} > str = str.gsub(/=\?iso-8859-(1|15)\?b\?(.*?)\?=/i){|x| > x.gsub(/=\?iso-8859-(1|15)\?b\?/i,"") [0..-3].unpack("m*").to_s.tr("_"," ")} > str > end > > not a perfect conversion, but at least -1 and -15, Q and B cases > are readable and that's good enough for me > > the Ruby experts may suggest now a way to roll both into one ;-) I still don't class myself as a Ruby expert :-) But I do notice the duplicated gsubs which are easy to remove because $2 contains whatever string matched the second parenthesised expression: str = str.gsub(/=\?iso-8859-(1|15)\?q\?(.*?)\?=/i){|x| $2.unpack("M*").to_s.tr("_"," ") } Then you can combine the two if you like by selecting the appropriate value for unpack: RFC2047_UNPACK = { 'q' => 'M*', 'b' => 'm*' } def decode(str) str.gsub(/=\?iso-8859-(1|15)\?(b|q)\?(.*?)\?=/i){|x| $3.unpack(RFC2047_UNPACK[$2.downcase]).to_s.tr("_"," ") } end Regards, Brian.