Class: ICU::SpoofChecker

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



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

VALUE spoof_checker_initialize(VALUE self)
{
    GET_SPOOF_CHECKER(this);
    this->rb_instance = self;
    this->service = FALSE;

    UErrorCode status = U_ZERO_ERROR;
    this->service = uspoof_open(&status);
    if (U_FAILURE(status)) {
        icu_rb_raise_icu_error(status);
    }

    return self;
}

Class Method Details

.available_checksObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'ext/icu/icu_spoof_checker.c', line 184

VALUE spoof_checker_available_checks(VALUE klass)
{
    VALUE iv = rb_iv_get(klass, k_checks_name);
    if (NIL_P(iv)) {
        iv = rb_hash_new();
        rb_hash_aset(iv, ID2SYM(rb_intern("single_script_confusable")), INT2NUM(USPOOF_SINGLE_SCRIPT_CONFUSABLE));
        rb_hash_aset(iv, ID2SYM(rb_intern("mixed_script_confusable")), INT2NUM(USPOOF_MIXED_SCRIPT_CONFUSABLE));
        rb_hash_aset(iv, ID2SYM(rb_intern("whole_script_confusable")), INT2NUM(USPOOF_WHOLE_SCRIPT_CONFUSABLE));
        rb_hash_aset(iv, ID2SYM(rb_intern("confusable")), INT2NUM(USPOOF_CONFUSABLE));
        // USPOOF_ANY_CASE deprecated in 58
        rb_hash_aset(iv, ID2SYM(rb_intern("restriction_level")), INT2NUM(USPOOF_RESTRICTION_LEVEL));
        // USPOOF_SINGLE_SCRIPT deprecated in 51
        rb_hash_aset(iv, ID2SYM(rb_intern("invisible")), INT2NUM(USPOOF_INVISIBLE));
        rb_hash_aset(iv, ID2SYM(rb_intern("char_limit")), INT2NUM(USPOOF_CHAR_LIMIT));
        rb_hash_aset(iv, ID2SYM(rb_intern("mixed_numbers")), INT2NUM(USPOOF_MIXED_NUMBERS));
        rb_hash_aset(iv, ID2SYM(rb_intern("all_checks")), INT2NUM(USPOOF_ALL_CHECKS));
        rb_hash_aset(iv, ID2SYM(rb_intern("aux_info")), INT2NUM(USPOOF_AUX_INFO));
        rb_iv_set(klass, k_checks_name, iv);
    }
    return iv;
}

.available_restriction_levelsObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'ext/icu/icu_spoof_checker.c', line 208

VALUE spoof_checker_available_restriction_levels(VALUE klass)
{
    VALUE iv = rb_iv_get(klass, k_restriction_level_name);
    if (NIL_P(iv)) {
        iv = rb_hash_new();
        rb_hash_aset(iv, ID2SYM(rb_intern("ascii")), INT2NUM(USPOOF_ASCII));
        rb_hash_aset(iv, ID2SYM(rb_intern("single_script_restrictive")), INT2NUM(USPOOF_SINGLE_SCRIPT_RESTRICTIVE));
        rb_hash_aset(iv, ID2SYM(rb_intern("highly_restrictive")), INT2NUM(USPOOF_HIGHLY_RESTRICTIVE));
        rb_hash_aset(iv, ID2SYM(rb_intern("moderately_restrictive")), INT2NUM(USPOOF_MODERATELY_RESTRICTIVE));
        rb_hash_aset(iv, ID2SYM(rb_intern("minimally_restrictive")), INT2NUM(USPOOF_MINIMALLY_RESTRICTIVE));
        rb_hash_aset(iv, ID2SYM(rb_intern("unrestrictive")), INT2NUM(USPOOF_UNRESTRICTIVE));
        rb_hash_aset(iv, ID2SYM(rb_intern("restriction_level_mask")), INT2NUM(USPOOF_RESTRICTION_LEVEL_MASK));
        rb_hash_aset(iv, ID2SYM(rb_intern("undefined_restrictive")), INT2NUM(USPOOF_UNDEFINED_RESTRICTIVE));
        rb_iv_set(klass, k_restriction_level_name, iv);
    }
    return iv;
}

Instance Method Details

#check(rb_str) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'ext/icu/icu_spoof_checker.c', line 151

VALUE spoof_checker_check(VALUE self, VALUE rb_str)
{
    StringValue(rb_str);
    GET_SPOOF_CHECKER(this);

    UErrorCode status = U_ZERO_ERROR;
    int32_t result = 0;

    // TODO: Migrate to uspoof_check2UTF8 once it's not draft
    if (icu_is_rb_str_as_utf_8(rb_str)) {
       result = uspoof_checkUTF8(this->service,
                                 RSTRING_PTR(rb_str),
                                 RSTRING_LENINT(rb_str),
                                 NULL,
                                 &status);
    } else {
        VALUE in = icu_ustring_from_rb_str(rb_str);
        // TODO: Migrate to uspoof_check once it's not draft
        result = uspoof_check(this->service,
                              icu_ustring_ptr(in),
                              icu_ustring_len(in),
                              NULL,
                              &status);
    }
    if (U_FAILURE(status)) {
        icu_rb_raise_icu_error(status);
    }

    return INT2NUM(result);
}

#checksObject



84
85
86
87
88
# File 'ext/icu/icu_spoof_checker.c', line 84

VALUE spoof_checker_get_checks(VALUE self)
{
    GET_SPOOF_CHECKER(this);
    return spoof_checker_get_checks_internal(this);
}

#checks=(checks) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'ext/icu/icu_spoof_checker.c', line 90

VALUE spoof_checker_set_checks(VALUE self, VALUE checks)
{
    GET_SPOOF_CHECKER(this);

    UErrorCode status = U_ZERO_ERROR;
    uspoof_setChecks(this->service, NUM2INT(checks), &status);
    if (U_FAILURE(status)) {
        icu_rb_raise_icu_error(status);
    }
    return spoof_checker_get_checks_internal(this);
}

#confusable?(str_a, str_b) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'ext/icu/icu_spoof_checker.c', line 102

VALUE spoof_checker_confusable(VALUE self, VALUE str_a, VALUE str_b)
{
    StringValue(str_a);
    StringValue(str_b);
    GET_SPOOF_CHECKER(this);

    VALUE tmp_a = icu_ustring_from_rb_str(str_a);
    VALUE tmp_b = icu_ustring_from_rb_str(str_b);
    UErrorCode status = U_ZERO_ERROR;
    int32_t result = uspoof_areConfusable(this->service,
                                          icu_ustring_ptr(tmp_a),
                                          icu_ustring_len(tmp_a),
                                          icu_ustring_ptr(tmp_b),
                                          icu_ustring_len(tmp_b),
                                          &status);

    return INT2NUM(result);
}

#get_skeleton(str) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'ext/icu/icu_spoof_checker.c', line 121

VALUE spoof_checker_get_skeleton(VALUE self, VALUE str)
{
    StringValue(str);
    GET_SPOOF_CHECKER(this);

    VALUE in = icu_ustring_from_rb_str(str);
    VALUE out = icu_ustring_init_with_capa_enc(icu_ustring_capa(in), ICU_RUBY_ENCODING_INDEX);
    int retried = FALSE;
    int32_t len_bytes;
    UErrorCode status = U_ZERO_ERROR;
    do {
        // UTF-8 version does the conversion internally so we relies on UChar version here!
        len_bytes = uspoof_getSkeleton(this->service, 0 /* deprecated */,
                                       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_bytes + 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_bytes);
}

#restriction_levelObject



61
62
63
64
65
# File 'ext/icu/icu_spoof_checker.c', line 61

VALUE spoof_checker_get_restriction_level(VALUE self)
{
    GET_SPOOF_CHECKER(this);
    return spoof_checker_get_restriction_level_internal(this);
}

#restriction_level=(level) ⇒ Object



67
68
69
70
71
72
# File 'ext/icu/icu_spoof_checker.c', line 67

VALUE spoof_checker_set_restriction_level(VALUE self, VALUE level)
{
    GET_SPOOF_CHECKER(this);
    uspoof_setRestrictionLevel(this->service, NUM2INT(level));
    return spoof_checker_get_restriction_level_internal(this);
}