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) 2009-2015, International Business Machines
7* Corporation and others. All Rights Reserved.
8*
9*******************************************************************************
10* file name: udatpg.cpp
11* encoding: UTF-8
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"
26#include "unicode/dtptngen.h"
27#include "ustrenum.h"
28
29U_NAMESPACE_USE
30
31U_CAPI UDateTimePatternGenerator * U_EXPORT2
32udatpg_open(const char *locale, UErrorCode *pErrorCode) {
33 if(locale==nullptr) {
34 return (UDateTimePatternGenerator *)DateTimePatternGenerator::createInstance(status&: *pErrorCode);
35 } else {
36 return (UDateTimePatternGenerator *)DateTimePatternGenerator::createInstance(uLocale: Locale(locale), status&: *pErrorCode);
37 }
38}
39
40U_CAPI UDateTimePatternGenerator * U_EXPORT2
41udatpg_openEmpty(UErrorCode *pErrorCode) {
42 return (UDateTimePatternGenerator *)DateTimePatternGenerator::createEmptyInstance(status&: *pErrorCode);
43}
44
45U_CAPI void U_EXPORT2
46udatpg_close(UDateTimePatternGenerator *dtpg) {
47 delete (DateTimePatternGenerator *)dtpg;
48}
49
50U_CAPI UDateTimePatternGenerator * U_EXPORT2
51udatpg_clone(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
52 if(U_FAILURE(code: *pErrorCode)) {
53 return nullptr;
54 }
55 return (UDateTimePatternGenerator *)(((const DateTimePatternGenerator *)dtpg)->clone());
56}
57
58U_CAPI int32_t U_EXPORT2
59udatpg_getBestPattern(UDateTimePatternGenerator *dtpg,
60 const char16_t *skeleton, int32_t length,
61 char16_t *bestPattern, int32_t capacity,
62 UErrorCode *pErrorCode) {
63 return udatpg_getBestPatternWithOptions(dtpg, skeleton, length,
64 options: UDATPG_MATCH_NO_OPTIONS,
65 bestPattern, capacity, pErrorCode);
66}
67
68U_CAPI int32_t U_EXPORT2
69udatpg_getBestPatternWithOptions(UDateTimePatternGenerator *dtpg,
70 const char16_t *skeleton, int32_t length,
71 UDateTimePatternMatchOptions options,
72 char16_t *bestPattern, int32_t capacity,
73 UErrorCode *pErrorCode) {
74 if(U_FAILURE(code: *pErrorCode)) {
75 return 0;
76 }
77 if(skeleton==nullptr && length!=0) {
78 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
79 return 0;
80 }
81 UnicodeString skeletonString((UBool)(length<0), skeleton, length);
82 UnicodeString result=((DateTimePatternGenerator *)dtpg)->getBestPattern(skeleton: skeletonString, options, status&: *pErrorCode);
83 return result.extract(dest: bestPattern, destCapacity: capacity, errorCode&: *pErrorCode);
84}
85
86U_CAPI int32_t U_EXPORT2
87udatpg_getSkeleton(UDateTimePatternGenerator * /* dtpg */,
88 const char16_t *pattern, int32_t length,
89 char16_t *skeleton, int32_t capacity,
90 UErrorCode *pErrorCode) {
91 if(U_FAILURE(code: *pErrorCode)) {
92 return 0;
93 }
94 if(pattern==nullptr && length!=0) {
95 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
96 return 0;
97 }
98 UnicodeString patternString((UBool)(length<0), pattern, length);
99 UnicodeString result=DateTimePatternGenerator::staticGetSkeleton(
100 pattern: patternString, status&: *pErrorCode);
101 return result.extract(dest: skeleton, destCapacity: capacity, errorCode&: *pErrorCode);
102}
103
104U_CAPI int32_t U_EXPORT2
105udatpg_getBaseSkeleton(UDateTimePatternGenerator * /* dtpg */,
106 const char16_t *pattern, int32_t length,
107 char16_t *skeleton, int32_t capacity,
108 UErrorCode *pErrorCode) {
109 if(U_FAILURE(code: *pErrorCode)) {
110 return 0;
111 }
112 if(pattern==nullptr && length!=0) {
113 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
114 return 0;
115 }
116 UnicodeString patternString((UBool)(length<0), pattern, length);
117 UnicodeString result=DateTimePatternGenerator::staticGetBaseSkeleton(
118 pattern: patternString, status&: *pErrorCode);
119 return result.extract(dest: skeleton, destCapacity: capacity, errorCode&: *pErrorCode);
120}
121
122U_CAPI UDateTimePatternConflict U_EXPORT2
123udatpg_addPattern(UDateTimePatternGenerator *dtpg,
124 const char16_t *pattern, int32_t patternLength,
125 UBool override,
126 char16_t *conflictingPattern, int32_t capacity, int32_t *pLength,
127 UErrorCode *pErrorCode) {
128 if(U_FAILURE(code: *pErrorCode)) {
129 return UDATPG_NO_CONFLICT;
130 }
131 if(pattern==nullptr && patternLength!=0) {
132 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
133 return UDATPG_NO_CONFLICT;
134 }
135 UnicodeString patternString((UBool)(patternLength<0), pattern, patternLength);
136 UnicodeString conflictingPatternString;
137 UDateTimePatternConflict result=((DateTimePatternGenerator *)dtpg)->
138 addPattern(pattern: patternString, override, conflictingPattern&: conflictingPatternString, status&: *pErrorCode);
139 int32_t length=conflictingPatternString.extract(dest: conflictingPattern, destCapacity: capacity, errorCode&: *pErrorCode);
140 if(pLength!=nullptr) {
141 *pLength=length;
142 }
143 return result;
144}
145
146U_CAPI void U_EXPORT2
147udatpg_setAppendItemFormat(UDateTimePatternGenerator *dtpg,
148 UDateTimePatternField field,
149 const char16_t *value, int32_t length) {
150 UnicodeString valueString((UBool)(length<0), value, length);
151 ((DateTimePatternGenerator *)dtpg)->setAppendItemFormat(field, value: valueString);
152}
153
154U_CAPI const char16_t * U_EXPORT2
155udatpg_getAppendItemFormat(const UDateTimePatternGenerator *dtpg,
156 UDateTimePatternField field,
157 int32_t *pLength) {
158 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getAppendItemFormat(field);
159 if(pLength!=nullptr) {
160 *pLength=result.length();
161 }
162 return result.getBuffer();
163}
164
165U_CAPI void U_EXPORT2
166udatpg_setAppendItemName(UDateTimePatternGenerator *dtpg,
167 UDateTimePatternField field,
168 const char16_t *value, int32_t length) {
169 UnicodeString valueString((UBool)(length<0), value, length);
170 ((DateTimePatternGenerator *)dtpg)->setAppendItemName(field, value: valueString);
171}
172
173U_CAPI const char16_t * U_EXPORT2
174udatpg_getAppendItemName(const UDateTimePatternGenerator *dtpg,
175 UDateTimePatternField field,
176 int32_t *pLength) {
177 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getAppendItemName(field);
178 if(pLength!=nullptr) {
179 *pLength=result.length();
180 }
181 return result.getBuffer();
182}
183
184U_CAPI int32_t U_EXPORT2
185udatpg_getFieldDisplayName(const UDateTimePatternGenerator *dtpg,
186 UDateTimePatternField field,
187 UDateTimePGDisplayWidth width,
188 char16_t *fieldName, int32_t capacity,
189 UErrorCode *pErrorCode) {
190 if (U_FAILURE(code: *pErrorCode))
191 return -1;
192 if (fieldName == nullptr ? capacity != 0 : capacity < 0) {
193 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
194 return -1;
195 }
196 UnicodeString result = ((const DateTimePatternGenerator *)dtpg)->getFieldDisplayName(field,width);
197 if (fieldName == nullptr) {
198 return result.length();
199 }
200 return result.extract(dest: fieldName, destCapacity: capacity, errorCode&: *pErrorCode);
201}
202
203U_CAPI void U_EXPORT2
204udatpg_setDateTimeFormat(const UDateTimePatternGenerator *dtpg,
205 const char16_t *dtFormat, int32_t length) {
206 UnicodeString dtFormatString((UBool)(length<0), dtFormat, length);
207 ((DateTimePatternGenerator *)dtpg)->setDateTimeFormat(dtFormatString);
208}
209
210U_CAPI const char16_t * U_EXPORT2
211udatpg_getDateTimeFormat(const UDateTimePatternGenerator *dtpg,
212 int32_t *pLength) {
213 UErrorCode status = U_ZERO_ERROR;
214 return udatpg_getDateTimeFormatForStyle(udtpg: dtpg, style: UDAT_MEDIUM, pLength, pErrorCode: &status);
215}
216
217U_CAPI void U_EXPORT2
218udatpg_setDateTimeFormatForStyle(UDateTimePatternGenerator *udtpg,
219 UDateFormatStyle style,
220 const char16_t *dateTimeFormat, int32_t length,
221 UErrorCode *pErrorCode) {
222 if (U_FAILURE(code: *pErrorCode)) {
223 return;
224 } else if (dateTimeFormat==nullptr) {
225 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
226 return;
227 }
228 DateTimePatternGenerator *dtpg = reinterpret_cast<DateTimePatternGenerator *>(udtpg);
229 UnicodeString dtFormatString((UBool)(length<0), dateTimeFormat, length);
230 dtpg->setDateTimeFormat(style, dateTimeFormat: dtFormatString, status&: *pErrorCode);
231}
232
233U_CAPI const char16_t* U_EXPORT2
234udatpg_getDateTimeFormatForStyle(const UDateTimePatternGenerator *udtpg,
235 UDateFormatStyle style, int32_t *pLength,
236 UErrorCode *pErrorCode) {
237 static const char16_t emptyString[] = { (char16_t)0 };
238 if (U_FAILURE(code: *pErrorCode)) {
239 if (pLength !=nullptr) {
240 *pLength = 0;
241 }
242 return emptyString;
243 }
244 const DateTimePatternGenerator *dtpg = reinterpret_cast<const DateTimePatternGenerator *>(udtpg);
245 const UnicodeString &result = dtpg->getDateTimeFormat(style, status&: *pErrorCode);
246 if (pLength != nullptr) {
247 *pLength=result.length();
248 }
249 // Note: The UnicodeString for the dateTimeFormat string in the DateTimePatternGenerator
250 // was NUL-terminated what it was set, to avoid doing it here which could re-allocate
251 // the buffe and affect and cont references to the string or its buffer.
252 return result.getBuffer();
253 }
254
255U_CAPI void U_EXPORT2
256udatpg_setDecimal(UDateTimePatternGenerator *dtpg,
257 const char16_t *decimal, int32_t length) {
258 UnicodeString decimalString((UBool)(length<0), decimal, length);
259 ((DateTimePatternGenerator *)dtpg)->setDecimal(decimalString);
260}
261
262U_CAPI const char16_t * U_EXPORT2
263udatpg_getDecimal(const UDateTimePatternGenerator *dtpg,
264 int32_t *pLength) {
265 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDecimal();
266 if(pLength!=nullptr) {
267 *pLength=result.length();
268 }
269 return result.getBuffer();
270}
271
272U_CAPI int32_t U_EXPORT2
273udatpg_replaceFieldTypes(UDateTimePatternGenerator *dtpg,
274 const char16_t *pattern, int32_t patternLength,
275 const char16_t *skeleton, int32_t skeletonLength,
276 char16_t *dest, int32_t destCapacity,
277 UErrorCode *pErrorCode) {
278 return udatpg_replaceFieldTypesWithOptions(dtpg, pattern, patternLength, skeleton, skeletonLength,
279 options: UDATPG_MATCH_NO_OPTIONS,
280 dest, destCapacity, pErrorCode);
281}
282
283U_CAPI int32_t U_EXPORT2
284udatpg_replaceFieldTypesWithOptions(UDateTimePatternGenerator *dtpg,
285 const char16_t *pattern, int32_t patternLength,
286 const char16_t *skeleton, int32_t skeletonLength,
287 UDateTimePatternMatchOptions options,
288 char16_t *dest, int32_t destCapacity,
289 UErrorCode *pErrorCode) {
290 if(U_FAILURE(code: *pErrorCode)) {
291 return 0;
292 }
293 if((pattern==nullptr && patternLength!=0) || (skeleton==nullptr && skeletonLength!=0)) {
294 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
295 return 0;
296 }
297 UnicodeString patternString((UBool)(patternLength<0), pattern, patternLength);
298 UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength);
299 UnicodeString result=((DateTimePatternGenerator *)dtpg)->replaceFieldTypes(pattern: patternString, skeleton: skeletonString, options, status&: *pErrorCode);
300 return result.extract(dest, destCapacity, errorCode&: *pErrorCode);
301}
302
303U_CAPI UEnumeration * U_EXPORT2
304udatpg_openSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
305 return uenum_openFromStringEnumeration(
306 adopted: ((DateTimePatternGenerator *)dtpg)->getSkeletons(status&: *pErrorCode),
307 ec: pErrorCode);
308}
309
310U_CAPI UEnumeration * U_EXPORT2
311udatpg_openBaseSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
312 return uenum_openFromStringEnumeration(
313 adopted: ((DateTimePatternGenerator *)dtpg)->getBaseSkeletons(status&: *pErrorCode),
314 ec: pErrorCode);
315}
316
317U_CAPI const char16_t * U_EXPORT2
318udatpg_getPatternForSkeleton(const UDateTimePatternGenerator *dtpg,
319 const char16_t *skeleton, int32_t skeletonLength,
320 int32_t *pLength) {
321 UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength);
322 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getPatternForSkeleton(skeleton: skeletonString);
323 if(pLength!=nullptr) {
324 *pLength=result.length();
325 }
326 return result.getBuffer();
327}
328
329U_CAPI UDateFormatHourCycle U_EXPORT2
330udatpg_getDefaultHourCycle(const UDateTimePatternGenerator *dtpg, UErrorCode* pErrorCode) {
331 return ((const DateTimePatternGenerator *)dtpg)->getDefaultHourCycle(status&: *pErrorCode);
332}
333
334#endif
335

source code of flutter_engine/third_party/icu/source/i18n/udatpg.cpp