Module: WWMD::Encoding

Included in:
String
Defined in:
lib/wwmd/class_extensions/mixins_string_encoding.rb

Overview

This is where character encodings should go as module methods to be used as mixins for the String class

Instance Method Summary collapse

Instance Method Details

#to_utf7(all = nil) ⇒ Object

String.to_utf7 mixin (complete hack but it works)

if all=true, encode all characters. if all.class=Regexp encode only characters in the passed regular expression else default to /[^0-9a-zA-Z]/

used by:

String.to_utf7
String.to_utf7!


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wwmd/class_extensions/mixins_string_encoding.rb', line 20

def to_utf7(all=nil)
  if all.kind_of?(Regexp)
    reg = all
  elsif all.kind_of?(TrueClass)
    reg = ESCAPE[:all]
  else
    reg = ESCAPE[:nalnum] || /[^a-zA-Z0-9]/
  end
  putd "DEBG:" + reg.inspect
  ret = ''
  self.each_byte do |b|
    if b.chr.match(reg)
      ret += "+" + Base64.encode64(b.chr.toutf16)[0..2] + "-"
    else
      ret += b.chr
    end
  end
  return ret
end