Module: ZhongwenTools::Unicode
- Defined in:
- lib/zhongwen_tools/unicode.rb
Class Method Summary collapse
- .ascii?(str) ⇒ Boolean
- .from_codepoint(str) ⇒ Object
- .multibyte?(str) ⇒ Boolean
- .to_codepoint(str) ⇒ Object
Class Method Details
.ascii?(str) ⇒ Boolean
17 18 19 |
# File 'lib/zhongwen_tools/unicode.rb', line 17 def self.ascii?(str) str.chars.to_a.size == str.bytes.to_a.size end |
.from_codepoint(str) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/zhongwen_tools/unicode.rb', line 9 def self.from_codepoint(str) results = (str.split(/\\?u/) - ['']).map do |s| [s.hex].pack("U") end results.join end |
.multibyte?(str) ⇒ Boolean
21 22 23 |
# File 'lib/zhongwen_tools/unicode.rb', line 21 def self.multibyte?(str) !ascii?(str) end |
.to_codepoint(str) ⇒ Object
5 6 7 |
# File 'lib/zhongwen_tools/unicode.rb', line 5 def self.to_codepoint(str) str.chars.map{ |c| "\\u%04x" % c.unpack("U")[0] }.join end |