Module: GoogleWebTranslate::StringEscaping
- Defined in:
- lib/google_web_translate/string_escaping.rb
Overview
String escaping/unescaping code from syck/encoding.rb
Constant Summary collapse
- ESCAPES =
%w[\x00 \x01 \x02 \x03 \x04 \x05 \x06 \a \x08 \t \n \v \f \r \x0e \x0f \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17 \x18 \x19 \x1a \e \x1c \x1d \x1e \x1f].freeze
- UNESCAPES =
{ 'a' => "\x07", 'b' => "\x08", 't' => "\x09", 'n' => "\x0a", 'v' => "\x0b", 'f' => "\x0c", 'r' => "\x0d", 'e' => "\x1b", '\\' => '\\' }.freeze
Class Method Summary collapse
-
.escape(value, skip = '') ⇒ String
Escape unprintable characters such as newlines.
-
.unescape(value) ⇒ String
Unescape character escapes such as ānā to their character equivalents.
- .unquote(value) ⇒ Object
Class Method Details
.escape(value, skip = '') ⇒ String
Escape unprintable characters such as newlines.
20 21 22 23 24 25 26 |
# 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) ⇒ String
Unescape character escapes such as ānā to their character equivalents.
31 32 33 34 35 36 37 38 39 40 41 42 |
# 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) ⇒ Object
44 45 46 47 48 49 50 |
# 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 |