Class: ICU::Normalizer

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'ext/icu/icu_normalizer.c', line 46

VALUE normalizer_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE sym_name;
    VALUE sym_mode;
    rb_scan_args(argc, argv, "02", &sym_name, &sym_mode);
    if (NIL_P(sym_name)) {
        sym_name = ID2SYM(ID_nfc);
    }
    if (NIL_P(sym_mode)) {
        sym_mode = ID2SYM(ID_decompose);
    }
    int mode = UNORM2_DECOMPOSE;
    if (sym_mode == ID2SYM(ID_compose)) {
        mode = UNORM2_COMPOSE;
    }
    GET_NORMALIZER(this);
    this->rb_instance = self;
    this->customized = FALSE;

    UErrorCode status = U_ZERO_ERROR;
    this->service = (UNormalizer2*)unorm2_getInstance(NULL,
                                                      rb_id2name(SYM2ID(sym_name)),
                                                      mode,
                                                      &status);
    if (U_FAILURE(status)) {
        icu_rb_raise_icu_error(status);
    }

    return self;
}

Instance Method Details

#normalize(rb_str) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'ext/icu/icu_normalizer.c', line 77

VALUE normalizer_normalize(VALUE self, VALUE rb_str)
{
    StringValue(rb_str);
    GET_NORMALIZER(this);
    VALUE in = icu_ustring_from_rb_str(rb_str);
    VALUE out = icu_ustring_init_with_capa_enc(RSTRING_LENINT(rb_str) * 2 + RUBY_C_STRING_TERMINATOR_SIZE, ICU_RUBY_ENCODING_INDEX);

    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = unorm2_normalize(this->service,
                               icu_ustring_ptr(in), icu_ustring_len(in),
                               icu_ustring_ptr(out), icu_ustring_capa(out),
                               &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            icu_ustring_resize(out, len + RUBY_C_STRING_TERMINATOR_SIZE);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);

    return icu_ustring_to_rb_enc_str_with_len(out, len);
}