Class: ICU::Collator

Inherits:
Object
  • Object
show all
Defined in:
lib/icu/collator.rb,
ext/icu/icu_collator.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'ext/icu/icu_collator.c', line 40

VALUE collator_initialize(VALUE self, VALUE locale)
{
    StringValue(locale);
    GET_COLLATOR(this);

    this->enc_idx = 0;
    this->rb_instance = self;
    UErrorCode status = U_ZERO_ERROR;
    this->service = ucol_open(StringValueCStr(locale), &status);
    if (U_FAILURE(status)) {
        icu_rb_raise_icu_error(status);
    }

    return self;
}

Class Method Details

.sort(locale, strings) ⇒ Object



3
4
5
6
# File 'lib/icu/collator.rb', line 3

def self.sort(locale, strings)
  self.new(locale)
      .sort(strings)
end

Instance Method Details

#compare(str_a, str_b) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'ext/icu/icu_collator.c', line 83

VALUE collator_compare(VALUE self, VALUE str_a, VALUE str_b)
{
    StringValue(str_a);
    StringValue(str_b);
    GET_COLLATOR(this);
    UCollationResult result = UCOL_EQUAL;

    if (icu_is_rb_str_as_utf_8(str_a) &&
        icu_is_rb_str_as_utf_8(str_b)) {
        UErrorCode status = U_ZERO_ERROR;
        result = ucol_strcollUTF8(this->service,
                                  RSTRING_PTR(str_a),
                                  RSTRING_LENINT(str_a),
                                  RSTRING_PTR(str_b),
                                  RSTRING_LENINT(str_b),
                                  &status);
        if (U_FAILURE(status)) {
            icu_rb_raise_icu_error(status);
        }
    } else {
        VALUE tmp_a = icu_ustring_from_rb_str(str_a);
        VALUE tmp_b = icu_ustring_from_rb_str(str_b);
        result = ucol_strcoll(this->service,
                              icu_ustring_ptr(tmp_a), icu_ustring_len(tmp_a),
                              icu_ustring_ptr(tmp_b), icu_ustring_len(tmp_b));
    }

    return INT2NUM(result);
}

#equal?(str_a, str_b) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/icu/collator.rb', line 20

def equal?(str_a, str_b)
  compare(str_a, str_b).zero?
end

#greater?(str_a, str_b) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/icu/collator.rb', line 12

def greater?(str_a, str_b)
  compare(str_a, str_b) > 0
end

#greater_or_equal?(str_a, str_b) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/icu/collator.rb', line 16

def greater_or_equal?(str_a, str_b)
  compare(str_a, str_b) >= 0
end

#locale(*args) ⇒ Object

ULOC_ACTUAL_LOCALE

This is locale the data actually comes from.

ULOC_VALID_LOCALE
This is the most specific locale supported by ICU.


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'ext/icu/icu_collator.c', line 61

VALUE collator_locale(int argc, VALUE* argv, VALUE self)
{
    GET_COLLATOR(this);
    VALUE valid;

    rb_scan_args(argc, argv, "01", &valid);
    if (NIL_P(valid)) {
        valid = ID2SYM(ID_valid);
    }

    ULocDataLocaleType type = ULOC_VALID_LOCALE;
    if (SYM2ID(valid) != ID_valid) {
        type = ULOC_ACTUAL_LOCALE;
    }
    UErrorCode status = U_ZERO_ERROR;
    const char* locale_str = ucol_getLocaleByType(this->service, type, &status);
    if (U_FAILURE(status)) {
        icu_rb_raise_icu_error(status);
    }
    return locale_str != NULL ? rb_str_new_cstr(locale_str) : Qnil;
}

#rulesObject



113
114
115
116
117
118
119
120
121
122
# File 'ext/icu/icu_collator.c', line 113

VALUE collator_rules(VALUE self)
{
    GET_COLLATOR(this);
    int32_t len;
    const UChar* res = ucol_getRules(this->service, &len);
    VALUE str = icu_ustring_from_uchar_str(res, len);
    VALUE ret = icu_ustring_to_rb_enc_str(str);
    icu_ustring_clear_ptr(str);
    return ret;
}

#sort(strings) ⇒ Object



8
9
10
# File 'lib/icu/collator.rb', line 8

def sort(strings)
  strings.sort { |a, b| compare(a, b) }
end