]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/udatpg.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / i18n / udatpg.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
46f4442e
A
3/*
4*******************************************************************************
5*
2ca993e8 6* Copyright (C) 2009-2015, International Business Machines
46f4442e
A
7* Corporation and others. All Rights Reserved.
8*
9*******************************************************************************
10* file name: udatpg.cpp
f3c0d7a5 11* encoding: UTF-8
46f4442e
A
12* tab size: 8 (not used)
13* indentation:4
14*
15* created on: 2007jul30
16* created by: Markus W. Scherer
17*/
18
19#include "unicode/utypes.h"
20
21#if !UCONFIG_NO_FORMATTING
22
23#include "unicode/udatpg.h"
24#include "unicode/uenum.h"
25#include "unicode/strenum.h"
b331163b 26#include "unicode/unistr.h"
46f4442e 27#include "unicode/dtptngen.h"
b331163b 28#include "unicode/uchar.h"
46f4442e 29#include "ustrenum.h"
b331163b 30#include "dtitv_impl.h"
46f4442e
A
31
32U_NAMESPACE_USE
33
51004dcb 34U_CAPI UDateTimePatternGenerator * U_EXPORT2
46f4442e
A
35udatpg_open(const char *locale, UErrorCode *pErrorCode) {
36 if(locale==NULL) {
37 return (UDateTimePatternGenerator *)DateTimePatternGenerator::createInstance(*pErrorCode);
38 } else {
39 return (UDateTimePatternGenerator *)DateTimePatternGenerator::createInstance(Locale(locale), *pErrorCode);
40 }
41}
42
51004dcb 43U_CAPI UDateTimePatternGenerator * U_EXPORT2
46f4442e
A
44udatpg_openEmpty(UErrorCode *pErrorCode) {
45 return (UDateTimePatternGenerator *)DateTimePatternGenerator::createEmptyInstance(*pErrorCode);
46}
47
51004dcb 48U_CAPI void U_EXPORT2
46f4442e
A
49udatpg_close(UDateTimePatternGenerator *dtpg) {
50 delete (DateTimePatternGenerator *)dtpg;
51}
52
51004dcb 53U_CAPI UDateTimePatternGenerator * U_EXPORT2
46f4442e
A
54udatpg_clone(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
55 if(U_FAILURE(*pErrorCode)) {
56 return NULL;
57 }
58 return (UDateTimePatternGenerator *)(((const DateTimePatternGenerator *)dtpg)->clone());
59}
60
51004dcb 61U_CAPI int32_t U_EXPORT2
46f4442e
A
62udatpg_getBestPattern(UDateTimePatternGenerator *dtpg,
63 const UChar *skeleton, int32_t length,
64 UChar *bestPattern, int32_t capacity,
65 UErrorCode *pErrorCode) {
729e4ab9
A
66 return udatpg_getBestPatternWithOptions(dtpg, skeleton, length,
67 UDATPG_MATCH_NO_OPTIONS,
68 bestPattern, capacity, pErrorCode);
69}
70
51004dcb 71U_CAPI int32_t U_EXPORT2
729e4ab9
A
72udatpg_getBestPatternWithOptions(UDateTimePatternGenerator *dtpg,
73 const UChar *skeleton, int32_t length,
74 UDateTimePatternMatchOptions options,
75 UChar *bestPattern, int32_t capacity,
76 UErrorCode *pErrorCode) {
46f4442e
A
77 if(U_FAILURE(*pErrorCode)) {
78 return 0;
79 }
80 if(skeleton==NULL && length!=0) {
81 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
82 return 0;
83 }
84 UnicodeString skeletonString((UBool)(length<0), skeleton, length);
729e4ab9 85 UnicodeString result=((DateTimePatternGenerator *)dtpg)->getBestPattern(skeletonString, options, *pErrorCode);
46f4442e
A
86 return result.extract(bestPattern, capacity, *pErrorCode);
87}
88
51004dcb 89U_CAPI int32_t U_EXPORT2
2ca993e8 90udatpg_getSkeleton(UDateTimePatternGenerator * /* dtpg */,
46f4442e
A
91 const UChar *pattern, int32_t length,
92 UChar *skeleton, int32_t capacity,
93 UErrorCode *pErrorCode) {
94 if(U_FAILURE(*pErrorCode)) {
95 return 0;
96 }
97 if(pattern==NULL && length!=0) {
98 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
99 return 0;
100 }
101 UnicodeString patternString((UBool)(length<0), pattern, length);
2ca993e8
A
102 UnicodeString result=DateTimePatternGenerator::staticGetSkeleton(
103 patternString, *pErrorCode);
46f4442e
A
104 return result.extract(skeleton, capacity, *pErrorCode);
105}
106
51004dcb 107U_CAPI int32_t U_EXPORT2
2ca993e8 108udatpg_getBaseSkeleton(UDateTimePatternGenerator * /* dtpg */,
46f4442e
A
109 const UChar *pattern, int32_t length,
110 UChar *skeleton, int32_t capacity,
111 UErrorCode *pErrorCode) {
112 if(U_FAILURE(*pErrorCode)) {
113 return 0;
114 }
115 if(pattern==NULL && length!=0) {
116 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
117 return 0;
118 }
119 UnicodeString patternString((UBool)(length<0), pattern, length);
2ca993e8
A
120 UnicodeString result=DateTimePatternGenerator::staticGetBaseSkeleton(
121 patternString, *pErrorCode);
46f4442e
A
122 return result.extract(skeleton, capacity, *pErrorCode);
123}
124
51004dcb 125U_CAPI UDateTimePatternConflict U_EXPORT2
46f4442e
A
126udatpg_addPattern(UDateTimePatternGenerator *dtpg,
127 const UChar *pattern, int32_t patternLength,
128 UBool override,
129 UChar *conflictingPattern, int32_t capacity, int32_t *pLength,
130 UErrorCode *pErrorCode) {
131 if(U_FAILURE(*pErrorCode)) {
132 return UDATPG_NO_CONFLICT;
133 }
134 if(pattern==NULL && patternLength!=0) {
135 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
136 return UDATPG_NO_CONFLICT;
137 }
138 UnicodeString patternString((UBool)(patternLength<0), pattern, patternLength);
139 UnicodeString conflictingPatternString;
140 UDateTimePatternConflict result=((DateTimePatternGenerator *)dtpg)->
141 addPattern(patternString, override, conflictingPatternString, *pErrorCode);
142 int32_t length=conflictingPatternString.extract(conflictingPattern, capacity, *pErrorCode);
143 if(pLength!=NULL) {
144 *pLength=length;
145 }
146 return result;
147}
148
51004dcb 149U_CAPI void U_EXPORT2
46f4442e
A
150udatpg_setAppendItemFormat(UDateTimePatternGenerator *dtpg,
151 UDateTimePatternField field,
152 const UChar *value, int32_t length) {
153 UnicodeString valueString((UBool)(length<0), value, length);
154 ((DateTimePatternGenerator *)dtpg)->setAppendItemFormat(field, valueString);
155}
156
51004dcb 157U_CAPI const UChar * U_EXPORT2
46f4442e
A
158udatpg_getAppendItemFormat(const UDateTimePatternGenerator *dtpg,
159 UDateTimePatternField field,
160 int32_t *pLength) {
161 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getAppendItemFormat(field);
162 if(pLength!=NULL) {
163 *pLength=result.length();
164 }
165 return result.getBuffer();
166}
167
51004dcb 168U_CAPI void U_EXPORT2
46f4442e
A
169udatpg_setAppendItemName(UDateTimePatternGenerator *dtpg,
170 UDateTimePatternField field,
171 const UChar *value, int32_t length) {
172 UnicodeString valueString((UBool)(length<0), value, length);
173 ((DateTimePatternGenerator *)dtpg)->setAppendItemName(field, valueString);
174}
175
51004dcb 176U_CAPI const UChar * U_EXPORT2
46f4442e
A
177udatpg_getAppendItemName(const UDateTimePatternGenerator *dtpg,
178 UDateTimePatternField field,
179 int32_t *pLength) {
180 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getAppendItemName(field);
181 if(pLength!=NULL) {
182 *pLength=result.length();
183 }
184 return result.getBuffer();
185}
186
0f5d89e8
A
187U_CAPI int32_t U_EXPORT2
188udatpg_getFieldDisplayName(const UDateTimePatternGenerator *dtpg,
189 UDateTimePatternField field,
190 UDateTimePGDisplayWidth width,
191 UChar *fieldName, int32_t capacity,
192 UErrorCode *pErrorCode) {
193 if (U_FAILURE(*pErrorCode))
194 return -1;
195 if (fieldName == NULL ? capacity != 0 : capacity < 0) {
196 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
197 return -1;
198 }
199 UnicodeString result = ((const DateTimePatternGenerator *)dtpg)->getFieldDisplayName(field,width);
200 if (fieldName == NULL) {
201 return result.length();
202 }
203 return result.extract(fieldName, capacity, *pErrorCode);
204}
205
51004dcb 206U_CAPI void U_EXPORT2
46f4442e
A
207udatpg_setDateTimeFormat(const UDateTimePatternGenerator *dtpg,
208 const UChar *dtFormat, int32_t length) {
209 UnicodeString dtFormatString((UBool)(length<0), dtFormat, length);
210 ((DateTimePatternGenerator *)dtpg)->setDateTimeFormat(dtFormatString);
211}
212
51004dcb 213U_CAPI const UChar * U_EXPORT2
46f4442e
A
214udatpg_getDateTimeFormat(const UDateTimePatternGenerator *dtpg,
215 int32_t *pLength) {
216 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDateTimeFormat();
217 if(pLength!=NULL) {
218 *pLength=result.length();
219 }
220 return result.getBuffer();
221}
222
51004dcb 223U_CAPI void U_EXPORT2
46f4442e
A
224udatpg_setDecimal(UDateTimePatternGenerator *dtpg,
225 const UChar *decimal, int32_t length) {
226 UnicodeString decimalString((UBool)(length<0), decimal, length);
227 ((DateTimePatternGenerator *)dtpg)->setDecimal(decimalString);
228}
229
51004dcb 230U_CAPI const UChar * U_EXPORT2
46f4442e
A
231udatpg_getDecimal(const UDateTimePatternGenerator *dtpg,
232 int32_t *pLength) {
233 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDecimal();
234 if(pLength!=NULL) {
235 *pLength=result.length();
236 }
237 return result.getBuffer();
238}
239
51004dcb 240U_CAPI int32_t U_EXPORT2
46f4442e
A
241udatpg_replaceFieldTypes(UDateTimePatternGenerator *dtpg,
242 const UChar *pattern, int32_t patternLength,
243 const UChar *skeleton, int32_t skeletonLength,
244 UChar *dest, int32_t destCapacity,
245 UErrorCode *pErrorCode) {
729e4ab9
A
246 return udatpg_replaceFieldTypesWithOptions(dtpg, pattern, patternLength, skeleton, skeletonLength,
247 UDATPG_MATCH_NO_OPTIONS,
248 dest, destCapacity, pErrorCode);
249}
250
51004dcb 251U_CAPI int32_t U_EXPORT2
729e4ab9
A
252udatpg_replaceFieldTypesWithOptions(UDateTimePatternGenerator *dtpg,
253 const UChar *pattern, int32_t patternLength,
254 const UChar *skeleton, int32_t skeletonLength,
255 UDateTimePatternMatchOptions options,
256 UChar *dest, int32_t destCapacity,
257 UErrorCode *pErrorCode) {
46f4442e
A
258 if(U_FAILURE(*pErrorCode)) {
259 return 0;
260 }
261 if((pattern==NULL && patternLength!=0) || (skeleton==NULL && skeletonLength!=0)) {
262 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
263 return 0;
264 }
265 UnicodeString patternString((UBool)(patternLength<0), pattern, patternLength);
266 UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength);
729e4ab9 267 UnicodeString result=((DateTimePatternGenerator *)dtpg)->replaceFieldTypes(patternString, skeletonString, options, *pErrorCode);
46f4442e
A
268 return result.extract(dest, destCapacity, *pErrorCode);
269}
270
51004dcb 271U_CAPI UEnumeration * U_EXPORT2
46f4442e 272udatpg_openSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
729e4ab9 273 return uenum_openFromStringEnumeration(
46f4442e
A
274 ((DateTimePatternGenerator *)dtpg)->getSkeletons(*pErrorCode),
275 pErrorCode);
276}
277
51004dcb 278U_CAPI UEnumeration * U_EXPORT2
46f4442e 279udatpg_openBaseSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
729e4ab9 280 return uenum_openFromStringEnumeration(
46f4442e
A
281 ((DateTimePatternGenerator *)dtpg)->getBaseSkeletons(*pErrorCode),
282 pErrorCode);
283}
284
51004dcb 285U_CAPI const UChar * U_EXPORT2
46f4442e
A
286udatpg_getPatternForSkeleton(const UDateTimePatternGenerator *dtpg,
287 const UChar *skeleton, int32_t skeletonLength,
288 int32_t *pLength) {
289 UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength);
290 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getPatternForSkeleton(skeletonString);
291 if(pLength!=NULL) {
292 *pLength=result.length();
293 }
294 return result.getBuffer();
295}
296
b331163b
A
297// Helper function for uadatpg_remapPatternWithOptionsLoc
298static int32_t
299_doReplaceAndReturnAdj( UDateTimePatternGenerator *dtpg, uint32_t options, UBool matchHourLen,
300 UnicodeString &patternString, const UnicodeString &skeleton, const UnicodeString &otherCycSkeleton,
301 int32_t timePatStart, int32_t timePatLimit, int32_t timeNonHourStart, int32_t timeNonHourLimit,
302 UErrorCode *pErrorCode) {
303 if (matchHourLen) {
304 options |= UDATPG_MATCH_HOUR_FIELD_LENGTH;
305 }
306 UnicodeString replacement=((DateTimePatternGenerator *)dtpg)->getBestPattern(otherCycSkeleton, (UDateTimePatternMatchOptions)options, *pErrorCode);
307 if (U_FAILURE(*pErrorCode)) {
308 return 0;
309 }
0f5d89e8 310 UnicodeString stringForOrigSkeleton=((DateTimePatternGenerator *)dtpg)->getBestPattern(skeleton, UDATPG_MATCH_ALL_FIELDS_LENGTH, *pErrorCode); // match orig field lengths
b331163b
A
311 if (U_SUCCESS(*pErrorCode)) {
312 int32_t index = patternString.indexOf(stringForOrigSkeleton);
313 if (index >= 0) {
314 int32_t stringForOrigSkelLen = stringForOrigSkeleton.length();
315 patternString.replace(index, stringForOrigSkelLen, replacement);
316 return replacement.length() - stringForOrigSkelLen;
317 }
318 } else {
319 *pErrorCode = U_ZERO_ERROR;
320 }
321 if (timeNonHourStart >= 0 && timeNonHourLimit > timeNonHourStart) {
322 // find any minutes/seconds/milliseconds part of replacement, set that back to the
323 // minutes/seconds/milliseconds part of the original pattern.
324 // First get the minutes/seconds/milliseconds part of the original pattern.
325 UnicodeString nonHour;
326 patternString.extractBetween(timeNonHourStart, timeNonHourLimit, nonHour);
327 // Now scan to find position from just after hours to end of minutes/seconds/milliseconds.
328 timeNonHourStart = -1;
329 timeNonHourLimit = 0;
330 UBool inQuoted = FALSE;
331 int32_t repPos, repLen = replacement.length();
332 for (repPos = 0; repPos < repLen; repPos++) {
333 UChar repChr = replacement.charAt(repPos);
334 if (repChr == 0x27 /* ASCII-range single quote */) {
335 inQuoted = !inQuoted;
336 } else if (!inQuoted) {
337 if (repChr==LOW_H || repChr==CAP_H || repChr==CAP_K || repChr==LOW_K) { // hHKk, hour
338 timeNonHourStart = repPos + 1;
339 } else if (timeNonHourStart < 0 && (repChr==LOW_M || repChr==LOW_S)) { // 'm' or 's' and we did not have hour
340 timeNonHourStart = repPos;
341 }
342 if (!u_isWhitespace(repChr) && timeNonHourStart >= 0 && repChr!=LOW_A) { // NonHour portion should not include 'a'
343 timeNonHourLimit = repPos + 1;
344 }
345 }
346 }
347 // If we found minutes/seconds/milliseconds in replacement, restore that part to original.
348 if (timeNonHourStart >= 0 && timeNonHourLimit > timeNonHourStart) {
349 replacement.replaceBetween(timeNonHourStart, timeNonHourLimit, nonHour);
350 }
351 }
352 patternString.replaceBetween(timePatStart, timePatLimit, replacement);
353 return replacement.length() - (timePatLimit - timePatStart); // positive if replacement is longer
354}
355
356/*
357 * uadatpg_remapPatternWithOptionsLoc
358 *
359 * Thee general idea is:
360 * 1. Scan the pattern for one or more time subpatterns
361 * 2. For each time subpattern, if the hour pattern characters don't match the
362 * time cycle that we want to force to, then:
363 * a) Save the nonHour portion of the subpattern (from just after hours to end
364 * of minutes/seconds/ milliseconds)
365 * b) Turn the pattern characters in that subpattern into a skeleton, but with
366 * the hour pattern characters switched to the desired time cycle
367 * c) Use that skeleton to get the locale's corresponding replacement pattern
368 * for the desired time cycle (with all desired elements - min, sec, etc.)
369 * d) In that replacement pattern, find the new nonHour portion, and restore
370 * that to the original nonHour portion
371 * e) Finally, replace the original time subpattern with the adjusted
372 * replacement.
373 */
57a6839d
A
374U_CAPI int32_t U_EXPORT2
375uadatpg_remapPatternWithOptions(UDateTimePatternGenerator *dtpg,
376 const UChar *pattern, int32_t patternLength,
377 UDateTimePatternMatchOptions options,
378 UChar *newPattern, int32_t newPatternCapacity,
379 UErrorCode *pErrorCode) {
380 if (U_FAILURE(*pErrorCode)) {
381 return 0;
382 }
383 if ( pattern==NULL || ((newPattern==NULL)? newPatternCapacity!=0: newPatternCapacity<0) ) {
384 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
385 return 0;
386 }
387 UnicodeString patternString((patternLength < 0), pattern, patternLength);
388 UBool force12 = ((options & UADATPG_FORCE_HOUR_CYCLE_MASK) == UADATPG_FORCE_12_HOUR_CYCLE);
389 UBool force24 = ((options & UADATPG_FORCE_HOUR_CYCLE_MASK) == UADATPG_FORCE_24_HOUR_CYCLE);
390 if (force12 || force24) {
391 UBool inQuoted = FALSE;
392 UBool inTimePat = FALSE;
393 UBool needReplacement = FALSE;
394 int32_t timePatStart = 0;
395 int32_t timePatLimit = 0;
b331163b
A
396 int32_t timeNonHourStart = -1;
397 int32_t timeNonHourLimit = 0;
398 UnicodeString skeleton, otherCycSkeleton;
2ca993e8 399 UnicodeString timePatChars("abBhHKkmsSzZOvVXx", -1, US_INV); // all pattern chars for times
b331163b 400 int32_t numForcedH = 0;
57a6839d 401 int32_t patPos, patLen = patternString.length();
b331163b 402
57a6839d
A
403 for (patPos = 0; patPos < patLen; patPos++) {
404 UChar patChr = patternString.charAt(patPos);
b331163b 405 UChar otherCycPatChr = patChr;
57a6839d
A
406 if (patChr == 0x27 /* ASCII-range single quote */) {
407 inQuoted = !inQuoted;
408 } else if (!inQuoted) {
b331163b 409 if (timePatChars.indexOf(patChr) >= 0) {
57a6839d
A
410 // in a time pattern
411 if (!inTimePat) {
412 inTimePat = TRUE;
413 timePatStart = patPos;
b331163b 414 timeNonHourStart = -1;
57a6839d 415 skeleton.remove();
b331163b
A
416 otherCycSkeleton.remove();
417 numForcedH = 0;
57a6839d 418 }
b331163b
A
419 if (patChr==LOW_H || patChr==CAP_K) { // hK, hour, 12-hour cycle
420 if (force24) {
421 otherCycPatChr = CAP_H; // force to H
57a6839d 422 needReplacement = TRUE;
b331163b
A
423 timeNonHourStart = patPos + 1;
424 // If we are switching from a 12-hour cycle to a 24-hour cycle
425 // and the pattern for 12-hour cycle was zero-padded to 2 digits,
426 // make sure the new pattern for 24-hour cycle is also padded to
427 // 2 digits regardless of locale default, to match the
428 // expectations of the pattern provider. However we don't need
429 // to do this going the other direction (from 24- to 12-hour
430 // cycles, don't require that the 12-hour cycle has zero padding
431 // just because the 24-hour cycle did; the 12-hour cycle will
432 // add other elements such as a, so there wil be a length change
433 // anyway).
434 numForcedH++;
57a6839d 435 }
b331163b
A
436 } else if (patChr==CAP_H || patChr==LOW_K) { // Hk, hour, 24-hour cycle
437 if (force12) {
438 otherCycPatChr = LOW_H; // force to h
57a6839d 439 needReplacement = TRUE;
b331163b 440 timeNonHourStart = patPos + 1;
57a6839d 441 }
b331163b
A
442 } else if (timeNonHourStart < 0 && (patChr==LOW_M || patChr==LOW_S)) { // 'm' or 's' and we did not have hour
443 timeNonHourStart = patPos;
57a6839d
A
444 }
445 skeleton.append(patChr);
b331163b 446 otherCycSkeleton.append(otherCycPatChr);
57a6839d
A
447 } else if ((patChr >= 0x41 && patChr <= 0x5A) || (patChr >= 0x61 && patChr <= 0x7A)) {
448 // a non-time pattern character, forces end of any time pattern
449 if (inTimePat) {
450 inTimePat = FALSE;
451 if (needReplacement) {
452 needReplacement = FALSE;
453 // do replacement
b331163b
A
454 int32_t posAdjust = _doReplaceAndReturnAdj(dtpg, options, numForcedH >= 2, patternString, skeleton, otherCycSkeleton,
455 timePatStart, timePatLimit, timeNonHourStart, timeNonHourLimit, pErrorCode);
57a6839d
A
456 patLen += posAdjust;
457 patPos += posAdjust;
458 }
459 }
460 }
b331163b
A
461 if (inTimePat && !u_isWhitespace(patChr)) {
462 timePatLimit = patPos + 1;
2ca993e8 463 if (timeNonHourStart >= 0 && patChr!=LOW_A && patChr!=LOW_B && patChr!=CAP_B) { // NonHour portion should not include 'a','b','B'
b331163b
A
464 timeNonHourLimit = timePatLimit;
465 }
466 }
57a6839d
A
467 }
468 }
469 // end of string
b331163b 470 if (needReplacement) {
57a6839d 471 // do replacement
b331163b
A
472 _doReplaceAndReturnAdj(dtpg, options, numForcedH >= 2, patternString, skeleton, otherCycSkeleton,
473 timePatStart, timePatLimit, timeNonHourStart, timeNonHourLimit, pErrorCode);
57a6839d
A
474 }
475 }
476 return patternString.extract(newPattern, newPatternCapacity, *pErrorCode);
477}
478
46f4442e 479#endif