@@ -147,7 +147,8 @@ static size_t uchar_length(UConverter *converter,
147
147
static int32_t uchar_convert (UConverter * converter ,
148
148
UChar * dest , int32_t destlen ,
149
149
const char * str , int32_t srclen );
150
- static void icu_set_collation_attributes (UCollator * collator , const char * loc );
150
+ static void icu_set_collation_attributes (UCollator * collator , const char * loc ,
151
+ UErrorCode * status );
151
152
#endif
152
153
153
154
/*
@@ -2496,6 +2497,7 @@ pg_ucol_open(const char *loc_str)
2496
2497
{
2497
2498
UCollator * collator ;
2498
2499
UErrorCode status ;
2500
+ const char * orig_str = loc_str ;
2499
2501
char * fixed_str = NULL ;
2500
2502
2501
2503
/*
@@ -2544,11 +2546,27 @@ pg_ucol_open(const char *loc_str)
2544
2546
collator = ucol_open (loc_str , & status );
2545
2547
if (U_FAILURE (status ))
2546
2548
ereport (ERROR ,
2549
+ /* use original string for error report */
2547
2550
(errmsg ("could not open collator for locale \"%s\": %s" ,
2548
- loc_str , u_errorName (status ))));
2551
+ orig_str , u_errorName (status ))));
2549
2552
2550
2553
if (U_ICU_VERSION_MAJOR_NUM < 54 )
2551
- icu_set_collation_attributes (collator , loc_str );
2554
+ {
2555
+ status = U_ZERO_ERROR ;
2556
+ icu_set_collation_attributes (collator , loc_str , & status );
2557
+
2558
+ /*
2559
+ * Pretend the error came from ucol_open(), for consistent error
2560
+ * message across ICU versions.
2561
+ */
2562
+ if (U_FAILURE (status ))
2563
+ {
2564
+ ucol_close (collator );
2565
+ ereport (ERROR ,
2566
+ (errmsg ("could not open collator for locale \"%s\": %s" ,
2567
+ orig_str , u_errorName (status ))));
2568
+ }
2569
+ }
2552
2570
2553
2571
if (fixed_str != NULL )
2554
2572
pfree (fixed_str );
@@ -2698,9 +2716,9 @@ icu_from_uchar(char **result, const UChar *buff_uchar, int32_t len_uchar)
2698
2716
*/
2699
2717
pg_attribute_unused ()
2700
2718
static void
2701
- icu_set_collation_attributes (UCollator * collator , const char * loc )
2719
+ icu_set_collation_attributes (UCollator * collator , const char * loc ,
2720
+ UErrorCode * status )
2702
2721
{
2703
- UErrorCode status ;
2704
2722
int32_t len ;
2705
2723
char * icu_locale_id ;
2706
2724
char * lower_str ;
@@ -2713,15 +2731,13 @@ icu_set_collation_attributes(UCollator *collator, const char *loc)
2713
2731
* locale ID, e.g. "und@colcaselevel=yes;colstrength=primary", by
2714
2732
* uloc_canonicalize().
2715
2733
*/
2716
- status = U_ZERO_ERROR ;
2717
- len = uloc_canonicalize (loc , NULL , 0 , & status );
2734
+ * status = U_ZERO_ERROR ;
2735
+ len = uloc_canonicalize (loc , NULL , 0 , status );
2718
2736
icu_locale_id = palloc (len + 1 );
2719
- status = U_ZERO_ERROR ;
2720
- len = uloc_canonicalize (loc , icu_locale_id , len + 1 , & status );
2721
- if (U_FAILURE (status ))
2722
- ereport (ERROR ,
2723
- (errmsg ("canonicalization failed for locale string \"%s\": %s" ,
2724
- loc , u_errorName (status ))));
2737
+ * status = U_ZERO_ERROR ;
2738
+ len = uloc_canonicalize (loc , icu_locale_id , len + 1 , status );
2739
+ if (U_FAILURE (* status ))
2740
+ return ;
2725
2741
2726
2742
lower_str = asc_tolower (icu_locale_id , strlen (icu_locale_id ));
2727
2743
@@ -2743,7 +2759,7 @@ icu_set_collation_attributes(UCollator *collator, const char *loc)
2743
2759
UColAttribute uattr ;
2744
2760
UColAttributeValue uvalue ;
2745
2761
2746
- status = U_ZERO_ERROR ;
2762
+ * status = U_ZERO_ERROR ;
2747
2763
2748
2764
* e = '\0' ;
2749
2765
name = token ;
@@ -2793,22 +2809,12 @@ icu_set_collation_attributes(UCollator *collator, const char *loc)
2793
2809
else if (strcmp (value , "upper" ) == 0 )
2794
2810
uvalue = UCOL_UPPER_FIRST ;
2795
2811
else
2796
- status = U_ILLEGAL_ARGUMENT_ERROR ;
2797
-
2798
- if (status == U_ZERO_ERROR )
2799
- ucol_setAttribute (collator , uattr , uvalue , & status );
2800
-
2801
- /*
2802
- * Pretend the error came from ucol_open(), for consistent error
2803
- * message across ICU versions.
2804
- */
2805
- if (U_FAILURE (status ))
2806
2812
{
2807
- ucol_close (collator );
2808
- ereport (ERROR ,
2809
- (errmsg ("could not open collator for locale \"%s\": %s" ,
2810
- loc , u_errorName (status ))));
2813
+ * status = U_ILLEGAL_ARGUMENT_ERROR ;
2814
+ break ;
2811
2815
}
2816
+
2817
+ ucol_setAttribute (collator , uattr , uvalue , status );
2812
2818
}
2813
2819
}
2814
2820
0 commit comments