1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1998-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
13 * Modification History:
15 * Date Name Description
16 * 11/23/98 stephen Creation.
17 * 03/12/99 stephen Modified for new C API.
18 * 08/07/2003 george Reunify printf implementations
19 ******************************************************************************
22 #include "unicode/utypes.h"
24 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
26 #include "unicode/ustring.h"
27 #include "unicode/utf16.h"
33 /* ANSI style formatting */
34 /* Use US-ASCII characters only for formatting */
37 #define UFMT_SIMPLE_PERCENT {ufmt_simple_percent, u_printf_simple_percent_handler}
39 #define UFMT_STRING {ufmt_string, u_printf_string_handler}
41 #define UFMT_CHAR {ufmt_char, u_printf_char_handler}
43 #define UFMT_INT {ufmt_int, u_printf_integer_handler}
45 #define UFMT_UINT {ufmt_int, u_printf_uinteger_handler}
47 #define UFMT_OCTAL {ufmt_int, u_printf_octal_handler}
49 #define UFMT_HEX {ufmt_int, u_printf_hex_handler}
51 #define UFMT_DOUBLE {ufmt_double, u_printf_double_handler}
53 #define UFMT_SCIENTIFIC {ufmt_double, u_printf_scientific_handler}
55 #define UFMT_SCIDBL {ufmt_double, u_printf_scidbl_handler}
57 #define UFMT_COUNT {ufmt_count, u_printf_count_handler}
59 /* non-ANSI extensions */
60 /* Use US-ASCII characters only for formatting */
63 #define UFMT_POINTER {ufmt_pointer, u_printf_pointer_handler}
65 #define UFMT_SPELLOUT {ufmt_double, u_printf_spellout_handler}
67 #define UFMT_PERCENT {ufmt_double, u_printf_percent_handler}
68 /* C K is old format */
69 #define UFMT_UCHAR {ufmt_uchar, u_printf_uchar_handler}
70 /* S U is old format */
71 #define UFMT_USTRING {ufmt_ustring, u_printf_ustring_handler}
74 #define UFMT_EMPTY {ufmt_empty, NULL}
77 * A u_printf handler function.
78 * A u_printf handler is responsible for handling a single u_printf
79 * format specification, for example 'd' or 's'.
80 * @param stream The UFILE to which to write output.
81 * @param info A pointer to a <TT>u_printf_spec_info</TT> struct containing
82 * information on the format specification.
83 * @param args A pointer to the argument data
84 * @return The number of Unicode characters written to <TT>stream</TT>.
86 typedef int32_t U_EXPORT2
87 u_printf_handler(const u_printf_stream_handler *handler,
90 ULocaleBundle *formatBundle,
91 const u_printf_spec_info *info,
92 const ufmt_args *args);
94 typedef struct u_printf_info {
96 u_printf_handler *handler;
100 * Struct encapsulating a single uprintf format specification.
102 typedef struct u_printf_spec {
103 u_printf_spec_info fInfo; /* Information on this spec */
104 int32_t fWidthPos; /* Position of width in arg list */
105 int32_t fPrecisionPos; /* Position of precision in arg list */
106 int32_t fArgPos; /* Position of data in arg list */
109 #define UPRINTF_NUM_FMT_HANDLERS 108
111 /* We do not use handlers for 0-0x1f */
112 #define UPRINTF_BASE_FMT_HANDLERS 0x20
114 /* buffer size for formatting */
115 #define UPRINTF_BUFFER_SIZE 1024
116 #define UPRINTF_SYMBOL_BUFFER_SIZE 8
118 static const UChar gNullStr[] = {0x28, 0x6E, 0x75, 0x6C, 0x6C, 0x29, 0}; /* "(null)" */
119 static const UChar gSpaceStr[] = {0x20, 0}; /* " " */
121 /* Sets the sign of a format based on u_printf_spec_info */
122 /* TODO: Is setting the prefix symbol to a positive sign a good idea in all locales? */
124 u_printf_set_sign(UNumberFormat *format,
125 const u_printf_spec_info *info,
127 int32_t *prefixBufLen,
130 if(info->fShowSign) {
131 *prefixBufLen = unum_getTextAttribute(format,
132 UNUM_POSITIVE_PREFIX,
137 /* Setting UNUM_PLUS_SIGN_SYMBOL affects the exponent too. */
138 /* unum_setSymbol(format, UNUM_PLUS_SIGN_SYMBOL, gSpaceStr, 1, &status); */
139 unum_setTextAttribute(format, UNUM_POSITIVE_PREFIX, gSpaceStr, 1, status);
142 UChar plusSymbol[UPRINTF_SYMBOL_BUFFER_SIZE];
145 symbolLen = unum_getSymbol(format,
146 UNUM_PLUS_SIGN_SYMBOL,
148 UPRV_LENGTHOF(plusSymbol),
150 unum_setTextAttribute(format,
151 UNUM_POSITIVE_PREFIX,
163 u_printf_reset_sign(UNumberFormat *format,
164 const u_printf_spec_info *info,
166 int32_t *prefixBufLen,
169 if(info->fShowSign) {
170 unum_setTextAttribute(format,
171 UNUM_POSITIVE_PREFIX,
181 u_printf_simple_percent_handler(const u_printf_stream_handler *handler,
183 ULocaleBundle *formatBundle,
184 const u_printf_spec_info *info,
185 const ufmt_args *args)
187 static const UChar PERCENT[] = { UP_PERCENT };
189 /* put a single '%' onto the output */
190 return handler->write(context, PERCENT, 1);
195 u_printf_string_handler(const u_printf_stream_handler *handler,
197 ULocaleBundle *formatBundle,
198 const u_printf_spec_info *info,
199 const ufmt_args *args)
202 UChar buffer[UFMT_DEFAULT_BUFFER_SIZE];
203 int32_t len, written;
205 const char *arg = (const char*)(args[0].ptrValue);
207 /* convert from the default codepage to Unicode */
209 argSize = (int32_t)strlen(arg) + 1;
210 if (argSize >= MAX_UCHAR_BUFFER_SIZE(buffer)) {
211 s = ufmt_defaultCPToUnicode(arg, argSize,
212 (UChar *)uprv_malloc(MAX_UCHAR_BUFFER_NEEDED(argSize)),
213 MAX_UCHAR_BUFFER_NEEDED(argSize));
219 s = ufmt_defaultCPToUnicode(arg, argSize, buffer,
220 UPRV_LENGTHOF(buffer));
224 s = (UChar *)gNullStr;
228 /* width = minimum # of characters to write */
229 /* precision = maximum # of characters to write */
230 if (info->fPrecision != -1 && info->fPrecision < len) {
231 len = info->fPrecision;
234 written = handler->pad_and_justify(context, info, s, len);
237 if (gNullStr != s && buffer != s) {
245 u_printf_char_handler(const u_printf_stream_handler *handler,
247 ULocaleBundle *formatBundle,
248 const u_printf_spec_info *info,
249 const ufmt_args *args)
251 UChar s[U16_MAX_LENGTH+1];
252 int32_t len = 1, written;
253 unsigned char arg = (unsigned char)(args[0].int64Value);
255 /* convert from default codepage to Unicode */
256 ufmt_defaultCPToUnicode((const char *)&arg, 2, s, UPRV_LENGTHOF(s));
258 /* Remember that this may be an MBCS character */
263 /* width = minimum # of characters to write */
264 /* precision = maximum # of characters to write */
265 /* precision is ignored when handling a char */
267 written = handler->pad_and_justify(context, info, s, len);
273 u_printf_double_handler(const u_printf_stream_handler *handler,
275 ULocaleBundle *formatBundle,
276 const u_printf_spec_info *info,
277 const ufmt_args *args)
279 double num = (double) (args[0].doubleValue);
280 UNumberFormat *format;
281 UChar result[UPRINTF_BUFFER_SIZE];
282 UChar prefixBuffer[UPRINTF_BUFFER_SIZE];
283 int32_t prefixBufferLen = sizeof(prefixBuffer);
284 int32_t minDecimalDigits;
285 int32_t maxDecimalDigits;
287 UErrorCode status = U_ZERO_ERROR;
291 /* mask off any necessary bits */
292 /* if(! info->fIsLongDouble)
295 /* get the formatter */
296 format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL);
302 /* save the formatter's state */
303 minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS);
304 maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS);
306 /* set the appropriate flags and number of decimal digits on the formatter */
307 if(info->fPrecision != -1) {
308 /* set the # of decimal digits */
309 unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision);
311 else if(info->fAlt) {
312 /* '#' means always show decimal point */
313 /* copy of printf behavior on Solaris - '#' shows 6 digits */
314 unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
317 /* # of decimal digits is 6 if precision not specified regardless of locale */
318 unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
321 /* set whether to show the sign */
322 if (info->fShowSign) {
323 u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
326 /* format the number */
327 resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status);
329 if (U_FAILURE(status)) {
333 /* restore the number format */
334 /* TODO: Is this needed? */
335 unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits);
336 unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits);
338 if (info->fShowSign) {
339 /* Reset back to original value regardless of what the error was */
340 UErrorCode localStatus = U_ZERO_ERROR;
341 u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
344 return handler->pad_and_justify(context, info, result, resultLen);
349 u_printf_integer_handler(const u_printf_stream_handler *handler,
351 ULocaleBundle *formatBundle,
352 const u_printf_spec_info *info,
353 const ufmt_args *args)
355 int64_t num = args[0].int64Value;
356 UNumberFormat *format;
357 UChar result[UPRINTF_BUFFER_SIZE];
358 UChar prefixBuffer[UPRINTF_BUFFER_SIZE];
359 int32_t prefixBufferLen = sizeof(prefixBuffer);
360 int32_t minDigits = -1;
362 UErrorCode status = U_ZERO_ERROR;
366 /* mask off any necessary bits */
369 else if (!info->fIsLongLong)
372 /* get the formatter */
373 format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL);
379 /* set the appropriate flags on the formatter */
381 /* set the minimum integer digits */
382 if(info->fPrecision != -1) {
383 /* set the minimum # of digits */
384 minDigits = unum_getAttribute(format, UNUM_MIN_INTEGER_DIGITS);
385 unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, info->fPrecision);
388 /* set whether to show the sign */
389 if(info->fShowSign) {
390 u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
393 /* format the number */
394 resultLen = unum_formatInt64(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status);
396 if (U_FAILURE(status)) {
400 /* restore the number format */
401 if (minDigits != -1) {
402 unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, minDigits);
405 if (info->fShowSign) {
406 /* Reset back to original value regardless of what the error was */
407 UErrorCode localStatus = U_ZERO_ERROR;
408 u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
411 return handler->pad_and_justify(context, info, result, resultLen);
415 u_printf_hex_handler(const u_printf_stream_handler *handler,
417 ULocaleBundle *formatBundle,
418 const u_printf_spec_info *info,
419 const ufmt_args *args)
421 int64_t num = args[0].int64Value;
422 UChar result[UPRINTF_BUFFER_SIZE];
423 int32_t len = UPRINTF_BUFFER_SIZE;
426 /* mask off any necessary bits */
429 else if (!info->fIsLongLong)
432 /* format the number, preserving the minimum # of digits */
433 ufmt_64tou(result, &len, num, 16,
434 (UBool)(info->fSpec == 0x0078),
435 (info->fPrecision == -1 && info->fZero) ? info->fWidth : info->fPrecision);
437 /* convert to alt form, if desired */
438 if(num != 0 && info->fAlt && len < UPRINTF_BUFFER_SIZE - 2) {
439 /* shift the formatted string right by 2 chars */
440 memmove(result + 2, result, len * sizeof(UChar));
442 result[1] = info->fSpec;
446 return handler->pad_and_justify(context, info, result, len);
450 u_printf_octal_handler(const u_printf_stream_handler *handler,
452 ULocaleBundle *formatBundle,
453 const u_printf_spec_info *info,
454 const ufmt_args *args)
456 int64_t num = args[0].int64Value;
457 UChar result[UPRINTF_BUFFER_SIZE];
458 int32_t len = UPRINTF_BUFFER_SIZE;
461 /* mask off any necessary bits */
464 else if (!info->fIsLongLong)
467 /* format the number, preserving the minimum # of digits */
468 ufmt_64tou(result, &len, num, 8,
469 FALSE, /* doesn't matter for octal */
470 info->fPrecision == -1 && info->fZero ? info->fWidth : info->fPrecision);
472 /* convert to alt form, if desired */
473 if(info->fAlt && result[0] != 0x0030 && len < UPRINTF_BUFFER_SIZE - 1) {
474 /* shift the formatted string right by 1 char */
475 memmove(result + 1, result, len * sizeof(UChar));
480 return handler->pad_and_justify(context, info, result, len);
484 u_printf_uinteger_handler(const u_printf_stream_handler *handler,
486 ULocaleBundle *formatBundle,
487 const u_printf_spec_info *info,
488 const ufmt_args *args)
490 int64_t num = args[0].int64Value;
491 UNumberFormat *format;
492 UChar result[UPRINTF_BUFFER_SIZE];
493 int32_t minDigits = -1;
495 UErrorCode status = U_ZERO_ERROR;
497 /* TODO: Fix this once uint64_t can be formatted. */
500 else if (!info->fIsLongLong)
503 /* get the formatter */
504 format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL);
510 /* set the appropriate flags on the formatter */
512 /* set the minimum integer digits */
513 if(info->fPrecision != -1) {
514 /* set the minimum # of digits */
515 minDigits = unum_getAttribute(format, UNUM_MIN_INTEGER_DIGITS);
516 unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, info->fPrecision);
519 /* To mirror other stdio implementations, we ignore the sign argument */
521 /* format the number */
522 resultLen = unum_formatInt64(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status);
524 if (U_FAILURE(status)) {
528 /* restore the number format */
529 if (minDigits != -1) {
530 unum_setAttribute(format, UNUM_MIN_INTEGER_DIGITS, minDigits);
533 return handler->pad_and_justify(context, info, result, resultLen);
537 u_printf_pointer_handler(const u_printf_stream_handler *handler,
539 ULocaleBundle *formatBundle,
540 const u_printf_spec_info *info,
541 const ufmt_args *args)
543 UChar result[UPRINTF_BUFFER_SIZE];
544 int32_t len = UPRINTF_BUFFER_SIZE;
546 /* format the pointer in hex */
547 ufmt_ptou(result, &len, args[0].ptrValue, TRUE/*, info->fPrecision*/);
549 return handler->pad_and_justify(context, info, result, len);
553 u_printf_scientific_handler(const u_printf_stream_handler *handler,
555 ULocaleBundle *formatBundle,
556 const u_printf_spec_info *info,
557 const ufmt_args *args)
559 double num = (double) (args[0].doubleValue);
560 UNumberFormat *format;
561 UChar result[UPRINTF_BUFFER_SIZE];
562 UChar prefixBuffer[UPRINTF_BUFFER_SIZE];
563 int32_t prefixBufferLen = sizeof(prefixBuffer);
564 int32_t minDecimalDigits;
565 int32_t maxDecimalDigits;
566 UErrorCode status = U_ZERO_ERROR;
567 UChar srcExpBuf[UPRINTF_SYMBOL_BUFFER_SIZE];
568 int32_t srcLen, expLen;
570 UChar expBuf[UPRINTF_SYMBOL_BUFFER_SIZE];
574 /* mask off any necessary bits */
575 /* if(! info->fIsLongDouble)
578 /* get the formatter */
579 format = u_locbund_getNumberFormat(formatBundle, UNUM_SCIENTIFIC);
585 /* set the appropriate flags on the formatter */
587 srcLen = unum_getSymbol(format,
588 UNUM_EXPONENTIAL_SYMBOL,
593 /* Upper/lower case the e */
594 if (info->fSpec == (UChar)0x65 /* e */) {
595 expLen = u_strToLower(expBuf, (int32_t)sizeof(expBuf),
597 formatBundle->fLocale,
601 expLen = u_strToUpper(expBuf, (int32_t)sizeof(expBuf),
603 formatBundle->fLocale,
607 unum_setSymbol(format,
608 UNUM_EXPONENTIAL_SYMBOL,
613 /* save the formatter's state */
614 minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS);
615 maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS);
617 /* set the appropriate flags and number of decimal digits on the formatter */
618 if(info->fPrecision != -1) {
619 /* set the # of decimal digits */
620 if (info->fOrigSpec == (UChar)0x65 /* e */ || info->fOrigSpec == (UChar)0x45 /* E */) {
621 unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision);
624 unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, 1);
625 unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, info->fPrecision);
628 else if(info->fAlt) {
629 /* '#' means always show decimal point */
630 /* copy of printf behavior on Solaris - '#' shows 6 digits */
631 unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
634 /* # of decimal digits is 6 if precision not specified */
635 unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
638 /* set whether to show the sign */
639 if (info->fShowSign) {
640 u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
643 /* format the number */
644 resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status);
646 if (U_FAILURE(status)) {
650 /* restore the number format */
651 /* TODO: Is this needed? */
652 unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits);
653 unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits);
655 /* Since we're the only one using the scientific
656 format, we don't need to save the old exponent value. */
657 /*unum_setSymbol(format,
658 UNUM_EXPONENTIAL_SYMBOL,
663 if (info->fShowSign) {
664 /* Reset back to original value regardless of what the error was */
665 UErrorCode localStatus = U_ZERO_ERROR;
666 u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
669 return handler->pad_and_justify(context, info, result, resultLen);
673 u_printf_percent_handler(const u_printf_stream_handler *handler,
675 ULocaleBundle *formatBundle,
676 const u_printf_spec_info *info,
677 const ufmt_args *args)
679 double num = (double) (args[0].doubleValue);
680 UNumberFormat *format;
681 UChar result[UPRINTF_BUFFER_SIZE];
682 UChar prefixBuffer[UPRINTF_BUFFER_SIZE];
683 int32_t prefixBufferLen = sizeof(prefixBuffer);
684 int32_t minDecimalDigits;
685 int32_t maxDecimalDigits;
687 UErrorCode status = U_ZERO_ERROR;
691 /* mask off any necessary bits */
692 /* if(! info->fIsLongDouble)
695 /* get the formatter */
696 format = u_locbund_getNumberFormat(formatBundle, UNUM_PERCENT);
702 /* save the formatter's state */
703 minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS);
704 maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS);
706 /* set the appropriate flags and number of decimal digits on the formatter */
707 if(info->fPrecision != -1) {
708 /* set the # of decimal digits */
709 unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision);
711 else if(info->fAlt) {
712 /* '#' means always show decimal point */
713 /* copy of printf behavior on Solaris - '#' shows 6 digits */
714 unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
717 /* # of decimal digits is 6 if precision not specified */
718 unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
721 /* set whether to show the sign */
722 if (info->fShowSign) {
723 u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
726 /* format the number */
727 resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status);
729 if (U_FAILURE(status)) {
733 /* restore the number format */
734 /* TODO: Is this needed? */
735 unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits);
736 unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits);
738 if (info->fShowSign) {
739 /* Reset back to original value regardless of what the error was */
740 UErrorCode localStatus = U_ZERO_ERROR;
741 u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
744 return handler->pad_and_justify(context, info, result, resultLen);
748 u_printf_ustring_handler(const u_printf_stream_handler *handler,
750 ULocaleBundle *formatBundle,
751 const u_printf_spec_info *info,
752 const ufmt_args *args)
754 int32_t len, written;
755 const UChar *arg = (const UChar*)(args[0].ptrValue);
757 /* allocate enough space for the buffer */
763 /* width = minimum # of characters to write */
764 /* precision = maximum # of characters to write */
765 if (info->fPrecision != -1 && info->fPrecision < len) {
766 len = info->fPrecision;
769 /* determine if the string should be padded */
770 written = handler->pad_and_justify(context, info, arg, len);
776 u_printf_uchar_handler(const u_printf_stream_handler *handler,
778 ULocaleBundle *formatBundle,
779 const u_printf_spec_info *info,
780 const ufmt_args *args)
783 UChar arg = (UChar)(args[0].int64Value);
785 /* width = minimum # of characters to write */
786 /* precision = maximum # of characters to write */
787 /* precision is ignored when handling a uchar */
789 /* determine if the string should be padded */
790 written = handler->pad_and_justify(context, info, &arg, 1);
796 u_printf_scidbl_handler(const u_printf_stream_handler *handler,
798 ULocaleBundle *formatBundle,
799 const u_printf_spec_info *info,
800 const ufmt_args *args)
802 u_printf_spec_info scidbl_info;
803 double num = args[0].doubleValue;
805 UNumberFormat *format;
806 int32_t maxSigDecimalDigits, significantDigits;
808 memcpy(&scidbl_info, info, sizeof(u_printf_spec_info));
810 /* determine whether to use 'd', 'e' or 'f' notation */
811 if (scidbl_info.fPrecision == -1 && num == uprv_trunc(num))
813 /* use 'f' notation */
814 scidbl_info.fSpec = 0x0066;
815 scidbl_info.fPrecision = 0;
816 /* call the double handler */
817 retVal = u_printf_double_handler(handler, context, formatBundle, &scidbl_info, args);
819 else if(num < 0.0001 || (scidbl_info.fPrecision < 1 && 1000000.0 <= num)
820 || (scidbl_info.fPrecision != -1 && num > uprv_pow10(scidbl_info.fPrecision)))
822 /* use 'e' or 'E' notation */
823 scidbl_info.fSpec = scidbl_info.fSpec - 2;
824 if (scidbl_info.fPrecision == -1) {
825 scidbl_info.fPrecision = 5;
827 /* call the scientific handler */
828 retVal = u_printf_scientific_handler(handler, context, formatBundle, &scidbl_info, args);
831 format = u_locbund_getNumberFormat(formatBundle, UNUM_DECIMAL);
832 /* Check for null pointer */
833 if (format == NULL) {
836 maxSigDecimalDigits = unum_getAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS);
837 significantDigits = scidbl_info.fPrecision;
839 /* use 'f' notation */
840 scidbl_info.fSpec = 0x0066;
841 if (significantDigits == -1) {
842 significantDigits = 6;
844 unum_setAttribute(format, UNUM_SIGNIFICANT_DIGITS_USED, TRUE);
845 unum_setAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS, significantDigits);
846 /* call the double handler */
847 retVal = u_printf_double_handler(handler, context, formatBundle, &scidbl_info, args);
848 unum_setAttribute(format, UNUM_MAX_SIGNIFICANT_DIGITS, maxSigDecimalDigits);
849 unum_setAttribute(format, UNUM_SIGNIFICANT_DIGITS_USED, FALSE);
855 u_printf_count_handler(const u_printf_stream_handler *handler,
857 ULocaleBundle *formatBundle,
858 const u_printf_spec_info *info,
859 const ufmt_args *args)
861 int32_t *count = (int32_t*)(args[0].ptrValue);
863 /* in the special case of count, the u_printf_spec_info's width */
864 /* will contain the # of chars written thus far */
865 *count = info->fWidth;
871 u_printf_spellout_handler(const u_printf_stream_handler *handler,
873 ULocaleBundle *formatBundle,
874 const u_printf_spec_info *info,
875 const ufmt_args *args)
877 double num = (double) (args[0].doubleValue);
878 UNumberFormat *format;
879 UChar result[UPRINTF_BUFFER_SIZE];
880 UChar prefixBuffer[UPRINTF_BUFFER_SIZE];
881 int32_t prefixBufferLen = sizeof(prefixBuffer);
882 int32_t minDecimalDigits;
883 int32_t maxDecimalDigits;
885 UErrorCode status = U_ZERO_ERROR;
889 /* mask off any necessary bits */
890 /* if(! info->fIsLongDouble)
893 /* get the formatter */
894 format = u_locbund_getNumberFormat(formatBundle, UNUM_SPELLOUT);
900 /* save the formatter's state */
901 minDecimalDigits = unum_getAttribute(format, UNUM_MIN_FRACTION_DIGITS);
902 maxDecimalDigits = unum_getAttribute(format, UNUM_MAX_FRACTION_DIGITS);
904 /* set the appropriate flags and number of decimal digits on the formatter */
905 if(info->fPrecision != -1) {
906 /* set the # of decimal digits */
907 unum_setAttribute(format, UNUM_FRACTION_DIGITS, info->fPrecision);
909 else if(info->fAlt) {
910 /* '#' means always show decimal point */
911 /* copy of printf behavior on Solaris - '#' shows 6 digits */
912 unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
915 /* # of decimal digits is 6 if precision not specified */
916 unum_setAttribute(format, UNUM_FRACTION_DIGITS, 6);
919 /* set whether to show the sign */
920 if (info->fShowSign) {
921 u_printf_set_sign(format, info, prefixBuffer, &prefixBufferLen, &status);
924 /* format the number */
925 resultLen = unum_formatDouble(format, num, result, UPRINTF_BUFFER_SIZE, 0, &status);
927 if (U_FAILURE(status)) {
931 /* restore the number format */
932 /* TODO: Is this needed? */
933 unum_setAttribute(format, UNUM_MIN_FRACTION_DIGITS, minDecimalDigits);
934 unum_setAttribute(format, UNUM_MAX_FRACTION_DIGITS, maxDecimalDigits);
936 if (info->fShowSign) {
937 /* Reset back to original value regardless of what the error was */
938 UErrorCode localStatus = U_ZERO_ERROR;
939 u_printf_reset_sign(format, info, prefixBuffer, &prefixBufferLen, &localStatus);
942 return handler->pad_and_justify(context, info, result, resultLen);
945 /* Use US-ASCII characters only for formatting. Most codepages have
946 characters 20-7F from Unicode. Using any other codepage specific
947 characters will make it very difficult to format the string on
948 non-Unicode machines */
949 static const u_printf_info g_u_printf_infos[UPRINTF_NUM_FMT_HANDLERS] = {
951 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
952 UFMT_EMPTY, UFMT_SIMPLE_PERCENT,UFMT_EMPTY, UFMT_EMPTY,
953 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
954 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
957 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
958 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
959 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
960 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
963 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR,
964 UFMT_EMPTY, UFMT_SCIENTIFIC, UFMT_EMPTY, UFMT_SCIDBL,
965 #ifdef U_USE_OBSOLETE_IO_FORMATTING
966 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_UCHAR/*deprecated*/,
968 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
970 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
973 UFMT_PERCENT, UFMT_EMPTY, UFMT_EMPTY, UFMT_USTRING,
974 #ifdef U_USE_OBSOLETE_IO_FORMATTING
975 UFMT_EMPTY, UFMT_USTRING/*deprecated*/,UFMT_SPELLOUT, UFMT_EMPTY,
977 UFMT_EMPTY, UFMT_EMPTY, UFMT_SPELLOUT, UFMT_EMPTY,
979 UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
980 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
983 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_CHAR,
984 UFMT_INT, UFMT_SCIENTIFIC, UFMT_DOUBLE, UFMT_SCIDBL,
985 UFMT_EMPTY, UFMT_INT, UFMT_EMPTY, UFMT_EMPTY,
986 UFMT_EMPTY, UFMT_EMPTY, UFMT_COUNT, UFMT_OCTAL,
989 UFMT_POINTER, UFMT_EMPTY, UFMT_EMPTY, UFMT_STRING,
990 UFMT_EMPTY, UFMT_UINT, UFMT_EMPTY, UFMT_EMPTY,
991 UFMT_HEX, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
992 UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY, UFMT_EMPTY,
995 /* flag characters for uprintf */
996 #define FLAG_MINUS 0x002D
997 #define FLAG_PLUS 0x002B
998 #define FLAG_SPACE 0x0020
999 #define FLAG_POUND 0x0023
1000 #define FLAG_ZERO 0x0030
1001 #define FLAG_PAREN 0x0028
1003 #define ISFLAG(s) (s) == FLAG_MINUS || \
1004 (s) == FLAG_PLUS || \
1005 (s) == FLAG_SPACE || \
1006 (s) == FLAG_POUND || \
1007 (s) == FLAG_ZERO || \
1010 /* special characters for uprintf */
1011 #define SPEC_ASTERISK 0x002A
1012 #define SPEC_DOLLARSIGN 0x0024
1013 #define SPEC_PERIOD 0x002E
1014 #define SPEC_PERCENT 0x0025
1016 /* unicode digits */
1017 #define DIGIT_ZERO 0x0030
1018 #define DIGIT_ONE 0x0031
1019 #define DIGIT_TWO 0x0032
1020 #define DIGIT_THREE 0x0033
1021 #define DIGIT_FOUR 0x0034
1022 #define DIGIT_FIVE 0x0035
1023 #define DIGIT_SIX 0x0036
1024 #define DIGIT_SEVEN 0x0037
1025 #define DIGIT_EIGHT 0x0038
1026 #define DIGIT_NINE 0x0039
1028 #define ISDIGIT(s) (s) == DIGIT_ZERO || \
1029 (s) == DIGIT_ONE || \
1030 (s) == DIGIT_TWO || \
1031 (s) == DIGIT_THREE || \
1032 (s) == DIGIT_FOUR || \
1033 (s) == DIGIT_FIVE || \
1034 (s) == DIGIT_SIX || \
1035 (s) == DIGIT_SEVEN || \
1036 (s) == DIGIT_EIGHT || \
1039 /* u_printf modifiers */
1040 #define MOD_H 0x0068
1041 #define MOD_LOWERL 0x006C
1042 #define MOD_L 0x004C
1044 #define ISMOD(s) (s) == MOD_H || \
1045 (s) == MOD_LOWERL || \
1047 /* Returns an array of the parsed argument type given in the format string. */
1048 static ufmt_args* parseArguments(const UChar *alias, va_list ap, UErrorCode *status) {
1049 ufmt_args *arglist = NULL;
1050 ufmt_type_info *typelist = NULL;
1051 UBool *islonglong = NULL;
1055 uint16_t handlerNum;
1056 const UChar *aliasStart = alias;
1058 /* get maximum number of arguments */
1061 while(*alias != UP_PERCENT && *alias != 0x0000) {
1065 if(*alias == 0x0000) {
1071 /* handle the pos number */
1072 if(ISDIGIT(*alias)) {
1074 /* handle positional parameters */
1075 if(ISDIGIT(*alias)) {
1076 pos = (int) (*alias++ - DIGIT_ZERO);
1078 while(ISDIGIT(*alias)) {
1080 pos += (int) (*alias++ - DIGIT_ZERO);
1084 /* if there is no '$', don't read anything */
1085 if(*alias != SPEC_DOLLARSIGN) {
1097 /* create the parsed argument list */
1098 typelist = (ufmt_type_info*)uprv_malloc(sizeof(ufmt_type_info) * size);
1099 islonglong = (UBool*)uprv_malloc(sizeof(UBool) * size);
1100 arglist = (ufmt_args*)uprv_malloc(sizeof(ufmt_args) * size);
1102 /* If malloc failed, return NULL */
1103 if (!typelist || !islonglong || !arglist) {
1105 uprv_free(typelist);
1109 uprv_free(islonglong);
1116 *status = U_MEMORY_ALLOCATION_ERROR;
1120 /* reset alias back to the beginning */
1125 while(*alias != UP_PERCENT && *alias != 0x0000) {
1129 if(*alias == 0x0000) {
1135 /* handle positional parameters */
1136 if(ISDIGIT(*alias)) {
1137 pos = (int) (*alias++ - DIGIT_ZERO);
1139 while(ISDIGIT(*alias)) {
1141 pos += (int) (*alias++ - DIGIT_ZERO);
1144 /* offset position by 1 */
1147 /* skip over everything except for the type */
1148 while (ISMOD(*alias) || ISFLAG(*alias) || ISDIGIT(*alias) ||
1149 *alias == SPEC_ASTERISK || *alias == SPEC_PERIOD || *alias == SPEC_DOLLARSIGN) {
1150 islonglong[pos] = FALSE;
1151 if (ISMOD(*alias)) {
1153 if (*alias == MOD_LOWERL) {
1154 islonglong[pos] = TRUE;
1161 /* store the argument type in the correct position of the parsed argument list */
1162 handlerNum = (uint16_t)(type - UPRINTF_BASE_FMT_HANDLERS);
1163 if (handlerNum < UPRINTF_NUM_FMT_HANDLERS) {
1164 typelist[pos] = g_u_printf_infos[ handlerNum ].info;
1166 typelist[pos] = ufmt_empty;
1170 /* store argument in arglist */
1171 for (pos = 0; pos < size; pos++) {
1172 switch (typelist[pos]) {
1176 arglist[pos].ptrValue = va_arg(ap, void*);
1181 if (islonglong[pos]) {
1182 arglist[pos].int64Value = va_arg(ap, int64_t);
1185 arglist[pos].int64Value = va_arg(ap, int32_t);
1189 arglist[pos].floatValue = (float) va_arg(ap, double);
1192 arglist[pos].doubleValue = va_arg(ap, double);
1195 /* else args is ignored */
1196 arglist[pos].ptrValue = NULL;
1201 uprv_free(typelist);
1202 uprv_free(islonglong);
1207 /* We parse the argument list in Unicode */
1209 u_printf_parse(const u_printf_stream_handler *streamHandler,
1212 u_localized_print_string *locStringContext,
1213 ULocaleBundle *formatBundle,
1217 uint16_t handlerNum;
1219 ufmt_type_info argType;
1220 u_printf_handler *handler;
1222 u_printf_spec_info *info = &(spec.fInfo);
1224 const UChar *alias = fmt;
1225 const UChar *backup;
1226 const UChar *lastAlias;
1227 const UChar *orgAlias = fmt;
1228 /* parsed argument list */
1229 ufmt_args *arglist = NULL; /* initialized it to avoid compiler warnings */
1230 UErrorCode status = U_ZERO_ERROR;
1231 if (!locStringContext || locStringContext->available >= 0) {
1232 /* get the parsed list of argument types */
1233 arglist = parseArguments(orgAlias, ap, &status);
1235 /* Return error if parsing failed. */
1236 if (U_FAILURE(status)) {
1241 /* iterate through the pattern */
1242 while(!locStringContext || locStringContext->available >= 0) {
1244 /* find the next '%' */
1246 while(*alias != UP_PERCENT && *alias != 0x0000) {
1250 /* write any characters before the '%' */
1251 if(alias > lastAlias) {
1252 *written += (streamHandler->write)(context, lastAlias, (int32_t)(alias - lastAlias));
1255 /* break if at end of string */
1256 if(*alias == 0x0000) {
1260 /* initialize spec to default values */
1261 spec.fWidthPos = -1;
1262 spec.fPrecisionPos = -1;
1265 uprv_memset(info, 0, sizeof(*info));
1266 info->fPrecision = -1;
1268 info->fPadChar = 0x0020;
1270 /* skip over the initial '%' */
1273 /* Check for positional argument */
1274 if(ISDIGIT(*alias)) {
1276 /* Save the current position */
1279 /* handle positional parameters */
1280 if(ISDIGIT(*alias)) {
1281 spec.fArgPos = (int) (*alias++ - DIGIT_ZERO);
1283 while(ISDIGIT(*alias)) {
1285 spec.fArgPos += (int) (*alias++ - DIGIT_ZERO);
1289 /* if there is no '$', don't read anything */
1290 if(*alias != SPEC_DOLLARSIGN) {
1299 /* Get any format flags */
1300 while(ISFLAG(*alias)) {
1308 /* always show sign */
1310 info->fShowSign = TRUE;
1313 /* use space if no sign present */
1315 info->fShowSign = TRUE;
1316 info->fSpace = TRUE;
1319 /* use alternate form */
1324 /* pad with leading zeroes */
1327 info->fPadChar = 0x0030;
1330 /* pad character specified */
1333 /* TODO test that all four are numbers */
1334 /* first four characters are hex values for pad char */
1335 info->fPadChar = (UChar)ufmt_digitvalue(*alias++);
1336 info->fPadChar = (UChar)((info->fPadChar * 16) + ufmt_digitvalue(*alias++));
1337 info->fPadChar = (UChar)((info->fPadChar * 16) + ufmt_digitvalue(*alias++));
1338 info->fPadChar = (UChar)((info->fPadChar * 16) + ufmt_digitvalue(*alias++));
1340 /* final character is ignored */
1349 /* width is specified out of line */
1350 if(*alias == SPEC_ASTERISK) {
1357 /* Save the current position */
1360 /* handle positional parameters */
1361 if(ISDIGIT(*alias)) {
1362 spec.fWidthPos = (int) (*alias++ - DIGIT_ZERO);
1364 while(ISDIGIT(*alias)) {
1365 spec.fWidthPos *= 10;
1366 spec.fWidthPos += (int) (*alias++ - DIGIT_ZERO);
1370 /* if there is no '$', don't read anything */
1371 if(*alias != SPEC_DOLLARSIGN) {
1372 spec.fWidthPos = -1;
1379 /* read the width, if present */
1380 else if(ISDIGIT(*alias)){
1381 info->fWidth = (int) (*alias++ - DIGIT_ZERO);
1383 while(ISDIGIT(*alias)) {
1385 info->fWidth += (int) (*alias++ - DIGIT_ZERO);
1389 /* Get the precision */
1391 if(*alias == SPEC_PERIOD) {
1393 /* eat up the '.' */
1396 /* precision is specified out of line */
1397 if(*alias == SPEC_ASTERISK) {
1399 info->fPrecision = -2;
1404 /* save the current position */
1407 /* handle positional parameters */
1408 if(ISDIGIT(*alias)) {
1409 spec.fPrecisionPos = (int) (*alias++ - DIGIT_ZERO);
1411 while(ISDIGIT(*alias)) {
1412 spec.fPrecisionPos *= 10;
1413 spec.fPrecisionPos += (int) (*alias++ - DIGIT_ZERO);
1416 /* if there is no '$', don't read anything */
1417 if(*alias != SPEC_DOLLARSIGN) {
1418 spec.fPrecisionPos = -1;
1427 /* read the precision */
1428 else if(ISDIGIT(*alias)){
1429 info->fPrecision = (int) (*alias++ - DIGIT_ZERO);
1431 while(ISDIGIT(*alias)) {
1432 info->fPrecision *= 10;
1433 info->fPrecision += (int) (*alias++ - DIGIT_ZERO);
1438 /* Get any modifiers */
1444 info->fIsShort = TRUE;
1447 /* long or long long */
1449 if(*alias == MOD_LOWERL) {
1450 info->fIsLongLong = TRUE;
1451 /* skip over the next 'l' */
1455 info->fIsLong = TRUE;
1460 info->fIsLongDouble = TRUE;
1465 /* finally, get the specifier letter */
1466 info->fSpec = *alias++;
1467 info->fOrigSpec = info->fSpec;
1469 /* fill in the precision and width, if specified out of line */
1471 /* width specified out of line */
1472 if(spec.fInfo.fWidth == -2) {
1473 if(spec.fWidthPos == -1) {
1474 /* read the width from the argument list */
1475 info->fWidth = va_arg(ap, int32_t);
1477 /* else handle positional parameter */
1479 /* if it's negative, take the absolute value and set left alignment */
1480 if(info->fWidth < 0) {
1481 info->fWidth *= -1; /* Make positive */
1486 /* precision specified out of line */
1487 if(info->fPrecision == -2) {
1488 if(spec.fPrecisionPos == -1) {
1489 /* read the precision from the argument list */
1490 info->fPrecision = va_arg(ap, int32_t);
1492 /* else handle positional parameter */
1494 /* if it's negative, set it to zero */
1495 if(info->fPrecision < 0)
1496 info->fPrecision = 0;
1499 handlerNum = (uint16_t)(info->fSpec - UPRINTF_BASE_FMT_HANDLERS);
1500 if (handlerNum < UPRINTF_NUM_FMT_HANDLERS) {
1501 /* query the info function for argument information */
1502 argType = g_u_printf_infos[ handlerNum ].info;
1504 /* goto the correct argument on arg_list if position is specified */
1505 if (spec.fArgPos > 0) {
1506 /* offset position by 1 */
1510 /* set the spec's width to the # of chars written */
1511 info->fWidth = *written;
1512 /* fall through to set the pointer */
1517 args.ptrValue = arglist[spec.fArgPos].ptrValue;
1522 args.int64Value = arglist[spec.fArgPos].int64Value;
1525 args.floatValue = arglist[spec.fArgPos].floatValue;
1528 args.doubleValue = arglist[spec.fArgPos].doubleValue;
1531 /* else args is ignored */
1532 args.ptrValue = NULL;
1535 } else { /* no positional argument specified */
1538 /* set the spec's width to the # of chars written */
1539 info->fWidth = *written;
1540 /* fall through to set the pointer */
1545 args.ptrValue = va_arg(ap, void*);
1550 if (info->fIsLongLong) {
1551 args.int64Value = va_arg(ap, int64_t);
1554 args.int64Value = va_arg(ap, int32_t);
1558 args.floatValue = (float) va_arg(ap, double);
1561 args.doubleValue = va_arg(ap, double);
1564 /* else args is ignored */
1565 args.ptrValue = NULL;
1570 /* call the handler function */
1571 handler = g_u_printf_infos[ handlerNum ].handler;
1573 *written += (*handler)(streamHandler, context, formatBundle, info, &args);
1576 /* just echo unknown tags */
1577 *written += (streamHandler->write)(context, fmt, (int32_t)(alias - lastAlias));
1581 /* just echo unknown tags */
1582 *written += (streamHandler->write)(context, fmt, (int32_t)(alias - lastAlias));
1585 /* delete parsed argument list */
1586 if (arglist != NULL) {
1589 /* return # of characters in this format that have been parsed. */
1590 return (int32_t)(alias - fmt);
1593 #endif /* #if !UCONFIG_NO_FORMATTING */