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
/\\[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.
Class Method Details
.decode!(text) ⇒ String
Decodes all unicode chars from escape sequences in place
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
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 |