Class: ICU::Locale

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Object



13
14
15
16
17
18
# File 'ext/icu/icu_locale.c', line 13

VALUE locale_initialize(VALUE self, VALUE id)
{
    id = rb_str_enc_to_ascii_as_utf8(id);
    rb_iv_set(self, "@id", id);
    return self;
}

Instance Attribute Details

#encObject (readonly)

Returns the value of attribute enc.



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

def enc
  @enc
end

#idObject (readonly) Also known as: to_s

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.availableObject



26
27
28
29
30
31
32
33
34
# File 'ext/icu/icu_locale.c', line 26

VALUE locale_singleton_available(VALUE klass)
{
    int32_t len = uloc_countAvailable();
    VALUE result = rb_ary_new2(len);
    for (int32_t i = 0; i < len; ++i) {
        rb_ary_push(result, locale_new_from_cstr(uloc_getAvailable(i)));
    }
    return result;
}

.defaultObject



42
43
44
45
# File 'ext/icu/icu_locale.c', line 42

VALUE locale_singleton_get_default(VALUE klass)
{
    return locale_singleton_get_default_internal();
}

.default=(val) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'ext/icu/icu_locale.c', line 47

VALUE locale_singleton_set_default(VALUE klass, VALUE val)
{
    val = rb_str_enc_to_ascii_as_utf8(val);
    UErrorCode status = U_ZERO_ERROR;
    uloc_setDefault(RSTRING_PTR(val), &status);
    if (U_FAILURE(status)) {
        icu_rb_raise_icu_error(status);
    }
    return locale_singleton_get_default_internal();
}

.for_language_tag(tag) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'ext/icu/icu_locale.c', line 58

VALUE locale_singleton_for_language_tag(VALUE klass, VALUE tag)
{
    tag = rb_str_enc_to_ascii_as_utf8(tag);
    int32_t buffer_capa = 64;
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_forLanguageTag(RSTRING_PTR(tag),
                                  buffer,
                                  buffer_capa,
                                  NULL /* parsedLength of tag */,
                                  &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE loc = locale_new_from_cstr(buffer);
    char_buffer_free(buffer);

    return loc;
}

.for_lcid(lcid) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/icu/icu_locale.c', line 92

VALUE locale_singleton_for_lcid(VALUE klass, VALUE lcid)
{
    uint32_t host_id = NUM2UINT(lcid);
    int32_t buffer_capa = 64;
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getLocaleForLCID(host_id,
                                    buffer,
                                    buffer_capa,
                                    &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE res = locale_new_from_cstr(buffer);
    char_buffer_free(buffer);
    return res;
}

.iso_countriesObject



124
125
126
127
128
129
130
131
132
133
134
# File 'ext/icu/icu_locale.c', line 124

VALUE locale_singleton_iso_countries(VALUE klass)
{
    const char* const* ary = uloc_getISOCountries();
    VALUE result = rb_ary_new2(250); // the number of countries now is 249.
    while (*ary != NULL) {
        // NUL-terminated C string allocated by ICU, so use the buffer function
        rb_ary_push(result, char_buffer_to_rb_str(*ary));
        ary++;
    }
    return result;
}

.iso_languagesObject



136
137
138
139
140
141
142
143
144
145
146
# File 'ext/icu/icu_locale.c', line 136

VALUE locale_singleton_iso_languages(VALUE klass)
{
    const char* const* ary = uloc_getISOLanguages();
    VALUE result = rb_ary_new2(250); // the number of countries now is 249.
    while (*ary != NULL) {
        // NUL-terminated C string allocated by ICU, so use the buffer function
        rb_ary_push(result, char_buffer_to_rb_str(*ary));
        ary++;
    }
    return result;
}

Instance Method Details

#==(other) ⇒ Object Also known as: ===



5
6
7
# File 'lib/icu/locale.rb', line 5

def ==(other)
  other.is_a?(self.class) && other.id == self.id
end

#base_nameObject



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'ext/icu/icu_locale.c', line 387

VALUE locale_base_name(VALUE self)
{
    int32_t buffer_capa = 64;
    VALUE id = rb_iv_get(self, "@id");
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getBaseName(RSTRING_PTR(id),
                               buffer,
                               buffer_capa,
                               &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE res = char_buffer_to_rb_str(buffer);
    char_buffer_free(buffer);
    return res;
}

#canonical_nameObject



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'ext/icu/icu_locale.c', line 419

VALUE locale_canonical_name(VALUE self)
{
    int32_t buffer_capa = 64;
    VALUE id = rb_iv_get(self, "@id");
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_canonicalize(RSTRING_PTR(id),
                                buffer,
                                buffer_capa,
                                &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE res = char_buffer_to_rb_str(buffer);
    char_buffer_free(buffer);
    return res;
}

#character_orientationObject



600
601
602
603
604
605
606
# File 'ext/icu/icu_locale.c', line 600

VALUE locale_character_orientation(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    UErrorCode status = U_ZERO_ERROR;
    ULayoutType result = uloc_getCharacterOrientation(RSTRING_PTR(id), &status);
    return locale_layout_symbol(result);
}

#countryObject



616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'ext/icu/icu_locale.c', line 616

VALUE locale_country(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    int32_t buffer_capa = 64;
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getCountry(RSTRING_PTR(id),
                              buffer,
                              buffer_capa,
                              &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE loc = char_buffer_to_rb_str(buffer);
    char_buffer_free(buffer);
    return loc;
}

#display_country(*args) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'ext/icu/icu_locale.c', line 193

VALUE locale_display_country(int argc, VALUE* argv, VALUE self)
{
    VALUE display_locale;
    rb_scan_args(argc, argv, "01", &display_locale);
    if (!NIL_P(display_locale)) {
        display_locale = rb_str_enc_to_ascii_as_utf8(display_locale);
    }

    VALUE id = rb_iv_get(self, "@id");
    VALUE buffer = icu_ustring_init_with_capa_enc(64, ICU_RUBY_ENCODING_INDEX);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;

    do {
        len = uloc_getDisplayCountry(RSTRING_PTR(id),
                                     NIL_P(display_locale) ? NULL : RSTRING_PTR(display_locale),
                                     icu_ustring_ptr(buffer),
                                     icu_ustring_capa(buffer),
                                     &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            icu_ustring_resize(buffer, 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(buffer, len);
}

#display_language(*args) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'ext/icu/icu_locale.c', line 226

VALUE locale_display_language(int argc, VALUE* argv, VALUE self)
{
    VALUE display_locale;
    rb_scan_args(argc, argv, "01", &display_locale);
    // if use NULL instead of the default, ICU fails which doesn't align with the doc at 59.1
    display_locale = rb_str_enc_to_ascii_as_utf8((NIL_P(display_locale) ?
                                                      locale_singleton_get_default_internal() :
                                                      display_locale));

    VALUE id = rb_iv_get(self, "@id");
    VALUE buffer = icu_ustring_init_with_capa_enc(64, ICU_RUBY_ENCODING_INDEX);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getDisplayLanguage(RSTRING_PTR(id),
                                      RSTRING_PTR(display_locale),
                                      icu_ustring_ptr(buffer),
                                      icu_ustring_capa(buffer),
                                      &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            icu_ustring_resize(buffer, 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(buffer, len);
}

#display_name(*args) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'ext/icu/icu_locale.c', line 259

VALUE locale_display_name(int argc, VALUE* argv, VALUE self)
{
    VALUE display_locale;
    rb_scan_args(argc, argv, "01", &display_locale);
    if (!NIL_P(display_locale)) {
        display_locale = rb_str_enc_to_ascii_as_utf8(display_locale);
    }

    VALUE id = rb_iv_get(self, "@id");
    VALUE buffer = icu_ustring_init_with_capa_enc(64, ICU_RUBY_ENCODING_INDEX);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getDisplayName(RSTRING_PTR(id),
                                  NIL_P(display_locale) ? NULL : RSTRING_PTR(display_locale),
                                  icu_ustring_ptr(buffer),
                                  icu_ustring_capa(buffer),
                                  &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            icu_ustring_resize(buffer, 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(buffer, len);
}

#display_script(*args) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/icu/icu_locale.c', line 291

VALUE locale_display_script(int argc, VALUE* argv, VALUE self)
{
    VALUE display_locale;
    rb_scan_args(argc, argv, "01", &display_locale);
    if (!NIL_P(display_locale)) {
        display_locale = rb_str_enc_to_ascii_as_utf8(display_locale);
    }

    VALUE id = rb_iv_get(self, "@id");
    VALUE buffer = icu_ustring_init_with_capa_enc(64, ICU_RUBY_ENCODING_INDEX);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getDisplayScript(RSTRING_PTR(id),
                                    NIL_P(display_locale) ? NULL : RSTRING_PTR(display_locale),
                                    icu_ustring_ptr(buffer),
                                    icu_ustring_capa(buffer),
                                    &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            icu_ustring_resize(buffer, 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(buffer, len);
}

#display_variant(*args) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'ext/icu/icu_locale.c', line 323

VALUE locale_display_variant(int argc, VALUE* argv, VALUE self)
{
    VALUE display_locale;
    rb_scan_args(argc, argv, "01", &display_locale);
    if (!NIL_P(display_locale)) {
        display_locale = rb_str_enc_to_ascii_as_utf8(display_locale);
    }

    VALUE id = rb_iv_get(self, "@id");
    VALUE buffer = icu_ustring_init_with_capa_enc(64, ICU_RUBY_ENCODING_INDEX);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getDisplayVariant(RSTRING_PTR(id),
                                     NIL_P(display_locale) ? NULL : RSTRING_PTR(display_locale),
                                     icu_ustring_ptr(buffer),
                                     icu_ustring_capa(buffer),
                                     &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            icu_ustring_resize(buffer, 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(buffer, len);
}

#iso_countryObject



483
484
485
486
487
# File 'ext/icu/icu_locale.c', line 483

VALUE locale_iso_country(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    return rb_str_new_cstr(uloc_getISO3Country(RSTRING_PTR(id)));
}

#iso_languageObject



489
490
491
492
493
# File 'ext/icu/icu_locale.c', line 489

VALUE locale_iso_language(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    return rb_str_new_cstr(uloc_getISO3Language(RSTRING_PTR(id)));
}

#keyword(keyword) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'ext/icu/icu_locale.c', line 495

VALUE locale_keyword(VALUE self, VALUE keyword)
{
    keyword = rb_str_enc_to_ascii_as_utf8(keyword);
    int32_t buffer_capa = 64;
    VALUE id = rb_iv_get(self, "@id");
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getKeywordValue(RSTRING_PTR(id),
                                   RSTRING_PTR(keyword),
                                   buffer,
                                   buffer_capa,
                                   &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE res = char_buffer_to_rb_str(buffer);
    char_buffer_free(buffer);
    return res;
}

#keywordsObject



529
530
531
532
533
534
535
# File 'ext/icu/icu_locale.c', line 529

VALUE locale_keywords(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    UErrorCode status = U_ZERO_ERROR;
    UEnumeration* result = uloc_openKeywords(RSTRING_PTR(id), &status);
    return icu_enum_to_rb_ary(result, status, 3);
}

#languageObject



648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'ext/icu/icu_locale.c', line 648

VALUE locale_language(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    int32_t buffer_capa = 64;
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getLanguage(RSTRING_PTR(id),
                               buffer,
                               buffer_capa,
                               &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE loc = char_buffer_to_rb_str(buffer);
    char_buffer_free(buffer);
    return loc;
}

#language_tag(*args) ⇒ Object



148
149
150
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
181
182
183
184
185
# File 'ext/icu/icu_locale.c', line 148

VALUE locale_language_tag(int argc, VALUE* argv, VALUE self)
{
    VALUE strict;
    rb_scan_args(argc, argv, "01", &strict);
    if (strict != Qtrue) {
        strict = Qfalse;
    }

    VALUE id = rb_iv_get(self, "@id");
    int32_t buffer_capa = 64;
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_toLanguageTag(RSTRING_PTR(id),
                                 buffer,
                                 buffer_capa,
                                 strict == Qtrue ? TRUE : FALSE,
                                 &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE loc = char_buffer_to_rb_str(buffer);
    char_buffer_free(buffer);
    return loc;
}

#lcidObject



187
188
189
190
191
# File 'ext/icu/icu_locale.c', line 187

VALUE locale_lcid(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    return ULONG2NUM(uloc_getLCID(RSTRING_PTR(id)));
}

#line_orientationObject



608
609
610
611
612
613
614
# File 'ext/icu/icu_locale.c', line 608

VALUE locale_line_orientation(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    UErrorCode status = U_ZERO_ERROR;
    ULayoutType result = uloc_getLineOrientation(RSTRING_PTR(id), &status);
    return locale_layout_symbol(result);
}

#nameObject



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'ext/icu/icu_locale.c', line 355

VALUE locale_name(VALUE self)
{
    int32_t buffer_capa = 64;
    VALUE id = rb_iv_get(self, "@id");
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getName(RSTRING_PTR(id),
                           buffer,
                           buffer_capa,
                           &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE res = char_buffer_to_rb_str(buffer);
    char_buffer_free(buffer);
    return res;
}

#parentObject



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'ext/icu/icu_locale.c', line 451

VALUE locale_parent(VALUE self)
{
    int32_t buffer_capa = 64;
    VALUE id = rb_iv_get(self, "@id");
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getParent(RSTRING_PTR(id),
                             buffer,
                             buffer_capa,
                             &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE res = char_buffer_to_rb_str(buffer);
    char_buffer_free(buffer);
    return res;
}

#scriptObject



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'ext/icu/icu_locale.c', line 680

VALUE locale_script(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    int32_t buffer_capa = 64;
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getScript(RSTRING_PTR(id),
                             buffer,
                             buffer_capa,
                             &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE res = char_buffer_to_rb_str(buffer);
    char_buffer_free(buffer);
    return res;
}

#variantObject



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'ext/icu/icu_locale.c', line 712

VALUE locale_variant(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    int32_t buffer_capa = 64;
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_getVariant(RSTRING_PTR(id),
                              buffer,
                              buffer_capa,
                              &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE res = char_buffer_to_rb_str(buffer);
    char_buffer_free(buffer);
    return res;
}

#with_keyword(keyword, value) ⇒ Object

TODO: check the keyword and value



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'ext/icu/icu_locale.c', line 538

VALUE locale_with_keyword(VALUE self, VALUE keyword, VALUE value)
{
    keyword = rb_str_enc_to_ascii_as_utf8(keyword);
    int32_t len_keyword = RSTRING_LENINT(keyword);
    if (len_keyword == 0) {
        rb_raise(rb_eArgError, "invalid value for keyword: %+"PRIsVALUE, self);
    }
    if (!NIL_P(value)) {
        value = rb_str_enc_to_ascii_as_utf8(value);
    }

    VALUE id = rb_iv_get(self, "@id");
    int32_t len_id = RSTRING_LENINT(id);
    int32_t buffer_capa = 64 + len_id + len_keyword + (NIL_P(value) ? 0 : RSTRING_LENINT(value));
    char* buffer = char_buffer_new(buffer_capa);
    memmove(buffer, RSTRING_PTR(id), len_id);
    buffer[len_id] = '\0';

    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t buffer_required;
    do {
        buffer_required = uloc_setKeywordValue(RSTRING_PTR(keyword),
                                               NIL_P(value) ? NULL : RSTRING_PTR(value),
                                               buffer,
                                               buffer_capa,
                                               &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = buffer_required;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    // NUL is inserted by ICU when buffer is enough

    VALUE res = locale_new_from_cstr(buffer);
    char_buffer_free(buffer);
    return res;
}

#with_keywords(keywords) ⇒ Object



12
13
14
15
16
17
# File 'lib/icu/locale.rb', line 12

def with_keywords(keywords)
  keywords.reduce(self) do |locale, (keyword, value)|
    # p locale, keyword, value
    locale.with_keyword(keyword, value)
  end
end

#with_likely_subtagsObject



744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# File 'ext/icu/icu_locale.c', line 744

VALUE locale_with_likely_subtags(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    int32_t buffer_capa = 64;
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_addLikelySubtags(RSTRING_PTR(id),
                                    buffer,
                                    buffer_capa,
                                    &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE res = locale_new_from_cstr(buffer);
    char_buffer_free(buffer);
    return res;
}

#with_minimized_subtagsObject



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'ext/icu/icu_locale.c', line 776

VALUE locale_with_minimized_subtags(VALUE self)
{
    VALUE id = rb_iv_get(self, "@id");
    int32_t buffer_capa = 64;
    char* buffer = char_buffer_new(buffer_capa);
    UErrorCode status = U_ZERO_ERROR;
    int retried = FALSE;
    int32_t len;
    do {
        len = uloc_minimizeSubtags(RSTRING_PTR(id),
                                   buffer,
                                   buffer_capa,
                                   &status);
        if (!retried && status == U_BUFFER_OVERFLOW_ERROR) {
            retried = TRUE;
            buffer_capa = len + RUBY_C_STRING_TERMINATOR_SIZE;
            char_buffer_resize(buffer, buffer_capa);
            status = U_ZERO_ERROR;
        } else if (U_FAILURE(status)) {
            char_buffer_free(buffer);
            icu_rb_raise_icu_error(status);
        } else { // retried == true && U_SUCCESS(status)
            break;
        }
    } while (retried);
    buffer[len] = '\0';

    VALUE res = locale_new_from_cstr(buffer);
    char_buffer_free(buffer);
    return res;
}