|
18 | 18 |
|
19 | 19 | #include "builtin/Array.h"
|
20 | 20 | #include "builtin/intl/CommonFunctions.h"
|
| 21 | +#include "builtin/intl/StringAsciiChars.h" |
21 | 22 | #include "gc/AllocKind.h"
|
22 | 23 | #include "gc/GCContext.h"
|
23 | 24 | #include "icu4x/GraphemeClusterSegmenter.hpp"
|
| 25 | +#include "icu4x/Locale.hpp" |
24 | 26 | #include "icu4x/SentenceSegmenter.hpp"
|
25 | 27 | #include "icu4x/WordSegmenter.hpp"
|
26 | 28 | #include "js/CallArgs.h"
|
@@ -400,7 +402,7 @@ struct WordSegmenter {
|
400 | 402 | SegmenterBreakIteratorType<WordSegmenterBreakIteratorTwoByte>;
|
401 | 403 |
|
402 | 404 | static constexpr auto& create =
|
403 |
| - icu4x::capi::icu4x_WordSegmenter_create_auto_mv1; |
| 405 | + icu4x::capi::icu4x_WordSegmenter_create_auto_with_content_locale_mv1; |
404 | 406 | static constexpr auto& destroy = icu4x::capi::icu4x_WordSegmenter_destroy_mv1;
|
405 | 407 | };
|
406 | 408 |
|
@@ -444,19 +446,74 @@ struct SentenceSegmenter {
|
444 | 446 | SegmenterBreakIteratorType<SentenceSegmenterBreakIteratorTwoByte>;
|
445 | 447 |
|
446 | 448 | static constexpr auto& create =
|
447 |
| - icu4x::capi::icu4x_SentenceSegmenter_create_mv1; |
| 449 | + icu4x::capi::icu4x_SentenceSegmenter_create_with_content_locale_mv1; |
448 | 450 | static constexpr auto& destroy =
|
449 | 451 | icu4x::capi::icu4x_SentenceSegmenter_destroy_mv1;
|
450 | 452 | };
|
451 | 453 |
|
| 454 | +class ICU4XLocaleDeleter { |
| 455 | + public: |
| 456 | + void operator()(icu4x::capi::Locale* ptr) { |
| 457 | + icu4x::capi::icu4x_Locale_destroy_mv1(ptr); |
| 458 | + } |
| 459 | +}; |
| 460 | + |
| 461 | +using UniqueICU4XLocale = |
| 462 | + mozilla::UniquePtr<icu4x::capi::Locale, ICU4XLocaleDeleter>; |
| 463 | + |
| 464 | +static UniqueICU4XLocale CreateICU4XLocale(JSContext* cx, |
| 465 | + Handle<JSString*> str) { |
| 466 | + auto* linear = str->ensureLinear(cx); |
| 467 | + if (!linear) { |
| 468 | + return nullptr; |
| 469 | + } |
| 470 | + |
| 471 | + icu4x::capi::icu4x_Locale_from_string_mv1_result result{}; |
| 472 | + { |
| 473 | + intl::StringAsciiChars chars(linear); |
| 474 | + if (!chars.init(cx)) { |
| 475 | + return nullptr; |
| 476 | + } |
| 477 | + |
| 478 | + auto span = static_cast<mozilla::Span<const char>>(chars); |
| 479 | + result = |
| 480 | + icu4x::capi::icu4x_Locale_from_string_mv1({span.data(), span.size()}); |
| 481 | + } |
| 482 | + |
| 483 | + if (!result.is_ok) { |
| 484 | + intl::ReportInternalError(cx); |
| 485 | + return nullptr; |
| 486 | + } |
| 487 | + return UniqueICU4XLocale{result.ok}; |
| 488 | +} |
| 489 | + |
452 | 490 | /**
|
453 |
| - * Create a new ICU4X segmenter instance. |
| 491 | + * Create a new, locale-invariant ICU4X segmenter instance. |
454 | 492 | */
|
455 | 493 | template <typename Interface>
|
456 | 494 | static typename Interface::Segmenter* CreateSegmenter() {
|
457 | 495 | return Interface::create();
|
458 | 496 | }
|
459 | 497 |
|
| 498 | +/** |
| 499 | + * Create a new ICU4X segmenter instance, tailored for |locale|. |
| 500 | + */ |
| 501 | +template <typename Interface> |
| 502 | +static typename Interface::Segmenter* CreateSegmenter( |
| 503 | + JSContext* cx, Handle<JSString*> locale) { |
| 504 | + auto loc = CreateICU4XLocale(cx, locale); |
| 505 | + if (!loc) { |
| 506 | + return nullptr; |
| 507 | + } |
| 508 | + |
| 509 | + auto result = Interface::create(loc.get()); |
| 510 | + if (!result.is_ok) { |
| 511 | + intl::ReportInternalError(cx); |
| 512 | + return nullptr; |
| 513 | + } |
| 514 | + return result.ok; |
| 515 | +} |
| 516 | + |
460 | 517 | static bool EnsureInternalsResolved(JSContext* cx,
|
461 | 518 | Handle<SegmenterObject*> segmenter) {
|
462 | 519 | if (segmenter->getLocale()) {
|
@@ -506,15 +563,15 @@ static bool EnsureInternalsResolved(JSContext* cx,
|
506 | 563 | break;
|
507 | 564 | }
|
508 | 565 | case SegmenterGranularity::Word: {
|
509 |
| - auto* seg = CreateSegmenter<WordSegmenter>(); |
| 566 | + auto* seg = CreateSegmenter<WordSegmenter>(cx, locale); |
510 | 567 | if (!seg) {
|
511 | 568 | return false;
|
512 | 569 | }
|
513 | 570 | segmenter->setSegmenter(seg);
|
514 | 571 | break;
|
515 | 572 | }
|
516 | 573 | case SegmenterGranularity::Sentence: {
|
517 |
| - auto* seg = CreateSegmenter<SentenceSegmenter>(); |
| 574 | + auto* seg = CreateSegmenter<SentenceSegmenter>(cx, locale); |
518 | 575 | if (!seg) {
|
519 | 576 | return false;
|
520 | 577 | }
|
|
0 commit comments