File: string.rb

package info (click to toggle)
ruby-toml-rb 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,864 kB
  • sloc: ruby: 1,398; makefile: 6
file content (68 lines) | stat: -rw-r--r-- 1,476 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module TomlRB
  # Used in primitive.citrus
  module BasicString
    SPECIAL_CHARS = {
      "\\0" => "\0",
      "\\t" => "\t",
      "\\b" => "\b",
      "\\f" => "\f",
      "\\n" => "\n",
      "\\r" => "\r",
      '\\"' => '"',
      "\\\\" => "\\"
    }.freeze

    def value
      aux = TomlRB::BasicString.transform_escaped_chars first.value

      aux[1...-1]
    end

    # Replace the unicode escaped characters with the corresponding character
    # e.g. \u03B4 => ?
    def self.decode_unicode(str)
      [str[2..-1].to_i(16)].pack("U")
    end

    def self.transform_escaped_chars(str)
      str.gsub(/\\(u[\da-fA-F]{4}|U[\da-fA-F]{8}|.)/) do |m|
        if m.size == 2
          SPECIAL_CHARS[m] || parse_error(m)
        else
          decode_unicode(m).force_encoding("UTF-8")
        end
      end
    end

    def self.parse_error(m)
      fail ParseError.new "Escape sequence #{m} is reserved"
    end
  end

  module LiteralString
    def value
      first.value[1...-1]
    end
  end

  module MultilineString
    def value
      return "" if captures[:text].empty?
      aux = captures[:text].first.value

      # Remove spaces on multilined Singleline strings
      aux.gsub!(/\\\r?\n[\n\t\r ]*/, "")

      TomlRB::BasicString.transform_escaped_chars aux
    end
  end

  module MultilineLiteral
    def value
      return "" if captures[:text].empty?
      aux = captures[:text].first.value

      aux.gsub(/\\\r?\n[\n\t\r ]*/, "")
    end
  end
end