dayperiodrules.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. * dayperiodrules.cpp
  9. *
  10. * created on: 2016-01-20
  11. * created by: kazede
  12. */
  13. #include "dayperiodrules.h"
  14. #include "unicode/ures.h"
  15. #include "charstr.h"
  16. #include "cstring.h"
  17. #include "ucln_in.h"
  18. #include "uhash.h"
  19. #include "umutex.h"
  20. #include "uresimp.h"
  21. U_NAMESPACE_BEGIN
  22. namespace {
  23. struct DayPeriodRulesData : public UMemory {
  24. DayPeriodRulesData() : localeToRuleSetNumMap(nullptr), rules(nullptr), maxRuleSetNum(0) {}
  25. UHashtable *localeToRuleSetNumMap;
  26. DayPeriodRules *rules;
  27. int32_t maxRuleSetNum;
  28. } *data = nullptr;
  29. enum CutoffType {
  30. CUTOFF_TYPE_UNKNOWN = -1,
  31. CUTOFF_TYPE_BEFORE,
  32. CUTOFF_TYPE_AFTER, // TODO: AFTER is deprecated in CLDR 29. Remove.
  33. CUTOFF_TYPE_FROM,
  34. CUTOFF_TYPE_AT
  35. };
  36. } // namespace
  37. struct DayPeriodRulesDataSink : public ResourceSink {
  38. DayPeriodRulesDataSink() {
  39. for (int32_t i = 0; i < UPRV_LENGTHOF(cutoffs); ++i) { cutoffs[i] = 0; }
  40. }
  41. virtual ~DayPeriodRulesDataSink();
  42. virtual void put(const char *key, ResourceValue &value, UBool, UErrorCode &errorCode) override {
  43. ResourceTable dayPeriodData = value.getTable(errorCode);
  44. if (U_FAILURE(errorCode)) { return; }
  45. for (int32_t i = 0; dayPeriodData.getKeyAndValue(i, key, value); ++i) {
  46. if (uprv_strcmp(key, "locales") == 0) {
  47. ResourceTable locales = value.getTable(errorCode);
  48. if (U_FAILURE(errorCode)) { return; }
  49. for (int32_t j = 0; locales.getKeyAndValue(j, key, value); ++j) {
  50. UnicodeString setNum_str = value.getUnicodeString(errorCode);
  51. int32_t setNum = parseSetNum(setNum_str, errorCode);
  52. uhash_puti(data->localeToRuleSetNumMap, const_cast<char *>(key), setNum, &errorCode);
  53. }
  54. } else if (uprv_strcmp(key, "rules") == 0) {
  55. // Allocate one more than needed to skip [0]. See comment in parseSetNum().
  56. data->rules = new DayPeriodRules[data->maxRuleSetNum + 1];
  57. if (data->rules == nullptr) {
  58. errorCode = U_MEMORY_ALLOCATION_ERROR;
  59. return;
  60. }
  61. ResourceTable rules = value.getTable(errorCode);
  62. processRules(rules, key, value, errorCode);
  63. if (U_FAILURE(errorCode)) { return; }
  64. }
  65. }
  66. }
  67. void processRules(const ResourceTable &rules, const char *key,
  68. ResourceValue &value, UErrorCode &errorCode) {
  69. if (U_FAILURE(errorCode)) { return; }
  70. for (int32_t i = 0; rules.getKeyAndValue(i, key, value); ++i) {
  71. ruleSetNum = parseSetNum(key, errorCode);
  72. ResourceTable ruleSet = value.getTable(errorCode);
  73. if (U_FAILURE(errorCode)) { return; }
  74. for (int32_t j = 0; ruleSet.getKeyAndValue(j, key, value); ++j) {
  75. period = DayPeriodRules::getDayPeriodFromString(key);
  76. if (period == DayPeriodRules::DAYPERIOD_UNKNOWN) {
  77. errorCode = U_INVALID_FORMAT_ERROR;
  78. return;
  79. }
  80. ResourceTable periodDefinition = value.getTable(errorCode);
  81. if (U_FAILURE(errorCode)) { return; }
  82. for (int32_t k = 0; periodDefinition.getKeyAndValue(k, key, value); ++k) {
  83. if (value.getType() == URES_STRING) {
  84. // Key-value pairs (e.g. before{6:00}).
  85. CutoffType type = getCutoffTypeFromString(key);
  86. addCutoff(type, value.getUnicodeString(errorCode), errorCode);
  87. if (U_FAILURE(errorCode)) { return; }
  88. } else {
  89. // Arrays (e.g. before{6:00, 24:00}).
  90. cutoffType = getCutoffTypeFromString(key);
  91. ResourceArray cutoffArray = value.getArray(errorCode);
  92. if (U_FAILURE(errorCode)) { return; }
  93. int32_t length = cutoffArray.getSize();
  94. for (int32_t l = 0; l < length; ++l) {
  95. cutoffArray.getValue(l, value);
  96. addCutoff(cutoffType, value.getUnicodeString(errorCode), errorCode);
  97. if (U_FAILURE(errorCode)) { return; }
  98. }
  99. }
  100. }
  101. setDayPeriodForHoursFromCutoffs(errorCode);
  102. for (int32_t k = 0; k < UPRV_LENGTHOF(cutoffs); ++k) {
  103. cutoffs[k] = 0;
  104. }
  105. }
  106. if (!data->rules[ruleSetNum].allHoursAreSet()) {
  107. errorCode = U_INVALID_FORMAT_ERROR;
  108. return;
  109. }
  110. }
  111. }
  112. // Members.
  113. int32_t cutoffs[25]; // [0] thru [24]: 24 is allowed in "before 24".
  114. // "Path" to data.
  115. int32_t ruleSetNum;
  116. DayPeriodRules::DayPeriod period;
  117. CutoffType cutoffType;
  118. // Helpers.
  119. static int32_t parseSetNum(const UnicodeString &setNumStr, UErrorCode &errorCode) {
  120. CharString cs;
  121. cs.appendInvariantChars(setNumStr, errorCode);
  122. return parseSetNum(cs.data(), errorCode);
  123. }
  124. static int32_t parseSetNum(const char *setNumStr, UErrorCode &errorCode) {
  125. if (U_FAILURE(errorCode)) { return -1; }
  126. if (uprv_strncmp(setNumStr, "set", 3) != 0) {
  127. errorCode = U_INVALID_FORMAT_ERROR;
  128. return -1;
  129. }
  130. int32_t i = 3;
  131. int32_t setNum = 0;
  132. while (setNumStr[i] != 0) {
  133. int32_t digit = setNumStr[i] - '0';
  134. if (digit < 0 || 9 < digit) {
  135. errorCode = U_INVALID_FORMAT_ERROR;
  136. return -1;
  137. }
  138. setNum = 10 * setNum + digit;
  139. ++i;
  140. }
  141. // Rule set number must not be zero. (0 is used to indicate "not found" by hashmap.)
  142. // Currently ICU data conveniently starts numbering rule sets from 1.
  143. if (setNum == 0) {
  144. errorCode = U_INVALID_FORMAT_ERROR;
  145. return -1;
  146. } else {
  147. return setNum;
  148. }
  149. }
  150. void addCutoff(CutoffType type, const UnicodeString &hour_str, UErrorCode &errorCode) {
  151. if (U_FAILURE(errorCode)) { return; }
  152. if (type == CUTOFF_TYPE_UNKNOWN) {
  153. errorCode = U_INVALID_FORMAT_ERROR;
  154. return;
  155. }
  156. int32_t hour = parseHour(hour_str, errorCode);
  157. if (U_FAILURE(errorCode)) { return; }
  158. cutoffs[hour] |= 1 << type;
  159. }
  160. // Translate the cutoffs[] array to day period rules.
  161. void setDayPeriodForHoursFromCutoffs(UErrorCode &errorCode) {
  162. DayPeriodRules &rule = data->rules[ruleSetNum];
  163. for (int32_t startHour = 0; startHour <= 24; ++startHour) {
  164. // AT cutoffs must be either midnight or noon.
  165. if (cutoffs[startHour] & (1 << CUTOFF_TYPE_AT)) {
  166. if (startHour == 0 && period == DayPeriodRules::DAYPERIOD_MIDNIGHT) {
  167. rule.fHasMidnight = true;
  168. } else if (startHour == 12 && period == DayPeriodRules::DAYPERIOD_NOON) {
  169. rule.fHasNoon = true;
  170. } else {
  171. errorCode = U_INVALID_FORMAT_ERROR; // Bad data.
  172. return;
  173. }
  174. }
  175. // FROM/AFTER and BEFORE must come in a pair.
  176. if (cutoffs[startHour] & (1 << CUTOFF_TYPE_FROM) ||
  177. cutoffs[startHour] & (1 << CUTOFF_TYPE_AFTER)) {
  178. for (int32_t hour = startHour + 1;; ++hour) {
  179. if (hour == startHour) {
  180. // We've gone around the array once and can't find a BEFORE.
  181. errorCode = U_INVALID_FORMAT_ERROR;
  182. return;
  183. }
  184. if (hour == 25) { hour = 0; }
  185. if (cutoffs[hour] & (1 << CUTOFF_TYPE_BEFORE)) {
  186. rule.add(startHour, hour, period);
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. // Translate "before" to CUTOFF_TYPE_BEFORE, for example.
  194. static CutoffType getCutoffTypeFromString(const char *type_str) {
  195. if (uprv_strcmp(type_str, "from") == 0) {
  196. return CUTOFF_TYPE_FROM;
  197. } else if (uprv_strcmp(type_str, "before") == 0) {
  198. return CUTOFF_TYPE_BEFORE;
  199. } else if (uprv_strcmp(type_str, "after") == 0) {
  200. return CUTOFF_TYPE_AFTER;
  201. } else if (uprv_strcmp(type_str, "at") == 0) {
  202. return CUTOFF_TYPE_AT;
  203. } else {
  204. return CUTOFF_TYPE_UNKNOWN;
  205. }
  206. }
  207. // Gets the numerical value of the hour from the Unicode string.
  208. static int32_t parseHour(const UnicodeString &time, UErrorCode &errorCode) {
  209. if (U_FAILURE(errorCode)) {
  210. return 0;
  211. }
  212. int32_t hourLimit = time.length() - 3;
  213. // `time` must look like "x:00" or "xx:00".
  214. // If length is wrong or `time` doesn't end with ":00", error out.
  215. if ((hourLimit != 1 && hourLimit != 2) ||
  216. time[hourLimit] != 0x3A || time[hourLimit + 1] != 0x30 ||
  217. time[hourLimit + 2] != 0x30) {
  218. errorCode = U_INVALID_FORMAT_ERROR;
  219. return 0;
  220. }
  221. // If `time` doesn't begin with a number in [0, 24], error out.
  222. // Note: "24:00" is possible in "before 24:00".
  223. int32_t hour = time[0] - 0x30;
  224. if (hour < 0 || 9 < hour) {
  225. errorCode = U_INVALID_FORMAT_ERROR;
  226. return 0;
  227. }
  228. if (hourLimit == 2) {
  229. int32_t hourDigit2 = time[1] - 0x30;
  230. if (hourDigit2 < 0 || 9 < hourDigit2) {
  231. errorCode = U_INVALID_FORMAT_ERROR;
  232. return 0;
  233. }
  234. hour = hour * 10 + hourDigit2;
  235. if (hour > 24) {
  236. errorCode = U_INVALID_FORMAT_ERROR;
  237. return 0;
  238. }
  239. }
  240. return hour;
  241. }
  242. }; // struct DayPeriodRulesDataSink
  243. struct DayPeriodRulesCountSink : public ResourceSink {
  244. virtual ~DayPeriodRulesCountSink();
  245. virtual void put(const char *key, ResourceValue &value, UBool, UErrorCode &errorCode) override {
  246. ResourceTable rules = value.getTable(errorCode);
  247. if (U_FAILURE(errorCode)) { return; }
  248. for (int32_t i = 0; rules.getKeyAndValue(i, key, value); ++i) {
  249. int32_t setNum = DayPeriodRulesDataSink::parseSetNum(key, errorCode);
  250. if (setNum > data->maxRuleSetNum) {
  251. data->maxRuleSetNum = setNum;
  252. }
  253. }
  254. }
  255. };
  256. // Out-of-line virtual destructors.
  257. DayPeriodRulesDataSink::~DayPeriodRulesDataSink() {}
  258. DayPeriodRulesCountSink::~DayPeriodRulesCountSink() {}
  259. namespace {
  260. UInitOnce initOnce {};
  261. U_CFUNC UBool U_CALLCONV dayPeriodRulesCleanup() {
  262. delete[] data->rules;
  263. uhash_close(data->localeToRuleSetNumMap);
  264. delete data;
  265. data = nullptr;
  266. return true;
  267. }
  268. } // namespace
  269. void U_CALLCONV DayPeriodRules::load(UErrorCode &errorCode) {
  270. if (U_FAILURE(errorCode)) {
  271. return;
  272. }
  273. data = new DayPeriodRulesData();
  274. data->localeToRuleSetNumMap = uhash_open(uhash_hashChars, uhash_compareChars, nullptr, &errorCode);
  275. LocalUResourceBundlePointer rb_dayPeriods(ures_openDirect(nullptr, "dayPeriods", &errorCode));
  276. // Get the largest rule set number (so we allocate enough objects).
  277. DayPeriodRulesCountSink countSink;
  278. ures_getAllItemsWithFallback(rb_dayPeriods.getAlias(), "rules", countSink, errorCode);
  279. // Populate rules.
  280. DayPeriodRulesDataSink sink;
  281. ures_getAllItemsWithFallback(rb_dayPeriods.getAlias(), "", sink, errorCode);
  282. ucln_i18n_registerCleanup(UCLN_I18N_DAYPERIODRULES, dayPeriodRulesCleanup);
  283. }
  284. const DayPeriodRules *DayPeriodRules::getInstance(const Locale &locale, UErrorCode &errorCode) {
  285. umtx_initOnce(initOnce, DayPeriodRules::load, errorCode);
  286. // If the entire day period rules data doesn't conform to spec (even if the part we want
  287. // does), return nullptr.
  288. if(U_FAILURE(errorCode)) { return nullptr; }
  289. const char *localeCode = locale.getBaseName();
  290. char name[ULOC_FULLNAME_CAPACITY];
  291. char parentName[ULOC_FULLNAME_CAPACITY];
  292. if (uprv_strlen(localeCode) < ULOC_FULLNAME_CAPACITY) {
  293. uprv_strcpy(name, localeCode);
  294. // Treat empty string as root.
  295. if (*name == '\0') {
  296. uprv_strcpy(name, "root");
  297. }
  298. } else {
  299. errorCode = U_BUFFER_OVERFLOW_ERROR;
  300. return nullptr;
  301. }
  302. int32_t ruleSetNum = 0; // NB there is no rule set 0 and 0 is returned upon lookup failure.
  303. while (*name != '\0') {
  304. ruleSetNum = uhash_geti(data->localeToRuleSetNumMap, name);
  305. if (ruleSetNum == 0) {
  306. // name and parentName can't be the same pointer, so fill in parent then copy to child.
  307. uloc_getParent(name, parentName, ULOC_FULLNAME_CAPACITY, &errorCode);
  308. if (*parentName == '\0') {
  309. // Saves a lookup in the hash table.
  310. break;
  311. }
  312. uprv_strcpy(name, parentName);
  313. } else {
  314. break;
  315. }
  316. }
  317. if (ruleSetNum <= 0 || data->rules[ruleSetNum].getDayPeriodForHour(0) == DAYPERIOD_UNKNOWN) {
  318. // If day period for hour 0 is UNKNOWN then day period for all hours are UNKNOWN.
  319. // Data doesn't exist even with fallback.
  320. return nullptr;
  321. } else {
  322. return &data->rules[ruleSetNum];
  323. }
  324. }
  325. DayPeriodRules::DayPeriodRules() : fHasMidnight(false), fHasNoon(false) {
  326. for (int32_t i = 0; i < 24; ++i) {
  327. fDayPeriodForHour[i] = DayPeriodRules::DAYPERIOD_UNKNOWN;
  328. }
  329. }
  330. double DayPeriodRules::getMidPointForDayPeriod(
  331. DayPeriodRules::DayPeriod dayPeriod, UErrorCode &errorCode) const {
  332. if (U_FAILURE(errorCode)) { return -1; }
  333. int32_t startHour = getStartHourForDayPeriod(dayPeriod, errorCode);
  334. int32_t endHour = getEndHourForDayPeriod(dayPeriod, errorCode);
  335. // Can't obtain startHour or endHour; bail out.
  336. if (U_FAILURE(errorCode)) { return -1; }
  337. double midPoint = (startHour + endHour) / 2.0;
  338. if (startHour > endHour) {
  339. // dayPeriod wraps around midnight. Shift midPoint by 12 hours, in the direction that
  340. // lands it in [0, 24).
  341. midPoint += 12;
  342. if (midPoint >= 24) {
  343. midPoint -= 24;
  344. }
  345. }
  346. return midPoint;
  347. }
  348. int32_t DayPeriodRules::getStartHourForDayPeriod(
  349. DayPeriodRules::DayPeriod dayPeriod, UErrorCode &errorCode) const {
  350. if (U_FAILURE(errorCode)) { return -1; }
  351. if (dayPeriod == DAYPERIOD_MIDNIGHT) { return 0; }
  352. if (dayPeriod == DAYPERIOD_NOON) { return 12; }
  353. if (fDayPeriodForHour[0] == dayPeriod && fDayPeriodForHour[23] == dayPeriod) {
  354. // dayPeriod wraps around midnight. Start hour is later than end hour.
  355. for (int32_t i = 22; i >= 1; --i) {
  356. if (fDayPeriodForHour[i] != dayPeriod) {
  357. return (i + 1);
  358. }
  359. }
  360. } else {
  361. for (int32_t i = 0; i <= 23; ++i) {
  362. if (fDayPeriodForHour[i] == dayPeriod) {
  363. return i;
  364. }
  365. }
  366. }
  367. // dayPeriod doesn't exist in rule set; set error and exit.
  368. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  369. return -1;
  370. }
  371. int32_t DayPeriodRules::getEndHourForDayPeriod(
  372. DayPeriodRules::DayPeriod dayPeriod, UErrorCode &errorCode) const {
  373. if (U_FAILURE(errorCode)) { return -1; }
  374. if (dayPeriod == DAYPERIOD_MIDNIGHT) { return 0; }
  375. if (dayPeriod == DAYPERIOD_NOON) { return 12; }
  376. if (fDayPeriodForHour[0] == dayPeriod && fDayPeriodForHour[23] == dayPeriod) {
  377. // dayPeriod wraps around midnight. End hour is before start hour.
  378. for (int32_t i = 1; i <= 22; ++i) {
  379. if (fDayPeriodForHour[i] != dayPeriod) {
  380. // i o'clock is when a new period starts, therefore when the old period ends.
  381. return i;
  382. }
  383. }
  384. } else {
  385. for (int32_t i = 23; i >= 0; --i) {
  386. if (fDayPeriodForHour[i] == dayPeriod) {
  387. return (i + 1);
  388. }
  389. }
  390. }
  391. // dayPeriod doesn't exist in rule set; set error and exit.
  392. errorCode = U_ILLEGAL_ARGUMENT_ERROR;
  393. return -1;
  394. }
  395. DayPeriodRules::DayPeriod DayPeriodRules::getDayPeriodFromString(const char *type_str) {
  396. if (uprv_strcmp(type_str, "midnight") == 0) {
  397. return DAYPERIOD_MIDNIGHT;
  398. } else if (uprv_strcmp(type_str, "noon") == 0) {
  399. return DAYPERIOD_NOON;
  400. } else if (uprv_strcmp(type_str, "morning1") == 0) {
  401. return DAYPERIOD_MORNING1;
  402. } else if (uprv_strcmp(type_str, "afternoon1") == 0) {
  403. return DAYPERIOD_AFTERNOON1;
  404. } else if (uprv_strcmp(type_str, "evening1") == 0) {
  405. return DAYPERIOD_EVENING1;
  406. } else if (uprv_strcmp(type_str, "night1") == 0) {
  407. return DAYPERIOD_NIGHT1;
  408. } else if (uprv_strcmp(type_str, "morning2") == 0) {
  409. return DAYPERIOD_MORNING2;
  410. } else if (uprv_strcmp(type_str, "afternoon2") == 0) {
  411. return DAYPERIOD_AFTERNOON2;
  412. } else if (uprv_strcmp(type_str, "evening2") == 0) {
  413. return DAYPERIOD_EVENING2;
  414. } else if (uprv_strcmp(type_str, "night2") == 0) {
  415. return DAYPERIOD_NIGHT2;
  416. } else if (uprv_strcmp(type_str, "am") == 0) {
  417. return DAYPERIOD_AM;
  418. } else if (uprv_strcmp(type_str, "pm") == 0) {
  419. return DAYPERIOD_PM;
  420. } else {
  421. return DAYPERIOD_UNKNOWN;
  422. }
  423. }
  424. void DayPeriodRules::add(int32_t startHour, int32_t limitHour, DayPeriod period) {
  425. for (int32_t i = startHour; i != limitHour; ++i) {
  426. if (i == 24) { i = 0; }
  427. fDayPeriodForHour[i] = period;
  428. }
  429. }
  430. UBool DayPeriodRules::allHoursAreSet() {
  431. for (int32_t i = 0; i < 24; ++i) {
  432. if (fDayPeriodForHour[i] == DAYPERIOD_UNKNOWN) { return false; }
  433. }
  434. return true;
  435. }
  436. U_NAMESPACE_END