Coverage Report

Created: 2025-06-24 06:54

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