Bug 1848636 [wpt PR 41445] - A browser init failure should cause a restart, a=testonly
[gecko.git] / mozglue / misc / Printf.cpp
blob10e06ff51c8f3eaa70e5efb562426c74f0afadb4
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 * Portable safe sprintf code.
10 * Author: Kipp E.B. Hickman
13 #include "double-conversion/double-to-string.h"
14 #include "mozilla/AllocPolicy.h"
15 #include "mozilla/Printf.h"
16 #include "mozilla/UniquePtrExtensions.h"
17 #include "mozilla/Vector.h"
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #if defined(XP_WIN)
25 # include <windows.h>
26 #endif
28 using double_conversion::DoubleToStringConverter;
29 using DTSC = DoubleToStringConverter;
32 * Numbered Argument State
34 struct NumArgState {
35 int type; // type of the current ap
36 va_list ap; // point to the corresponding position on ap
39 using NumArgStateVector =
40 mozilla::Vector<NumArgState, 20, mozilla::MallocAllocPolicy>;
42 // For values up to and including TYPE_DOUBLE, the lowest bit indicates
43 // whether the type is signed (0) or unsigned (1).
44 #define TYPE_SHORT 0
45 #define TYPE_USHORT 1
46 #define TYPE_INTN 2
47 #define TYPE_UINTN 3
48 #define TYPE_LONG 4
49 #define TYPE_ULONG 5
50 #define TYPE_LONGLONG 6
51 #define TYPE_ULONGLONG 7
52 #define TYPE_DOUBLE 8
53 #define TYPE_STRING 9
54 #define TYPE_INTSTR 10
55 #define TYPE_POINTER 11
56 #if defined(XP_WIN)
57 # define TYPE_WSTRING 12
58 #endif
59 #define TYPE_SCHAR 14
60 #define TYPE_UCHAR 15
61 #define TYPE_UNKNOWN 20
63 #define FLAG_LEFT 0x1
64 #define FLAG_SIGNED 0x2
65 #define FLAG_SPACED 0x4
66 #define FLAG_ZEROS 0x8
67 #define FLAG_NEG 0x10
69 static const char hex[] = "0123456789abcdef";
70 static const char HEX[] = "0123456789ABCDEF";
72 // Fill into the buffer using the data in src
73 bool mozilla::PrintfTarget::fill2(const char* src, int srclen, int width,
74 int flags) {
75 char space = ' ';
77 width -= srclen;
78 if (width > 0 && (flags & FLAG_LEFT) == 0) { // Right adjusting
79 if (flags & FLAG_ZEROS) {
80 space = '0';
82 while (--width >= 0) {
83 if (!emit(&space, 1)) {
84 return false;
89 // Copy out the source data
90 if (!emit(src, srclen)) {
91 return false;
94 if (width > 0 && (flags & FLAG_LEFT) != 0) { // Left adjusting
95 while (--width >= 0) {
96 if (!emit(&space, 1)) {
97 return false;
101 return true;
105 * Fill a number. The order is: optional-sign zero-filling conversion-digits
107 bool mozilla::PrintfTarget::fill_n(const char* src, int srclen, int width,
108 int prec, int type, int flags) {
109 int zerowidth = 0;
110 int precwidth = 0;
111 int signwidth = 0;
112 int leftspaces = 0;
113 int rightspaces = 0;
114 int cvtwidth;
115 char sign;
117 if ((type & 1) == 0) {
118 if (flags & FLAG_NEG) {
119 sign = '-';
120 signwidth = 1;
121 } else if (flags & FLAG_SIGNED) {
122 sign = '+';
123 signwidth = 1;
124 } else if (flags & FLAG_SPACED) {
125 sign = ' ';
126 signwidth = 1;
129 cvtwidth = signwidth + srclen;
131 if (prec > 0 && (type != TYPE_DOUBLE)) {
132 if (prec > srclen) {
133 precwidth = prec - srclen; // Need zero filling
134 cvtwidth += precwidth;
138 if ((flags & FLAG_ZEROS) && ((type == TYPE_DOUBLE) || (prec < 0))) {
139 if (width > cvtwidth) {
140 zerowidth = width - cvtwidth; // Zero filling
141 cvtwidth += zerowidth;
145 if (flags & FLAG_LEFT) {
146 if (width > cvtwidth) {
147 // Space filling on the right (i.e. left adjusting)
148 rightspaces = width - cvtwidth;
150 } else {
151 if (width > cvtwidth) {
152 // Space filling on the left (i.e. right adjusting)
153 leftspaces = width - cvtwidth;
156 while (--leftspaces >= 0) {
157 if (!emit(" ", 1)) {
158 return false;
161 if (signwidth) {
162 if (!emit(&sign, 1)) {
163 return false;
166 while (--precwidth >= 0) {
167 if (!emit("0", 1)) {
168 return false;
171 while (--zerowidth >= 0) {
172 if (!emit("0", 1)) {
173 return false;
176 if (!emit(src, uint32_t(srclen))) {
177 return false;
179 while (--rightspaces >= 0) {
180 if (!emit(" ", 1)) {
181 return false;
184 return true;
187 // All that the cvt_* functions care about as far as the TYPE_* constants is
188 // that the low bit is set to indicate unsigned, or unset to indicate signed.
189 // So we don't try to hard to ensure that the passed TYPE_* constant lines
190 // up with the actual size of the number being printed here. The main printf
191 // code, below, does have to care so that the correct bits are extracted from
192 // the varargs list.
193 bool mozilla::PrintfTarget::appendIntDec(int32_t num) {
194 int flags = 0;
195 long n = num;
196 if (n < 0) {
197 n = -n;
198 flags |= FLAG_NEG;
200 return cvt_l(n, -1, -1, 10, TYPE_INTN, flags, hex);
203 bool mozilla::PrintfTarget::appendIntDec(uint32_t num) {
204 return cvt_l(num, -1, -1, 10, TYPE_UINTN, 0, hex);
207 bool mozilla::PrintfTarget::appendIntOct(uint32_t num) {
208 return cvt_l(num, -1, -1, 8, TYPE_UINTN, 0, hex);
211 bool mozilla::PrintfTarget::appendIntHex(uint32_t num) {
212 return cvt_l(num, -1, -1, 16, TYPE_UINTN, 0, hex);
215 bool mozilla::PrintfTarget::appendIntDec(int64_t num) {
216 int flags = 0;
217 if (num < 0) {
218 num = -num;
219 flags |= FLAG_NEG;
221 return cvt_ll(num, -1, -1, 10, TYPE_INTN, flags, hex);
224 bool mozilla::PrintfTarget::appendIntDec(uint64_t num) {
225 return cvt_ll(num, -1, -1, 10, TYPE_UINTN, 0, hex);
228 bool mozilla::PrintfTarget::appendIntOct(uint64_t num) {
229 return cvt_ll(num, -1, -1, 8, TYPE_UINTN, 0, hex);
232 bool mozilla::PrintfTarget::appendIntHex(uint64_t num) {
233 return cvt_ll(num, -1, -1, 16, TYPE_UINTN, 0, hex);
236 /* Convert a long into its printable form. */
237 bool mozilla::PrintfTarget::cvt_l(long num, int width, int prec, int radix,
238 int type, int flags, const char* hexp) {
239 char cvtbuf[100];
240 char* cvt;
241 int digits;
243 // according to the man page this needs to happen
244 if ((prec == 0) && (num == 0)) {
245 return fill_n("", 0, width, prec, type, flags);
248 // Converting decimal is a little tricky. In the unsigned case we
249 // need to stop when we hit 10 digits. In the signed case, we can
250 // stop when the number is zero.
251 cvt = cvtbuf + sizeof(cvtbuf);
252 digits = 0;
253 while (num) {
254 int digit = (((unsigned long)num) % radix) & 0xF;
255 *--cvt = hexp[digit];
256 digits++;
257 num = (long)(((unsigned long)num) / radix);
259 if (digits == 0) {
260 *--cvt = '0';
261 digits++;
264 // Now that we have the number converted without its sign, deal with
265 // the sign and zero padding.
266 return fill_n(cvt, digits, width, prec, type, flags);
269 /* Convert a 64-bit integer into its printable form. */
270 bool mozilla::PrintfTarget::cvt_ll(int64_t num, int width, int prec, int radix,
271 int type, int flags, const char* hexp) {
272 // According to the man page, this needs to happen.
273 if (prec == 0 && num == 0) {
274 return fill_n("", 0, width, prec, type, flags);
277 // Converting decimal is a little tricky. In the unsigned case we
278 // need to stop when we hit 10 digits. In the signed case, we can
279 // stop when the number is zero.
280 int64_t rad = int64_t(radix);
281 char cvtbuf[100];
282 char* cvt = cvtbuf + sizeof(cvtbuf);
283 int digits = 0;
284 while (num != 0) {
285 int64_t quot = uint64_t(num) / rad;
286 int64_t rem = uint64_t(num) % rad;
287 int32_t digit = int32_t(rem);
288 *--cvt = hexp[digit & 0xf];
289 digits++;
290 num = quot;
292 if (digits == 0) {
293 *--cvt = '0';
294 digits++;
297 // Now that we have the number converted without its sign, deal with
298 // the sign and zero padding.
299 return fill_n(cvt, digits, width, prec, type, flags);
302 template <size_t N>
303 constexpr static size_t lengthof(const char (&)[N]) {
304 return N - 1;
307 // Longest possible output from ToFixed for positive numbers:
308 // [0-9]{kMaxFixedDigitsBeforePoint}\.[0-9]{kMaxFixedDigitsAfterPoint}?
309 constexpr int FIXED_MAX_CHARS =
310 DTSC::kMaxFixedDigitsBeforePoint + 1 + DTSC::kMaxFixedDigitsAfterPoint;
312 // Longest possible output from ToExponential:
313 // [1-9]\.[0-9]{kMaxExponentialDigits}e[+-][0-9]{1,3}
314 // (std::numeric_limits<double>::max() has exponent 308).
315 constexpr int EXPONENTIAL_MAX_CHARS =
316 lengthof("1.") + DTSC::kMaxExponentialDigits + lengthof("e+999");
318 // Longest possible output from ToPrecise:
319 // [0-9\.]{kMaxPrecisionDigits+1} or
320 // [1-9]\.[0-9]{kMaxPrecisionDigits-1}e[+-][0-9]{1,3}
321 constexpr int PRECISE_MAX_CHARS =
322 lengthof("1.") + DTSC::kMaxPrecisionDigits - 1 + lengthof("e+999");
324 constexpr int DTSC_MAX_CHARS =
325 std::max({FIXED_MAX_CHARS, EXPONENTIAL_MAX_CHARS, PRECISE_MAX_CHARS});
328 * Convert a double precision floating point number into its printable
329 * form.
331 bool mozilla::PrintfTarget::cvt_f(double d, char c, int width, int prec,
332 int flags) {
333 bool lower = islower(c);
334 const char* inf = lower ? "inf" : "INF";
335 const char* nan = lower ? "nan" : "NAN";
336 char e = lower ? 'e' : 'E';
337 DoubleToStringConverter converter(DTSC::UNIQUE_ZERO | DTSC::NO_TRAILING_ZERO |
338 DTSC::EMIT_POSITIVE_EXPONENT_SIGN,
339 inf, nan, e, 0, 0, 4, 0, 2);
340 // Longest of the above cases, plus space for a terminal nul character.
341 char buf[DTSC_MAX_CHARS + 1];
342 double_conversion::StringBuilder builder(buf, sizeof(buf));
343 bool success = false;
344 if (std::signbit(d)) {
345 d = std::abs(d);
346 flags |= FLAG_NEG;
348 if (!std::isfinite(d)) {
349 flags &= ~FLAG_ZEROS;
351 // "If the precision is missing, it shall be taken as 6."
352 if (prec < 0) {
353 prec = 6;
355 switch (c) {
356 case 'e':
357 case 'E':
358 success = converter.ToExponential(d, prec, &builder);
359 break;
360 case 'f':
361 case 'F':
362 success = converter.ToFixed(d, prec, &builder);
363 break;
364 case 'g':
365 case 'G':
366 // "If an explicit precision is zero, it shall be taken as 1."
367 success = converter.ToPrecision(d, prec ? prec : 1, &builder);
368 break;
370 if (!success) {
371 return false;
373 int len = builder.position();
374 char* cvt = builder.Finalize();
375 return fill_n(cvt, len, width, prec, TYPE_DOUBLE, flags);
379 * Convert a string into its printable form. "width" is the output
380 * width. "prec" is the maximum number of characters of "s" to output,
381 * where -1 means until NUL.
383 bool mozilla::PrintfTarget::cvt_s(const char* s, int width, int prec,
384 int flags) {
385 if (prec == 0) {
386 return true;
388 if (!s) {
389 s = "(null)";
392 // Limit string length by precision value
393 int slen = int(strlen(s));
394 if (0 < prec && prec < slen) {
395 slen = prec;
398 // and away we go
399 return fill2(s, slen, width, flags);
403 * BuildArgArray stands for Numbered Argument list Sprintf
404 * for example,
405 * fmp = "%4$i, %2$d, %3s, %1d";
406 * the number must start from 1, and no gap among them
408 static bool BuildArgArray(const char* fmt, va_list ap, NumArgStateVector& nas) {
409 size_t number = 0, cn = 0, i;
410 const char* p;
411 char c;
413 // First pass:
414 // Detemine how many legal % I have got, then allocate space.
416 p = fmt;
417 i = 0;
418 while ((c = *p++) != 0) {
419 if (c != '%') {
420 continue;
422 if ((c = *p++) == '%') { // skip %% case
423 continue;
426 while (c != 0) {
427 if (c > '9' || c < '0') {
428 if (c == '$') { // numbered argument case
429 if (i > 0) {
430 MOZ_CRASH("Bad format string");
432 number++;
433 } else { // non-numbered argument case
434 if (number > 0) {
435 MOZ_CRASH("Bad format string");
437 i = 1;
439 break;
442 c = *p++;
446 if (number == 0) {
447 return true;
450 // Only allow a limited number of arguments.
451 MOZ_RELEASE_ASSERT(number <= 20);
453 if (!nas.growByUninitialized(number)) {
454 return false;
457 for (i = 0; i < number; i++) {
458 nas[i].type = TYPE_UNKNOWN;
461 // Second pass:
462 // Set nas[].type.
464 p = fmt;
465 while ((c = *p++) != 0) {
466 if (c != '%') {
467 continue;
469 c = *p++;
470 if (c == '%') {
471 continue;
474 cn = 0;
475 while (c && c != '$') { // should improve error check later
476 cn = cn * 10 + c - '0';
477 c = *p++;
480 if (!c || cn < 1 || cn > number) {
481 MOZ_CRASH("Bad format string");
484 // nas[cn] starts from 0, and make sure nas[cn].type is not assigned.
485 cn--;
486 if (nas[cn].type != TYPE_UNKNOWN) {
487 continue;
490 c = *p++;
492 // flags
493 while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) {
494 c = *p++;
497 // width
498 if (c == '*') {
499 // not supported feature, for the argument is not numbered
500 MOZ_CRASH("Bad format string");
503 while ((c >= '0') && (c <= '9')) {
504 c = *p++;
507 // precision
508 if (c == '.') {
509 c = *p++;
510 if (c == '*') {
511 // not supported feature, for the argument is not numbered
512 MOZ_CRASH("Bad format string");
515 while ((c >= '0') && (c <= '9')) {
516 c = *p++;
520 // size
521 nas[cn].type = TYPE_INTN;
522 if (c == 'h') {
523 nas[cn].type = TYPE_SHORT;
524 c = *p++;
525 if (c == 'h') {
526 nas[cn].type = TYPE_SCHAR;
527 c = *p++;
529 } else if (c == 'L') {
530 nas[cn].type = TYPE_LONGLONG;
531 c = *p++;
532 } else if (c == 'l') {
533 nas[cn].type = TYPE_LONG;
534 c = *p++;
535 if (c == 'l') {
536 nas[cn].type = TYPE_LONGLONG;
537 c = *p++;
539 } else if (c == 'z' || c == 'I') {
540 static_assert(sizeof(size_t) == sizeof(int) ||
541 sizeof(size_t) == sizeof(long) ||
542 sizeof(size_t) == sizeof(long long),
543 "size_t is not one of the expected sizes");
544 nas[cn].type = sizeof(size_t) == sizeof(int) ? TYPE_INTN
545 : sizeof(size_t) == sizeof(long) ? TYPE_LONG
546 : TYPE_LONGLONG;
547 c = *p++;
548 } else if (c == 't') {
549 static_assert(sizeof(ptrdiff_t) == sizeof(int) ||
550 sizeof(ptrdiff_t) == sizeof(long) ||
551 sizeof(ptrdiff_t) == sizeof(long long),
552 "ptrdiff_t is not one of the expected sizes");
553 nas[cn].type = sizeof(ptrdiff_t) == sizeof(int) ? TYPE_INTN
554 : sizeof(ptrdiff_t) == sizeof(long) ? TYPE_LONG
555 : TYPE_LONGLONG;
556 c = *p++;
557 } else if (c == 'j') {
558 static_assert(sizeof(intmax_t) == sizeof(int) ||
559 sizeof(intmax_t) == sizeof(long) ||
560 sizeof(intmax_t) == sizeof(long long),
561 "intmax_t is not one of the expected sizes");
562 nas[cn].type = sizeof(intmax_t) == sizeof(int) ? TYPE_INTN
563 : sizeof(intmax_t) == sizeof(long) ? TYPE_LONG
564 : TYPE_LONGLONG;
565 c = *p++;
568 // format
569 switch (c) {
570 case 'd':
571 case 'c':
572 case 'i':
573 break;
575 case 'o':
576 case 'u':
577 case 'x':
578 case 'X':
579 // Mark as unsigned type.
580 nas[cn].type |= 1;
581 break;
583 case 'e':
584 case 'E':
585 case 'f':
586 case 'F':
587 case 'g':
588 case 'G':
589 nas[cn].type = TYPE_DOUBLE;
590 break;
592 case 'p':
593 nas[cn].type = TYPE_POINTER;
594 break;
596 case 'S':
597 #if defined(XP_WIN)
598 nas[cn].type = TYPE_WSTRING;
599 #else
600 MOZ_ASSERT(0);
601 nas[cn].type = TYPE_UNKNOWN;
602 #endif
603 break;
605 case 's':
606 #if defined(XP_WIN)
607 if (nas[cn].type == TYPE_LONG) {
608 nas[cn].type = TYPE_WSTRING;
609 break;
611 #endif
612 // Other type sizes are not supported here.
613 MOZ_ASSERT(nas[cn].type == TYPE_INTN);
614 nas[cn].type = TYPE_STRING;
615 break;
617 case 'n':
618 nas[cn].type = TYPE_INTSTR;
619 break;
621 default:
622 MOZ_ASSERT(0);
623 nas[cn].type = TYPE_UNKNOWN;
624 break;
627 // get a legal para.
628 if (nas[cn].type == TYPE_UNKNOWN) {
629 MOZ_CRASH("Bad format string");
633 // Third pass:
634 // Fill nas[].ap.
636 cn = 0;
637 while (cn < number) {
638 // A TYPE_UNKNOWN here means that the format asked for a
639 // positional argument without specifying the meaning of some
640 // earlier argument.
641 MOZ_ASSERT(nas[cn].type != TYPE_UNKNOWN);
643 va_copy(nas[cn].ap, ap);
645 switch (nas[cn].type) {
646 case TYPE_SCHAR:
647 case TYPE_UCHAR:
648 case TYPE_SHORT:
649 case TYPE_USHORT:
650 case TYPE_INTN:
651 case TYPE_UINTN:
652 (void)va_arg(ap, int);
653 break;
654 case TYPE_LONG:
655 (void)va_arg(ap, long);
656 break;
657 case TYPE_ULONG:
658 (void)va_arg(ap, unsigned long);
659 break;
660 case TYPE_LONGLONG:
661 (void)va_arg(ap, long long);
662 break;
663 case TYPE_ULONGLONG:
664 (void)va_arg(ap, unsigned long long);
665 break;
666 case TYPE_STRING:
667 (void)va_arg(ap, char*);
668 break;
669 case TYPE_INTSTR:
670 (void)va_arg(ap, int*);
671 break;
672 case TYPE_DOUBLE:
673 (void)va_arg(ap, double);
674 break;
675 case TYPE_POINTER:
676 (void)va_arg(ap, void*);
677 break;
678 #if defined(XP_WIN)
679 case TYPE_WSTRING:
680 (void)va_arg(ap, wchar_t*);
681 break;
682 #endif
684 default:
685 MOZ_CRASH();
688 cn++;
691 return true;
694 mozilla::PrintfTarget::PrintfTarget() : mEmitted(0) {}
696 bool mozilla::PrintfTarget::vprint(const char* fmt, va_list ap) {
697 char c;
698 int flags, width, prec, radix, type;
699 union {
700 char ch;
701 int i;
702 long l;
703 long long ll;
704 double d;
705 const char* s;
706 int* ip;
707 void* p;
708 #if defined(XP_WIN)
709 const wchar_t* ws;
710 #endif
711 } u{};
712 const char* hexp;
713 int i;
715 // Build an argument array, IF the fmt is numbered argument
716 // list style, to contain the Numbered Argument list pointers.
718 NumArgStateVector nas;
719 if (!BuildArgArray(fmt, ap, nas)) {
720 // the fmt contains error Numbered Argument format, jliu@netscape.com
721 MOZ_CRASH("Bad format string");
724 while ((c = *fmt++) != 0) {
725 if (c != '%') {
726 if (!emit(fmt - 1, 1)) {
727 return false;
730 continue;
733 // Gobble up the % format string. Hopefully we have handled all
734 // of the strange cases!
735 flags = 0;
736 c = *fmt++;
737 if (c == '%') {
738 // quoting a % with %%
739 if (!emit(fmt - 1, 1)) {
740 return false;
743 continue;
746 if (!nas.empty()) {
747 // the fmt contains the Numbered Arguments feature
748 i = 0;
749 while (c && c != '$') { // should improve error check later
750 i = (i * 10) + (c - '0');
751 c = *fmt++;
754 if (nas[i - 1].type == TYPE_UNKNOWN) {
755 MOZ_CRASH("Bad format string");
758 ap = nas[i - 1].ap;
759 c = *fmt++;
762 // Examine optional flags. Note that we do not implement the
763 // '#' flag of sprintf(). The ANSI C spec. of the '#' flag is
764 // somewhat ambiguous and not ideal, which is perhaps why
765 // the various sprintf() implementations are inconsistent
766 // on this feature.
767 while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) {
768 if (c == '-') {
769 flags |= FLAG_LEFT;
771 if (c == '+') {
772 flags |= FLAG_SIGNED;
774 if (c == ' ') {
775 flags |= FLAG_SPACED;
777 if (c == '0') {
778 flags |= FLAG_ZEROS;
780 c = *fmt++;
782 if (flags & FLAG_SIGNED) {
783 flags &= ~FLAG_SPACED;
785 if (flags & FLAG_LEFT) {
786 flags &= ~FLAG_ZEROS;
789 // width
790 if (c == '*') {
791 c = *fmt++;
792 width = va_arg(ap, int);
793 if (width < 0) {
794 width = -width;
795 flags |= FLAG_LEFT;
796 flags &= ~FLAG_ZEROS;
798 } else {
799 width = 0;
800 while ((c >= '0') && (c <= '9')) {
801 width = (width * 10) + (c - '0');
802 c = *fmt++;
806 // precision
807 prec = -1;
808 if (c == '.') {
809 c = *fmt++;
810 if (c == '*') {
811 c = *fmt++;
812 prec = va_arg(ap, int);
813 } else {
814 prec = 0;
815 while ((c >= '0') && (c <= '9')) {
816 prec = (prec * 10) + (c - '0');
817 c = *fmt++;
822 // size
823 type = TYPE_INTN;
824 if (c == 'h') {
825 type = TYPE_SHORT;
826 c = *fmt++;
827 if (c == 'h') {
828 type = TYPE_SCHAR;
829 c = *fmt++;
831 } else if (c == 'L') {
832 type = TYPE_LONGLONG;
833 c = *fmt++;
834 } else if (c == 'l') {
835 type = TYPE_LONG;
836 c = *fmt++;
837 if (c == 'l') {
838 type = TYPE_LONGLONG;
839 c = *fmt++;
841 } else if (c == 'z' || c == 'I') {
842 static_assert(sizeof(size_t) == sizeof(int) ||
843 sizeof(size_t) == sizeof(long) ||
844 sizeof(size_t) == sizeof(long long),
845 "size_t is not one of the expected sizes");
846 type = sizeof(size_t) == sizeof(int) ? TYPE_INTN
847 : sizeof(size_t) == sizeof(long) ? TYPE_LONG
848 : TYPE_LONGLONG;
849 c = *fmt++;
850 } else if (c == 't') {
851 static_assert(sizeof(ptrdiff_t) == sizeof(int) ||
852 sizeof(ptrdiff_t) == sizeof(long) ||
853 sizeof(ptrdiff_t) == sizeof(long long),
854 "ptrdiff_t is not one of the expected sizes");
855 type = sizeof(ptrdiff_t) == sizeof(int) ? TYPE_INTN
856 : sizeof(ptrdiff_t) == sizeof(long) ? TYPE_LONG
857 : TYPE_LONGLONG;
858 c = *fmt++;
859 } else if (c == 'j') {
860 static_assert(sizeof(intmax_t) == sizeof(int) ||
861 sizeof(intmax_t) == sizeof(long) ||
862 sizeof(intmax_t) == sizeof(long long),
863 "intmax_t is not one of the expected sizes");
864 type = sizeof(intmax_t) == sizeof(int) ? TYPE_INTN
865 : sizeof(intmax_t) == sizeof(long) ? TYPE_LONG
866 : TYPE_LONGLONG;
867 c = *fmt++;
870 // format
871 hexp = hex;
872 switch (c) {
873 case 'd':
874 case 'i': // decimal/integer
875 radix = 10;
876 goto fetch_and_convert;
878 case 'o': // octal
879 radix = 8;
880 type |= 1;
881 goto fetch_and_convert;
883 case 'u': // unsigned decimal
884 radix = 10;
885 type |= 1;
886 goto fetch_and_convert;
888 case 'x': // unsigned hex
889 radix = 16;
890 type |= 1;
891 goto fetch_and_convert;
893 case 'X': // unsigned HEX
894 radix = 16;
895 hexp = HEX;
896 type |= 1;
897 goto fetch_and_convert;
899 fetch_and_convert:
900 switch (type) {
901 case TYPE_SCHAR:
902 u.l = (signed char)va_arg(ap, int);
903 if (u.l < 0) {
904 u.l = -u.l;
905 flags |= FLAG_NEG;
907 goto do_long;
908 case TYPE_UCHAR:
909 u.l = (unsigned char)va_arg(ap, unsigned int);
910 goto do_long;
911 case TYPE_SHORT:
912 u.l = (short)va_arg(ap, int);
913 if (u.l < 0) {
914 u.l = -u.l;
915 flags |= FLAG_NEG;
917 goto do_long;
918 case TYPE_USHORT:
919 u.l = (unsigned short)va_arg(ap, unsigned int);
920 goto do_long;
921 case TYPE_INTN:
922 u.l = va_arg(ap, int);
923 if (u.l < 0) {
924 u.l = -u.l;
925 flags |= FLAG_NEG;
927 goto do_long;
928 case TYPE_UINTN:
929 u.l = (long)va_arg(ap, unsigned int);
930 goto do_long;
932 case TYPE_LONG:
933 u.l = va_arg(ap, long);
934 if (u.l < 0) {
935 u.l = -u.l;
936 flags |= FLAG_NEG;
938 goto do_long;
939 case TYPE_ULONG:
940 u.l = (long)va_arg(ap, unsigned long);
941 do_long:
942 if (!cvt_l(u.l, width, prec, radix, type, flags, hexp)) {
943 return false;
946 break;
948 case TYPE_LONGLONG:
949 u.ll = va_arg(ap, long long);
950 if (u.ll < 0) {
951 u.ll = -u.ll;
952 flags |= FLAG_NEG;
954 goto do_longlong;
955 case TYPE_POINTER:
956 u.ll = (uintptr_t)va_arg(ap, void*);
957 goto do_longlong;
958 case TYPE_ULONGLONG:
959 u.ll = va_arg(ap, unsigned long long);
960 do_longlong:
961 if (!cvt_ll(u.ll, width, prec, radix, type, flags, hexp)) {
962 return false;
965 break;
967 break;
969 case 'e':
970 case 'E':
971 case 'f':
972 case 'F':
973 case 'g':
974 case 'G':
975 u.d = va_arg(ap, double);
976 if (!cvt_f(u.d, c, width, prec, flags)) {
977 return false;
980 break;
982 case 'c':
983 if ((flags & FLAG_LEFT) == 0) {
984 while (width-- > 1) {
985 if (!emit(" ", 1)) {
986 return false;
990 switch (type) {
991 case TYPE_SHORT:
992 case TYPE_INTN:
993 u.ch = va_arg(ap, int);
994 if (!emit(&u.ch, 1)) {
995 return false;
997 break;
999 if (flags & FLAG_LEFT) {
1000 while (width-- > 1) {
1001 if (!emit(" ", 1)) {
1002 return false;
1006 break;
1008 case 'p':
1009 type = TYPE_POINTER;
1010 radix = 16;
1011 goto fetch_and_convert;
1013 case 's':
1014 if (type == TYPE_INTN) {
1015 u.s = va_arg(ap, const char*);
1016 if (!cvt_s(u.s, width, prec, flags)) {
1017 return false;
1019 break;
1021 MOZ_ASSERT(type == TYPE_LONG);
1022 [[fallthrough]];
1023 case 'S':
1024 #if defined(XP_WIN)
1026 u.ws = va_arg(ap, const wchar_t*);
1028 int rv = WideCharToMultiByte(CP_ACP, 0, u.ws, -1, NULL, 0, NULL, NULL);
1029 if (rv == 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION) {
1030 if (!cvt_s("<unicode errors in string>", width, prec, flags)) {
1031 return false;
1033 } else {
1034 if (rv == 0) {
1035 rv = 1;
1037 UniqueFreePtr<char[]> buf((char*)malloc(rv));
1038 WideCharToMultiByte(CP_ACP, 0, u.ws, -1, buf.get(), rv, NULL, NULL);
1039 buf[rv - 1] = '\0';
1041 if (!cvt_s(buf.get(), width, prec, flags)) {
1042 return false;
1046 #else
1047 // Not supported here.
1048 MOZ_ASSERT(0);
1049 #endif
1050 break;
1052 case 'n':
1053 u.ip = va_arg(ap, int*);
1054 if (u.ip) {
1055 *u.ip = mEmitted;
1057 break;
1059 default:
1060 // Not a % token after all... skip it
1061 if (!emit("%", 1)) {
1062 return false;
1064 if (!emit(fmt - 1, 1)) {
1065 return false;
1070 return true;
1073 /************************************************************************/
1075 bool mozilla::PrintfTarget::print(const char* format, ...) {
1076 va_list ap;
1078 va_start(ap, format);
1079 bool result = vprint(format, ap);
1080 va_end(ap);
1081 return result;
1084 #undef TYPE_SHORT
1085 #undef TYPE_USHORT
1086 #undef TYPE_INTN
1087 #undef TYPE_UINTN
1088 #undef TYPE_LONG
1089 #undef TYPE_ULONG
1090 #undef TYPE_LONGLONG
1091 #undef TYPE_ULONGLONG
1092 #undef TYPE_STRING
1093 #undef TYPE_DOUBLE
1094 #undef TYPE_INTSTR
1095 #undef TYPE_POINTER
1096 #undef TYPE_WSTRING
1097 #undef TYPE_UNKNOWN
1098 #undef TYPE_SCHAR
1099 #undef TYPE_UCHAR
1101 #undef FLAG_LEFT
1102 #undef FLAG_SIGNED
1103 #undef FLAG_SPACED
1104 #undef FLAG_ZEROS
1105 #undef FLAG_NEG