Debian Codegraph @

intl/icu/source/i18n/messageformat2_data_model.cpp

raw
1 // © 2024 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include "unicode/utypes.h"
5
6 #if !UCONFIG_NO_NORMALIZATION
7
8 #if !UCONFIG_NO_FORMATTING
9
10 #if !UCONFIG_NO_MF2
11
12 #include "unicode/messageformat2_data_model.h"
13 #include "messageformat2_allocation.h"
14 #include "messageformat2_macros.h"
15 #include "uvector.h"
16
17 U_NAMESPACE_BEGIN
18
19 namespace message2 {
20
21 // Implementation
22
23 //------------------ SelectorKeys
24
25 const Key* SelectorKeys::getKeysInternal() const {
26 return keys.getAlias();
27 }
28
29 // Lexically order key lists
30 bool SelectorKeys::operator<(const SelectorKeys& other) const {
31 // Handle key lists of different sizes first --
32 // this case does have to be handled (even though it would
33 // reflect a data model error) because of the need to produce
34 // partial output
35 if (len < other.len) {
36 return true;
37 }
38 if (len > other.len) {
39 return false;
40 }
41
42 for (int32_t i = 0; i < len; i++) {
43 if (keys[i] < other.keys[i]) {
44 return true;
45 }
46 if (!(keys[i] == other.keys[i])) {
47 return false;
48 }
49 }
50 // If we've reached here, all keys must be equal
51 return false;
52 }
53
54 SelectorKeys::Builder::Builder(UErrorCode& status) {
55 keys = createUVector(status);
56 }
57
58 SelectorKeys::Builder& SelectorKeys::Builder::add(Key&& key, UErrorCode& status) noexcept {
59 U_ASSERT(keys != nullptr);
60 if (U_SUCCESS(status)) {
61 Key* k = create<Key>(std::move(key), status);
62 keys->adoptElement(k, status);
63 }
64 return *this;
65 }
66
67 SelectorKeys SelectorKeys::Builder::build(UErrorCode& status) const {
68 if (U_FAILURE(status)) {
69 return {};
70 }
71 U_ASSERT(keys != nullptr);
72 return SelectorKeys(*keys, status);
73 }
74
75 SelectorKeys::Builder::~Builder() {
76 if (keys != nullptr) {
77 delete keys;
78 }
79 }
80
81 SelectorKeys::SelectorKeys(const UVector& ks, UErrorCode& status) : len(ks.size()) {
82 Key* result = copyVectorToArray<Key>(ks, status);
83 if (U_FAILURE(status)) {
84 return;
85 }
86 keys.adoptInstead(result);
87 }
88
89 SelectorKeys& SelectorKeys::operator=(SelectorKeys other) noexcept {
90 swap(*this, other);
91 return *this;
92 }
93
94 SelectorKeys::SelectorKeys(const SelectorKeys& other) : len(other.len) {
95 UErrorCode localErrorCode = U_ZERO_ERROR;
96 if (len != 0) {
97 keys.adoptInstead(copyArray(other.keys.getAlias(), len, localErrorCode));
98 }
99 if (U_FAILURE(localErrorCode)) {
100 len = 0;
101 }
102 }
103
104 SelectorKeys::~SelectorKeys() {
105 len = 0;
106 }
107
108 //------------------ Literal
109
110 bool Literal::operator<(const Literal& other) const {
111 // Ignore quoting for the purposes of ordering
112 return contents < other.contents;
113 }
114
115 bool Literal::operator==(const Literal& other) const {
116 // Ignore quoting for the purposes of ordering
117 return contents == other.contents;
118 }
119
120 UnicodeString Literal::quoted() const {
121 UnicodeString result(PIPE);
122 result += unquoted();
123 result += PIPE;
124 return result;
125 }
126
127 const UnicodeString& Literal::unquoted() const { return contents; }
128
129 Literal& Literal::operator=(Literal other) noexcept {
130 swap(*this, other);
131
132 return *this;
133 }
134
135 Literal::~Literal() {
136 thisIsQuoted = false;
137 }
138
139 //------------------ Operand
140
141 Operand::Operand(const Operand& other) : contents(other.contents) {}
142
143 Operand& Operand::operator=(Operand other) noexcept {
144 swap(*this, other);
145
146 return *this;
147 }
148
149 UBool Operand::isVariable() const {
150 return (contents.has_value() && std::holds_alternative<VariableName>(*contents));
151 }
152 UBool Operand::isLiteral() const {
153 return (contents.has_value() && std::holds_alternative<Literal>(*contents));
154 }
155 UBool Operand::isNull() const { return !contents.has_value(); }
156
157 const Literal& Operand::asLiteral() const {
158 U_ASSERT(isLiteral());
159 return *(std::get_if<Literal>(&(*contents)));
160 }
161
162 const VariableName& Operand::asVariable() const {
163 U_ASSERT(isVariable());
164 return *(std::get_if<VariableName>(&(*contents)));
165 }
166
167 Operand::~Operand() {}
168
169 //---------------- Key
170
171 Key& Key::operator=(Key other) noexcept {
172 swap(*this, other);
173 return *this;
174 }
175
176 bool Key::operator<(const Key& other) const {
177 // Arbitrarily treat * as greater than all concrete keys
178 if (isWildcard()) {
179 return false;
180 }
181 if (other.isWildcard()) {
182 return true;
183 }
184 return (asLiteral() < other.asLiteral());
185 }
186
187 bool Key::operator==(const Key& other) const {
188 if (isWildcard()) {
189 return other.isWildcard();
190 }
191 if (other.isWildcard()) {
192 return false;
193 }
194 return (asLiteral() == other.asLiteral());
195 }
196
197 const Literal& Key::asLiteral() const {
198 U_ASSERT(!isWildcard());
199 return *contents;
200 }
201
202 Key::~Key() {}
203
204 //------------------------ Operator
205
206 OptionMap::OptionMap(const UVector& opts, UErrorCode& status) : len(opts.size()) {
207 Option* result = copyVectorToArray<Option>(opts, status);
208 if (U_FAILURE(status)) {
209 bogus = true;
210 return;
211 }
212 options.adoptInstead(result);
213 bogus = false;
214 }
215
216 OptionMap::OptionMap(const OptionMap& other) : len(other.len) {
217 U_ASSERT(!other.bogus);
218 if (len == 0) {
219 bogus = false;
220 return;
221 }
222 UErrorCode localErrorCode = U_ZERO_ERROR;
223 Option* result = copyArray(other.options.getAlias(), len, localErrorCode);
224 if (U_FAILURE(localErrorCode)) {
225 bogus = true;
226 return;
227 }
228 bogus = false;
229 options.adoptInstead(result);
230 }
231
232 OptionMap& OptionMap::operator=(OptionMap other) {
233 swap(*this, other);
234 return *this;
235 }
236
237 const Option& OptionMap::getOption(int32_t i, UErrorCode& status) const {
238 if (U_FAILURE(status) || bogus) {
239 if (bogus) {
240 status = U_MEMORY_ALLOCATION_ERROR;
241 }
242 } else {
243 U_ASSERT(options.isValid());
244 U_ASSERT(i < len);
245 }
246 return options[i];
247 }
248
249 int32_t OptionMap::size() const {
250 U_ASSERT(options.isValid() || len == 0);
251 return len;
252 }
253
254 OptionMap::~OptionMap() {}
255
256 OptionMap OptionMap::Builder::build(UErrorCode& status) {
257 return OptionMap(*options, status);
258 }
259
260 OptionMap::Builder::Builder(UErrorCode& status) {
261 options = createStringUVector(status);
262 }
263
264 OptionMap::Builder::Builder(OptionMap::Builder&& other) {
265 checkDuplicates = other.checkDuplicates;
266 options = other.options;
267 other.options = nullptr;
268 }
269
270 OptionMap::Builder& OptionMap::Builder::operator=(OptionMap::Builder other) noexcept {
271 swap(*this, other);
272 return *this;
273 }
274
275 /* static */ OptionMap::Builder OptionMap::Builder::attributes(UErrorCode& status) {
276 Builder b(status);
277 // The same code is re-used for representing attributes and options.
278 // Duplicate attributes are allowed, while duplicate options are disallowed.
279 b.checkDuplicates = false;
280 return b;
281 }
282
283 static UBool hasOptionNamed(const UVector& v, const UnicodeString& s) {
284 for (int32_t i = 0; i < v.size(); i++) {
285 const Option* opt = static_cast<Option*>(v[i]);
286 U_ASSERT(opt != nullptr);
287 if (opt->getName() == s) {
288 return true;
289 }
290 }
291 return false;
292 }
293
294 OptionMap::Builder& OptionMap::Builder::add(Option&& opt, UErrorCode& status) {
295 THIS_ON_ERROR(status);
296
297 // If the option name is already in the map, emit a data model error
298 if (checkDuplicates && hasOptionNamed(*options, opt.getName())) {
299 status = U_MF_DUPLICATE_OPTION_NAME_ERROR;
300 } else {
301 Option* newOption = create<Option>(std::move(opt), status);
302 options->adoptElement(newOption, status);
303 }
304 return *this;
305 }
306
307 OptionMap::Builder::~Builder() {
308 if (options != nullptr) {
309 delete options;
310 }
311 }
312
313 const OptionMap& Operator::getOptionsInternal() const {
314 return options;
315 }
316
317 Option::Option(const Option& other): name(other.name), rand(other.rand) {}
318
319 Option& Option::operator=(Option other) noexcept {
320 swap(*this, other);
321 return *this;
322 }
323
324 Option::~Option() {}
325
326 Operator::Builder::Builder(UErrorCode& status) : options(OptionMap::Builder(status)) {}
327
328 Operator::Builder& Operator::Builder::setFunctionName(FunctionName&& func) {
329 functionName = std::move(func);
330 return *this;
331 }
332
333 const FunctionName& Operator::getFunctionName() const {
334 return name;
335 }
336
337 Operator::Builder& Operator::Builder::addOption(const UnicodeString &key, Operand&& value, UErrorCode& errorCode) noexcept {
338 THIS_ON_ERROR(errorCode);
339
340 options.add(Option(key, std::move(value)), errorCode);
341 return *this;
342 }
343
344 Operator Operator::Builder::build(UErrorCode& errorCode) {
345 return Operator(functionName, options.build(errorCode));
346 }
347
348 Operator::Operator(const Operator& other) noexcept
349 : name(other.name), options(other.options) {}
350
351 Operator& Operator::operator=(Operator other) noexcept {
352 swap(*this, other);
353 return *this;
354 }
355
356 // Function call
357
358 Operator::Operator(const FunctionName& f, const OptionMap& opts) : name(f), options(opts) {}
359
360 Operator::Builder::~Builder() {}
361
362 Operator::~Operator() {}
363
364 // ------------ Markup
365
366 Markup::Builder::Builder(UErrorCode& status)
367 : options(OptionMap::Builder(status)), attributes(OptionMap::Builder::attributes(status)) {}
368
369 Markup::Markup(UMarkupType ty, UnicodeString n, OptionMap&& o, OptionMap&& a)
370 : type(ty), name(n), options(std::move(o)), attributes(std::move(a)) {}
371
372 Markup::Builder& Markup::Builder::addOption(const UnicodeString &key,
373 Operand&& value,
374 UErrorCode& errorCode) {
375 options.add(Option(key, std::move(value)), errorCode);
376 return *this;
377 }
378
379 Markup::Builder& Markup::Builder::addAttribute(const UnicodeString &key,
380 Operand&& value,
381 UErrorCode& errorCode) {
382 attributes.add(Option(key, std::move(value)), errorCode);
383 return *this;
384 }
385
386 Markup Markup::Builder::build(UErrorCode& errorCode) {
387 Markup result;
388
389 if (U_FAILURE(errorCode)) {
390 return result;
391 }
392
393 if (type == UMARKUP_COUNT || name.length() == 0) {
394 // One of `setOpen()`, `setClose()`, or `setStandalone()`
395 // must be called before calling build()
396 // setName() must be called before calling build()
397 errorCode = U_INVALID_STATE_ERROR;
398 } else {
399 result = Markup(type,
400 name,
401 options.build(errorCode),
402 attributes.build(errorCode));
403 }
404 return result;
405 }
406
407 Markup::Builder::~Builder() {}
408
409 Markup::~Markup() {}
410
411 // ------------ Expression
412
413 Expression::Builder::Builder(UErrorCode& status)
414 : attributes(OptionMap::Builder::attributes(status)) {}
415
416 UBool Expression::isStandaloneAnnotation() const {
417 return rand.isNull();
418 }
419
420 // Returns true for function calls with operands as well as
421 // standalone annotations.
422 UBool Expression::isFunctionCall() const {
423 return rator.has_value();
424 }
425
426 const Operator* Expression::getOperator(UErrorCode& status) const {
427 NULL_ON_ERROR(status);
428
429 if (!isFunctionCall()) {
430 status = U_INVALID_STATE_ERROR;
431 return nullptr;
432 }
433 U_ASSERT(rator);
434 return &(*rator);
435 }
436
437 // May return null operand
438 const Operand& Expression::getOperand() const { return rand; }
439
440 Expression::Builder& Expression::Builder::setOperand(Operand&& rAnd) {
441 hasOperand = true;
442 rand = std::move(rAnd);
443 return *this;
444 }
445
446 Expression::Builder& Expression::Builder::setOperator(Operator&& rAtor) {
447 hasOperator = true;
448 rator = std::move(rAtor);
449 return *this;
450 }
451
452 Expression::Builder& Expression::Builder::addAttribute(const UnicodeString& k,
453 Operand&& v,
454 UErrorCode& status) {
455 attributes.add(Option(k, std::move(v)), status);
456 return *this;
457 }
458
459 Expression Expression::Builder::build(UErrorCode& errorCode) {
460 Expression result;
461
462 if (U_FAILURE(errorCode)) {
463 return result;
464 }
465
466 if ((!hasOperand || rand.isNull()) && !hasOperator) {
467 errorCode = U_INVALID_STATE_ERROR;
468 return result;
469 }
470
471 OptionMap attributeMap = attributes.build(errorCode);
472 if (hasOperand && hasOperator) {
473 result = Expression(rator, rand, std::move(attributeMap));
474 } else if (hasOperand && !hasOperator) {
475 result = Expression(rand, std::move(attributeMap));
476 } else {
477 // rator is valid, rand is not valid
478 result = Expression(rator, std::move(attributeMap));
479 }
480 return result;
481 }
482
483 Expression::Expression() : rator(std::nullopt) {}
484
485 Expression::Expression(const Expression& other) : rator(other.rator), rand(other.rand), attributes(other.attributes) {}
486
487 Expression& Expression::operator=(Expression other) noexcept {
488 swap(*this, other);
489 return *this;
490 }
491
492 Expression::Builder::~Builder() {}
493
494 Expression::~Expression() {}
495
496 // ----------- PatternPart
497
498 // PatternPart needs a copy constructor in order to make Pattern deeply copyable
499 // If !isRawText and the copy of the other expression fails,
500 // then isBogus() will be true for this PatternPart
501 PatternPart::PatternPart(const PatternPart& other) : piece(other.piece) {}
502
503 const Expression& PatternPart::contents() const {
504 U_ASSERT(isExpression());
505 return *std::get_if<Expression>(&piece);
506 }
507
508 const Markup& PatternPart::asMarkup() const {
509 U_ASSERT(isMarkup());
510 return *std::get_if<Markup>(&piece);
511 }
512
513 // Precondition: isText();
514 const UnicodeString& PatternPart::asText() const {
515 U_ASSERT(isText());
516 return *std::get_if<UnicodeString>(&piece);
517 }
518
519 PatternPart& PatternPart::operator=(PatternPart other) noexcept {
520 swap(*this, other);
521 return *this;
522 }
523
524 PatternPart::~PatternPart() {}
525
526 // ---------------- Pattern
527
528 Pattern::Pattern(const UVector& ps, UErrorCode& status) : len(ps.size()) {
529 if (U_FAILURE(status)) {
530 return;
531 }
532 PatternPart* result = copyVectorToArray<PatternPart>(ps, status);
533 CHECK_ERROR(status);
534 parts.adoptInstead(result);
535 }
536
537 // Copy constructor
538 Pattern::Pattern(const Pattern& other) : len(other.len) {
539 U_ASSERT(!other.bogus);
540 UErrorCode localErrorCode = U_ZERO_ERROR;
541 if (len == 0) {
542 parts.adoptInstead(nullptr);
543 } else {
544 parts.adoptInstead(copyArray(other.parts.getAlias(), len, localErrorCode));
545 }
546 if (U_FAILURE(localErrorCode)) {
547 bogus = true;
548 }
549 }
550
551 int32_t Pattern::numParts() const {
552 U_ASSERT(!bogus);
553 return len;
554 }
555
556 const PatternPart& Pattern::getPart(int32_t i) const {
557 U_ASSERT(!bogus && i < numParts());
558 return parts[i];
559 }
560
561 Pattern::Builder::Builder(UErrorCode& status) {
562 parts = createUVector(status);
563 }
564
565 Pattern Pattern::Builder::build(UErrorCode& status) const noexcept {
566 if (U_FAILURE(status)) {
567 return {};
568 }
569 U_ASSERT(parts != nullptr);
570 return Pattern(*parts, status);
571 }
572
573 Pattern::Builder& Pattern::Builder::add(Expression&& part, UErrorCode& status) noexcept {
574 U_ASSERT(parts != nullptr);
575 if (U_SUCCESS(status)) {
576 PatternPart* l = create<PatternPart>(PatternPart(std::move(part)), status);
577 parts->adoptElement(l, status);
578 }
579 return *this;
580 }
581
582 Pattern::Builder& Pattern::Builder::add(Markup&& part, UErrorCode& status) noexcept {
583 U_ASSERT(parts != nullptr);
584 if (U_SUCCESS(status)) {
585 PatternPart* l = create<PatternPart>(PatternPart(std::move(part)), status);
586 parts->adoptElement(l, status);
587 }
588 return *this;
589 }
590
591 Pattern::Builder& Pattern::Builder::add(UnicodeString&& part, UErrorCode& status) noexcept {
592 U_ASSERT(parts != nullptr);
593 if (U_SUCCESS(status)) {
594 PatternPart* l = create<PatternPart>(PatternPart(std::move(part)), status);
595 parts->adoptElement(l, status);
596 }
597 return *this;
598 }
599
600 Pattern& Pattern::operator=(Pattern other) noexcept {
601 swap(*this, other);
602
603 return *this;
604 }
605
606 Pattern::Builder::~Builder() {
607 if (parts != nullptr) {
608 delete parts;
609 }
610 }
611
612 Pattern::~Pattern() {}
613
614 // ---------------- Binding
615
616 const Expression& Binding::getValue() const {
617 return expr;
618 }
619
620 /* static */ Binding Binding::input(UnicodeString&& variableName, Expression&& rhs, UErrorCode& errorCode) {
621 Binding b;
622 if (U_SUCCESS(errorCode)) {
623 const Operand& rand = rhs.getOperand();
624 if (!(rand.isVariable() && (rand.asVariable() == variableName))) {
625 errorCode = U_INVALID_STATE_ERROR;
626 } else {
627 const Operator* rator = rhs.getOperator(errorCode);
628 bool hasOperator = U_SUCCESS(errorCode);
629 // Clear error code -- the "error" from the absent operator
630 // is handled
631 errorCode = U_ZERO_ERROR;
632 b = Binding(variableName, std::move(rhs));
633 b.local = false;
634 if (hasOperator) {
635 rator = b.getValue().getOperator(errorCode);
636 U_ASSERT(U_SUCCESS(errorCode));
637 b.annotation = rator;
638 } else {
639 b.annotation = nullptr;
640 }
641 U_ASSERT(!hasOperator || b.annotation != nullptr);
642 }
643 }
644 return b;
645 }
646
647 const OptionMap& Binding::getOptionsInternal() const {
648 U_ASSERT(annotation != nullptr);
649 return annotation->getOptionsInternal();
650 }
651
652 void Binding::updateAnnotation() {
653 UErrorCode localErrorCode = U_ZERO_ERROR;
654 const Operator* rator = expr.getOperator(localErrorCode);
655 if (U_FAILURE(localErrorCode)) {
656 return;
657 }
658 U_ASSERT(U_SUCCESS(localErrorCode));
659 annotation = rator;
660 }
661
662 Binding::Binding(const Binding& other) : var(other.var), expr(other.expr), local(other.local) {
663 updateAnnotation();
664 }
665
666 Binding& Binding::operator=(Binding other) noexcept {
667 swap(*this, other);
668 return *this;
669 }
670
671 Binding::~Binding() {}
672
673 // --------------- Variant
674
675 Variant& Variant::operator=(Variant other) noexcept {
676 swap(*this, other);
677 return *this;
678 }
679
680 Variant::Variant(const Variant& other) : k(other.k), p(other.p) {}
681
682 Variant::~Variant() {}
683
684 // ------------- Matcher
685
686 Matcher& Matcher::operator=(Matcher other) {
687 swap(*this, other);
688 return *this;
689 }
690
691 Matcher::Matcher(const Matcher& other) {
692 U_ASSERT(!other.bogus);
693 numSelectors = other.numSelectors;
694 numVariants = other.numVariants;
695 UErrorCode localErrorCode = U_ZERO_ERROR;
696 selectors.adoptInstead(copyArray<VariableName>(other.selectors.getAlias(),
697 numSelectors,
698 localErrorCode));
699 variants.adoptInstead(copyArray<Variant>(other.variants.getAlias(),
700 numVariants,
701 localErrorCode));
702 if (U_FAILURE(localErrorCode)) {
703 bogus = true;
704 }
705 }
706
707 Matcher::Matcher(VariableName* ss, int32_t ns, Variant* vs, int32_t nv)
708 : selectors(ss), numSelectors(ns), variants(vs), numVariants(nv) {}
709
710 Matcher::~Matcher() {}
711
712 // --------------- MFDataModel
713
714 const Pattern& MFDataModel::getPattern() const {
715 if (std::holds_alternative<Matcher>(body)) {
716 // Return reference to empty pattern if this is a selectors message
717 return empty;
718 }
719 return *(std::get_if<Pattern>(&body));
720 }
721
722 // Returns nullptr if no bindings
723 const Binding* MFDataModel::getLocalVariablesInternal() const {
724 U_ASSERT(!bogus);
725 U_ASSERT(bindingsLen == 0 || bindings.isValid());
726 return bindings.getAlias();
727 }
728
729 const VariableName* MFDataModel::getSelectorsInternal() const {
730 U_ASSERT(!bogus);
731 U_ASSERT(!hasPattern());
732 return std::get_if<Matcher>(&body)->selectors.getAlias();
733 }
734
735 const Variant* MFDataModel::getVariantsInternal() const {
736 U_ASSERT(!bogus);
737 U_ASSERT(!hasPattern());
738 return std::get_if<Matcher>(&body)->variants.getAlias();
739 }
740
741 MFDataModel::Builder::Builder(UErrorCode& status) {
742 bindings = createUVector(status);
743 }
744
745 // Invalidate pattern and create selectors/variants if necessary
746 void MFDataModel::Builder::buildSelectorsMessage(UErrorCode& status) {
747 CHECK_ERROR(status);
748
749 if (hasPattern) {
750 selectors = createUVector(status);
751 variants = createUVector(status);
752 hasPattern = false;
753 }
754 hasPattern = false;
755 hasSelectors = true;
756 }
757
758 void MFDataModel::Builder::checkDuplicate(const VariableName& var, UErrorCode& status) const {
759 CHECK_ERROR(status);
760
761 // This means that handling declarations is quadratic in the number of variables,
762 // but the `UVector` of locals in the builder could be changed to a `Hashtable`
763 // if that's a problem
764 // Note: this also doesn't check _all_ duplicate declaration errors,
765 // see MessageFormatter::Checker::checkDeclarations()
766 for (int32_t i = 0; i < bindings->size(); i++) {
767 if ((static_cast<Binding*>(bindings->elementAt(i)))->getVariable() == var) {
768 status = U_MF_DUPLICATE_DECLARATION_ERROR;
769 break;
770 }
771 }
772 }
773
774 MFDataModel::Builder& MFDataModel::Builder::addBinding(Binding&& b, UErrorCode& status) {
775 if (U_SUCCESS(status)) {
776 U_ASSERT(bindings != nullptr);
777 checkDuplicate(b.getVariable(), status);
778 UErrorCode savedStatus = status;
779 if (status == U_MF_DUPLICATE_DECLARATION_ERROR) {
780 // Want to add the binding anyway even if it's a duplicate
781 status = U_ZERO_ERROR;
782 }
783 bindings->adoptElement(create<Binding>(std::move(b), status), status);
784 if (U_SUCCESS(status) || savedStatus == U_MF_DUPLICATE_DECLARATION_ERROR) {
785 status = savedStatus;
786 }
787 }
788 return *this;
789 }
790
791 MFDataModel::Builder& MFDataModel::Builder::addSelector(VariableName&& selector,
792 UErrorCode& status) {
793 THIS_ON_ERROR(status);
794
795 buildSelectorsMessage(status);
796 U_ASSERT(selectors != nullptr);
797 selectors->adoptElement(create<VariableName>(std::move(selector), status), status);
798
799 return *this;
800 }
801
802 /*
803 `pattern` must be non-null
804 */
805 MFDataModel::Builder& MFDataModel::Builder::addVariant(SelectorKeys&& keys, Pattern&& pattern, UErrorCode& errorCode) noexcept {
806 buildSelectorsMessage(errorCode);
807 Variant* v = create<Variant>(Variant(std::move(keys), std::move(pattern)), errorCode);
808 if (U_SUCCESS(errorCode)) {
809 variants->adoptElement(v, errorCode);
810 }
811 return *this;
812 }
813
814 MFDataModel::Builder& MFDataModel::Builder::setPattern(Pattern&& pat) {
815 pattern = std::move(pat);
816 hasPattern = true;
817 hasSelectors = false;
818 // Invalidate variants
819 if (variants != nullptr) {
820 variants->removeAllElements();
821 }
822 return *this;
823 }
824
825 MFDataModel::MFDataModel(const MFDataModel& other) : body(Pattern()) {
826 U_ASSERT(!other.bogus);
827
828 UErrorCode localErrorCode = U_ZERO_ERROR;
829
830 if (other.hasPattern()) {
831 body = *std::get_if<Pattern>(&other.body);
832 } else {
833 const VariableName* otherSelectors = other.getSelectorsInternal();
834 const Variant* otherVariants = other.getVariantsInternal();
835 int32_t numSelectors = other.numSelectors();
836 int32_t numVariants = other.numVariants();
837 VariableName* copiedSelectors = copyArray(otherSelectors, numSelectors, localErrorCode);
838 Variant* copiedVariants = copyArray(otherVariants, numVariants, localErrorCode);
839 if (U_FAILURE(localErrorCode)) {
840 bogus = true;
841 return;
842 }
843 body = Matcher(copiedSelectors, numSelectors, copiedVariants, numVariants);
844 }
845
846 bindingsLen = other.bindingsLen;
847 if (bindingsLen > 0) {
848 bindings.adoptInstead(copyArray(other.bindings.getAlias(), bindingsLen, localErrorCode));
849 }
850 if (U_FAILURE(localErrorCode)) {
851 bogus = true;
852 }
853 }
854
855 MFDataModel::MFDataModel(const MFDataModel::Builder& builder, UErrorCode& errorCode) noexcept : body(Pattern()) {
856 CHECK_ERROR(errorCode);
857
858 if (builder.hasPattern) {
859 body.emplace<Pattern>(builder.pattern);
860 } else {
861 U_ASSERT(builder.variants != nullptr);
862 U_ASSERT(builder.selectors != nullptr);
863 int32_t numVariants = builder.variants->size();
864 int32_t numSelectors = builder.selectors->size();
865 LocalArray<Variant> variants(copyVectorToArray<Variant>(*builder.variants, errorCode), errorCode);
866 LocalArray<VariableName> selectors(copyVectorToArray<VariableName>(*builder.selectors,
867 errorCode),
868 errorCode);
869 if (U_FAILURE(errorCode)) {
870 bogus = true;
871 return;
872 }
873 body.emplace<Matcher>(Matcher(selectors.orphan(), numSelectors, variants.orphan(), numVariants));
874 }
875
876 U_ASSERT(builder.bindings != nullptr);
877 bindingsLen = builder.bindings->size();
878 if (bindingsLen > 0) {
879 bindings.adoptInstead(copyVectorToArray<Binding>(*builder.bindings, errorCode));
880 }
881 if (U_FAILURE(errorCode)) {
882 bogus = true;
883 }
884 }
885
886 MFDataModel::MFDataModel() : body(Pattern()) {}
887
888 MFDataModel& MFDataModel::operator=(MFDataModel other) noexcept {
889 U_ASSERT(!other.bogus);
890 swap(*this, other);
891 return *this;
892 }
893
894 MFDataModel MFDataModel::Builder::build(UErrorCode& errorCode) const noexcept {
895 if (U_FAILURE(errorCode)) {
896 return {};
897 }
898 if (!hasPattern && !hasSelectors) {
899 errorCode = U_INVALID_STATE_ERROR;
900 }
901 return MFDataModel(*this, errorCode);
902 }
903
904 MFDataModel::~MFDataModel() {}
905 MFDataModel::Builder::~Builder() {
906 if (selectors != nullptr) {
907 delete selectors;
908 }
909 if (variants != nullptr) {
910 delete variants;
911 }
912 if (bindings != nullptr) {
913 delete bindings;
914 }
915 }
916 } // namespace message2
917
918 U_NAMESPACE_END
919
920 #endif /* #if !UCONFIG_NO_MF2 */
921
922 #endif /* #if !UCONFIG_NO_FORMATTING */
923
924 #endif /* #if !UCONFIG_NO_NORMALIZATION */