Module: JavaProperties::Encoding::SpecialChars

Defined in:
lib/java_properties/specialchars.rb

Overview

Modules for encoding and decoding special characters for Java properties files.

Class Method Summary collapse

Class Method Details

.decode(string) ⇒ Object

Decodes any X characters read in from a properties file.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/java_properties/specialchars.rb', line 33

def self.decode string
  # Replace \n escaped chars with proper characters
  string.gsub!( /\\(.)/ ) do |c|
    char = $1 if c =~ /\\(.)/
    case char
    when 't'
      "\t"
    when 'n'
      "\n"
    when 'r'
      "\r"
    when 'f'
      "\f"
    else
      char
    end
  end
  string
end

.encode(string) ⇒ Object

Encodes all special characters by replacing them with the proper X escaped value.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/java_properties/specialchars.rb', line 13

def self.encode string
  # Replace special chars with proper \n escaped characters
  string.gsub!( /([\t\n\r\f])/ ) do |c|
    char = $1
    case char
    when "\t"
      '\\t'
    when "\n"
      '\\n'
    when "\r"
      '\\r'
    when "\f"
      '\\f'
    end
  end
  string
end