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
/\\[uU]([0-9a-fA-F]{4,5}|10[0-9a-fA-F]{4})/- UNICODE_ESCAPE =
Escape char for unicode chars
"\\u"
Class Method Summary collapse
-
.decode!(text) ⇒ String
Decodes all unicode chars from escape sequences in place.
-
.encode!(text) ⇒ String
Decodes all unicode chars into escape sequences in place.
- .hex(codepoint) ⇒ Object
- .unicode(code) ⇒ Object
Class Method Details
.decode!(text) ⇒ String
Decodes all unicode chars from escape sequences in place
18 19 20 21 22 23 |
# File 'lib/java-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
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/java-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 |
.hex(codepoint) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/java-properties/encoding/unicode.rb', line 48 def self.hex(codepoint) hex = codepoint.to_s(16) size = [4, hex.size].max target_size = size.even? ? size : size+1 hex.rjust(target_size, '0') end |
.unicode(code) ⇒ Object
44 45 46 |
# File 'lib/java-properties/encoding/unicode.rb', line 44 def self.unicode(code) code.chr(::Encoding::UTF_8) end |