]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/fldset.cpp
2 ************************************************************************
3 * Copyright (c) 2007, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ************************************************************************
11 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/regex.h"
15 FieldsSet::FieldsSet() {
19 FieldsSet::FieldsSet(int32_t fieldCount
) {
20 construct((UDebugEnumType
)-1, fieldCount
);
23 FieldsSet::FieldsSet(UDebugEnumType field
) {
24 construct(field
, udbg_enumCount(field
));
27 FieldsSet::~FieldsSet() {
31 int32_t FieldsSet::fieldCount() const {
35 void FieldsSet::construct(UDebugEnumType field
, int32_t fieldCount
) {
37 if(fieldCount
> U_FIELDS_SET_MAX
) {
38 fieldCount
= U_FIELDS_SET_MAX
;
40 fFieldCount
= fieldCount
;
44 UnicodeString
FieldsSet::diffFrom(const FieldsSet
& other
, UErrorCode
& status
) const {
46 if(!isSameType(other
)) {
47 status
= U_ILLEGAL_ARGUMENT_ERROR
;
48 return UnicodeString("U_ILLEGAL_ARGUMENT_ERROR: FieldsSet of a different type!");
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
);
56 const UnicodeString
& fieldName
= udbg_enumString(
59 str
= str
+ fieldName
+ UnicodeString("=")+myVal
+UnicodeString(" not ")+theirVal
+UnicodeString(", ");
61 str
= str
+ UnicodeString("some field") + "=" + myVal
+" not " + theirVal
+", ";
68 static UnicodeString
*split(const UnicodeString
&src
, UChar ch
, int32_t &splits
)
73 while((offset
= src
.indexOf(ch
, offset
+ 1)) >= 0) {
77 UnicodeString
*result
= new UnicodeString
[splits
];
83 while((end
= src
.indexOf(ch
, start
)) >= 0) {
84 src
.extractBetween(start
, end
, result
[split
++]);
88 src
.extractBetween(start
, src
.length(), result
[split
]);
93 int32_t FieldsSet::parseFrom(const UnicodeString
& str
, const
94 FieldsSet
* inheritFrom
, UErrorCode
& status
) {
98 if(U_FAILURE(status
)) {
102 int32_t destCount
= 0;
103 UnicodeString
*dest
= split(str
, 0x002C /* ',' */, destCount
);
105 for(int i
= 0; i
< destCount
; i
+= 1) {
107 UnicodeString
*kv
= split(dest
[i
], 0x003D /* '=' */, dc
);
110 it_errln(UnicodeString("dc == ") + dc
+ UnicodeString("?"));
113 int32_t field
= handleParseName(inheritFrom
, kv
[0], kv
[1], status
);
115 if(U_FAILURE(status
)) {
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
)));
126 handleParseValue(inheritFrom
, field
, kv
[1], status
);
128 if(U_FAILURE(status
)) {
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
)));
149 UBool
FieldsSet::isSameType(const FieldsSet
& other
) const {
150 return((&other
==this)||
151 ((other
.fFieldCount
==fFieldCount
) && (other
.fEnum
==fEnum
)));
154 void FieldsSet::clear() {
155 for (int i
=0; i
<fieldCount(); i
++) {
161 void FieldsSet::clear(int32_t field
) {
162 if (field
<0|| field
>=fieldCount()) {
166 fIsSet
[field
] = FALSE
;
168 void FieldsSet::set(int32_t field
, int32_t amount
) {
169 if (field
<0|| field
>=fieldCount()) {
172 fValue
[field
] = amount
;
173 fIsSet
[field
] = TRUE
;
176 UBool
FieldsSet::isSet(int32_t field
) const {
177 if (field
<0|| field
>=fieldCount()) {
180 return fIsSet
[field
];
182 int32_t FieldsSet::get(int32_t field
) const {
183 if (field
<0|| field
>=fieldCount()) {
186 return fValue
[field
];
190 int32_t FieldsSet::handleParseName(const FieldsSet
* /* inheritFrom */, const UnicodeString
& name
, const UnicodeString
& /* substr*/ , UErrorCode
& status
) {
192 return udbg_enumByString(fEnum
, name
);
194 status
= U_UNSUPPORTED_ERROR
;
199 void FieldsSet::parseValueDefault(const FieldsSet
* inheritFrom
, int32_t field
, const UnicodeString
& substr
, UErrorCode
& status
) {
201 if(substr
.length()==0) { // inherit requested
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
;
209 value
= inheritFrom
->get((UCalendarDateFields
)field
);
211 value
= udbg_stoi(substr
);
216 void FieldsSet::parseValueEnum(UDebugEnumType type
, const FieldsSet
* inheritFrom
, int32_t field
, const UnicodeString
& substr
, UErrorCode
& status
) {
217 int32_t value
= udbg_enumByString(type
, substr
);
222 parseValueDefault(inheritFrom
,field
,substr
,status
);
226 void FieldsSet::handleParseValue(const FieldsSet
* inheritFrom
, int32_t field
, const UnicodeString
& substr
, UErrorCode
& status
) {
227 parseValueDefault(inheritFrom
, field
, substr
, status
);
233 CalendarFieldsSet::CalendarFieldsSet() :
234 FieldsSet(UDBG_UCalendarDateFields
) {
235 // base class will call clear.
238 CalendarFieldsSet::~CalendarFieldsSet() {
241 void 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.
246 parseValueDefault(inheritFrom
, field
, substr
, status
);
251 * set the specified fields on this calendar. Doesn't clear first. Returns any errors the caller
253 void 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
);
264 * return true if the calendar matches in these fields
266 UBool
CalendarFieldsSet::matches(Calendar
*cal
, CalendarFieldsSet
&diffSet
,
267 UErrorCode
& status
) const {
269 if (U_FAILURE(status
))
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
))
276 if (calVal
!= get((UCalendarDateFields
)i
)) {
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));;
296 DateTimeStyleSet::DateTimeStyleSet() :
297 FieldsSet(DTS_COUNT
) {
301 DateTimeStyleSet::~DateTimeStyleSet() {
305 UDateFormatStyle
DateTimeStyleSet::getDateStyle() const {
306 if(!isSet(DTS_DATE
)) {
309 return (UDateFormatStyle
)get(DTS_DATE
);
314 UDateFormatStyle
DateTimeStyleSet::getTimeStyle() const {
315 if(!isSet(DTS_TIME
)) {
318 return (UDateFormatStyle
)get(DTS_TIME
);
322 void 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
);
334 parseValueEnum(UDBG_UDateFormatStyle
, inheritFrom
, field
, substr
, status
);
338 int32_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
343 } else if(name
== kTIME
) {
346 status
= U_ILLEGAL_ARGUMENT_ERROR
;
351 #endif /*!UCONFIG_NO_FORMAT*/