Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter-text/extractor.rb

Instance Method Summary collapse

Instance Method Details

#codepoint_lengthObject

Helper function to count the character length by first converting to an array. This is needed because with unicode strings, the return value of length may be incorrect



12
13
14
15
16
17
18
# File 'lib/twitter-text/extractor.rb', line 12

def codepoint_length
  if respond_to? :codepoints
    length
  else
    chars.kind_of?(Enumerable) ? chars.to_a.size : chars.size
  end
end

#to_codepoint_aObject

Helper function to convert this string into an array of unicode code points.



21
22
23
24
25
26
27
28
29
# File 'lib/twitter-text/extractor.rb', line 21

def to_codepoint_a
  @to_codepoint_a ||= if chars.kind_of?(Enumerable)
    chars.to_a
  else
    codepoint_array = []
    0.upto(codepoint_length - 1) { |i| codepoint_array << [chars.slice(i)].pack('U') }
    codepoint_array
  end
end