@@ -2759,6 +2759,12 @@ S_calculate_LC_ALL_string(pTHX_ const char ** category_locales_list,
2759
2759
* doesn't have to worry about it being
2760
2760
* per-thread, nor needs to arrange for its
2761
2761
* clean-up.
2762
+ * WANT_PL_setlocale_buf the function stores the calculated string
2763
+ * into the per-thread buffer PL_setlocale_buf
2764
+ * and returns a pointer to that. The buffer
2765
+ * is cleaned up automatically in process
2766
+ * destruction. This return method avoids
2767
+ * extra copies in some circumstances.
2762
2768
* WANT_VOID NULL is returned. This is used when the
2763
2769
* function is being called only for its side
2764
2770
* effect of updating
@@ -2903,6 +2909,14 @@ S_calculate_LC_ALL_string(pTHX_ const char ** category_locales_list,
2903
2909
2904
2910
/* If all categories have the same locale, we already know the answer */
2905
2911
if (! disparate ) {
2912
+ if (returning == WANT_PL_setlocale_buf ) {
2913
+ save_to_buffer (locales_list [0 ],
2914
+ & PL_setlocale_buf ,
2915
+ & PL_setlocale_bufsize );
2916
+ retval = PL_setlocale_buf ;
2917
+ }
2918
+ else {
2919
+
2906
2920
retval = locales_list [0 ];
2907
2921
2908
2922
/* If a temporary is wanted for the return, and we had to create
@@ -2917,11 +2931,22 @@ S_calculate_LC_ALL_string(pTHX_ const char ** category_locales_list,
2917
2931
/* In all cases here, there's nothing we create that needs to be
2918
2932
* freed, so leave 'free_if_void_return' set to the default
2919
2933
* 'false'. */
2934
+ }
2920
2935
}
2921
2936
else { /* Here, not all categories have the same locale */
2922
2937
2923
2938
char * constructed ;
2924
2939
2940
+ /* If returning to PL_setlocale_buf, set up to write directly to it,
2941
+ * being sure it is resized to be large enough */
2942
+ if (returning == WANT_PL_setlocale_buf ) {
2943
+ set_save_buffer_min_size (total_len ,
2944
+ & PL_setlocale_buf ,
2945
+ & PL_setlocale_bufsize );
2946
+ constructed = PL_setlocale_buf ;
2947
+ }
2948
+ else { /* Otherwise we need new memory to hold the calculated value. */
2949
+
2925
2950
Newx (constructed , total_len , char );
2926
2951
2927
2952
/* If returning the new memory, it must be set up to be freed
@@ -2932,6 +2957,7 @@ S_calculate_LC_ALL_string(pTHX_ const char ** category_locales_list,
2932
2957
else {
2933
2958
free_if_void_return = true;
2934
2959
}
2960
+ }
2935
2961
2936
2962
constructed [0 ] = '\0' ;
2937
2963
0 commit comments