Debian Codegraph @

intl/components/src/ICU4CGlue.cpp

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include "mozilla/intl/ICU4CGlue.h"
6 #include "unicode/uformattedvalue.h"
7
8 namespace mozilla::intl {
9
10 // Starting with ICU 59, UChar defaults to char16_t.
11 static_assert(std::is_same_v<UChar, char16_t>,
12 "Gecko doesn't support redefining UChar to a different type");
13
14 ICUError ToICUError(UErrorCode status) {
15 MOZ_ASSERT(!U_SUCCESS(status));
16 switch (status) {
17 case U_MEMORY_ALLOCATION_ERROR:
18 return ICUError::OutOfMemory;
19 default:
20 return ICUError::InternalError;
21 }
22 }
23
24 ICUResult ToICUResult(UErrorCode status) {
25 if (U_SUCCESS(status)) {
26 return Ok();
27 }
28 return Err(ToICUError(status));
29 }
30
31 // static
32 Result<Span<const char16_t>, ICUError> FormattedResult::ToSpanImpl(
33 const UFormattedValue* value) {
34 UErrorCode status = U_ZERO_ERROR;
35 int32_t strLength;
36 const char16_t* str = ufmtval_getString(value, &strLength, &status);
37 if (U_FAILURE(status)) {
38 return Err(ToICUError(status));
39 }
40
41 return Span{str, AssertedCast<size_t>(strLength)};
42 }
43
44 } // namespace mozilla::intl