Class: UTF8Utils::Chars

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/utf8_utils/chars.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bytesObject (readonly)

Returns the value of attribute bytes.



4
5
6
# File 'lib/utf8_utils/chars.rb', line 4

def bytes
  @bytes
end

#positionObject (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

#firstObject



32
33
34
# File 'lib/utf8_utils/chars.rb', line 32

def first
  entries.first
end

#tidy_bytesObject

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_sObject

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