@@ -140,6 +140,7 @@ static char *IsoLocaleName(const char *);
140
140
*/
141
141
static UConverter * icu_converter = NULL ;
142
142
143
+ static UCollator * pg_ucol_open (const char * loc_str );
143
144
static void init_icu_converter (void );
144
145
static size_t uchar_length (UConverter * converter ,
145
146
const char * str , int32_t len );
@@ -1430,17 +1431,8 @@ make_icu_collator(const char *iculocstr,
1430
1431
{
1431
1432
#ifdef USE_ICU
1432
1433
UCollator * collator ;
1433
- UErrorCode status ;
1434
1434
1435
- status = U_ZERO_ERROR ;
1436
- collator = ucol_open (iculocstr , & status );
1437
- if (U_FAILURE (status ))
1438
- ereport (ERROR ,
1439
- (errmsg ("could not open collator for locale \"%s\": %s" ,
1440
- iculocstr , u_errorName (status ))));
1441
-
1442
- if (U_ICU_VERSION_MAJOR_NUM < 54 )
1443
- icu_set_collation_attributes (collator , iculocstr );
1435
+ collator = pg_ucol_open (iculocstr );
1444
1436
1445
1437
/*
1446
1438
* If rules are specified, we extract the rules of the standard collation,
@@ -1451,6 +1443,7 @@ make_icu_collator(const char *iculocstr,
1451
1443
const UChar * default_rules ;
1452
1444
UChar * agg_rules ;
1453
1445
UChar * my_rules ;
1446
+ UErrorCode status ;
1454
1447
int32_t length ;
1455
1448
1456
1449
default_rules = ucol_getRules (collator , & length );
@@ -1722,16 +1715,11 @@ get_collation_actual_version(char collprovider, const char *collcollate)
1722
1715
if (collprovider == COLLPROVIDER_ICU )
1723
1716
{
1724
1717
UCollator * collator ;
1725
- UErrorCode status ;
1726
1718
UVersionInfo versioninfo ;
1727
1719
char buf [U_MAX_VERSION_STRING_LENGTH ];
1728
1720
1729
- status = U_ZERO_ERROR ;
1730
- collator = ucol_open (collcollate , & status );
1731
- if (U_FAILURE (status ))
1732
- ereport (ERROR ,
1733
- (errmsg ("could not open collator for locale \"%s\": %s" ,
1734
- collcollate , u_errorName (status ))));
1721
+ collator = pg_ucol_open (collcollate );
1722
+
1735
1723
ucol_getVersion (collator , versioninfo );
1736
1724
ucol_close (collator );
1737
1725
@@ -2505,6 +2493,43 @@ pg_strnxfrm_prefix(char *dest, size_t destsize, const char *src,
2505
2493
}
2506
2494
2507
2495
#ifdef USE_ICU
2496
+
2497
+ /*
2498
+ * Wrapper around ucol_open() to handle API differences for older ICU
2499
+ * versions.
2500
+ */
2501
+ static UCollator *
2502
+ pg_ucol_open (const char * loc_str )
2503
+ {
2504
+ UCollator * collator ;
2505
+ UErrorCode status ;
2506
+
2507
+ /*
2508
+ * Must never open default collator, because it depends on the environment
2509
+ * and may change at any time.
2510
+ *
2511
+ * NB: the default collator is not the same as the collator for the root
2512
+ * locale. The root locale may be specified as the empty string, "und", or
2513
+ * "root". The default collator is opened by passing NULL to ucol_open().
2514
+ */
2515
+ if (loc_str == NULL )
2516
+ ereport (ERROR ,
2517
+ (errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
2518
+ errmsg ("opening default collator is not supported" )));
2519
+
2520
+ status = U_ZERO_ERROR ;
2521
+ collator = ucol_open (loc_str , & status );
2522
+ if (U_FAILURE (status ))
2523
+ ereport (ERROR ,
2524
+ (errmsg ("could not open collator for locale \"%s\": %s" ,
2525
+ loc_str , u_errorName (status ))));
2526
+
2527
+ if (U_ICU_VERSION_MAJOR_NUM < 54 )
2528
+ icu_set_collation_attributes (collator , loc_str );
2529
+
2530
+ return collator ;
2531
+ }
2532
+
2508
2533
static void
2509
2534
init_icu_converter (void )
2510
2535
{
@@ -2771,17 +2796,8 @@ check_icu_locale(const char *icu_locale)
2771
2796
{
2772
2797
#ifdef USE_ICU
2773
2798
UCollator * collator ;
2774
- UErrorCode status ;
2775
-
2776
- status = U_ZERO_ERROR ;
2777
- collator = ucol_open (icu_locale , & status );
2778
- if (U_FAILURE (status ))
2779
- ereport (ERROR ,
2780
- (errmsg ("could not open collator for locale \"%s\": %s" ,
2781
- icu_locale , u_errorName (status ))));
2782
2799
2783
- if (U_ICU_VERSION_MAJOR_NUM < 54 )
2784
- icu_set_collation_attributes (collator , icu_locale );
2800
+ collator = pg_ucol_open (icu_locale );
2785
2801
ucol_close (collator );
2786
2802
#else
2787
2803
ereport (ERROR ,
0 commit comments