Class: Cicu::Ruleset

Inherits:
Object
  • Object
show all
Defined in:
ext/cicu/cicu.c

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'ext/cicu/cicu.c', line 29

static VALUE ruleset_initialize(VALUE self, VALUE rules) {
  UChar uRules[BUF_SIZE];
  UErrorCode status = U_ZERO_ERROR;
  int32_t uRulesLen = 0;
  UCollator *collator;

  if (TYPE(rules) != T_STRING) {
    rb_raise(rb_eTypeError, "rules must be a string");
  }
  if (RSTRING_LEN(rules) > BUF_SIZE) {
    rb_raise(rb_eArgError, "given ruleset string exceeds buffer size");
  }

  str_to_utf16(rules, uRules, &uRulesLen);
  collator = ucol_openRules(uRules, uRulesLen, UCOL_ON, UCOL_DEFAULT_STRENGTH, NULL, &status); 

  if (U_SUCCESS(status)) {
    DATA_PTR(self) = collator;
  }
  else {
    rb_raise(rb_eArgError, "Failed to initialize ICU ruleset");
  }

  return self;
}

Instance Method Details

#sort_key(string) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/cicu/cicu.c', line 55

static VALUE ruleset_sort_key(VALUE self, VALUE string) {
  UCollator *collator;
  char str[BUF_SIZE];
  UChar ustr[BUF_SIZE];
  int32_t len = 0;
  int32_t uLen = 0;

  if (TYPE(string) != T_STRING) {
    rb_raise(rb_eTypeError, "sort_key expects a string argument");
  }
  if (RSTRING_LEN(string) > BUF_SIZE) {
    rb_raise(rb_eArgError, "given string for sort_key exceeds buffer size");
  }

  Data_Get_Struct(self, UCollator, collator);
  str_to_utf16(string, ustr, &uLen);
  len = ucol_getSortKey(collator, ustr, uLen, (uint8_t*)str, BUF_SIZE);

  return rb_str_new(str, len-1);
}