Module: JavaProperties::Encoding::Separators

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

Overview

Module to escape separators as : or =

Constant Summary collapse

ENCODE_SEPARATOR_MARKER =

Marker for all separators

Returns:

  • (Regexp)
/[ :=]/
ESCAPING_MARKER =

Marker for already escaped separators

Returns:

  • (Regexp)
/\\/
ESCAPE =

Char to use for escaping

Returns:

  • (String)
"\\"
DECODE_SEPARATOR_MARKER =

Marker for all escaped separators

Returns:

  • (Regexp)
/\\([ :=])/

Class Method Summary collapse

Class Method Details

.decode!(text) ⇒ String

Removes escapes from escaped separators

Parameters:

  • text (text)

Returns:

  • (String)

    The unescaped text for chaining


43
44
45
46
47
48
# File 'lib/java-properties/encoding/separators.rb', line 43

def self.decode!(text)
  text.gsub!(DECODE_SEPARATOR_MARKER) do
    $1
  end
  text
end

.encode!(text) ⇒ String

Escapes all not already escaped separators

Parameters:

  • text (text)

Returns:

  • (String)

    The escaped text for chaining


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/java-properties/encoding/separators.rb', line 26

def self.encode!(text)
  buffer = StringIO.new
  last_token = ''
  text.each_char do |char|
    if char =~ ENCODE_SEPARATOR_MARKER && last_token !~ ESCAPING_MARKER
      buffer << ESCAPE
    end
    buffer << char
    last_token = char
  end
  text.replace buffer.string
  text
end