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