Class: UTF8Utils::Chars
- Inherits:
-
Object
- Object
- UTF8Utils::Chars
- Includes:
- Enumerable
- Defined in:
- lib/utf8_utils/chars.rb
Instance Attribute Summary collapse
-
#bytes ⇒ Object
readonly
Returns the value of attribute bytes.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
Instance Method Summary collapse
- #first ⇒ Object
-
#initialize(string) ⇒ Chars
constructor
A new instance of Chars.
-
#tidy_bytes ⇒ Object
Attempt to clean up malformed characters.
-
#to_s ⇒ Object
Cast to string.
Constructor Details
#initialize(string) ⇒ Chars
Returns a new instance of Chars.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/utf8_utils/chars.rb', line 9 def initialize(string) @position = 0 begin # Create an array of bytes without raising an ArgumentError in 1.9.x # when the string contains invalid UTF-8 characters @bytes = string.each_byte.map {|b| Byte.new(b)} rescue LocalJumpError # 1.8.6's `each_byte` does not return an Enumerable @bytes = [] string.each_byte { |b| @bytes << Byte.new(b) } end end |
Instance Attribute Details
#bytes ⇒ Object (readonly)
Returns the value of attribute bytes.
4 5 6 |
# File 'lib/utf8_utils/chars.rb', line 4 def bytes @bytes end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
5 6 7 |
# File 'lib/utf8_utils/chars.rb', line 5 def position @position end |
Instance Method Details
#first ⇒ Object
32 33 34 |
# File 'lib/utf8_utils/chars.rb', line 32 def first entries.first end |
#tidy_bytes ⇒ Object
Attempt to clean up malformed characters.
23 24 25 |
# File 'lib/utf8_utils/chars.rb', line 23 def tidy_bytes Chars.new(entries.map {|c| c.tidy.to_s}.compact.join) end |
#to_s ⇒ Object
Cast to string.
28 29 30 |
# File 'lib/utf8_utils/chars.rb', line 28 def to_s entries.flatten.map {|b| b.to_i }.pack("C*").unpack("U*").pack("U*") end |