Module: ProperProperties::Encoding::Unicode

Defined in:
lib/proper_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)

    The encoded text for chaining



18
19
20
21
22
23
# File 'lib/proper_properties/encoding/unicode.rb', line 18

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)

    The decoded text for chaining



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/proper_properties/encoding/unicode.rb', line 28

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