PageRenderTime 68ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/ruby/1.9.1/gems/mail-2.4.1/lib/mail/version_specific/ruby_1_8.rb

https://bitbucket.org/mulligan/extractext
Ruby | 98 lines | 79 code | 13 blank | 6 comment | 6 complexity | ee6e8dac855130041e6d9312886554fa MD5 | raw file
Possible License(s): Apache-2.0, MIT, GPL-3.0, GPL-2.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, BSD-2-Clause, JSON
  1. # encoding: utf-8
  2. module Mail
  3. class Ruby18
  4. require 'base64'
  5. # Escapes any parenthesis in a string that are unescaped. This can't
  6. # use the Ruby 1.9.1 regexp feature of negative look behind so we have
  7. # to do two replacement, first unescape everything, then re-escape it
  8. def Ruby18.escape_paren( str )
  9. re = /\\\)/
  10. str = str.gsub(re) { |s| ')'}
  11. re = /\\\(/
  12. str = str.gsub(re) { |s| '('}
  13. re = /([\(\)])/ # Only match unescaped parens
  14. str.gsub(re) { |s| '\\' + s }
  15. end
  16. def Ruby18.paren( str )
  17. str = $1 if str =~ /^\((.*)?\)$/
  18. str = escape_paren( str )
  19. '(' + str + ')'
  20. end
  21. def Ruby18.escape_bracket( str )
  22. re = /\\\>/
  23. str = str.gsub(re) { |s| '>'}
  24. re = /\\\</
  25. str = str.gsub(re) { |s| '<'}
  26. re = /([\<\>])/ # Only match unescaped parens
  27. str.gsub(re) { |s| '\\' + s }
  28. end
  29. def Ruby18.bracket( str )
  30. str = $1 if str =~ /^\<(.*)?\>$/
  31. str = escape_bracket( str )
  32. '<' + str + '>'
  33. end
  34. def Ruby18.decode_base64(str)
  35. Base64.decode64(str) if str
  36. end
  37. def Ruby18.encode_base64(str)
  38. Base64.encode64(str)
  39. end
  40. def Ruby18.has_constant?(klass, string)
  41. klass.constants.include?( string )
  42. end
  43. def Ruby18.get_constant(klass, string)
  44. klass.const_get( string )
  45. end
  46. def Ruby18.b_value_encode(str, encoding)
  47. # Ruby 1.8 requires an encoding to work
  48. raise ArgumentError, "Must supply an encoding" if encoding.nil?
  49. encoding = encoding.to_s.upcase.gsub('_', '-')
  50. [Encodings::Base64.encode(str), encoding]
  51. end
  52. def Ruby18.b_value_decode(str)
  53. match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m)
  54. if match
  55. encoding = match[1]
  56. str = Ruby18.decode_base64(match[2])
  57. end
  58. str
  59. end
  60. def Ruby18.q_value_encode(str, encoding)
  61. # Ruby 1.8 requires an encoding to work
  62. raise ArgumentError, "Must supply an encoding" if encoding.nil?
  63. encoding = encoding.to_s.upcase.gsub('_', '-')
  64. [Encodings::QuotedPrintable.encode(str), encoding]
  65. end
  66. def Ruby18.q_value_decode(str)
  67. match = str.match(/\=\?(.+)?\?[Qq]\?(.+)?\?\=/m)
  68. if match
  69. encoding = match[1]
  70. str = Encodings::QuotedPrintable.decode(match[2].gsub(/_/, '=20'))
  71. end
  72. str
  73. end
  74. def Ruby18.param_decode(str, encoding)
  75. URI.unescape(str)
  76. end
  77. def Ruby18.param_encode(str)
  78. encoding = $KCODE.to_s.downcase
  79. language = Configuration.instance.param_encode_language
  80. "#{encoding}'#{language}'#{URI.escape(str)}"
  81. end
  82. end
  83. end