-
-
Notifications
You must be signed in to change notification settings - Fork 790
/
Copy pathdtptngen.cpp
3029 lines (2712 loc) · 112 KB
/
dtptngen.cpp
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2007-2016, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*
* File DTPTNGEN.CPP
*
*******************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/datefmt.h"
#include "unicode/decimfmt.h"
#include "unicode/dtfmtsym.h"
#include "unicode/dtptngen.h"
#include "unicode/localpointer.h"
#include "unicode/simpleformatter.h"
#include "unicode/smpdtfmt.h"
#include "unicode/udat.h"
#include "unicode/udatpg.h"
#include "unicode/uniset.h"
#include "unicode/uloc.h"
#include "unicode/ures.h"
#include "unicode/ustring.h"
#include "unicode/rep.h"
#include "unicode/region.h"
#include "cpputils.h"
#include "mutex.h"
#include "umutex.h"
#include "cmemory.h"
#include "cstring.h"
#include "locbased.h"
#include "hash.h"
#include "uhash.h"
#include "ulocimp.h"
#include "uresimp.h"
#include "ulocimp.h"
#include "dtptngen_impl.h"
#include "ucln_in.h"
#include "charstr.h"
#include "uassert.h"
#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
/**
* If we are on EBCDIC, use an iterator which will
* traverse the bundles in ASCII order.
*/
#define U_USE_ASCII_BUNDLE_ITERATOR
#define U_SORT_ASCII_BUNDLE_ITERATOR
#endif
#if defined(U_USE_ASCII_BUNDLE_ITERATOR)
#include "unicode/ustring.h"
#include "uarrsort.h"
struct UResAEntry {
char16_t *key;
UResourceBundle *item;
};
struct UResourceBundleAIterator {
UResourceBundle *bund;
UResAEntry *entries;
int32_t num;
int32_t cursor;
};
/* Must be C linkage to pass function pointer to the sort function */
U_CDECL_BEGIN
static int32_t U_CALLCONV
ures_a_codepointSort(const void *context, const void *left, const void *right) {
//CompareContext *cmp=(CompareContext *)context;
return u_strcmp(((const UResAEntry *)left)->key,
((const UResAEntry *)right)->key);
}
U_CDECL_END
static void ures_a_open(UResourceBundleAIterator *aiter, UResourceBundle *bund, UErrorCode *status) {
if(U_FAILURE(*status)) {
return;
}
aiter->bund = bund;
aiter->num = ures_getSize(aiter->bund);
aiter->cursor = 0;
#if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
aiter->entries = nullptr;
#else
aiter->entries = (UResAEntry*)uprv_malloc(sizeof(UResAEntry)*aiter->num);
for(int i=0;i<aiter->num;i++) {
aiter->entries[i].item = ures_getByIndex(aiter->bund, i, nullptr, status);
const char *akey = ures_getKey(aiter->entries[i].item);
int32_t len = uprv_strlen(akey)+1;
aiter->entries[i].key = (char16_t*)uprv_malloc(len*sizeof(char16_t));
u_charsToUChars(akey, aiter->entries[i].key, len);
}
uprv_sortArray(aiter->entries, aiter->num, sizeof(UResAEntry), ures_a_codepointSort, nullptr, true, status);
#endif
}
static void ures_a_close(UResourceBundleAIterator *aiter) {
#if defined(U_SORT_ASCII_BUNDLE_ITERATOR)
for(int i=0;i<aiter->num;i++) {
uprv_free(aiter->entries[i].key);
ures_close(aiter->entries[i].item);
}
#endif
}
static const char16_t *ures_a_getNextString(UResourceBundleAIterator *aiter, int32_t *len, const char **key, UErrorCode *err) {
#if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
return ures_getNextString(aiter->bund, len, key, err);
#else
if(U_FAILURE(*err)) return nullptr;
UResourceBundle *item = aiter->entries[aiter->cursor].item;
const char16_t* ret = ures_getString(item, len, err);
*key = ures_getKey(item);
aiter->cursor++;
return ret;
#endif
}
#endif
U_NAMESPACE_BEGIN
// *****************************************************************************
// class DateTimePatternGenerator
// *****************************************************************************
static const char16_t Canonical_Items[] = {
// GyQMwWEDFdaHmsSv
CAP_G, LOW_Y, CAP_Q, CAP_M, LOW_W, CAP_W, CAP_E,
CAP_D, CAP_F, LOW_D, LOW_A, // The UDATPG_x_FIELD constants and these fields have a different order than in ICU4J
CAP_H, LOW_M, LOW_S, CAP_S, LOW_V, 0
};
static const dtTypeElem dtTypes[] = {
// patternChar, field, type, minLen, weight
{CAP_G, UDATPG_ERA_FIELD, DT_SHORT, 1, 3,},
{CAP_G, UDATPG_ERA_FIELD, DT_LONG, 4, 0},
{CAP_G, UDATPG_ERA_FIELD, DT_NARROW, 5, 0},
{LOW_Y, UDATPG_YEAR_FIELD, DT_NUMERIC, 1, 20},
{CAP_Y, UDATPG_YEAR_FIELD, DT_NUMERIC + DT_DELTA, 1, 20},
{LOW_U, UDATPG_YEAR_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 20},
{LOW_R, UDATPG_YEAR_FIELD, DT_NUMERIC + 3*DT_DELTA, 1, 20},
{CAP_U, UDATPG_YEAR_FIELD, DT_SHORT, 1, 3},
{CAP_U, UDATPG_YEAR_FIELD, DT_LONG, 4, 0},
{CAP_U, UDATPG_YEAR_FIELD, DT_NARROW, 5, 0},
{CAP_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC, 1, 2},
{CAP_Q, UDATPG_QUARTER_FIELD, DT_SHORT, 3, 0},
{CAP_Q, UDATPG_QUARTER_FIELD, DT_LONG, 4, 0},
{CAP_Q, UDATPG_QUARTER_FIELD, DT_NARROW, 5, 0},
{LOW_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
{LOW_Q, UDATPG_QUARTER_FIELD, DT_SHORT - DT_DELTA, 3, 0},
{LOW_Q, UDATPG_QUARTER_FIELD, DT_LONG - DT_DELTA, 4, 0},
{LOW_Q, UDATPG_QUARTER_FIELD, DT_NARROW - DT_DELTA, 5, 0},
{CAP_M, UDATPG_MONTH_FIELD, DT_NUMERIC, 1, 2},
{CAP_M, UDATPG_MONTH_FIELD, DT_SHORT, 3, 0},
{CAP_M, UDATPG_MONTH_FIELD, DT_LONG, 4, 0},
{CAP_M, UDATPG_MONTH_FIELD, DT_NARROW, 5, 0},
{CAP_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
{CAP_L, UDATPG_MONTH_FIELD, DT_SHORT - DT_DELTA, 3, 0},
{CAP_L, UDATPG_MONTH_FIELD, DT_LONG - DT_DELTA, 4, 0},
{CAP_L, UDATPG_MONTH_FIELD, DT_NARROW - DT_DELTA, 5, 0},
{LOW_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 1},
{LOW_W, UDATPG_WEEK_OF_YEAR_FIELD, DT_NUMERIC, 1, 2},
{CAP_W, UDATPG_WEEK_OF_MONTH_FIELD, DT_NUMERIC, 1, 0},
{CAP_E, UDATPG_WEEKDAY_FIELD, DT_SHORT, 1, 3},
{CAP_E, UDATPG_WEEKDAY_FIELD, DT_LONG, 4, 0},
{CAP_E, UDATPG_WEEKDAY_FIELD, DT_NARROW, 5, 0},
{CAP_E, UDATPG_WEEKDAY_FIELD, DT_SHORTER, 6, 0},
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 2},
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_SHORT - 2*DT_DELTA, 3, 0},
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_NARROW - 2*DT_DELTA, 5, 0},
{LOW_C, UDATPG_WEEKDAY_FIELD, DT_SHORTER - 2*DT_DELTA, 6, 0},
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, // LOW_E is currently not used in CLDR data, should not be canonical
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_SHORT - DT_DELTA, 3, 0},
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_LONG - DT_DELTA, 4, 0},
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_NARROW - DT_DELTA, 5, 0},
{LOW_E, UDATPG_WEEKDAY_FIELD, DT_SHORTER - DT_DELTA, 6, 0},
{LOW_D, UDATPG_DAY_FIELD, DT_NUMERIC, 1, 2},
{LOW_G, UDATPG_DAY_FIELD, DT_NUMERIC + DT_DELTA, 1, 20}, // really internal use, so we don't care
{CAP_D, UDATPG_DAY_OF_YEAR_FIELD, DT_NUMERIC, 1, 3},
{CAP_F, UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, DT_NUMERIC, 1, 0},
{LOW_A, UDATPG_DAYPERIOD_FIELD, DT_SHORT, 1, 3},
{LOW_A, UDATPG_DAYPERIOD_FIELD, DT_LONG, 4, 0},
{LOW_A, UDATPG_DAYPERIOD_FIELD, DT_NARROW, 5, 0},
{LOW_B, UDATPG_DAYPERIOD_FIELD, DT_SHORT - DT_DELTA, 1, 3},
{LOW_B, UDATPG_DAYPERIOD_FIELD, DT_LONG - DT_DELTA, 4, 0},
{LOW_B, UDATPG_DAYPERIOD_FIELD, DT_NARROW - DT_DELTA, 5, 0},
// b needs to be closer to a than to B, so we make this 3*DT_DELTA
{CAP_B, UDATPG_DAYPERIOD_FIELD, DT_SHORT - 3*DT_DELTA, 1, 3},
{CAP_B, UDATPG_DAYPERIOD_FIELD, DT_LONG - 3*DT_DELTA, 4, 0},
{CAP_B, UDATPG_DAYPERIOD_FIELD, DT_NARROW - 3*DT_DELTA, 5, 0},
{CAP_H, UDATPG_HOUR_FIELD, DT_NUMERIC + 10*DT_DELTA, 1, 2}, // 24 hour
{LOW_K, UDATPG_HOUR_FIELD, DT_NUMERIC + 11*DT_DELTA, 1, 2}, // 24 hour
{LOW_H, UDATPG_HOUR_FIELD, DT_NUMERIC, 1, 2}, // 12 hour
{CAP_K, UDATPG_HOUR_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, // 12 hour
// The C code has had versions of the following 3, keep & update. Should not need these, but...
// Without these, certain tests using e.g. staticGetSkeleton fail because j/J in patterns
// get skipped instead of mapped to the right hour chars, for example in
// DateFormatTest::TestPatternFromSkeleton
// IntlTestDateTimePatternGeneratorAPI:: testStaticGetSkeleton
// DateIntervalFormatTest::testTicket11985
// Need to investigate better handling of jJC replacement e.g. in staticGetSkeleton.
{CAP_J, UDATPG_HOUR_FIELD, DT_NUMERIC + 5*DT_DELTA, 1, 2}, // 12/24 hour no AM/PM
{LOW_J, UDATPG_HOUR_FIELD, DT_NUMERIC + 6*DT_DELTA, 1, 6}, // 12/24 hour
{CAP_C, UDATPG_HOUR_FIELD, DT_NUMERIC + 7*DT_DELTA, 1, 6}, // 12/24 hour with preferred dayPeriods for 12
{LOW_M, UDATPG_MINUTE_FIELD, DT_NUMERIC, 1, 2},
{LOW_S, UDATPG_SECOND_FIELD, DT_NUMERIC, 1, 2},
{CAP_A, UDATPG_SECOND_FIELD, DT_NUMERIC + DT_DELTA, 1, 1000},
{CAP_S, UDATPG_FRACTIONAL_SECOND_FIELD, DT_NUMERIC, 1, 1000},
{LOW_V, UDATPG_ZONE_FIELD, DT_SHORT - 2*DT_DELTA, 1, 0},
{LOW_V, UDATPG_ZONE_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
{LOW_Z, UDATPG_ZONE_FIELD, DT_SHORT, 1, 3},
{LOW_Z, UDATPG_ZONE_FIELD, DT_LONG, 4, 0},
{CAP_Z, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 3},
{CAP_Z, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
{CAP_Z, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 5, 0},
{CAP_O, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 0},
{CAP_O, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
{CAP_V, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 0},
{CAP_V, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 2, 0},
{CAP_V, UDATPG_ZONE_FIELD, DT_LONG-1 - DT_DELTA, 3, 0},
{CAP_V, UDATPG_ZONE_FIELD, DT_LONG-2 - DT_DELTA, 4, 0},
{CAP_X, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 0},
{CAP_X, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 2, 0},
{CAP_X, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
{LOW_X, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 0},
{LOW_X, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 2, 0},
{LOW_X, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
{0, UDATPG_FIELD_COUNT, 0, 0, 0} , // last row of dtTypes[]
};
static const char* const CLDR_FIELD_APPEND[] = {
"Era", "Year", "Quarter", "Month", "Week", "*", "Day-Of-Week",
"*", "*", "Day", "*", // The UDATPG_x_FIELD constants and these fields have a different order than in ICU4J
"Hour", "Minute", "Second", "*", "Timezone"
};
static const char* const CLDR_FIELD_NAME[UDATPG_FIELD_COUNT] = {
"era", "year", "quarter", "month", "week", "weekOfMonth", "weekday",
"dayOfYear", "weekdayOfMonth", "day", "dayperiod", // The UDATPG_x_FIELD constants and these fields have a different order than in ICU4J
"hour", "minute", "second", "*", "zone"
};
static const char* const CLDR_FIELD_WIDTH[] = { // [UDATPG_WIDTH_COUNT]
"", "-short", "-narrow"
};
static constexpr UDateTimePGDisplayWidth UDATPG_WIDTH_APPENDITEM = UDATPG_WIDE;
static constexpr int32_t UDATPG_FIELD_KEY_MAX = 24; // max length of CLDR field tag (type + width)
// For appendItems
static const char16_t UDATPG_ItemFormat[]= {0x7B, 0x30, 0x7D, 0x20, 0x251C, 0x7B, 0x32, 0x7D, 0x3A,
0x20, 0x7B, 0x31, 0x7D, 0x2524, 0}; // {0} \u251C{2}: {1}\u2524
//static const char16_t repeatedPatterns[6]={CAP_G, CAP_E, LOW_Z, LOW_V, CAP_Q, 0}; // "GEzvQ"
static const char DT_DateTimePatternsTag[]="DateTimePatterns";
static const char DT_DateAtTimePatternsTag[]="DateTimePatterns%atTime";
static const char DT_DateTimeCalendarTag[]="calendar";
static const char DT_DateTimeGregorianTag[]="gregorian";
static const char DT_DateTimeAppendItemsTag[]="appendItems";
static const char DT_DateTimeFieldsTag[]="fields";
static const char DT_DateTimeAvailableFormatsTag[]="availableFormats";
//static const UnicodeString repeatedPattern=UnicodeString(repeatedPatterns);
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateTimePatternGenerator)
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTSkeletonEnumeration)
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTRedundantEnumeration)
DateTimePatternGenerator* U_EXPORT2
DateTimePatternGenerator::createInstance(UErrorCode& status) {
return createInstance(Locale::getDefault(), status);
}
DateTimePatternGenerator* U_EXPORT2
DateTimePatternGenerator::createInstance(const Locale& locale, UErrorCode& status) {
if (U_FAILURE(status)) {
return nullptr;
}
LocalPointer<DateTimePatternGenerator> result(
new DateTimePatternGenerator(locale, status), status);
return U_SUCCESS(status) ? result.orphan() : nullptr;
}
DateTimePatternGenerator* U_EXPORT2
DateTimePatternGenerator::createInstanceNoStdPat(const Locale& locale, UErrorCode& status) {
if (U_FAILURE(status)) {
return nullptr;
}
LocalPointer<DateTimePatternGenerator> result(
new DateTimePatternGenerator(locale, status, true), status);
return U_SUCCESS(status) ? result.orphan() : nullptr;
}
DateTimePatternGenerator* U_EXPORT2
DateTimePatternGenerator::createEmptyInstance(UErrorCode& status) {
if (U_FAILURE(status)) {
return nullptr;
}
LocalPointer<DateTimePatternGenerator> result(
new DateTimePatternGenerator(status), status);
return U_SUCCESS(status) ? result.orphan() : nullptr;
}
DateTimePatternGenerator::DateTimePatternGenerator(UErrorCode &status) :
skipMatcher(nullptr),
fAvailableFormatKeyHash(nullptr),
fDefaultHourFormatChar(0),
internalErrorCode(U_ZERO_ERROR)
{
fp = new FormatParser();
dtMatcher = new DateTimeMatcher();
distanceInfo = new DistanceInfo();
patternMap = new PatternMap();
if (fp == nullptr || dtMatcher == nullptr || distanceInfo == nullptr || patternMap == nullptr) {
internalErrorCode = status = U_MEMORY_ALLOCATION_ERROR;
}
}
DateTimePatternGenerator::DateTimePatternGenerator(const Locale& locale, UErrorCode &status, UBool skipStdPatterns) :
skipMatcher(nullptr),
fAvailableFormatKeyHash(nullptr),
fDefaultHourFormatChar(0),
internalErrorCode(U_ZERO_ERROR)
{
fp = new FormatParser();
dtMatcher = new DateTimeMatcher();
distanceInfo = new DistanceInfo();
patternMap = new PatternMap();
if (fp == nullptr || dtMatcher == nullptr || distanceInfo == nullptr || patternMap == nullptr) {
internalErrorCode = status = U_MEMORY_ALLOCATION_ERROR;
}
else {
initData(locale, status, skipStdPatterns);
}
}
DateTimePatternGenerator::DateTimePatternGenerator(const DateTimePatternGenerator& other) :
UObject(),
skipMatcher(nullptr),
fAvailableFormatKeyHash(nullptr),
fDefaultHourFormatChar(0),
internalErrorCode(U_ZERO_ERROR)
{
fp = new FormatParser();
dtMatcher = new DateTimeMatcher();
distanceInfo = new DistanceInfo();
patternMap = new PatternMap();
if (fp == nullptr || dtMatcher == nullptr || distanceInfo == nullptr || patternMap == nullptr) {
internalErrorCode = U_MEMORY_ALLOCATION_ERROR;
}
*this=other;
}
DateTimePatternGenerator&
DateTimePatternGenerator::operator=(const DateTimePatternGenerator& other) {
// reflexive case
if (&other == this) {
return *this;
}
internalErrorCode = other.internalErrorCode;
pLocale = other.pLocale;
fDefaultHourFormatChar = other.fDefaultHourFormatChar;
*fp = *(other.fp);
dtMatcher->copyFrom(other.dtMatcher->skeleton);
*distanceInfo = *(other.distanceInfo);
for (int32_t style = UDAT_FULL; style <= UDAT_SHORT; style++) {
dateTimeFormat[style] = other.dateTimeFormat[style];
}
decimal = other.decimal;
for (int32_t style = UDAT_FULL; style <= UDAT_SHORT; style++) {
dateTimeFormat[style].getTerminatedBuffer(); // NUL-terminate for the C API.
}
decimal.getTerminatedBuffer();
delete skipMatcher;
if ( other.skipMatcher == nullptr ) {
skipMatcher = nullptr;
}
else {
skipMatcher = new DateTimeMatcher(*other.skipMatcher);
if (skipMatcher == nullptr)
{
internalErrorCode = U_MEMORY_ALLOCATION_ERROR;
return *this;
}
}
for (int32_t i=0; i< UDATPG_FIELD_COUNT; ++i ) {
appendItemFormats[i] = other.appendItemFormats[i];
appendItemFormats[i].getTerminatedBuffer(); // NUL-terminate for the C API.
for (int32_t j=0; j< UDATPG_WIDTH_COUNT; ++j ) {
fieldDisplayNames[i][j] = other.fieldDisplayNames[i][j];
fieldDisplayNames[i][j].getTerminatedBuffer(); // NUL-terminate for the C API.
}
}
patternMap->copyFrom(*other.patternMap, internalErrorCode);
copyHashtable(other.fAvailableFormatKeyHash, internalErrorCode);
return *this;
}
bool
DateTimePatternGenerator::operator==(const DateTimePatternGenerator& other) const {
if (this == &other) {
return true;
}
if ((pLocale==other.pLocale) && (patternMap->equals(*other.patternMap)) &&
(decimal==other.decimal)) {
for (int32_t style = UDAT_FULL; style <= UDAT_SHORT; style++) {
if (dateTimeFormat[style] != other.dateTimeFormat[style]) {
return false;
}
}
for ( int32_t i=0 ; i<UDATPG_FIELD_COUNT; ++i ) {
if (appendItemFormats[i] != other.appendItemFormats[i]) {
return false;
}
for (int32_t j=0; j< UDATPG_WIDTH_COUNT; ++j ) {
if (fieldDisplayNames[i][j] != other.fieldDisplayNames[i][j]) {
return false;
}
}
}
return true;
}
else {
return false;
}
}
bool
DateTimePatternGenerator::operator!=(const DateTimePatternGenerator& other) const {
return !operator==(other);
}
DateTimePatternGenerator::~DateTimePatternGenerator() {
delete fAvailableFormatKeyHash;
delete fp;
delete dtMatcher;
delete distanceInfo;
delete patternMap;
delete skipMatcher;
}
namespace {
UInitOnce initOnce {};
UHashtable *localeToAllowedHourFormatsMap = nullptr;
// Value deleter for hashmap.
U_CFUNC void U_CALLCONV deleteAllowedHourFormats(void *ptr) {
uprv_free(ptr);
}
// Close hashmap at cleanup.
U_CFUNC UBool U_CALLCONV allowedHourFormatsCleanup() {
uhash_close(localeToAllowedHourFormatsMap);
return true;
}
enum AllowedHourFormat{
ALLOWED_HOUR_FORMAT_UNKNOWN = -1,
ALLOWED_HOUR_FORMAT_h,
ALLOWED_HOUR_FORMAT_H,
ALLOWED_HOUR_FORMAT_K, // Added ICU-20383, used by JP
ALLOWED_HOUR_FORMAT_k, // Added ICU-20383, not currently used
ALLOWED_HOUR_FORMAT_hb,
ALLOWED_HOUR_FORMAT_hB,
ALLOWED_HOUR_FORMAT_Kb, // Added ICU-20383, not currently used
ALLOWED_HOUR_FORMAT_KB, // Added ICU-20383, not currently used
// ICU-20383 The following are unlikely and not currently used
ALLOWED_HOUR_FORMAT_Hb,
ALLOWED_HOUR_FORMAT_HB
};
} // namespace
void
DateTimePatternGenerator::initData(const Locale& locale, UErrorCode &status, UBool skipStdPatterns) {
//const char *baseLangName = locale.getBaseName(); // unused
if (U_FAILURE(status)) { return; }
if (locale.isBogus()) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
skipMatcher = nullptr;
fAvailableFormatKeyHash=nullptr;
addCanonicalItems(status);
if (!skipStdPatterns) { // skip to prevent circular dependency when called from SimpleDateFormat::construct
addICUPatterns(locale, status);
}
addCLDRData(locale, status);
setDateTimeFromCalendar(locale, status);
setDecimalSymbols(locale, status);
umtx_initOnce(initOnce, loadAllowedHourFormatsData, status);
getAllowedHourFormats(locale, status);
// If any of the above methods failed then the object is in an invalid state.
internalErrorCode = status;
} // DateTimePatternGenerator::initData
namespace {
struct AllowedHourFormatsSink : public ResourceSink {
// Initialize sub-sinks.
AllowedHourFormatsSink() {}
virtual ~AllowedHourFormatsSink();
virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
UErrorCode &errorCode) override {
ResourceTable timeData = value.getTable(errorCode);
if (U_FAILURE(errorCode)) { return; }
for (int32_t i = 0; timeData.getKeyAndValue(i, key, value); ++i) {
const char *regionOrLocale = key;
ResourceTable formatList = value.getTable(errorCode);
if (U_FAILURE(errorCode)) { return; }
// below we construct a list[] that has an entry for the "preferred" value at [0],
// followed by 1 or more entries for the "allowed" values, terminated with an
// entry for ALLOWED_HOUR_FORMAT_UNKNOWN (not included in length below)
LocalMemory<int32_t> list;
int32_t length = 0;
int32_t preferredFormat = ALLOWED_HOUR_FORMAT_UNKNOWN;
for (int32_t j = 0; formatList.getKeyAndValue(j, key, value); ++j) {
if (uprv_strcmp(key, "allowed") == 0) {
if (value.getType() == URES_STRING) {
length = 2; // 1 preferred to add later, 1 allowed to add now
if (list.allocateInsteadAndReset(length + 1) == nullptr) {
errorCode = U_MEMORY_ALLOCATION_ERROR;
return;
}
list[1] = getHourFormatFromUnicodeString(value.getUnicodeString(errorCode));
}
else {
ResourceArray allowedFormats = value.getArray(errorCode);
length = allowedFormats.getSize() + 1; // 1 preferred, getSize allowed
if (list.allocateInsteadAndReset(length + 1) == nullptr) {
errorCode = U_MEMORY_ALLOCATION_ERROR;
return;
}
for (int32_t k = 1; k < length; ++k) {
allowedFormats.getValue(k-1, value);
list[k] = getHourFormatFromUnicodeString(value.getUnicodeString(errorCode));
}
}
} else if (uprv_strcmp(key, "preferred") == 0) {
preferredFormat = getHourFormatFromUnicodeString(value.getUnicodeString(errorCode));
}
}
if (length > 1) {
list[0] = (preferredFormat!=ALLOWED_HOUR_FORMAT_UNKNOWN)? preferredFormat: list[1];
} else {
// fallback handling for missing data
length = 2; // 1 preferred, 1 allowed
if (list.allocateInsteadAndReset(length + 1) == nullptr) {
errorCode = U_MEMORY_ALLOCATION_ERROR;
return;
}
list[0] = (preferredFormat!=ALLOWED_HOUR_FORMAT_UNKNOWN)? preferredFormat: ALLOWED_HOUR_FORMAT_H;
list[1] = list[0];
}
list[length] = ALLOWED_HOUR_FORMAT_UNKNOWN;
// At this point list[] will have at least two non-ALLOWED_HOUR_FORMAT_UNKNOWN entries,
// followed by ALLOWED_HOUR_FORMAT_UNKNOWN.
uhash_put(localeToAllowedHourFormatsMap, const_cast<char *>(regionOrLocale), list.orphan(), &errorCode);
if (U_FAILURE(errorCode)) { return; }
}
}
AllowedHourFormat getHourFormatFromUnicodeString(const UnicodeString &s) {
if (s.length() == 1) {
if (s[0] == LOW_H) { return ALLOWED_HOUR_FORMAT_h; }
if (s[0] == CAP_H) { return ALLOWED_HOUR_FORMAT_H; }
if (s[0] == CAP_K) { return ALLOWED_HOUR_FORMAT_K; }
if (s[0] == LOW_K) { return ALLOWED_HOUR_FORMAT_k; }
} else if (s.length() == 2) {
if (s[0] == LOW_H && s[1] == LOW_B) { return ALLOWED_HOUR_FORMAT_hb; }
if (s[0] == LOW_H && s[1] == CAP_B) { return ALLOWED_HOUR_FORMAT_hB; }
if (s[0] == CAP_K && s[1] == LOW_B) { return ALLOWED_HOUR_FORMAT_Kb; }
if (s[0] == CAP_K && s[1] == CAP_B) { return ALLOWED_HOUR_FORMAT_KB; }
if (s[0] == CAP_H && s[1] == LOW_B) { return ALLOWED_HOUR_FORMAT_Hb; }
if (s[0] == CAP_H && s[1] == CAP_B) { return ALLOWED_HOUR_FORMAT_HB; }
}
return ALLOWED_HOUR_FORMAT_UNKNOWN;
}
};
} // namespace
AllowedHourFormatsSink::~AllowedHourFormatsSink() {}
U_CFUNC void U_CALLCONV DateTimePatternGenerator::loadAllowedHourFormatsData(UErrorCode &status) {
if (U_FAILURE(status)) { return; }
localeToAllowedHourFormatsMap = uhash_open(
uhash_hashChars, uhash_compareChars, nullptr, &status);
if (U_FAILURE(status)) { return; }
uhash_setValueDeleter(localeToAllowedHourFormatsMap, deleteAllowedHourFormats);
ucln_i18n_registerCleanup(UCLN_I18N_ALLOWED_HOUR_FORMATS, allowedHourFormatsCleanup);
LocalUResourceBundlePointer rb(ures_openDirect(nullptr, "supplementalData", &status));
if (U_FAILURE(status)) { return; }
AllowedHourFormatsSink sink;
// TODO: Currently in the enumeration each table allocates a new array.
// Try to reduce the number of memory allocations. Consider storing a
// UVector32 with the concatenation of all of the sub-arrays, put the start index
// into the hashmap, store 6 single-value sub-arrays right at the beginning of the
// vector (at index enum*2) for easy data sharing, copy sub-arrays into runtime
// object. Remember to clean up the vector, too.
ures_getAllItemsWithFallback(rb.getAlias(), "timeData", sink, status);
}
static int32_t* getAllowedHourFormatsLangCountry(const char* language, const char* country, UErrorCode& status) {
CharString langCountry;
langCountry.append(language, status);
langCountry.append('_', status);
langCountry.append(country, status);
int32_t* allowedFormats;
allowedFormats = static_cast<int32_t*>(uhash_get(localeToAllowedHourFormatsMap, langCountry.data()));
if (allowedFormats == nullptr) {
allowedFormats = static_cast<int32_t*>(uhash_get(localeToAllowedHourFormatsMap, const_cast<char*>(country)));
}
return allowedFormats;
}
void DateTimePatternGenerator::getAllowedHourFormats(const Locale &locale, UErrorCode &status) {
if (U_FAILURE(status)) { return; }
const char *language = locale.getLanguage();
CharString baseCountry = ulocimp_getRegionForSupplementalData(locale.getName(), false, status);
const char* country = baseCountry.data();
Locale maxLocale; // must be here for correct lifetime
if (*language == '\0' || *country == '\0') {
maxLocale = locale;
UErrorCode localStatus = U_ZERO_ERROR;
maxLocale.addLikelySubtags(localStatus);
if (U_SUCCESS(localStatus)) {
language = maxLocale.getLanguage();
country = maxLocale.getCountry();
}
}
if (*language == '\0') {
// Unexpected, but fail gracefully
language = "und";
}
if (*country == '\0') {
country = "001";
}
int32_t* allowedFormats = getAllowedHourFormatsLangCountry(language, country, status);
// We need to check if there is an hour cycle on locale
char buffer[8];
int32_t count = locale.getKeywordValue("hours", buffer, sizeof(buffer), status);
fDefaultHourFormatChar = 0;
if (U_SUCCESS(status) && count > 0) {
if(uprv_strcmp(buffer, "h24") == 0) {
fDefaultHourFormatChar = LOW_K;
} else if(uprv_strcmp(buffer, "h23") == 0) {
fDefaultHourFormatChar = CAP_H;
} else if(uprv_strcmp(buffer, "h12") == 0) {
fDefaultHourFormatChar = LOW_H;
} else if(uprv_strcmp(buffer, "h11") == 0) {
fDefaultHourFormatChar = CAP_K;
}
}
// Check if the region has an alias
if (allowedFormats == nullptr) {
UErrorCode localStatus = U_ZERO_ERROR;
const Region* region = Region::getInstance(country, localStatus);
if (U_SUCCESS(localStatus)) {
country = region->getRegionCode(); // the real region code
allowedFormats = getAllowedHourFormatsLangCountry(language, country, status);
}
}
if (allowedFormats != nullptr) { // Lookup is successful
// Here allowedFormats points to a list consisting of key for preferredFormat,
// followed by one or more keys for allowedFormats, then followed by ALLOWED_HOUR_FORMAT_UNKNOWN.
if (!fDefaultHourFormatChar) {
switch (allowedFormats[0]) {
case ALLOWED_HOUR_FORMAT_h: fDefaultHourFormatChar = LOW_H; break;
case ALLOWED_HOUR_FORMAT_H: fDefaultHourFormatChar = CAP_H; break;
case ALLOWED_HOUR_FORMAT_K: fDefaultHourFormatChar = CAP_K; break;
case ALLOWED_HOUR_FORMAT_k: fDefaultHourFormatChar = LOW_K; break;
default: fDefaultHourFormatChar = CAP_H; break;
}
}
for (int32_t i = 0; i < UPRV_LENGTHOF(fAllowedHourFormats); ++i) {
fAllowedHourFormats[i] = allowedFormats[i + 1];
if (fAllowedHourFormats[i] == ALLOWED_HOUR_FORMAT_UNKNOWN) {
break;
}
}
} else { // Lookup failed, twice
if (!fDefaultHourFormatChar) {
fDefaultHourFormatChar = CAP_H;
}
fAllowedHourFormats[0] = ALLOWED_HOUR_FORMAT_H;
fAllowedHourFormats[1] = ALLOWED_HOUR_FORMAT_UNKNOWN;
}
}
UDateFormatHourCycle
DateTimePatternGenerator::getDefaultHourCycle(UErrorCode& status) const {
if (U_FAILURE(status)) {
return UDAT_HOUR_CYCLE_23;
}
if (fDefaultHourFormatChar == 0) {
// We need to return something, but the caller should ignore it
// anyways since the returned status is a failure.
status = U_UNSUPPORTED_ERROR;
return UDAT_HOUR_CYCLE_23;
}
switch (fDefaultHourFormatChar) {
case CAP_K:
return UDAT_HOUR_CYCLE_11;
case LOW_H:
return UDAT_HOUR_CYCLE_12;
case CAP_H:
return UDAT_HOUR_CYCLE_23;
case LOW_K:
return UDAT_HOUR_CYCLE_24;
default:
UPRV_UNREACHABLE_EXIT;
}
}
UnicodeString
DateTimePatternGenerator::getSkeleton(const UnicodeString& pattern, UErrorCode&
/*status*/) {
FormatParser fp2;
DateTimeMatcher matcher;
PtnSkeleton localSkeleton;
matcher.set(pattern, &fp2, localSkeleton);
return localSkeleton.getSkeleton();
}
UnicodeString
DateTimePatternGenerator::staticGetSkeleton(
const UnicodeString& pattern, UErrorCode& /*status*/) {
FormatParser fp;
DateTimeMatcher matcher;
PtnSkeleton localSkeleton;
matcher.set(pattern, &fp, localSkeleton);
return localSkeleton.getSkeleton();
}
UnicodeString
DateTimePatternGenerator::getBaseSkeleton(const UnicodeString& pattern, UErrorCode& /*status*/) {
FormatParser fp2;
DateTimeMatcher matcher;
PtnSkeleton localSkeleton;
matcher.set(pattern, &fp2, localSkeleton);
return localSkeleton.getBaseSkeleton();
}
UnicodeString
DateTimePatternGenerator::staticGetBaseSkeleton(
const UnicodeString& pattern, UErrorCode& /*status*/) {
FormatParser fp;
DateTimeMatcher matcher;
PtnSkeleton localSkeleton;
matcher.set(pattern, &fp, localSkeleton);
return localSkeleton.getBaseSkeleton();
}
void
DateTimePatternGenerator::addICUPatterns(const Locale& locale, UErrorCode& status) {
if (U_FAILURE(status)) {
return;
}
LocalUResourceBundlePointer rb(ures_open(nullptr, locale.getBaseName(), &status));
CharString calendarTypeToUse; // to be filled in with the type to use, if all goes well
getCalendarTypeToUse(locale, calendarTypeToUse, status);
// HACK to get around the fact that the old SimpleDateFormat code (actually, Calendar::getCalendarTypeForLocale() )
// returns "gregorian" for ja_JP_TRADITIONAL instead of "japanese"
if (uprv_strcmp(locale.getBaseName(), "ja_JP_TRADITIONAL") == 0) {
calendarTypeToUse.clear().append("gregorian", status);
}
if (U_FAILURE(status)) {
return;
}
// TODO: See ICU-22867
CharString patternResourcePath;
patternResourcePath.append(DT_DateTimeCalendarTag, status)
.append('/', status)
.append(calendarTypeToUse, status)
.append('/', status)
.append(DT_DateTimePatternsTag, status);
LocalUResourceBundlePointer dateTimePatterns(ures_getByKeyWithFallback(rb.getAlias(), patternResourcePath.data(),
nullptr, &status));
if (ures_getType(dateTimePatterns.getAlias()) != URES_ARRAY || ures_getSize(dateTimePatterns.getAlias()) < 8) {
status = U_INVALID_FORMAT_ERROR;
return;
}
for (int32_t i = 0; U_SUCCESS(status) && i < DateFormat::kDateTime; i++) {
LocalUResourceBundlePointer patternRes(ures_getByIndex(dateTimePatterns.getAlias(), i, nullptr, &status));
UnicodeString pattern;
switch (ures_getType(patternRes.getAlias())) {
case URES_STRING:
pattern = ures_getUnicodeString(patternRes.getAlias(), &status);
break;
case URES_ARRAY:
pattern = ures_getUnicodeStringByIndex(patternRes.getAlias(), 0, &status);
break;
default:
status = U_INVALID_FORMAT_ERROR;
return;
}
if (U_SUCCESS(status)) {
UnicodeString conflictingPattern;
addPatternWithSkeleton(pattern, nullptr, false, conflictingPattern, status);
}
}
}
void
DateTimePatternGenerator::hackTimes(const UnicodeString& hackPattern, UErrorCode& status) {
UnicodeString conflictingString;
fp->set(hackPattern);
UnicodeString mmss;
UBool gotMm=false;
for (int32_t i=0; i<fp->itemNumber; ++i) {
UnicodeString field = fp->items[i];
if ( fp->isQuoteLiteral(field) ) {
if ( gotMm ) {
UnicodeString quoteLiteral;
fp->getQuoteLiteral(quoteLiteral, &i);
mmss += quoteLiteral;
}
}
else {
if (fp->isPatternSeparator(field) && gotMm) {
mmss+=field;
}
else {
char16_t ch=field.charAt(0);
if (ch==LOW_M) {
gotMm=true;
mmss+=field;
}
else {
if (ch==LOW_S) {
if (!gotMm) {
break;
}
mmss+= field;
addPattern(mmss, false, conflictingString, status);
break;
}
else {
if (gotMm || ch==LOW_Z || ch==CAP_Z || ch==LOW_V || ch==CAP_V) {
break;
}
}
}
}
}
}
}
#define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
void
DateTimePatternGenerator::getCalendarTypeToUse(const Locale& locale, CharString& destination, UErrorCode& err) {
destination.clear().append(DT_DateTimeGregorianTag, -1, err); // initial default
if ( U_SUCCESS(err) ) {
UErrorCode localStatus = U_ZERO_ERROR;
char localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY];
// obtain a locale that always has the calendar key value that should be used
ures_getFunctionalEquivalent(
localeWithCalendarKey,
ULOC_LOCALE_IDENTIFIER_CAPACITY,
nullptr,
"calendar",
"calendar",
locale.getName(),
nullptr,
false,
&localStatus);
localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY-1] = 0; // ensure null termination
// now get the calendar key value from that locale
// (the call to ures_getFunctionalEquivalent() above might fail, and if it does, localeWithCalendarKey
// won't contain a `calendar` keyword. If this happens, the line below will stomp on `destination`,
// so we have to check the return code before overwriting `destination`.)
if (U_SUCCESS(localStatus)) {
destination = ulocimp_getKeywordValue(localeWithCalendarKey, "calendar", localStatus);
}
// If the input locale was invalid, don't fail with missing resource error, instead
// continue with default of Gregorian.
if (U_FAILURE(localStatus) && localStatus != U_MISSING_RESOURCE_ERROR) {
err = localStatus;
}
}
}
void
DateTimePatternGenerator::consumeShortTimePattern(const UnicodeString& shortTimePattern,
UErrorCode& status) {
if (U_FAILURE(status)) { return; }
// ICU-20383 No longer set fDefaultHourFormatChar to the hour format character from
// this pattern; instead it is set from localeToAllowedHourFormatsMap which now
// includes entries for both preferred and allowed formats.
// HACK for hh:ss
hackTimes(shortTimePattern, status);
}
struct DateTimePatternGenerator::AppendItemFormatsSink : public ResourceSink {
// Destination for data, modified via setters.
DateTimePatternGenerator& dtpg;
AppendItemFormatsSink(DateTimePatternGenerator& _dtpg) : dtpg(_dtpg) {}
virtual ~AppendItemFormatsSink();
virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
UErrorCode &errorCode) override {
UDateTimePatternField field = dtpg.getAppendFormatNumber(key);
if (field == UDATPG_FIELD_COUNT) { return; }
const UnicodeString& valueStr = value.getUnicodeString(errorCode);
if (dtpg.getAppendItemFormat(field).isEmpty() && !valueStr.isEmpty()) {
dtpg.setAppendItemFormat(field, valueStr);
}
}
void fillInMissing() {
UnicodeString defaultItemFormat(true, UDATPG_ItemFormat, UPRV_LENGTHOF(UDATPG_ItemFormat)-1); // Read-only alias.
for (int32_t i = 0; i < UDATPG_FIELD_COUNT; i++) {
UDateTimePatternField field = static_cast<UDateTimePatternField>(i);
if (dtpg.getAppendItemFormat(field).isEmpty()) {
dtpg.setAppendItemFormat(field, defaultItemFormat);
}
}
}
};
struct DateTimePatternGenerator::AppendItemNamesSink : public ResourceSink {
// Destination for data, modified via setters.
DateTimePatternGenerator& dtpg;
AppendItemNamesSink(DateTimePatternGenerator& _dtpg) : dtpg(_dtpg) {}
virtual ~AppendItemNamesSink();
virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
UErrorCode &errorCode) override {
UDateTimePGDisplayWidth width;
UDateTimePatternField field = dtpg.getFieldAndWidthIndices(key, &width);
if (field == UDATPG_FIELD_COUNT) { return; }
ResourceTable detailsTable = value.getTable(errorCode);
if (U_FAILURE(errorCode)) { return; }
if (!detailsTable.findValue("dn", value)) { return; }
const UnicodeString& valueStr = value.getUnicodeString(errorCode);
if (U_SUCCESS(errorCode) && dtpg.getFieldDisplayName(field,width).isEmpty() && !valueStr.isEmpty()) {