Class: Unichars

Inherits:
Object
  • Object
show all
Defined in:
lib/unichars.rb

Overview

Unichars is a proxy class for String. It wraps a String and implements UTF-8 safe versions of various String operations. Unimplemented methods are forwarded to the wrapped string.

Unichars uses ActiveSupport::Multibyte::Chars as its superclass if it's loaded. Otherwise it will use its own Chars class which is basically a trimmed down version of ActiveSupport's Chars class.

require 'unichars'
Unichars.superclass #=> Chars

require 'active_support'
require 'unichars'
Unichars.superclass #=> ActiveSupport::Multibyte::Chars

Note that all the operations on strings are implemented using Glib2, so the outcome of the methods will be influenced by the Glib2 version installed on your system.

Constant Summary collapse

NORMALIZATION_FORMS =

Valid normalization forms

[:c, :kc, :d, :kd]

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.default_normalization_formObject

Returns the value of attribute default_normalization_form


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

def default_normalization_form
  @default_normalization_form
end

Instance Method Details

#downcaseObject

Returns a Unichars instance with the string in lowercase characters if they are are available for the supplied string.

Unichars.new('ORGANISÉE').downcase.to_s #=> organisée

52
53
54
# File 'lib/unichars.rb', line 52

def downcase
  self.class.new(Glib.utf8_downcase(@wrapped_string))
end

#normalize(form = Unichars.default_normalization_form) ⇒ Object

Returns a Unichars instance with the string in normalize form. See www.unicode.org/reports/tr15/tr15-29.html for more information about normalization.

form can be one of the following: :c, :kc, :d, or :kd.

decomposed = [101, 769].pack('U*')
composed = Unichars.new(decomposed).normalize(:kc)
composed.to_s.unpack('U*') #=> [233]

71
72
73
# File 'lib/unichars.rb', line 71

def normalize(form=Unichars.default_normalization_form)
  self.class.new(Glib.utf8_normalize(@wrapped_string, form))
end

#reverseObject

Returns a Unichars instance with the string in reverse order.

Unichars.new('Comment ça va?').reverse.to_s #=> av aç tnemmoC

59
60
61
# File 'lib/unichars.rb', line 59

def reverse
  self.class.new(Glib.utf8_reverse(@wrapped_string))
end

#sizeObject

Returns the length of the string expressed in codepoints.

Unichars.new('A ehm…, word.').size #=> 13

38
39
40
# File 'lib/unichars.rb', line 38

def size
  Glib.utf8_size(@wrapped_string)
end

#upcaseObject

Returns a Unichars instance with the string in capitals if they are are available for the supplied string.

Unichars.new('Sluß').upcase.to_s #=> SLUSS

45
46
47
# File 'lib/unichars.rb', line 45

def upcase
  self.class.new(Glib.utf8_upcase(@wrapped_string))
end