1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 * Copyright (C) 2013-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 *******************************************************************************
10 * file name: listformatter.cpp
11 * encoding: UTF-8
12 * tab size: 8 (not used)
13 * indentation:4
14 *
15 * created on: 2012aug27
16 * created by: Umesh P. Nair
17 */
18
19 #include "cmemory.h"
20 #include "unicode/fpositer.h" // FieldPositionIterator
21 #include "unicode/listformatter.h"
22 #include "unicode/simpleformatter.h"
23 #include "unicode/ulistformatter.h"
24 #include "fphdlimp.h"
25 #include "mutex.h"
26 #include "hash.h"
27 #include "cstring.h"
28 #include "uarrsort.h"
29 #include "ulocimp.h"
30 #include "charstr.h"
31 #include "ucln_in.h"
32 #include "uresimp.h"
33 #include "resource.h"
34
35 U_NAMESPACE_BEGIN
36
37 struct ListFormatInternal : public UMemory {
38 SimpleFormatter twoPattern;
39 SimpleFormatter startPattern;
40 SimpleFormatter middlePattern;
41 SimpleFormatter endPattern;
42
ListFormatInternalListFormatInternal43 ListFormatInternal(
44 const UnicodeString& two,
45 const UnicodeString& start,
46 const UnicodeString& middle,
47 const UnicodeString& end,
48 UErrorCode &errorCode) :
49 twoPattern(two, 2, 2, errorCode),
50 startPattern(start, 2, 2, errorCode),
51 middlePattern(middle, 2, 2, errorCode),
52 endPattern(end, 2, 2, errorCode) {}
53
ListFormatInternalListFormatInternal54 ListFormatInternal(const ListFormatData &data, UErrorCode &errorCode) :
55 twoPattern(data.twoPattern, errorCode),
56 startPattern(data.startPattern, errorCode),
57 middlePattern(data.middlePattern, errorCode),
58 endPattern(data.endPattern, errorCode) { }
59
ListFormatInternalListFormatInternal60 ListFormatInternal(const ListFormatInternal &other) :
61 twoPattern(other.twoPattern),
62 startPattern(other.startPattern),
63 middlePattern(other.middlePattern),
64 endPattern(other.endPattern) { }
65 };
66
67
68
69 static Hashtable* listPatternHash = nullptr;
70 static UMutex listFormatterMutex = U_MUTEX_INITIALIZER;
71 static const char STANDARD_STYLE[] = "standard";
72
73 U_CDECL_BEGIN
uprv_listformatter_cleanup()74 static UBool U_CALLCONV uprv_listformatter_cleanup() {
75 delete listPatternHash;
76 listPatternHash = nullptr;
77 return TRUE;
78 }
79
80 static void U_CALLCONV
uprv_deleteListFormatInternal(void * obj)81 uprv_deleteListFormatInternal(void *obj) {
82 delete static_cast<ListFormatInternal *>(obj);
83 }
84
85 U_CDECL_END
86
ListFormatter(const ListFormatter & other)87 ListFormatter::ListFormatter(const ListFormatter& other) :
88 owned(other.owned), data(other.data) {
89 if (other.owned != nullptr) {
90 owned = new ListFormatInternal(*other.owned);
91 data = owned;
92 }
93 }
94
operator =(const ListFormatter & other)95 ListFormatter& ListFormatter::operator=(const ListFormatter& other) {
96 if (this == &other) {
97 return *this;
98 }
99 delete owned;
100 if (other.owned) {
101 owned = new ListFormatInternal(*other.owned);
102 data = owned;
103 } else {
104 owned = nullptr;
105 data = other.data;
106 }
107 return *this;
108 }
109
initializeHash(UErrorCode & errorCode)110 void ListFormatter::initializeHash(UErrorCode& errorCode) {
111 if (U_FAILURE(errorCode)) {
112 return;
113 }
114
115 listPatternHash = new Hashtable();
116 if (listPatternHash == nullptr) {
117 errorCode = U_MEMORY_ALLOCATION_ERROR;
118 return;
119 }
120
121 listPatternHash->setValueDeleter(uprv_deleteListFormatInternal);
122 ucln_i18n_registerCleanup(UCLN_I18N_LIST_FORMATTER, uprv_listformatter_cleanup);
123
124 }
125
getListFormatInternal(const Locale & locale,const char * style,UErrorCode & errorCode)126 const ListFormatInternal* ListFormatter::getListFormatInternal(
127 const Locale& locale, const char *style, UErrorCode& errorCode) {
128 if (U_FAILURE(errorCode)) {
129 return nullptr;
130 }
131 CharString keyBuffer(locale.getName(), errorCode);
132 keyBuffer.append(':', errorCode).append(style, errorCode);
133 UnicodeString key(keyBuffer.data(), -1, US_INV);
134 ListFormatInternal* result = nullptr;
135 {
136 Mutex m(&listFormatterMutex);
137 if (listPatternHash == nullptr) {
138 initializeHash(errorCode);
139 if (U_FAILURE(errorCode)) {
140 return nullptr;
141 }
142 }
143 result = static_cast<ListFormatInternal*>(listPatternHash->get(key));
144 }
145 if (result != nullptr) {
146 return result;
147 }
148 result = loadListFormatInternal(locale, style, errorCode);
149 if (U_FAILURE(errorCode)) {
150 return nullptr;
151 }
152
153 {
154 Mutex m(&listFormatterMutex);
155 ListFormatInternal* temp = static_cast<ListFormatInternal*>(listPatternHash->get(key));
156 if (temp != nullptr) {
157 delete result;
158 result = temp;
159 } else {
160 listPatternHash->put(key, result, errorCode);
161 if (U_FAILURE(errorCode)) {
162 return nullptr;
163 }
164 }
165 }
166 return result;
167 }
168
169 static const UChar solidus = 0x2F;
170 static const UChar aliasPrefix[] = { 0x6C,0x69,0x73,0x74,0x50,0x61,0x74,0x74,0x65,0x72,0x6E,0x2F }; // "listPattern/"
171 enum {
172 kAliasPrefixLen = UPRV_LENGTHOF(aliasPrefix),
173 kStyleLenMax = 24 // longest currently is 14
174 };
175
176 struct ListFormatter::ListPatternsSink : public ResourceSink {
177 UnicodeString two, start, middle, end;
178 #if ((U_PLATFORM == U_PF_AIX) || (U_PLATFORM == U_PF_OS390)) && (U_CPLUSPLUS_VERSION < 11)
179 char aliasedStyle[kStyleLenMax+1];
ListPatternsSinkListFormatter::ListPatternsSink180 ListPatternsSink() {
181 uprv_memset(aliasedStyle, 0, kStyleLenMax+1);
182 }
183 #else
184 char aliasedStyle[kStyleLenMax+1] = {0};
185
ListPatternsSinkListFormatter::ListPatternsSink186 ListPatternsSink() {}
187 #endif
188 virtual ~ListPatternsSink();
189
setAliasedStyleListFormatter::ListPatternsSink190 void setAliasedStyle(UnicodeString alias) {
191 int32_t startIndex = alias.indexOf(aliasPrefix, kAliasPrefixLen, 0);
192 if (startIndex < 0) {
193 return;
194 }
195 startIndex += kAliasPrefixLen;
196 int32_t endIndex = alias.indexOf(solidus, startIndex);
197 if (endIndex < 0) {
198 endIndex = alias.length();
199 }
200 alias.extract(startIndex, endIndex-startIndex, aliasedStyle, kStyleLenMax+1, US_INV);
201 aliasedStyle[kStyleLenMax] = 0;
202 }
203
handleValueForPatternListFormatter::ListPatternsSink204 void handleValueForPattern(ResourceValue &value, UnicodeString &pattern, UErrorCode &errorCode) {
205 if (pattern.isEmpty()) {
206 if (value.getType() == URES_ALIAS) {
207 if (aliasedStyle[0] == 0) {
208 setAliasedStyle(value.getAliasUnicodeString(errorCode));
209 }
210 } else {
211 pattern = value.getUnicodeString(errorCode);
212 }
213 }
214 }
215
putListFormatter::ListPatternsSink216 virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
217 UErrorCode &errorCode) {
218 aliasedStyle[0] = 0;
219 if (value.getType() == URES_ALIAS) {
220 setAliasedStyle(value.getAliasUnicodeString(errorCode));
221 return;
222 }
223 ResourceTable listPatterns = value.getTable(errorCode);
224 for (int i = 0; U_SUCCESS(errorCode) && listPatterns.getKeyAndValue(i, key, value); ++i) {
225 if (uprv_strcmp(key, "2") == 0) {
226 handleValueForPattern(value, two, errorCode);
227 } else if (uprv_strcmp(key, "end") == 0) {
228 handleValueForPattern(value, end, errorCode);
229 } else if (uprv_strcmp(key, "middle") == 0) {
230 handleValueForPattern(value, middle, errorCode);
231 } else if (uprv_strcmp(key, "start") == 0) {
232 handleValueForPattern(value, start, errorCode);
233 }
234 }
235 }
236 };
237
238 // Virtual destructors must be defined out of line.
~ListPatternsSink()239 ListFormatter::ListPatternsSink::~ListPatternsSink() {}
240
loadListFormatInternal(const Locale & locale,const char * style,UErrorCode & errorCode)241 ListFormatInternal* ListFormatter::loadListFormatInternal(
242 const Locale& locale, const char * style, UErrorCode& errorCode) {
243 UResourceBundle* rb = ures_open(nullptr, locale.getName(), &errorCode);
244 rb = ures_getByKeyWithFallback(rb, "listPattern", rb, &errorCode);
245 if (U_FAILURE(errorCode)) {
246 ures_close(rb);
247 return nullptr;
248 }
249 ListFormatter::ListPatternsSink sink;
250 char currentStyle[kStyleLenMax+1];
251 uprv_strncpy(currentStyle, style, kStyleLenMax);
252 currentStyle[kStyleLenMax] = 0;
253
254 for (;;) {
255 ures_getAllItemsWithFallback(rb, currentStyle, sink, errorCode);
256 if (U_FAILURE(errorCode) || sink.aliasedStyle[0] == 0 || uprv_strcmp(currentStyle, sink.aliasedStyle) == 0) {
257 break;
258 }
259 uprv_strcpy(currentStyle, sink.aliasedStyle);
260 }
261 ures_close(rb);
262 if (U_FAILURE(errorCode)) {
263 return nullptr;
264 }
265 if (sink.two.isEmpty() || sink.start.isEmpty() || sink.middle.isEmpty() || sink.end.isEmpty()) {
266 errorCode = U_MISSING_RESOURCE_ERROR;
267 return nullptr;
268 }
269 ListFormatInternal* result = new ListFormatInternal(sink.two, sink.start, sink.middle, sink.end, errorCode);
270 if (result == nullptr) {
271 errorCode = U_MEMORY_ALLOCATION_ERROR;
272 return nullptr;
273 }
274 if (U_FAILURE(errorCode)) {
275 delete result;
276 return nullptr;
277 }
278 return result;
279 }
280
createInstance(UErrorCode & errorCode)281 ListFormatter* ListFormatter::createInstance(UErrorCode& errorCode) {
282 Locale locale; // The default locale.
283 return createInstance(locale, errorCode);
284 }
285
createInstance(const Locale & locale,UErrorCode & errorCode)286 ListFormatter* ListFormatter::createInstance(const Locale& locale, UErrorCode& errorCode) {
287 return createInstance(locale, STANDARD_STYLE, errorCode);
288 }
289
createInstance(const Locale & locale,const char * style,UErrorCode & errorCode)290 ListFormatter* ListFormatter::createInstance(const Locale& locale, const char *style, UErrorCode& errorCode) {
291 const ListFormatInternal* listFormatInternal = getListFormatInternal(locale, style, errorCode);
292 if (U_FAILURE(errorCode)) {
293 return nullptr;
294 }
295 ListFormatter* p = new ListFormatter(listFormatInternal);
296 if (p == nullptr) {
297 errorCode = U_MEMORY_ALLOCATION_ERROR;
298 return nullptr;
299 }
300 return p;
301 }
302
ListFormatter(const ListFormatData & listFormatData,UErrorCode & errorCode)303 ListFormatter::ListFormatter(const ListFormatData& listFormatData, UErrorCode &errorCode) {
304 owned = new ListFormatInternal(listFormatData, errorCode);
305 data = owned;
306 }
307
ListFormatter(const ListFormatInternal * listFormatterInternal)308 ListFormatter::ListFormatter(const ListFormatInternal* listFormatterInternal) : owned(nullptr), data(listFormatterInternal) {
309 }
310
~ListFormatter()311 ListFormatter::~ListFormatter() {
312 delete owned;
313 }
314
315 /**
316 * Joins first and second using the pattern pat.
317 * On entry offset is an offset into first or -1 if offset unspecified.
318 * On exit offset is offset of second in result if recordOffset was set
319 * Otherwise if it was >=0 it is set to point into result where it used
320 * to point into first. On exit, result is the join of first and second
321 * according to pat. Any previous value of result gets replaced.
322 */
joinStringsAndReplace(const SimpleFormatter & pat,const UnicodeString & first,const UnicodeString & second,UnicodeString & result,UBool recordOffset,int32_t & offset,int32_t * offsetFirst,int32_t * offsetSecond,UErrorCode & errorCode)323 static void joinStringsAndReplace(
324 const SimpleFormatter& pat,
325 const UnicodeString& first,
326 const UnicodeString& second,
327 UnicodeString &result,
328 UBool recordOffset,
329 int32_t &offset,
330 int32_t *offsetFirst,
331 int32_t *offsetSecond,
332 UErrorCode& errorCode) {
333 if (U_FAILURE(errorCode)) {
334 return;
335 }
336 const UnicodeString *params[2] = {&first, &second};
337 int32_t offsets[2];
338 pat.formatAndReplace(
339 params,
340 UPRV_LENGTHOF(params),
341 result,
342 offsets,
343 UPRV_LENGTHOF(offsets),
344 errorCode);
345 if (U_FAILURE(errorCode)) {
346 return;
347 }
348 if (offsets[0] == -1 || offsets[1] == -1) {
349 errorCode = U_INVALID_FORMAT_ERROR;
350 return;
351 }
352 if (recordOffset) {
353 offset = offsets[1];
354 } else if (offset >= 0) {
355 offset += offsets[0];
356 }
357 if (offsetFirst != nullptr) *offsetFirst = offsets[0];
358 if (offsetSecond != nullptr) *offsetSecond = offsets[1];
359 }
360
format(const UnicodeString items[],int32_t nItems,UnicodeString & appendTo,UErrorCode & errorCode) const361 UnicodeString& ListFormatter::format(
362 const UnicodeString items[],
363 int32_t nItems,
364 UnicodeString& appendTo,
365 UErrorCode& errorCode) const {
366 int32_t offset;
367 return format(items, nItems, appendTo, -1, offset, errorCode);
368 }
369
370 #if !UCONFIG_NO_FORMATTING
format(const UnicodeString items[],int32_t nItems,UnicodeString & appendTo,FieldPositionIterator * posIter,UErrorCode & errorCode) const371 UnicodeString& ListFormatter::format(
372 const UnicodeString items[],
373 int32_t nItems,
374 UnicodeString & appendTo,
375 FieldPositionIterator* posIter,
376 UErrorCode& errorCode) const {
377 int32_t offset;
378 FieldPositionIteratorHandler handler(posIter, errorCode);
379 return format_(items, nItems, appendTo, -1, offset, &handler, errorCode);
380 };
381 #endif
382
format(const UnicodeString items[],int32_t nItems,UnicodeString & appendTo,int32_t index,int32_t & offset,UErrorCode & errorCode) const383 UnicodeString& ListFormatter::format(
384 const UnicodeString items[],
385 int32_t nItems,
386 UnicodeString& appendTo,
387 int32_t index,
388 int32_t &offset,
389 UErrorCode& errorCode) const {
390 return format_(items, nItems, appendTo, index, offset, nullptr, errorCode);
391 }
392
format_(const UnicodeString items[],int32_t nItems,UnicodeString & appendTo,int32_t index,int32_t & offset,FieldPositionHandler * handler,UErrorCode & errorCode) const393 UnicodeString& ListFormatter::format_(
394 const UnicodeString items[],
395 int32_t nItems,
396 UnicodeString& appendTo,
397 int32_t index,
398 int32_t &offset,
399 FieldPositionHandler* handler,
400 UErrorCode& errorCode) const {
401 #if !UCONFIG_NO_FORMATTING
402 offset = -1;
403 if (U_FAILURE(errorCode)) {
404 return appendTo;
405 }
406 if (data == nullptr) {
407 errorCode = U_INVALID_STATE_ERROR;
408 return appendTo;
409 }
410
411 if (nItems <= 0) {
412 return appendTo;
413 }
414 if (nItems == 1) {
415 if (index == 0) {
416 offset = appendTo.length();
417 }
418 if (handler != nullptr) {
419 handler->addAttribute(ULISTFMT_ELEMENT_FIELD,
420 appendTo.length(),
421 appendTo.length() + items[0].length());
422 }
423 appendTo.append(items[0]);
424 return appendTo;
425 }
426 UnicodeString result(items[0]);
427 if (index == 0) {
428 offset = 0;
429 }
430 int32_t offsetFirst;
431 int32_t offsetSecond;
432 int32_t prefixLength = 0;
433 // for n items, there are 2 * (n + 1) boundary including 0 and the upper
434 // edge.
435 MaybeStackArray<int32_t, 10> offsets((handler != nullptr) ? 2 * (nItems + 1): 0);
436 joinStringsAndReplace(
437 nItems == 2 ? data->twoPattern : data->startPattern,
438 result,
439 items[1],
440 result,
441 index == 1,
442 offset,
443 &offsetFirst,
444 &offsetSecond,
445 errorCode);
446 if (handler != nullptr) {
447 offsets[0] = 0;
448 prefixLength += offsetFirst;
449 offsets[1] = offsetSecond - prefixLength;
450 }
451 if (nItems > 2) {
452 for (int32_t i = 2; i < nItems - 1; ++i) {
453 joinStringsAndReplace(
454 data->middlePattern,
455 result,
456 items[i],
457 result,
458 index == i,
459 offset,
460 &offsetFirst,
461 &offsetSecond,
462 errorCode);
463 if (handler != nullptr) {
464 prefixLength += offsetFirst;
465 offsets[i] = offsetSecond - prefixLength;
466 }
467 }
468 joinStringsAndReplace(
469 data->endPattern,
470 result,
471 items[nItems - 1],
472 result,
473 index == nItems - 1,
474 offset,
475 &offsetFirst,
476 &offsetSecond,
477 errorCode);
478 if (handler != nullptr) {
479 prefixLength += offsetFirst;
480 offsets[nItems - 1] = offsetSecond - prefixLength;
481 }
482 }
483 if (handler != nullptr) {
484 // If there are already some data in appendTo, we need to adjust the index
485 // by shifting that lenght while insert into handler.
486 int32_t shift = appendTo.length() + prefixLength;
487 // Output the ULISTFMT_ELEMENT_FIELD in the order of the input elements
488 for (int32_t i = 0; i < nItems; ++i) {
489 offsets[i + nItems] = offsets[i] + items[i].length() + shift;
490 offsets[i] += shift;
491 handler->addAttribute(
492 ULISTFMT_ELEMENT_FIELD, // id
493 offsets[i], // index
494 offsets[i + nItems]); // limit
495 }
496 // The locale pattern may reorder the items (such as in ur-IN locale),
497 // so we cannot assume the array is in accendning order.
498 // To handle the edging case, just insert the two ends into the array
499 // and sort. Then we output ULISTFMT_LITERAL_FIELD if the indecies
500 // between the even and odd position are not the same in the sorted array.
501 offsets[2 * nItems] = shift - prefixLength;
502 offsets[2 * nItems + 1] = result.length() + shift - prefixLength;
503 uprv_sortArray(offsets.getAlias(), 2 * (nItems + 1), sizeof(int32_t),
504 uprv_int32Comparator, nullptr,
505 false, &errorCode);
506 for (int32_t i = 0; i <= nItems; ++i) {
507 if (offsets[i * 2] != offsets[i * 2 + 1]) {
508 handler->addAttribute(
509 ULISTFMT_LITERAL_FIELD, // id
510 offsets[i * 2], // index
511 offsets[i * 2 + 1]); // limit
512 }
513 }
514 }
515 if (U_SUCCESS(errorCode)) {
516 if (offset >= 0) {
517 offset += appendTo.length();
518 }
519 appendTo += result;
520 }
521 #endif
522 return appendTo;
523 }
524
525 U_NAMESPACE_END
526