OLD | NEW |
1 /* | 1 /* |
2 ******************************************************************************* | 2 ******************************************************************************* |
3 * | 3 * |
4 * Copyright (C) 2013-2014, International Business Machines | 4 * Copyright (C) 2013-2014, International Business Machines |
5 * Corporation and others. All Rights Reserved. | 5 * Corporation and others. All Rights Reserved. |
6 * | 6 * |
7 ******************************************************************************* | 7 ******************************************************************************* |
8 * file name: listformatter.cpp | 8 * file name: listformatter.cpp |
9 * encoding: US-ASCII | 9 * encoding: US-ASCII |
10 * tab size: 8 (not used) | 10 * tab size: 8 (not used) |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 static ListFormatInternal* loadListFormatInternal( | 171 static ListFormatInternal* loadListFormatInternal( |
172 const Locale& locale, const char * style, UErrorCode& errorCode) { | 172 const Locale& locale, const char * style, UErrorCode& errorCode) { |
173 UResourceBundle* rb = ures_open(NULL, locale.getName(), &errorCode); | 173 UResourceBundle* rb = ures_open(NULL, locale.getName(), &errorCode); |
174 if (U_FAILURE(errorCode)) { | 174 if (U_FAILURE(errorCode)) { |
175 ures_close(rb); | 175 ures_close(rb); |
176 return NULL; | 176 return NULL; |
177 } | 177 } |
178 rb = ures_getByKeyWithFallback(rb, "listPattern", rb, &errorCode); | 178 rb = ures_getByKeyWithFallback(rb, "listPattern", rb, &errorCode); |
179 rb = ures_getByKeyWithFallback(rb, style, rb, &errorCode); | 179 rb = ures_getByKeyWithFallback(rb, style, rb, &errorCode); |
180 | 180 |
181 // TODO(Travis Keep): This is a hack until fallbacks can be added for | |
182 // listPattern/duration and listPattern/duration-narrow in CLDR. | |
183 if (errorCode == U_MISSING_RESOURCE_ERROR) { | |
184 errorCode = U_ZERO_ERROR; | |
185 rb = ures_getByKeyWithFallback(rb, "standard", rb, &errorCode); | |
186 } | |
187 if (U_FAILURE(errorCode)) { | 181 if (U_FAILURE(errorCode)) { |
188 ures_close(rb); | 182 ures_close(rb); |
189 return NULL; | 183 return NULL; |
190 } | 184 } |
191 UnicodeString two, start, middle, end; | 185 UnicodeString two, start, middle, end; |
192 getStringByKey(rb, "2", two, errorCode); | 186 getStringByKey(rb, "2", two, errorCode); |
193 getStringByKey(rb, "start", start, errorCode); | 187 getStringByKey(rb, "start", start, errorCode); |
194 getStringByKey(rb, "middle", middle, errorCode); | 188 getStringByKey(rb, "middle", middle, errorCode); |
195 getStringByKey(rb, "end", end, errorCode); | 189 getStringByKey(rb, "end", end, errorCode); |
196 ures_close(rb); | 190 ures_close(rb); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 | 241 |
248 ListFormatter::~ListFormatter() { | 242 ListFormatter::~ListFormatter() { |
249 delete owned; | 243 delete owned; |
250 } | 244 } |
251 | 245 |
252 /** | 246 /** |
253 * Joins first and second using the pattern pat. | 247 * Joins first and second using the pattern pat. |
254 * On entry offset is an offset into first or -1 if offset unspecified. | 248 * On entry offset is an offset into first or -1 if offset unspecified. |
255 * On exit offset is offset of second in result if recordOffset was set | 249 * On exit offset is offset of second in result if recordOffset was set |
256 * Otherwise if it was >=0 it is set to point into result where it used | 250 * Otherwise if it was >=0 it is set to point into result where it used |
257 * to point into first. | 251 * to point into first. On exit, result is the join of first and second |
| 252 * according to pat. Any previous value of result gets replaced. |
258 */ | 253 */ |
259 static void joinStrings( | 254 static void joinStringsAndReplace( |
260 const SimplePatternFormatter& pat, | 255 const SimplePatternFormatter& pat, |
261 const UnicodeString& first, | 256 const UnicodeString& first, |
262 const UnicodeString& second, | 257 const UnicodeString& second, |
263 UnicodeString &result, | 258 UnicodeString &result, |
264 UBool recordOffset, | 259 UBool recordOffset, |
265 int32_t &offset, | 260 int32_t &offset, |
266 UErrorCode& errorCode) { | 261 UErrorCode& errorCode) { |
267 if (U_FAILURE(errorCode)) { | 262 if (U_FAILURE(errorCode)) { |
268 return; | 263 return; |
269 } | 264 } |
270 const UnicodeString *params[2] = {&first, &second}; | 265 const UnicodeString *params[2] = {&first, &second}; |
271 int32_t offsets[2]; | 266 int32_t offsets[2]; |
272 pat.format( | 267 pat.formatAndReplace( |
273 params, | 268 params, |
274 UPRV_LENGTHOF(params), | 269 UPRV_LENGTHOF(params), |
275 result, | 270 result, |
276 offsets, | 271 offsets, |
277 UPRV_LENGTHOF(offsets), | 272 UPRV_LENGTHOF(offsets), |
278 errorCode); | 273 errorCode); |
279 if (U_FAILURE(errorCode)) { | 274 if (U_FAILURE(errorCode)) { |
280 return; | 275 return; |
281 } | 276 } |
282 if (offsets[0] == -1 || offsets[1] == -1) { | 277 if (offsets[0] == -1 || offsets[1] == -1) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 if (nItems <= 0) { | 313 if (nItems <= 0) { |
319 return appendTo; | 314 return appendTo; |
320 } | 315 } |
321 if (nItems == 1) { | 316 if (nItems == 1) { |
322 if (index == 0) { | 317 if (index == 0) { |
323 offset = appendTo.length(); | 318 offset = appendTo.length(); |
324 } | 319 } |
325 appendTo.append(items[0]); | 320 appendTo.append(items[0]); |
326 return appendTo; | 321 return appendTo; |
327 } | 322 } |
328 if (nItems == 2) { | 323 UnicodeString result(items[0]); |
329 if (index == 0) { | |
330 offset = 0; | |
331 } | |
332 joinStrings( | |
333 data->twoPattern, | |
334 items[0], | |
335 items[1], | |
336 appendTo, | |
337 index == 1, | |
338 offset, | |
339 errorCode); | |
340 return appendTo; | |
341 } | |
342 UnicodeString temp[2]; | |
343 if (index == 0) { | 324 if (index == 0) { |
344 offset = 0; | 325 offset = 0; |
345 } | 326 } |
346 joinStrings( | 327 joinStringsAndReplace( |
347 data->startPattern, | 328 nItems == 2 ? data->twoPattern : data->startPattern, |
348 items[0], | 329 result, |
349 items[1], | 330 items[1], |
350 temp[0], | 331 result, |
351 index == 1, | 332 index == 1, |
352 offset, | 333 offset, |
353 errorCode); | 334 errorCode); |
354 int32_t i; | 335 if (nItems > 2) { |
355 int32_t pos = 0; | 336 for (int32_t i = 2; i < nItems - 1; ++i) { |
356 int32_t npos = 0; | 337 joinStringsAndReplace( |
357 UBool startsWithZeroPlaceholder = | 338 data->middlePattern, |
358 data->middlePattern.startsWithPlaceholder(0); | 339 result, |
359 for (i = 2; i < nItems - 1; ++i) { | 340 items[i], |
360 if (!startsWithZeroPlaceholder) { | 341 result, |
361 npos = (pos + 1) & 1; | 342 index == i, |
362 temp[npos].remove(); | 343 offset, |
363 } | 344 errorCode); |
364 joinStrings( | 345 } |
365 data->middlePattern, | 346 joinStringsAndReplace( |
366 temp[pos], | 347 data->endPattern, |
367 items[i], | 348 result, |
368 temp[npos], | 349 items[nItems - 1], |
369 index == i, | 350 result, |
370 offset, | 351 index == nItems - 1, |
371 errorCode); | 352 offset, |
372 pos = npos; | 353 errorCode); |
373 } | 354 } |
374 if (!data->endPattern.startsWithPlaceholder(0)) { | |
375 npos = (pos + 1) & 1; | |
376 temp[npos].remove(); | |
377 } | |
378 joinStrings( | |
379 data->endPattern, | |
380 temp[pos], | |
381 items[nItems - 1], | |
382 temp[npos], | |
383 index == nItems - 1, | |
384 offset, | |
385 errorCode); | |
386 if (U_SUCCESS(errorCode)) { | 355 if (U_SUCCESS(errorCode)) { |
387 if (offset >= 0) { | 356 if (offset >= 0) { |
388 offset += appendTo.length(); | 357 offset += appendTo.length(); |
389 } | 358 } |
390 appendTo += temp[npos]; | 359 appendTo += result; |
391 } | 360 } |
392 return appendTo; | 361 return appendTo; |
393 } | 362 } |
394 | 363 |
395 U_NAMESPACE_END | 364 U_NAMESPACE_END |
OLD | NEW |