module GoogleWebTranslate::StringEscaping
String escaping/unescaping code from syck/encoding.rb
Constants
- ESCAPES
- UNESCAPES
Public Class Methods
escape(value, skip = '')
click to toggle source
Escape unprintable characters such as newlines. @param value [String] The string to escape @param skip [String] Characters to not escape @return [String] The string with special characters escaped.
# File lib/google_web_translate/string_escaping.rb, line 20 def escape(value, skip = '') value.gsub(/\\/, '\\\\\\') .gsub(/"/, '\\"') .gsub(/([\x00-\x1f])/) do skip[$&] || ESCAPES[ $&.unpack('C')[0] ] end end
unescape(value)
click to toggle source
Unescape character escapes such as ānā to their character equivalents. @param value [String] The string to unescape @return [String] The string with special characters unescaped.
# File lib/google_web_translate/string_escaping.rb, line 31 def unescape(value) regex = /\\(?:([nevfbart\\])|0?x([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/ value.gsub(regex) do if Regexp.last_match(3) [Regexp.last_match(3).to_s.hex].pack('U*') elsif Regexp.last_match(2) [Regexp.last_match(2)].pack('H2') else UNESCAPES[Regexp.last_match(1)] end end end
unquote(value)
click to toggle source
# File lib/google_web_translate/string_escaping.rb, line 44 def unquote(value) if value && value[0] == value[-1] && %w[' "].include?(value[0]) value[1...-1] else value end end