Print this page
2964 need POSIX 2008 locale object support
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Gordon Ross <gordon.ross@nexenta.com>
Approved by: TBD
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libc/port/locale/strfmon.c
+++ new/usr/src/lib/libc/port/locale/strfmon.c
1 1 /*
2 + * Copyright 2013 Garrett D'Amore <garrett@damore.org>
2 3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
3 4 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
4 5 * All rights reserved.
5 6 *
6 7 * Redistribution and use in source and binary forms, with or without
7 8 * modification, are permitted provided that the following conditions
8 9 * are met:
9 10 * 1. Redistributions of source code must retain the above copyright
10 11 * notice, this list of conditions and the following disclaimer.
11 12 * 2. Redistributions in binary form must reproduce the above copyright
12 13 * notice, this list of conditions and the following disclaimer in the
13 14 * documentation and/or other materials provided with the distribution.
14 15 *
15 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 26 * SUCH DAMAGE.
26 27 *
27 28 */
28 29
29 30 #ifndef _LCONV_C99
30 31 #define _LCONV_C99
31 32 #endif
32 33
33 34 #include "lint.h"
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
34 35 #include <sys/types.h>
35 36 #include <ctype.h>
36 37 #include <errno.h>
37 38 #include <limits.h>
38 39 #include <locale.h>
39 40 #include <monetary.h>
40 41 #include <stdarg.h>
41 42 #include <stdio.h>
42 43 #include <stdlib.h>
43 44 #include <string.h>
45 +#include "localeimpl.h"
46 +#include "lmonetary.h"
47 +#include "lnumeric.h"
44 48
45 49 /* internal flags */
46 50 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
47 51 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
48 52 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
49 53 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
50 54 #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */
51 55 #define LEFT_JUSTIFY 0x20 /* left justify */
52 56 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
53 57 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
54 58
55 59 /* internal macros */
56 60 #define PRINT(CH) { \
57 61 if (dst >= s + maxsize) \
58 62 goto e2big_error; \
59 63 *dst++ = CH; \
60 64 }
61 65
62 66 #define PRINTS(STR) { \
63 - char *tmps = STR; \
67 + const char *tmps = STR; \
64 68 while (*tmps != '\0') \
65 69 PRINT(*tmps++); \
66 70 }
67 71
68 72 #define GET_NUMBER(VAR) { \
69 73 VAR = 0; \
70 74 while (isdigit((unsigned char)*fmt)) { \
71 75 if (VAR > INT_MAX / 10) \
72 76 goto e2big_error; \
73 77 VAR *= 10; \
74 78 VAR += *fmt - '0'; \
75 79 if (VAR < 0) \
76 80 goto e2big_error; \
77 81 fmt++; \
78 82 } \
79 83 }
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
80 84
81 85 #define GRPCPY(howmany) { \
82 86 int i = howmany; \
83 87 while (i-- > 0) { \
84 88 avalue_size--; \
85 89 *--bufend = *(avalue+avalue_size+padded); \
86 90 } \
87 91 }
88 92
89 93 #define GRPSEP { \
90 - *--bufend = thousands_sep; \
94 + bufend -= thousands_len; \
95 + (void) memcpy(bufend, thousands_sep, thousands_len); \
91 96 groups++; \
92 97 }
93 98
94 -static void __setup_vars(int, char *, char *, char *, char **);
95 -static int __calc_left_pad(int, char *);
96 -static char *__format_grouped_double(double, int *, int, int, int);
99 +static void setup_vars(const struct lc_monetary *, int, char *, char *, char *,
100 + const char **);
101 +static int calc_left_pad(const struct lc_monetary *, int, const char *);
102 +static char *format_grouped_double(const struct lc_monetary *,
103 + const struct lc_numeric *, double, int *, int, int, int);
97 104
98 105 ssize_t
99 -strfmon(char *_RESTRICT_KYWD s, size_t maxsize,
100 - const char *_RESTRICT_KYWD format, ...)
106 +strfmon_impl(char *_RESTRICT_KYWD s, size_t maxsize, locale_t loc,
107 + const char *_RESTRICT_KYWD format, va_list ap)
101 108 {
102 - va_list ap;
103 109 char *dst; /* output destination pointer */
104 110 const char *fmt; /* current format poistion pointer */
105 - struct lconv *lc; /* pointer to lconv structure */
106 111 char *asciivalue; /* formatted double pointer */
107 112
108 113 int flags; /* formatting options */
109 114 int pad_char; /* padding character */
110 115 int pad_size; /* pad size */
111 116 int width; /* field width */
112 117 int left_prec; /* left precision */
113 118 int right_prec; /* right precision */
114 119 double value; /* just value */
115 120 char space_char = ' '; /* space after currency */
116 121
117 - char cs_precedes; /* values gathered from struct lconv */
122 + char cs_precedes; /* values from struct lc_monetary */
118 123 char sep_by_space;
119 124 char sign_posn;
120 - char *signstr;
121 - char *currency_symbol;
125 + const char *signstr;
126 + const char *currency_symbol;
122 127
123 128 char *tmpptr; /* temporary vars */
124 129 int sverrno;
130 + const struct lc_monetary *lmon; /* monetary structure */
131 + const struct lc_numeric *lnum; /* numeric structure */
125 132
126 - va_start(ap, format);
133 + lmon = loc->monetary;
134 + lnum = loc->numeric;
127 135
128 - lc = localeconv();
129 136 dst = s;
130 137 fmt = format;
131 138 asciivalue = NULL;
132 139 currency_symbol = NULL;
133 140 pad_size = 0;
134 141
135 142 while (*fmt) {
136 143 /* pass nonformating characters AS IS */
137 144 if (*fmt != '%')
138 145 goto literal;
139 146
140 147 /* '%' found ! */
141 148
142 149 /* "%%" mean just '%' */
143 150 if (*(fmt+1) == '%') {
144 151 fmt++;
145 152 literal:
146 153 PRINT(*fmt++);
147 154 continue;
148 155 }
149 156
150 157 /* set up initial values */
151 158 flags = (NEED_GROUPING|LOCALE_POSN);
152 159 pad_char = ' '; /* padding character is "space" */
153 160 left_prec = -1; /* no left precision specified */
154 161 right_prec = -1; /* no right precision specified */
155 162 width = -1; /* no width specified */
156 163 value = 0; /* we have no value to print now */
157 164
158 165 /* Flags */
159 166 for (;;) {
160 167 switch (*++fmt) {
161 168 case '=': /* fill character */
162 169 pad_char = *++fmt;
163 170 if (pad_char == '\0')
164 171 goto format_error;
165 172 continue;
166 173 case '^': /* not group currency */
167 174 flags &= ~(NEED_GROUPING);
168 175 continue;
169 176 case '+': /* use locale defined signs */
170 177 if (flags & SIGN_POSN_USED)
171 178 goto format_error;
172 179 flags |= (SIGN_POSN_USED|LOCALE_POSN);
173 180 continue;
174 181 case '(': /* enclose negatives with () */
175 182 if (flags & SIGN_POSN_USED)
176 183 goto format_error;
177 184 flags |= (SIGN_POSN_USED|PARENTH_POSN);
178 185 continue;
179 186 case '!': /* suppress currency symbol */
180 187 flags |= SUPRESS_CURR_SYMBOL;
181 188 continue;
182 189 case '-': /* alignment (left) */
183 190 flags |= LEFT_JUSTIFY;
184 191 continue;
185 192 default:
186 193 break;
187 194 }
188 195 break;
189 196 }
190 197
191 198 /* field Width */
192 199 if (isdigit((unsigned char)*fmt)) {
193 200 GET_NUMBER(width);
194 201 /*
195 202 * Do we have enough space to put number with
196 203 * required width ?
197 204 */
198 205 if ((unsigned int)width >= maxsize - (dst - s))
199 206 goto e2big_error;
200 207 }
201 208
202 209 /* Left precision */
203 210 if (*fmt == '#') {
204 211 if (!isdigit((unsigned char)*++fmt))
205 212 goto format_error;
206 213 GET_NUMBER(left_prec);
207 214 if ((unsigned int)left_prec >= maxsize - (dst - s))
208 215 goto e2big_error;
209 216 }
210 217
211 218 /* Right precision */
212 219 if (*fmt == '.') {
213 220 if (!isdigit((unsigned char)*++fmt))
214 221 goto format_error;
215 222 GET_NUMBER(right_prec);
216 223 if ((unsigned int)right_prec >= maxsize - (dst - s) -
217 224 left_prec)
218 225 goto e2big_error;
219 226 }
220 227
221 228 /* Conversion Characters */
222 229 switch (*fmt++) {
223 230 case 'i': /* use internaltion currency format */
↓ open down ↓ |
85 lines elided |
↑ open up ↑ |
224 231 flags |= USE_INTL_CURRENCY;
225 232 break;
226 233 case 'n': /* use national currency format */
227 234 flags &= ~(USE_INTL_CURRENCY);
228 235 break;
229 236 default:
230 237 /* required char missing or premature EOS */
231 238 goto format_error;
232 239 }
233 240
234 - if (currency_symbol != NULL)
235 - free(currency_symbol);
236 241 if (flags & USE_INTL_CURRENCY) {
237 - currency_symbol = strdup(lc->int_curr_symbol);
242 + currency_symbol = lmon->int_curr_symbol;
243 + /* by definition three letters followed by a space */
238 244 if (currency_symbol != NULL)
239 - space_char = *(currency_symbol+3);
245 + space_char = currency_symbol[3];
240 246 } else
241 - currency_symbol = strdup(lc->currency_symbol);
247 + currency_symbol = lmon->currency_symbol;
242 248
243 - if (currency_symbol == NULL)
244 - goto end_error; /* ENOMEM. */
245 -
246 249 /* value itself */
247 250 value = va_arg(ap, double);
248 251
249 252 /* detect sign */
250 253 if (value < 0) {
251 254 flags |= IS_NEGATIVE;
252 255 value = -value;
253 256 }
254 257
255 258 /* fill left_prec with amount of padding chars */
256 259 if (left_prec >= 0) {
257 - pad_size = __calc_left_pad((flags ^ IS_NEGATIVE),
260 + pad_size = calc_left_pad(lmon, (flags ^ IS_NEGATIVE),
258 261 currency_symbol) -
259 - __calc_left_pad(flags, currency_symbol);
262 + calc_left_pad(lmon, flags, currency_symbol);
260 263 if (pad_size < 0)
261 264 pad_size = 0;
262 265 }
263 266
264 267 if (asciivalue != NULL)
265 268 free(asciivalue);
266 - asciivalue = __format_grouped_double(value, &flags,
269 + asciivalue = format_grouped_double(lmon, lnum, value, &flags,
267 270 left_prec, right_prec, pad_char);
268 271 if (asciivalue == NULL)
269 272 goto end_error; /* errno already set */
270 273 /* to ENOMEM by malloc() */
271 274
272 275 /* set some variables for later use */
273 - __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn,
274 - &signstr);
276 + setup_vars(lmon, flags, &cs_precedes, &sep_by_space,
277 + &sign_posn, &signstr);
275 278
276 279 /*
277 280 * Description of some LC_MONETARY's values:
278 281 *
279 282 * p_cs_precedes & n_cs_precedes
280 283 *
281 284 * = 1 - $currency_symbol precedes the value
282 285 * for a monetary quantity with a non-negative value
283 286 * = 0 - symbol succeeds the value
284 287 *
285 288 * p_sep_by_space & n_sep_by_space
286 289 *
287 290 * = 0 - no space separates $currency_symbol
288 291 * from the value for a monetary quantity with a
289 292 * non-negative value
290 293 * = 1 - space separates the symbol from the value
291 294 * = 2 - space separates the symbol and the sign string,
292 295 * if adjacent.
293 296 *
294 297 * p_sign_posn & n_sign_posn
295 298 *
296 299 * = 0 - parentheses enclose the quantity and the
297 300 * $currency_symbol
298 301 * = 1 - the sign string precedes the quantity and the
299 302 * $currency_symbol
300 303 * = 2 - the sign string succeeds the quantity and the
301 304 * $currency_symbol
302 305 * = 3 - the sign string precedes the $currency_symbol
303 306 * = 4 - the sign string succeeds the $currency_symbol
304 307 *
305 308 */
306 309
307 310 tmpptr = dst;
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
308 311
309 312 while (pad_size-- > 0)
310 313 PRINT(' ');
311 314
312 315 if (sign_posn == 0 && (flags & IS_NEGATIVE))
313 316 PRINT('(');
314 317
315 318 if (cs_precedes == 1) {
316 319 if (sign_posn == 1 || sign_posn == 3) {
317 320 PRINTS(signstr);
318 - if (sep_by_space == 2) /* XXX: ? */
321 + if (sep_by_space == 2)
319 322 PRINT(' ');
320 323 }
321 324
322 325 if (!(flags & SUPRESS_CURR_SYMBOL)) {
323 326 PRINTS(currency_symbol);
324 327
325 328 if (sign_posn == 4) {
326 329 if (sep_by_space == 2)
327 330 PRINT(space_char);
328 331 PRINTS(signstr);
329 332 if (sep_by_space == 1)
330 333 PRINT(' ');
331 334 } else if (sep_by_space == 1)
332 335 PRINT(space_char);
333 336 }
334 337 } else if (sign_posn == 1)
335 338 PRINTS(signstr);
336 339
337 340 PRINTS(asciivalue);
338 341
339 342 if (cs_precedes == 0) {
340 343 if (sign_posn == 3) {
341 344 if (sep_by_space == 1)
342 345 PRINT(' ');
343 346 PRINTS(signstr);
344 347 }
345 348
346 349 if (!(flags & SUPRESS_CURR_SYMBOL)) {
347 350 if ((sign_posn == 3 && sep_by_space == 2) ||
348 351 (sep_by_space == 1 && (sign_posn == 0 ||
349 352 sign_posn == 1 || sign_posn == 2 ||
350 353 sign_posn == 4)))
351 354 PRINT(space_char);
352 355 PRINTS(currency_symbol); /* XXX: len */
353 356 if (sign_posn == 4) {
354 357 if (sep_by_space == 2)
355 358 PRINT(' ');
356 359 PRINTS(signstr);
357 360 }
358 361 }
359 362 }
360 363
361 364 if (sign_posn == 2) {
362 365 if (sep_by_space == 2)
363 366 PRINT(' ');
364 367 PRINTS(signstr);
365 368 }
366 369
367 370 if (sign_posn == 0 && (flags & IS_NEGATIVE))
368 371 PRINT(')');
369 372
370 373 if (dst - tmpptr < width) {
371 374 if (flags & LEFT_JUSTIFY) {
372 375 while (dst - tmpptr < width)
373 376 PRINT(' ');
374 377 } else {
↓ open down ↓ |
46 lines elided |
↑ open up ↑ |
375 378 pad_size = dst-tmpptr;
376 379 (void) memmove(tmpptr + width-pad_size, tmpptr,
377 380 pad_size);
378 381 (void) memset(tmpptr, ' ', width-pad_size);
379 382 dst += width-pad_size;
380 383 }
381 384 }
382 385 }
383 386
384 387 PRINT('\0');
385 - va_end(ap);
386 388 free(asciivalue);
387 - free(currency_symbol);
388 389 return (dst - s - 1); /* size of put data except trailing '\0' */
389 390
390 391 e2big_error:
391 392 errno = E2BIG;
392 393 goto end_error;
393 394
394 395 format_error:
395 396 errno = EINVAL;
396 397
397 398 end_error:
398 399 sverrno = errno;
399 400 if (asciivalue != NULL)
400 401 free(asciivalue);
401 - if (currency_symbol != NULL)
402 - free(currency_symbol);
403 402 errno = sverrno;
404 - va_end(ap);
405 403 return (-1);
406 404 }
407 405
408 -static void
409 -__setup_vars(int flags, char *cs_precedes, char *sep_by_space,
410 - char *sign_posn, char **signstr)
406 +ssize_t
407 +strfmon(char *_RESTRICT_KYWD s, size_t maxsize,
408 + const char *_RESTRICT_KYWD format, ...)
411 409 {
410 + va_list ap;
411 + ssize_t ret;
412 412
413 - struct lconv *lc = localeconv();
413 + va_start(ap, format);
414 + ret = strfmon_impl(s, maxsize, uselocale(NULL), format, ap);
415 + va_end(ap);
416 + return (ret);
417 +}
414 418
419 +ssize_t
420 +strfmon_l(char *_RESTRICT_KYWD s, size_t maxsize, locale_t loc,
421 + const char *_RESTRICT_KYWD format, ...)
422 +{
423 + ssize_t ret;
424 + va_list ap;
425 + va_start(ap, format);
426 + ret = strfmon_impl(s, maxsize, loc, format, ap);
427 + va_end(ap);
428 + return (ret);
429 +}
430 +
431 +static void
432 +setup_vars(const struct lc_monetary *lmon, int flags, char *cs_precedes,
433 + char *sep_by_space, char *sign_posn, const char **signstr)
434 +{
415 435 if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
416 - *cs_precedes = lc->int_n_cs_precedes;
417 - *sep_by_space = lc->int_n_sep_by_space;
418 - *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
419 - *signstr = (lc->negative_sign[0] == '\0') ? "-"
420 - : lc->negative_sign;
436 + *cs_precedes = lmon->int_n_cs_precedes[0];
437 + *sep_by_space = lmon->int_n_sep_by_space[0];
438 + *sign_posn = (flags & PARENTH_POSN) ? 0 :
439 + lmon->int_n_sign_posn[0];
440 + *signstr = (lmon->negative_sign[0] == '\0') ? "-" :
441 + lmon->negative_sign;
421 442 } else if (flags & USE_INTL_CURRENCY) {
422 - *cs_precedes = lc->int_p_cs_precedes;
423 - *sep_by_space = lc->int_p_sep_by_space;
424 - *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn;
425 - *signstr = lc->positive_sign;
443 + *cs_precedes = lmon->int_p_cs_precedes[0];
444 + *sep_by_space = lmon->int_p_sep_by_space[0];
445 + *sign_posn = (flags & PARENTH_POSN) ? 0 :
446 + lmon->int_p_sign_posn[0];
447 + *signstr = lmon->positive_sign;
426 448 } else if (flags & IS_NEGATIVE) {
427 - *cs_precedes = lc->n_cs_precedes;
428 - *sep_by_space = lc->n_sep_by_space;
429 - *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
430 - *signstr = (lc->negative_sign[0] == '\0') ? "-"
431 - : lc->negative_sign;
449 + *cs_precedes = lmon->n_cs_precedes[0];
450 + *sep_by_space = lmon->n_sep_by_space[0];
451 + *sign_posn = (flags & PARENTH_POSN) ? 0 : lmon->n_sign_posn[0];
452 + *signstr = (lmon->negative_sign[0] == '\0') ? "-" :
453 + lmon->negative_sign;
432 454 } else {
433 - *cs_precedes = lc->p_cs_precedes;
434 - *sep_by_space = lc->p_sep_by_space;
435 - *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
436 - *signstr = lc->positive_sign;
455 + *cs_precedes = lmon->p_cs_precedes[0];
456 + *sep_by_space = lmon->p_sep_by_space[0];
457 + *sign_posn = (flags & PARENTH_POSN) ? 0 : lmon->p_sign_posn[0];
458 + *signstr = lmon->positive_sign;
437 459 }
438 460
439 - /* Set defult values for unspecified information. */
461 + /* Set default values for unspecified information. */
440 462 if (*cs_precedes != 0)
441 463 *cs_precedes = 1;
442 464 if (*sep_by_space == CHAR_MAX)
443 465 *sep_by_space = 0;
444 466 if (*sign_posn == CHAR_MAX)
445 467 *sign_posn = 0;
446 468 }
447 469
448 470 static int
449 -__calc_left_pad(int flags, char *cur_symb)
471 +calc_left_pad(const struct lc_monetary *lmon, int flags, const char *cur_symb)
450 472 {
451 -
452 - char cs_precedes, sep_by_space, sign_posn, *signstr;
473 + char cs_precedes, sep_by_space, sign_posn;
474 + const char *signstr;
453 475 int left_chars = 0;
454 476
455 - __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr);
477 + setup_vars(lmon, flags, &cs_precedes, &sep_by_space, &sign_posn,
478 + &signstr);
456 479
457 480 if (cs_precedes != 0) {
458 481 left_chars += strlen(cur_symb);
459 482 if (sep_by_space != 0)
460 483 left_chars++;
461 484 }
462 485
463 486 switch (sign_posn) {
464 487 case 1:
465 488 left_chars += strlen(signstr);
466 489 break;
467 490 case 3:
468 491 case 4:
469 492 if (cs_precedes != 0)
470 493 left_chars += strlen(signstr);
471 494 }
472 495 return (left_chars);
473 496 }
474 497
475 498 static int
476 -get_groups(int size, char *grouping)
499 +get_groups(int size, const char *grouping)
477 500 {
478 501
479 502 int chars = 0;
480 503
481 504 if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */
482 505 return (0);
483 506
484 507 while (size > (int)*grouping) {
485 508 chars++;
486 509 size -= (int)*grouping++;
487 510 /* no more grouping ? */
488 511 if (*grouping == CHAR_MAX)
489 512 break;
490 513 /* rest grouping with same value ? */
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
491 514 if (*grouping == 0) {
492 515 chars += (size - 1) / *(grouping - 1);
493 516 break;
494 517 }
495 518 }
496 519 return (chars);
497 520 }
498 521
499 522 /* convert double to ASCII */
500 523 static char *
501 -__format_grouped_double(double value, int *flags,
502 - int left_prec, int right_prec, int pad_char)
524 +format_grouped_double(const struct lc_monetary *lmon,
525 + const struct lc_numeric *lnum,
526 + double value, int *flags, int left_prec, int right_prec, int pad_char)
503 527 {
504 528
505 529 char *rslt;
506 530 char *avalue;
507 531 int avalue_size;
508 532 char fmt[32];
509 533
510 534 size_t bufsize;
511 535 char *bufend;
512 536
513 537 int padded;
514 538
515 - struct lconv *lc = localeconv();
516 - char *grouping;
517 - char decimal_point;
518 - char thousands_sep;
539 + const char *grouping;
540 + const char *decimal_point;
541 + const char *thousands_sep;
542 + int decimal_len;
543 + int thousands_len;
519 544
520 545 int groups = 0;
521 546
522 - grouping = lc->mon_grouping;
523 - decimal_point = *lc->mon_decimal_point;
524 - if (decimal_point == '\0')
525 - decimal_point = *lc->decimal_point;
526 - thousands_sep = *lc->mon_thousands_sep;
527 - if (thousands_sep == '\0')
528 - thousands_sep = *lc->thousands_sep;
547 + grouping = lmon->mon_grouping;
548 + decimal_point = lmon->mon_decimal_point;
549 + if (*decimal_point == '\0')
550 + decimal_point = lnum->decimal_point;
551 + thousands_sep = lmon->mon_thousands_sep;
552 + if (*thousands_sep == '\0')
553 + thousands_sep = lnum->thousands_sep;
529 554
555 + decimal_len = strlen(decimal_point); /* usually 1 */
556 + thousands_len = strlen(thousands_sep); /* 0 or 1 usually */
557 +
530 558 /* fill left_prec with default value */
531 559 if (left_prec == -1)
532 560 left_prec = 0;
533 561
534 562 /* fill right_prec with default value */
535 563 if (right_prec == -1) {
536 564 if (*flags & USE_INTL_CURRENCY)
537 - right_prec = lc->int_frac_digits;
565 + right_prec = lmon->int_frac_digits[0];
538 566 else
539 - right_prec = lc->frac_digits;
567 + right_prec = lmon->frac_digits[0];
540 568
541 569 if (right_prec == CHAR_MAX) /* POSIX locale ? */
542 570 right_prec = 2;
543 571 }
544 572
545 573 if (*flags & NEED_GROUPING)
546 574 left_prec += get_groups(left_prec, grouping);
547 575
548 576 /* convert to string */
549 577 (void) snprintf(fmt, sizeof (fmt), "%%%d.%df",
550 578 left_prec + right_prec + 1, right_prec);
551 579 avalue_size = asprintf(&avalue, fmt, value);
552 580 if (avalue_size < 0)
553 581 return (NULL);
554 582
555 - /* make sure that we've enough space for result string */
583 + /*
584 + * Make sure that we've enough space for result string.
585 + * This assumes that digits take up at least much space as
586 + * grouping and radix characters. The worst case currently known
587 + * is for Arabic, where two-byte UTF-8 sequences are used for both
588 + * decimal and thousands seperators, and groups can be a small as two
589 + * decimal digits. This will do no worse than doubling the storage
590 + * requirement.
591 + */
556 592 bufsize = strlen(avalue)*2+1;
557 593 rslt = calloc(1, bufsize);
558 594 if (rslt == NULL) {
559 595 free(avalue);
560 596 return (NULL);
561 597 }
562 598 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */
563 599
564 - /* skip spaces at beggining */
600 + /* skip spaces at beginning */
565 601 padded = 0;
566 602 while (avalue[padded] == ' ') {
567 603 padded++;
568 604 avalue_size--;
569 605 }
570 606
571 607 if (right_prec > 0) {
572 608 bufend -= right_prec;
573 609 (void) memcpy(bufend, avalue + avalue_size+padded-right_prec,
574 610 right_prec);
575 - *--bufend = decimal_point;
576 - avalue_size -= (right_prec + 1);
611 + bufend -= decimal_len;
612 + (void) memcpy(bufend, decimal_point, decimal_len);
613 + avalue_size -= (right_prec + decimal_len);
577 614 }
578 615
579 616 if ((*flags & NEED_GROUPING) &&
580 - thousands_sep != '\0' && /* XXX: need investigation */
617 + thousands_len != 0 &&
581 618 *grouping != CHAR_MAX &&
582 619 *grouping > 0) {
583 620 while (avalue_size > (int)*grouping) {
584 621 GRPCPY(*grouping);
585 622 GRPSEP;
586 623 grouping++;
587 624
588 625 /* no more grouping ? */
589 626 if (*grouping == CHAR_MAX)
590 627 break;
591 628
592 629 /* rest grouping with same value ? */
593 630 if (*grouping == 0) {
594 631 grouping--;
595 632 while (avalue_size > *grouping) {
596 633 GRPCPY(*grouping);
597 634 GRPSEP;
598 635 }
599 636 }
600 637 }
601 638 if (avalue_size != 0)
602 639 GRPCPY(avalue_size);
603 640 padded -= groups;
604 641
605 642 } else {
606 643 bufend -= avalue_size;
607 644 (void) memcpy(bufend, avalue+padded, avalue_size);
608 645 if (right_prec == 0)
609 646 padded--; /* decrease assumed $decimal_point */
610 647 }
611 648
612 649 /* do padding with pad_char */
613 650 if (padded > 0) {
614 651 bufend -= padded;
615 652 (void) memset(bufend, pad_char, padded);
616 653 }
617 654
618 655 bufsize = bufsize - (bufend - rslt) + 1;
619 656 (void) memmove(rslt, bufend, bufsize);
620 657 free(avalue);
621 658 return (rslt);
622 659 }
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX