Class: ICU::Collation::Collator

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-icu/collation.rb

Constant Summary collapse

ULOC_VALID_LOCALE =
1

Instance Method Summary collapse

Constructor Details

#initialize(locale) ⇒ Collator

Returns a new instance of Collator.



64
65
66
67
# File 'lib/ffi-icu/collation.rb', line 64

def initialize(locale)
  ptr = Lib.check_error { |error| Lib.ucol_open(locale, error) }
  @c = FFI::AutoPointer.new(ptr, Lib.method(:ucol_close))
end

Instance Method Details

#[](attribute) ⇒ Object



128
129
130
131
132
# File 'lib/ffi-icu/collation.rb', line 128

def [](attribute)
  ATTRIBUTE_VALUES_INVERSE[Lib.check_error do |error|
    Lib.ucol_getAttribute(@c, ATTRIBUTES[attribute], error)
  end]
end

#[]=(attribute, value) ⇒ Object



134
135
136
137
138
139
# File 'lib/ffi-icu/collation.rb', line 134

def []=(attribute, value)
  Lib.check_error do |error|
    Lib.ucol_setAttribute(@c, ATTRIBUTES[attribute], ATTRIBUTE_VALUES[value], error)
  end
  value
end

#collate(sortable) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/ffi-icu/collation.rb', line 104

def collate(sortable)
  unless sortable.respond_to?(:sort)
    raise ArgumentError, "argument must respond to :sort with arity of 2"
  end

  sortable.sort { |a, b| compare a, b }
end

#collation_key(string) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/ffi-icu/collation.rb', line 120

def collation_key(string)
  ptr = UCharPointer.from_string(string)
  size = Lib.ucol_getSortKey(@c, ptr, string.jlength, nil, 0)
  buffer = FFI::MemoryPointer.new(:char, size)
  Lib.ucol_getSortKey(@c, ptr, string.jlength, buffer, size)
  buffer.read_bytes(size - 1)
end

#compare(a, b) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/ffi-icu/collation.rb', line 73

def compare(a, b)
  Lib.ucol_strcoll(
    @c,
    UCharPointer.from_string(a), a.jlength,
    UCharPointer.from_string(b), b.jlength
  )
end

#equal?(*args) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ffi-icu/collation.rb', line 91

def equal?(*args)
  return super() if args.empty?

  if args.size != 2
    raise ArgumentError, "wrong number of arguments (#{args.size} for 2)"
  end

  a, b = args

  Lib.ucol_equal(@c, UCharPointer.from_string(a), a.jlength,
                     UCharPointer.from_string(b), b.jlength)
end

#greater?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/ffi-icu/collation.rb', line 81

def greater?(a, b)
  Lib.ucol_greater(@c, UCharPointer.from_string(a), a.jlength,
                       UCharPointer.from_string(b), b.jlength)
end

#greater_or_equal?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/ffi-icu/collation.rb', line 86

def greater_or_equal?(a, b)
  Lib.ucol_greaterOrEqual(@c, UCharPointer.from_string(a), a.jlength,
                              UCharPointer.from_string(b), b.jlength)
end

#localeObject



69
70
71
# File 'lib/ffi-icu/collation.rb', line 69

def locale
  Lib.check_error { |error| Lib.ucol_getLocale(@c, ULOC_VALID_LOCALE, error) }
end

#rulesObject



112
113
114
115
116
117
118
# File 'lib/ffi-icu/collation.rb', line 112

def rules
  @rules ||= begin
    length = FFI::MemoryPointer.new(:int)
    ptr = Lib.ucol_getRules(@c, length)
    ptr.read_array_of_uint16(length.read_int).pack("U*")
  end
end