1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*****************************************************************************************
* Copyright (C) 2015, International Business Machines
* Corporation and others. All Rights Reserved.
*****************************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/ulistformatter.h"
#include "unicode/listformatter.h"
#include "unicode/localpointer.h"
#include "cmemory.h"
#include "formattedval_impl.h"
U_NAMESPACE_USE
U_CAPI UListFormatter* U_EXPORT2
ulistfmt_open(const char* locale,
UErrorCode* status)
{
if (U_FAILURE(*status)) {
return nullptr;
}
LocalPointer<ListFormatter> listfmt(ListFormatter::createInstance(Locale(locale), *status));
if (U_FAILURE(*status)) {
return nullptr;
}
return (UListFormatter*)listfmt.orphan();
}
U_CAPI UListFormatter* U_EXPORT2
ulistfmt_openForType(const char* locale, UListFormatterType type,
UListFormatterWidth width, UErrorCode* status)
{
if (U_FAILURE(*status)) {
return nullptr;
}
LocalPointer<ListFormatter> listfmt(ListFormatter::createInstance(Locale(locale), type, width, *status));
if (U_FAILURE(*status)) {
return nullptr;
}
return (UListFormatter*)listfmt.orphan();
}
U_CAPI void U_EXPORT2
ulistfmt_close(UListFormatter *listfmt)
{
delete (ListFormatter*)listfmt;
}
// Magic number: FLST in ASCII
UPRV_FORMATTED_VALUE_CAPI_AUTO_IMPL(
FormattedList,
UFormattedList,
UFormattedListImpl,
UFormattedListApiHelper,
ulistfmt,
0x464C5354)
static UnicodeString* getUnicodeStrings(
const char16_t* const strings[],
const int32_t* stringLengths,
int32_t stringCount,
UnicodeString* length4StackBuffer,
LocalArray<UnicodeString>& maybeOwner,
UErrorCode& status) {
U_ASSERT(U_SUCCESS(status));
if (stringCount < 0 || (strings == nullptr && stringCount > 0)) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return nullptr;
}
UnicodeString* ustrings = length4StackBuffer;
if (stringCount > 4) {
maybeOwner.adoptInsteadAndCheckErrorCode(new UnicodeString[stringCount], status);
if (U_FAILURE(status)) {
return nullptr;
}
ustrings = maybeOwner.getAlias();
}
if (stringLengths == nullptr) {
for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
ustrings[stringIndex].setTo(true, strings[stringIndex], -1);
}
} else {
for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
ustrings[stringIndex].setTo(stringLengths[stringIndex] < 0, strings[stringIndex], stringLengths[stringIndex]);
}
}
return ustrings;
}
U_CAPI int32_t U_EXPORT2
ulistfmt_format(const UListFormatter* listfmt,
const char16_t* const strings[],
const int32_t * stringLengths,
int32_t stringCount,
char16_t* result,
int32_t resultCapacity,
UErrorCode* status)
{
if (U_FAILURE(*status)) {
return -1;
}
if ((result == nullptr) ? resultCapacity != 0 : resultCapacity < 0) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return -1;
}
UnicodeString length4StackBuffer[4];
LocalArray<UnicodeString> maybeOwner;
UnicodeString* ustrings = getUnicodeStrings(
strings, stringLengths, stringCount, length4StackBuffer, maybeOwner, *status);
if (U_FAILURE(*status)) {
return -1;
}
UnicodeString res;
if (result != nullptr) {
// nullptr destination for pure preflighting: empty dummy string
// otherwise, alias the destination buffer (copied from udat_format)
res.setTo(result, 0, resultCapacity);
}
reinterpret_cast<const ListFormatter*>(listfmt)->format( ustrings, stringCount, res, *status );
return res.extract(result, resultCapacity, *status);
}
U_CAPI void U_EXPORT2
ulistfmt_formatStringsToResult(
const UListFormatter* listfmt,
const char16_t* const strings[],
const int32_t * stringLengths,
int32_t stringCount,
UFormattedList* uresult,
UErrorCode* status) {
auto* result = UFormattedListApiHelper::validate(uresult, *status);
if (U_FAILURE(*status)) {
return;
}
UnicodeString length4StackBuffer[4];
LocalArray<UnicodeString> maybeOwner;
UnicodeString* ustrings = getUnicodeStrings(
strings, stringLengths, stringCount, length4StackBuffer, maybeOwner, *status);
if (U_FAILURE(*status)) {
return;
}
result->fImpl = reinterpret_cast<const ListFormatter*>(listfmt)
->formatStringsToValue(ustrings, stringCount, *status);
}
#endif /* #if !UCONFIG_NO_FORMATTING */
|