]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/fldset.cpp
ICU-400.42.tar.gz
[apple/icu.git] / icuSources / test / intltest / fldset.cpp
CommitLineData
46f4442e
A
1/*
2************************************************************************
3* Copyright (c) 2007, International Business Machines
4* Corporation and others. All Rights Reserved.
5************************************************************************
6*/
7
8#include "fldset.h"
9#include "intltest.h"
10
11#if !UCONFIG_NO_FORMATTING
12#include "unicode/regex.h"
13
14
15FieldsSet::FieldsSet() {
16 // NOTREACHED
17}
18
19FieldsSet::FieldsSet(int32_t fieldCount) {
20 construct((UDebugEnumType)-1, fieldCount);
21}
22
23FieldsSet::FieldsSet(UDebugEnumType field) {
24 construct(field, udbg_enumCount(field));
25}
26
27FieldsSet::~FieldsSet() {
28
29}
30
31int32_t FieldsSet::fieldCount() const {
32 return fFieldCount;
33}
34
35void FieldsSet::construct(UDebugEnumType field, int32_t fieldCount) {
36 fEnum = field;
37 if(fieldCount > U_FIELDS_SET_MAX) {
38 fieldCount = U_FIELDS_SET_MAX;
39 }
40 fFieldCount = fieldCount;
41 clear();
42}
43
44UnicodeString FieldsSet::diffFrom(const FieldsSet& other, UErrorCode& status) const {
45 UnicodeString str;
46 if(!isSameType(other)) {
47 status = U_ILLEGAL_ARGUMENT_ERROR;
48 return UnicodeString("U_ILLEGAL_ARGUMENT_ERROR: FieldsSet of a different type!");
49 }
50 for (int i=0; i<fieldCount(); i++) {
51 if (isSet((UCalendarDateFields)i)) {
52 int32_t myVal = get(i);
53 int32_t theirVal = other.get(i);
54
55 if(fEnum != -1) {
56 const UnicodeString& fieldName = udbg_enumString(
57 fEnum, i);
58
59 str = str + fieldName + UnicodeString("=")+myVal+UnicodeString(" not ")+theirVal+UnicodeString(", ");
60 } else {
61 str = str + UnicodeString("some field") + "=" + myVal+" not " + theirVal+", ";
62 }
63 }
64 }
65 return str;
66}
67
68static UnicodeString *split(const UnicodeString &src, UChar ch, int32_t &splits)
69{
70 int32_t offset = -1;
71
72 splits = 1;
73 while((offset = src.indexOf(ch, offset + 1)) >= 0) {
74 splits += 1;
75 }
76
77 UnicodeString *result = new UnicodeString[splits];
78
79 int32_t start = 0;
80 int32_t split = 0;
81 int32_t end;
82
83 while((end = src.indexOf(ch, start)) >= 0) {
84 src.extractBetween(start, end, result[split++]);
85 start = end + 1;
86 }
87
88 src.extractBetween(start, src.length(), result[split]);
89
90 return result;
91}
92
93int32_t FieldsSet::parseFrom(const UnicodeString& str, const
94 FieldsSet* inheritFrom, UErrorCode& status) {
95
96 int goodFields = 0;
97
98 if(U_FAILURE(status)) {
99 return -1;
100 }
101
102 int32_t destCount = 0;
103 UnicodeString *dest = split(str, 0x002C /* ',' */, destCount);
104
105 for(int i = 0; i < destCount; i += 1) {
106 int32_t dc = 0;
107 UnicodeString *kv = split(dest[i], 0x003D /* '=' */, dc);
108
109 if(dc != 2) {
110 it_errln(UnicodeString("dc == ") + dc + UnicodeString("?"));
111 }
112
113 int32_t field = handleParseName(inheritFrom, kv[0], kv[1], status);
114
115 if(U_FAILURE(status)) {
116 char ch[256];
117 const UChar *u = kv[0].getBuffer();
118 int32_t len = kv[0].length();
119 u_UCharsToChars(u, ch, len);
120 ch[len] = 0; /* include terminating \0 */
121 it_errln(UnicodeString("Parse Failed: Field ") + UnicodeString(ch) + UnicodeString(", err ") + UnicodeString(u_errorName(status)));
122 return -1;
123 }
124
125 if(field != -1) {
126 handleParseValue(inheritFrom, field, kv[1], status);
127
128 if(U_FAILURE(status)) {
129 char ch[256];
130 const UChar *u = kv[1].getBuffer();
131 int32_t len = kv[1].length();
132 u_UCharsToChars(u, ch, len);
133 ch[len] = 0; /* include terminating \0 */
134 it_errln(UnicodeString("Parse Failed: Value ") + UnicodeString(ch) + UnicodeString(", err ") + UnicodeString(u_errorName(status)));
135 return -1;
136 }
137
138 goodFields += 1;
139 }
140
141 delete[] kv;
142 }
143
144 delete[] dest;
145
146 return goodFields;
147}
148
149UBool FieldsSet::isSameType(const FieldsSet& other) const {
150 return((&other==this)||
151 ((other.fFieldCount==fFieldCount) && (other.fEnum==fEnum)));
152}
153
154void FieldsSet::clear() {
155 for (int i=0; i<fieldCount(); i++) {
156 fValue[i]=-1;
157 fIsSet[i]=FALSE;
158 }
159}
160
161void FieldsSet::clear(int32_t field) {
162 if (field<0|| field>=fieldCount()) {
163 return;
164 }
165 fValue[field] = -1;
166 fIsSet[field] = FALSE;
167}
168void FieldsSet::set(int32_t field, int32_t amount) {
169 if (field<0|| field>=fieldCount()) {
170 return;
171 }
172 fValue[field] = amount;
173 fIsSet[field] = TRUE;
174}
175
176UBool FieldsSet::isSet(int32_t field) const {
177 if (field<0|| field>=fieldCount()) {
178 return FALSE;
179 }
180 return fIsSet[field];
181}
182int32_t FieldsSet::get(int32_t field) const {
183 if (field<0|| field>=fieldCount()) {
184 return -1;
185 }
186 return fValue[field];
187}
188
189
190int32_t FieldsSet::handleParseName(const FieldsSet* /* inheritFrom */, const UnicodeString& name, const UnicodeString& /* substr*/ , UErrorCode& status) {
191 if(fEnum > -1) {
192 return udbg_enumByString(fEnum, name);
193 } else {
194 status = U_UNSUPPORTED_ERROR;
195 return -1;
196 }
197}
198
199void FieldsSet::parseValueDefault(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status) {
200 int32_t value = -1;
201 if(substr.length()==0) { // inherit requested
202 // inherit
203 if((inheritFrom == NULL) || !inheritFrom->isSet((UCalendarDateFields)field)) {
204 // couldn't inherit from field
205 it_errln(UnicodeString("Parse Failed: Couldn't inherit field ") + field + UnicodeString(" [") + UnicodeString(udbg_enumName(fEnum, field)) + UnicodeString("]"));
206 status = U_ILLEGAL_ARGUMENT_ERROR;
207 return;
208 }
209 value = inheritFrom->get((UCalendarDateFields)field);
210 } else {
211 value = udbg_stoi(substr);
212 }
213 set(field, value);
214}
215
216void FieldsSet::parseValueEnum(UDebugEnumType type, const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status) {
217 int32_t value = udbg_enumByString(type, substr);
218 if(value>=0) {
219 set(field, value);
220 } else {
221 // fallback
222 parseValueDefault(inheritFrom,field,substr,status);
223 }
224}
225
226void FieldsSet::handleParseValue(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status) {
227 parseValueDefault(inheritFrom, field, substr, status);
228}
229
230/// CAL FIELDS
231
232
233CalendarFieldsSet::CalendarFieldsSet() :
234FieldsSet(UDBG_UCalendarDateFields) {
235 // base class will call clear.
236}
237
238CalendarFieldsSet::~CalendarFieldsSet() {
239}
240
241void CalendarFieldsSet::handleParseValue(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status) {
242 if(field==UCAL_MONTH) {
243 parseValueEnum(UDBG_UCalendarMonths, inheritFrom, field, substr, status);
244 // will fallback to default.
245 } else {
246 parseValueDefault(inheritFrom, field, substr, status);
247 }
248}
249
250/**
251 * set the specified fields on this calendar. Doesn't clear first. Returns any errors the caller
252 */
253void CalendarFieldsSet::setOnCalendar(Calendar *cal, UErrorCode& /*status*/) const {
254 for (int i=0; i<UDAT_FIELD_COUNT; i++) {
255 if (isSet((UCalendarDateFields)i)) {
256 int32_t value = get((UCalendarDateFields)i);
257 //fprintf(stderr, "Setting: %s#%d=%d\n",udbg_enumName(UDBG_UCalendarDateFields,i),i,value);
258 cal->set((UCalendarDateFields)i, value);
259 }
260 }
261}
262
263/**
264 * return true if the calendar matches in these fields
265 */
266UBool CalendarFieldsSet::matches(Calendar *cal, CalendarFieldsSet &diffSet,
267 UErrorCode& status) const {
268 UBool match = TRUE;
269 if (U_FAILURE(status))
270 return FALSE;
271 for (int i=0; i<UDAT_FIELD_COUNT; i++) {
272 if (isSet((UCalendarDateFields)i)) {
273 int32_t calVal = cal->get((UCalendarDateFields)i, status);
274 if (U_FAILURE(status))
275 return FALSE;
276 if (calVal != get((UCalendarDateFields)i)) {
277 match = FALSE;
278 diffSet.set((UCalendarDateFields)i, calVal);
279 //fprintf(stderr, "match failed: %s#%d=%d != %d\n",udbg_enumName(UDBG_UCalendarDateFields,i),i,cal->get((UCalendarDateFields)i,status), get((UCalendarDateFields)i));;
280 }
281 }
282 }
283 return match;
284}
285
286
287enum {
288 DTS_DATE = 0,
289 DTS_TIME,
290 DTS_COUNT
291};
292
293/**
294 * DateTimeSet
295 * */
296DateTimeStyleSet::DateTimeStyleSet() :
297 FieldsSet(DTS_COUNT) {
298
299}
300
301DateTimeStyleSet::~DateTimeStyleSet() {
302
303}
304
305UDateFormatStyle DateTimeStyleSet::getDateStyle() const {
306 if(!isSet(DTS_DATE)) {
307 return UDAT_NONE;
308 } else {
309 return (UDateFormatStyle)get(DTS_DATE);
310 }
311}
312
313
314UDateFormatStyle DateTimeStyleSet::getTimeStyle() const {
315 if(!isSet(DTS_TIME)) {
316 return UDAT_NONE;
317 } else {
318 return (UDateFormatStyle)get(DTS_TIME);
319 }
320}
321
322void DateTimeStyleSet::handleParseValue(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status) {
323// int32_t value = udbg_enumByString(UDBG_UDateFormatStyle, substr);
324// fprintf(stderr, " HPV: %d -> %d\n", field, value);
325 UnicodeString kRELATIVE_("RELATIVE_");
326 if(substr.startsWith(kRELATIVE_)) {
327 UnicodeString relativeas(substr,kRELATIVE_.length());
328 parseValueEnum(UDBG_UDateFormatStyle, inheritFrom, field, relativeas, status);
329 // fix relative value
330 if(isSet(field) && U_SUCCESS(status)) {
331 set(field, get(field) | UDAT_RELATIVE);
332 }
333 } else {
334 parseValueEnum(UDBG_UDateFormatStyle, inheritFrom, field, substr, status);
335 }
336}
337
338int32_t DateTimeStyleSet::handleParseName(const FieldsSet* /* inheritFrom */, const UnicodeString& name, const UnicodeString& /* substr */, UErrorCode& status) {
339 UnicodeString kDATE("DATE"); // TODO: static
340 UnicodeString kTIME("TIME"); // TODO: static
341 if(name == kDATE ) {
342 return DTS_DATE;
343 } else if(name == kTIME) {
344 return DTS_TIME;
345 } else {
346 status = U_ILLEGAL_ARGUMENT_ERROR;
347 return -1;
348 }
349}
350
351#endif /*!UCONFIG_NO_FORMAT*/