Module: EDN::StringTransformer

Defined in:
lib/edn/string_transformer.rb

Constant Summary collapse

UNESCAPE_MAP =

Unescape characters in strings. Borrowed from json-pure gem.

Hash.new { |h, k| h[k] = k.chr }
EMPTY_8BIT_STRING =
''

Class Method Summary collapse

Class Method Details

.parse_string(string) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/edn/string_transformer.rb', line 23

def self.parse_string(string)
  string = string.to_s
  return '' if string.empty?
  string = string.gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c|
    #" Clear messed up syntax highlighting with Emacs.
    if u = UNESCAPE_MAP[$&[1]]
      u
    else # \uXXXX
      bytes = EMPTY_8BIT_STRING.dup
      i = 0
      while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
        bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
        i += 1
      end
      JSON.iconv('utf-8', 'utf-16be', bytes)
    end
  end
  if string.respond_to?(:force_encoding)
    string.force_encoding(::Encoding::UTF_8)
  end
  string
end