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();
37 U_CAPI UListFormatter
* U_EXPORT2
38 ulistfmt_openForType(const char* locale
, UListFormatterType type
,
39 UListFormatterWidth width
, UErrorCode
* status
)
41 if (U_FAILURE(*status
)) {
44 LocalPointer
<ListFormatter
> listfmt(ListFormatter::createInstance(Locale(locale
), type
, width
, *status
));
45 if (U_FAILURE(*status
)) {
48 return (UListFormatter
*)listfmt
.orphan();
53 ulistfmt_close(UListFormatter
*listfmt
)
55 delete (ListFormatter
*)listfmt
;
59 // Magic number: FLST in ASCII
60 UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL(
64 UFormattedListApiHelper
,
69 static UnicodeString
* getUnicodeStrings(
70 const UChar
* const strings
[],
71 const int32_t* stringLengths
,
73 UnicodeString
* length4StackBuffer
,
74 LocalArray
<UnicodeString
>& maybeOwner
,
76 U_ASSERT(U_SUCCESS(status
));
77 if (stringCount
< 0 || (strings
== NULL
&& stringCount
> 0)) {
78 status
= U_ILLEGAL_ARGUMENT_ERROR
;
81 UnicodeString
* ustrings
= length4StackBuffer
;
82 if (stringCount
> 4) {
83 maybeOwner
.adoptInsteadAndCheckErrorCode(new UnicodeString
[stringCount
], status
);
84 if (U_FAILURE(status
)) {
87 ustrings
= maybeOwner
.getAlias();
89 if (stringLengths
== NULL
) {
90 for (int32_t stringIndex
= 0; stringIndex
< stringCount
; stringIndex
++) {
91 ustrings
[stringIndex
].setTo(TRUE
, strings
[stringIndex
], -1);
94 for (int32_t stringIndex
= 0; stringIndex
< stringCount
; stringIndex
++) {
95 ustrings
[stringIndex
].setTo(stringLengths
[stringIndex
] < 0, strings
[stringIndex
], stringLengths
[stringIndex
]);
102 U_CAPI
int32_t U_EXPORT2
103 ulistfmt_format(const UListFormatter
* listfmt
,
104 const UChar
* const strings
[],
105 const int32_t * stringLengths
,
108 int32_t resultCapacity
,
111 if (U_FAILURE(*status
)) {
114 if ((result
== NULL
) ? resultCapacity
!= 0 : resultCapacity
< 0) {
115 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
118 UnicodeString length4StackBuffer
[4];
119 LocalArray
<UnicodeString
> maybeOwner
;
120 UnicodeString
* ustrings
= getUnicodeStrings(
121 strings
, stringLengths
, stringCount
, length4StackBuffer
, maybeOwner
, *status
);
122 if (U_FAILURE(*status
)) {
126 if (result
!= NULL
) {
127 // NULL destination for pure preflighting: empty dummy string
128 // otherwise, alias the destination buffer (copied from udat_format)
129 res
.setTo(result
, 0, resultCapacity
);
131 reinterpret_cast<const ListFormatter
*>(listfmt
)->format( ustrings
, stringCount
, res
, *status
);
132 return res
.extract(result
, resultCapacity
, *status
);
136 U_CAPI
void U_EXPORT2
137 ulistfmt_formatStringsToResult(
138 const UListFormatter
* listfmt
,
139 const UChar
* const strings
[],
140 const int32_t * stringLengths
,
142 UFormattedList
* uresult
,
143 UErrorCode
* status
) {
144 auto* result
= UFormattedListApiHelper::validate(uresult
, *status
);
145 if (U_FAILURE(*status
)) {
148 UnicodeString length4StackBuffer
[4];
149 LocalArray
<UnicodeString
> maybeOwner
;
150 UnicodeString
* ustrings
= getUnicodeStrings(
151 strings
, stringLengths
, stringCount
, length4StackBuffer
, maybeOwner
, *status
);
152 if (U_FAILURE(*status
)) {
155 result
->fImpl
= reinterpret_cast<const ListFormatter
*>(listfmt
)
156 ->formatStringsToValue(ustrings
, stringCount
, *status
);
160 #endif /* #if !UCONFIG_NO_FORMATTING */