From: Dave Burt Date: 2005-03-15T00:41:06+09:00 Subject: Re: [SOLUTION] Quoted Printable (#23) Hi, Testing. I found building a test suite before doing the code really helpful on this one, to get my head around the intricacies of the encoding. Actually thinking through the edge cases and working out expected results was necessary for me to develop this solution. Now, of course, this would have been a lot easier if I'd just been able to find the "builtin quoted printable facilities." What builtin quoted printable facilities? Anyway, here is my result: http://www.dave.burt.id.au/ruby/quoted-printable.rb And the tester: http://www.dave.burt.id.au/ruby/test-quoted-printable.rb The testing program generates test methods and test data dynamically. The public interface to my solution looks like this: module QuotedPrintable WHITESPACE = [?\t, ?\ ] WHITESPACE_REGEXP = /[\t ]/ WHITESPACE_ESCAPED_REGEXP = /=09|=20/ # bytes that do not need to be escaped PRINTABLES = ((?!..?~).to_a + WHITESPACE) - [?=] MAX_LINE_WIDTH = 76 NEWLINE = "\r\n" # additional bytes to escape for safety in an EBCDIC document EBCDIC_EXCEPTIONS = %w' ! " # $ @ [ \ ] ^ ` { | } ~ ' EBCDIC_PRINTABLES = PRINTABLES - EBCDIC_EXCEPTIONS # additional bytes to escape for safety in an XML document XML_EXCEPTIONS = %w' < > & ' XML_PRINTABLES = PRINTABLES - XML_EXCEPTIONS # Encode self to the quoted-printable transfer encoding def to_quoted_printable(printables = QuotedPrintable::PRINTABLES) # Decode self from the quoted-printable transfer encoding def from_quoted_printable # Functions that do quoted-printable encoding and decoding class << self # Return the quoted-printable escaped representation of the given byte # (byte must be a Fixnum between 0 and 255) def encode_byte(byte) # Return the byte corresponding to the given quoted-printable escape # sequence as a String. If it's not valid, return nil. def decode_sequence(escape_sequence) # Return the given string encoded as quoted-printable, including the # canonical \r\n line terminators. def encode_string(string, printables = PRINTABLES) # Consider the given string quoted-printable encoded, and decode it, # including translating line terminators to the native default. def decode_string(string) # Add quoted-printable conversions to String class String include QuotedPrintable # to_quoted_printable, from_quoted_printable end Cheers, Dave