1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *****************************************************************************************
5 * Copyright (C) 2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *****************************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/ulistformatter.h"
15 #include "unicode/listformatter.h"
16 #include "unicode/localpointer.h"
18 #include "formattedval_impl.h"
22 U_CAPI UListFormatter
* U_EXPORT2
23 ulistfmt_open(const char* locale
,
26 if (U_FAILURE(*status
)) {
29 LocalPointer
<ListFormatter
> listfmt(ListFormatter::createInstance(Locale(locale
), *status
));
30 if (U_FAILURE(*status
)) {
33 return (UListFormatter
*)listfmt
.orphan();
38 ulistfmt_close(UListFormatter
*listfmt
)
40 delete (ListFormatter
*)listfmt
;
44 // Magic number: FLST in ASCII
45 UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL(
49 UFormattedListApiHelper
,
54 static UnicodeString
* getUnicodeStrings(
55 const UChar
* const strings
[],
56 const int32_t* stringLengths
,
58 UnicodeString
* length4StackBuffer
,
59 LocalArray
<UnicodeString
>& maybeOwner
,
61 U_ASSERT(U_SUCCESS(status
));
62 if (stringCount
< 0 || (strings
== NULL
&& stringCount
> 0)) {
63 status
= U_ILLEGAL_ARGUMENT_ERROR
;
66 UnicodeString
* ustrings
= length4StackBuffer
;
67 if (stringCount
> 4) {
68 maybeOwner
.adoptInsteadAndCheckErrorCode(new UnicodeString
[stringCount
], status
);
69 if (U_FAILURE(status
)) {
72 ustrings
= maybeOwner
.getAlias();
74 if (stringLengths
== NULL
) {
75 for (int32_t stringIndex
= 0; stringIndex
< stringCount
; stringIndex
++) {
76 ustrings
[stringIndex
].setTo(TRUE
, strings
[stringIndex
], -1);
79 for (int32_t stringIndex
= 0; stringIndex
< stringCount
; stringIndex
++) {
80 ustrings
[stringIndex
].setTo(stringLengths
[stringIndex
] < 0, strings
[stringIndex
], stringLengths
[stringIndex
]);
87 U_CAPI
int32_t U_EXPORT2
88 ulistfmt_format(const UListFormatter
* listfmt
,
89 const UChar
* const strings
[],
90 const int32_t * stringLengths
,
93 int32_t resultCapacity
,
96 if (U_FAILURE(*status
)) {
99 if ((result
== NULL
) ? resultCapacity
!= 0 : resultCapacity
< 0) {
100 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
103 UnicodeString length4StackBuffer
[4];
104 LocalArray
<UnicodeString
> maybeOwner
;
105 UnicodeString
* ustrings
= getUnicodeStrings(
106 strings
, stringLengths
, stringCount
, length4StackBuffer
, maybeOwner
, *status
);
107 if (U_FAILURE(*status
)) {
111 if (result
!= NULL
) {
112 // NULL destination for pure preflighting: empty dummy string
113 // otherwise, alias the destination buffer (copied from udat_format)
114 res
.setTo(result
, 0, resultCapacity
);
116 reinterpret_cast<const ListFormatter
*>(listfmt
)->format( ustrings
, stringCount
, res
, *status
);
117 return res
.extract(result
, resultCapacity
, *status
);
121 U_CAPI
void U_EXPORT2
122 ulistfmt_formatStringsToResult(
123 const UListFormatter
* listfmt
,
124 const UChar
* const strings
[],
125 const int32_t * stringLengths
,
127 UFormattedList
* uresult
,
128 UErrorCode
* status
) {
129 auto* result
= UFormattedListApiHelper::validate(uresult
, *status
);
130 if (U_FAILURE(*status
)) {
133 UnicodeString length4StackBuffer
[4];
134 LocalArray
<UnicodeString
> maybeOwner
;
135 UnicodeString
* ustrings
= getUnicodeStrings(
136 strings
, stringLengths
, stringCount
, length4StackBuffer
, maybeOwner
, *status
);
137 if (U_FAILURE(*status
)) {
140 result
->fImpl
= reinterpret_cast<const ListFormatter
*>(listfmt
)
141 ->formatStringsToValue(ustrings
, stringCount
, *status
);
145 #endif /* #if !UCONFIG_NO_FORMATTING */