module ProperProperties::Encoding::Unicode
Module to encode and decode unicode chars @see ProperProperties::Encoding
Constants
- UNICODE_ESCAPE
Escape char for unicode chars @return [String]
- UNICODE_MARKER
Marker for encoded unicode chars @return [Regexp]
Public Class Methods
decode!(text)
click to toggle source
Decodes all unicode chars from escape sequences in place @param text [String] @return [String] The encoded text for chaining
# 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)
click to toggle source
Decodes all unicode chars into escape sequences in place @param text [String] @return [String] The decoded text for chaining
# 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
Private Class Methods
hex(codepoint)
click to toggle source
# File lib/proper_properties/encoding/unicode.rb, line 48 def self.hex(codepoint) hex = codepoint.to_s(16) size = hex.size # padding the hex value for uneven digest if (size % 2) == 1 "0#{hex}" else hex end end
unicode(code)
click to toggle source
# File lib/proper_properties/encoding/unicode.rb, line 44 def self.unicode(code) [code].pack("U") end