Class: Tomlrb::StringUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/tomlrb/string_utils.rb

Constant Summary collapse

SPECIAL_CHARS =
{
  '\\t'  => "\t",
  '\\b'  => "\b",
  '\\f'  => "\f",
  '\\n'  => "\n",
  '\\r'  => "\r",
  '\\"'  => '"',
  '\\\\' => '\\'
}.freeze

Class Method Summary collapse

Class Method Details

.multiline_replacements(str) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/tomlrb/string_utils.rb', line 14

def self.multiline_replacements(str)
  strip_spaces(str).gsub(/\\+\s*\n\s*/) {|matched|
    if matched.match(/\\+/)[0].length.odd?
      matched.gsub(/\\\s*\n\s*/, '')
    else
      matched
    end
  }
end

.replace_escaped_chars(str) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/tomlrb/string_utils.rb', line 24

def self.replace_escaped_chars(str)
  str.gsub(/\\(u[\da-fA-F]{4}|U[\da-fA-F]{8}|.)/) do |m|
    if m.size == 2
      SPECIAL_CHARS[m] || (raise Tomlrb::ParseError.new "Escape sequence #{m} is reserved")
    else
      m[2..-1].to_i(16).chr(Encoding::UTF_8)
    end
  end
end

.strip_spaces(str) ⇒ Object



34
35
36
37
# File 'lib/tomlrb/string_utils.rb', line 34

def self.strip_spaces(str)
  str[0] = '' if str[0] == "\n"
  str
end