* Format a numeric value per current LC_NUMERIC locale setting * * Returns the appropriately formatted string in a new allocated block, * caller must free. * * setDecimalLocale() must have been called earlier. */
| 241 | * setDecimalLocale() must have been called earlier. |
| 242 | */ |
| 243 | static char * |
| 244 | format_numeric_locale(const char *my_str) |
| 245 | { |
| 246 | char *new_str; |
| 247 | int new_len, |
| 248 | int_len, |
| 249 | leading_digits, |
| 250 | i, |
| 251 | new_str_pos; |
| 252 | |
| 253 | /* |
| 254 | * If the string doesn't look like a number, return it unchanged. This |
| 255 | * check is essential to avoid mangling already-localized "money" values. |
| 256 | */ |
| 257 | if (strspn(my_str, "0123456789+-.eE") != strlen(my_str)) |
| 258 | return pg_strdup(my_str); |
| 259 | |
| 260 | new_len = strlen(my_str) + additional_numeric_locale_len(my_str); |
| 261 | new_str = pg_malloc(new_len + 1); |
| 262 | new_str_pos = 0; |
| 263 | int_len = integer_digits(my_str); |
| 264 | |
| 265 | /* number of digits in first thousands group */ |
| 266 | leading_digits = int_len % groupdigits; |
| 267 | if (leading_digits == 0) |
| 268 | leading_digits = groupdigits; |
| 269 | |
| 270 | /* process sign */ |
| 271 | if (my_str[0] == '-' || my_str[0] == '+') |
| 272 | { |
| 273 | new_str[new_str_pos++] = my_str[0]; |
| 274 | my_str++; |
| 275 | } |
| 276 | |
| 277 | /* process integer part of number */ |
| 278 | for (i = 0; i < int_len; i++) |
| 279 | { |
| 280 | /* Time to insert separator? */ |
| 281 | if (i > 0 && --leading_digits == 0) |
| 282 | { |
| 283 | strcpy(&new_str[new_str_pos], thousands_sep); |
| 284 | new_str_pos += strlen(thousands_sep); |
| 285 | leading_digits = groupdigits; |
| 286 | } |
| 287 | new_str[new_str_pos++] = my_str[i]; |
| 288 | } |
| 289 | |
| 290 | /* handle decimal point if any */ |
| 291 | if (my_str[i] == '.') |
| 292 | { |
| 293 | strcpy(&new_str[new_str_pos], decimal_point); |
| 294 | new_str_pos += strlen(decimal_point); |
| 295 | i++; |
| 296 | } |
| 297 | |
| 298 | /* copy the rest (fractional digits and/or exponent, and \0 terminator) */ |
| 299 | strcpy(&new_str[new_str_pos], &my_str[i]); |
| 300 |
no test coverage detected