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
# File 'lib/tomlrb/string_utils.rb', line 14

def self.multiline_replacements(str)
  strip_spaces(str).gsub(/\\\n\s+/, '')
end

.replace_escaped_chars(str) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/tomlrb/string_utils.rb', line 18

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



28
29
30
31
# File 'lib/tomlrb/string_utils.rb', line 28

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