Method: U::String#casecmp

Defined in:
ext/u/rb_u_string_casecmp.c

#casecmp(other, locale = ENV['LC_COLLATE']) ⇒ Fixnum

Returns the comparison of #foldcase to other#foldcase using the linguistically correct rules of LOCALE. This is, however, only an approximation of a case-insensitive comparison. The LOCALE must be given as a language, region, and encoding, for example, “en_US.UTF-8”.

This operation is known as “collation” and you can find more information about the collation algorithm employed in the Unicode Technical Standard #10, see unicode.org/reports/tr10/.

Parameters:

Returns:

  • (Fixnum)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'ext/u/rb_u_string_casecmp.c', line 31

VALUE
rb_u_string_casecmp(int argc, VALUE *argv, VALUE self)
{
        const char *locale = NULL;

        VALUE rbother, rblocale;
        if (rb_scan_args(argc, argv, "11", &rbother, &rblocale) == 2)
                locale = StringValuePtr(rblocale);

        const struct rb_u_string *string = RVAL2USTRING(self);
        const struct rb_u_string *other = RVAL2USTRING_ANY(rbother);

        char *folded;
        size_t folded_n = foldcase(&folded, string, locale, NULL);

        char *folded_other;
        size_t folded_other_n = foldcase(&folded_other, other, locale, folded);

        errno = 0;
        int r = u_collate(folded, folded_n,
                          folded_other, folded_other_n,
                          locale);

        free(folded_other);
        free(folded);

        if (errno != 0)
                rb_u_raise_errno(errno, "can’t collate strings");

        return INT2FIX(r);
}