Module: JavaProperties::Encoding::Unicode

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

Overview

Module to encode and decode unicode chars

Constant Summary collapse

UNICODE_MARKER =

Marker for encoded unicode chars

Returns:

  • (Regexp)
/\\[uU]([0-9a-fA-F]{4,5}|10[0-9a-fA-F]{4})/
UNICODE_ESCAPE =

Escape char for unicode chars

Returns:

  • (String)
"\\u"

Class Method Summary collapse

Class Method Details

.decode!(text) ⇒ String

Decodes all unicode chars from escape sequences in place

Parameters:

  • text (String)

Returns:

  • (String)


17
18
19
20
21
22
# File 'lib/java-properties/encoding/unicode.rb', line 17

def self.decode!(text)
  text.gsub!(UNICODE_MARKER) do
    unicode($1.hex)
  end
  text
end

.encode!(text) ⇒ String

Decodes all unicode chars into escape sequences in place

Parameters:

  • text (String)

Returns:

  • (String)


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

def self.encode!(text)
  buffer = StringIO.new
  text.each_char do |char|
    if char.ascii_only?
      buffer << char
    else
      buffer << UNICODE_ESCAPE
      buffer << hex(char.codepoints.first)
    end
  end
  text.replace buffer.string
  text
end