Module: JavaProperties::Encoding::SpecialChars

Defined in:
lib/java-properties/encoding/special_chars.rb

Overview

Module to escape and unescape special chars

Constant Summary collapse

ESCAPING =

Lookup table for escaping special chars

Returns:

  • (Hash)
{
  "\t" => '\\t',
  "\r" => '\\r',
  "\n" => '\\n',
  "\f" => '\\f'
}.freeze
DESCAPING =

Lookup table to remove escaping from special chars

Returns:

  • (Hash)
ESCAPING.invert.freeze
DESCAPING_MARKER =

Marks a segment which has is an encoding special char

Returns:

  • (Regexp)
/\\./

Class Method Summary collapse

Class Method Details

.decode!(text) ⇒ String

Decodes the content a text by removing all escaping from special chars

Parameters:

  • text (String)

Returns:

  • (String)


38
39
40
41
42
43
# File 'lib/java-properties/encoding/special_chars.rb', line 38

def self.decode!(text)
  text.gsub!(DESCAPING_MARKER) do |match|
    DESCAPING.fetch(match, match)
  end
  text
end

.encode!(text) ⇒ String

Encodes the content a text by escaping all special chars

Parameters:

  • text (String)

Returns:

  • (String)


26
27
28
29
30
31
32
33
# File 'lib/java-properties/encoding/special_chars.rb', line 26

def self.encode!(text)
  buffer = StringIO.new
  text.each_char do |char|
    buffer << ESCAPING.fetch(char, char)
  end
  text.replace buffer.string
  text
end