Move the job of converting provided coeffects to ambient coeffects from callee to...
[hiphop-php.git] / hphp / zend / zend-printf.cpp
blobe1faa4473a2494532bbfc3727ec107c3a4eff4b2
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 | Copyright (c) 1998-2010 Zend Technologies Ltd. (http://www.zend.com) |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 2.00 of the Zend license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.zend.com/license/2_00.txt. |
12 | If you did not receive a copy of the Zend license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@zend.com so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
18 #include "hphp/zend/zend-printf.h"
20 #include "hphp/util/exception.h"
21 #include "hphp/zend/zend-string.h"
22 #include "hphp/zend/zend-strtod.h"
24 #include <cmath>
25 #include <ctype.h>
26 #include <limits.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <string.h>
31 namespace HPHP {
33 /* These definitions are copied from the Zend formatted output conversion
34 files so that we only need to make minimal changes to the Zend formatted
35 output conversion functions that are incorporated here.
37 #define NDIG 320
39 typedef enum {
40 LM_STD = 0,
41 LM_INTMAX_T,
42 LM_PTRDIFF_T,
43 LM_LONG_LONG,
44 LM_SIZE_T,
45 LM_LONG,
46 LM_LONG_DOUBLE
47 } length_modifier_e;
49 typedef enum {
50 NO = 0, YES = 1
51 } boolean_e;
53 #define NUM(c) (c - '0')
55 #define STR_TO_DEC(str, num) do { \
56 num = NUM(*str++); \
57 while (isdigit((int)*str)) { \
58 num *= 10; \
59 num += NUM(*str++); \
60 if (num >= INT_MAX / 10) { \
61 while (isdigit((int)*str++)) \
62 continue; \
63 break; \
64 } \
65 } \
66 } while (0)
69 * This macro does zero padding so that the precision
70 * requirement is satisfied. The padding is done by
71 * adding '0's to the left of the string that is going
72 * to be printed.
74 #define FIX_PRECISION(adjust, precision, s, s_len) do { \
75 if (adjust) \
76 while (s_len < precision) { \
77 *--s = '0'; \
78 s_len++; \
79 } \
80 } while (0)
82 typedef int64_t wide_int;
83 typedef uint64_t u_wide_int;
85 #define FALSE 0
86 #define TRUE 1
87 #define NUL '\0'
88 #define INT_NULL ((int *)0)
90 static const char* s_null = "(null)";
91 #define S_NULL_LEN 6
93 #define FLOAT_DIGITS 6
94 #define EXPONENT_LENGTH 10
96 #define HAVE_LOCALE_H 1
98 #ifdef HAVE_LOCALE_H
99 } // namespace HPHP
101 #include <locale.h>
103 namespace HPHP {
104 #define LCONV_DECIMAL_POINT (*lconv->decimal_point)
105 #else
106 #define LCONV_DECIMAL_POINT '.'
107 #endif
109 ///////////////////////////////////////////////////////////////////////////////
111 * Copyright (c) 2002, 2006 Todd C. Miller <Todd.Miller@courtesan.com>
113 * Permission to use, copy, modify, and distribute this software for any
114 * purpose with or without fee is hereby granted, provided that the above
115 * copyright notice and this permission notice appear in all copies.
117 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
118 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
120 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
121 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
122 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
123 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
125 * Sponsored in part by the Defense Advanced Research Projects
126 * Agency (DARPA) and Air Force Research Laboratory, Air Force
127 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
130 static char * __cvt(double value, int ndigit, int *decpt, int *sign,
131 int fmode, int pad) {
132 register char *s = nullptr;
133 char *p, *rve, c;
134 size_t siz;
136 if (ndigit < 0) {
137 siz = -ndigit + 1;
138 } else {
139 siz = ndigit + 1;
142 /* __dtoa() doesn't allocate space for 0 so we do it by hand */
143 if (value == 0.0) {
144 *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */
145 *sign = 0;
146 if ((rve = s = (char *)malloc(ndigit?siz:2)) == nullptr) {
147 return(nullptr);
149 *rve++ = '0';
150 *rve = '\0';
151 if (!ndigit) {
152 return(s);
154 } else {
155 p = zend_dtoa(value, fmode + 2, ndigit, decpt, sign, &rve);
156 if (*decpt == 9999) {
157 /* Infinity or Nan, convert to inf or nan like printf */
158 *decpt = 0;
159 c = *p;
160 zend_freedtoa(p);
161 return strdup(c == 'I' ? "INF" : "NAN");
163 /* Make a local copy and adjust rve to be in terms of s */
164 if (pad && fmode) {
165 siz += *decpt;
167 if ((s = (char *)malloc(siz+1)) == nullptr) {
168 zend_freedtoa(p);
169 return(nullptr);
171 (void)string_copy(s, p, siz);
172 rve = s + (rve - p);
173 zend_freedtoa(p);
176 /* Add trailing zeros */
177 if (pad) {
178 siz -= rve - s;
179 while (--siz) {
180 *rve++ = '0';
182 *rve = '\0';
185 return(s);
188 static inline char *php_ecvt(double value, int ndigit, int *decpt, int *sign) {
189 return(__cvt(value, ndigit, decpt, sign, 0, 1));
192 static inline char *php_fcvt(double value, int ndigit, int *decpt, int *sign) {
193 return(__cvt(value, ndigit, decpt, sign, 1, 1));
196 char *php_gcvt(double value, int ndigit, char dec_point,
197 char exponent, char *buf) {
198 char *digits, *dst, *src;
199 int i, decpt, sign;
201 digits = zend_dtoa(value, 2, ndigit, &decpt, &sign, nullptr);
202 if (decpt == 9999) {
204 * Infinity or NaN, convert to inf or nan with sign.
205 * We assume the buffer is at least ndigit long.
207 snprintf(buf, ndigit + 1, "%s%s", (sign && *digits == 'I') ? "-" : "",
208 *digits == 'I' ? "INF" : "NAN");
209 zend_freedtoa(digits);
210 return (buf);
213 dst = buf;
214 if (sign) {
215 *dst++ = '-';
218 if ((decpt >= 0 && decpt > ndigit) || decpt < -3) { /* use E-style */
219 /* exponential format (e.g. 1.2345e+13) */
220 if (--decpt < 0) {
221 sign = 1;
222 decpt = -decpt;
223 } else {
224 sign = 0;
226 src = digits;
227 *dst++ = *src++;
228 *dst++ = dec_point;
229 if (*src == '\0') {
230 *dst++ = '0';
231 } else {
232 do {
233 *dst++ = *src++;
234 } while (*src != '\0');
236 *dst++ = exponent;
237 if (sign) {
238 *dst++ = '-';
239 } else {
240 *dst++ = '+';
242 if (decpt < 10) {
243 *dst++ = '0' + decpt;
244 *dst = '\0';
245 } else {
246 /* XXX - optimize */
247 for (sign = decpt, i = 0; (sign /= 10) != 0; i++)
248 continue;
249 dst[i + 1] = '\0';
250 while (decpt != 0) {
251 dst[i--] = '0' + decpt % 10;
252 decpt /= 10;
255 } else if (decpt < 0) {
256 /* standard format 0. */
257 *dst++ = '0'; /* zero before decimal point */
258 *dst++ = dec_point;
259 do {
260 *dst++ = '0';
261 } while (++decpt < 0);
262 src = digits;
263 while (*src != '\0') {
264 *dst++ = *src++;
266 *dst = '\0';
267 } else {
268 /* standard format */
269 for (i = 0, src = digits; i < decpt; i++) {
270 if (*src != '\0') {
271 *dst++ = *src++;
272 } else {
273 *dst++ = '0';
276 if (*src != '\0') {
277 if (src == digits) {
278 *dst++ = '0'; /* zero before decimal point */
280 *dst++ = dec_point;
281 for (i = decpt; digits[i] != '\0'; i++) {
282 *dst++ = digits[i];
285 *dst = '\0';
287 zend_freedtoa(digits);
288 return (buf);
291 ///////////////////////////////////////////////////////////////////////////////
292 // Apache license
294 /* ====================================================================
295 * Copyright (c) 1995-1998 The Apache Group. All rights reserved.
297 * Redistribution and use in source and binary forms, with or without
298 * modification, are permitted provided that the following conditions
299 * are met:
301 * 1. Redistributions of source code must retain the above copyright
302 * notice, this list of conditions and the following disclaimer.
304 * 2. Redistributions in binary form must reproduce the above copyright
305 * notice, this list of conditions and the following disclaimer in
306 * the documentation and/or other materials provided with the
307 * distribution.
309 * 3. All advertising materials mentioning features or use of this
310 * software must display the following acknowledgment:
311 * "This product includes software developed by the Apache Group
312 * for use in the Apache HTTP server project (http://www.apache.org/)."
314 * 4. The names "Apache Server" and "Apache Group" must not be used to
315 * endorse or promote products derived from this software without
316 * prior written permission.
318 * 5. Redistributions of any form whatsoever must retain the following
319 * acknowledgment:
320 * "This product includes software developed by the Apache Group
321 * for use in the Apache HTTP server project (http://www.apache.org/)."
323 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
324 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
325 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
326 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
327 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
328 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
329 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
330 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
332 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
333 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
334 * OF THE POSSIBILITY OF SUCH DAMAGE.
335 * ====================================================================
337 * This software consists of voluntary contributions made by many
338 * individuals on behalf of the Apache Group and was originally based
339 * on public domain software written at the National Center for
340 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
341 * For more information on the Apache Group and the Apache HTTP server
342 * project, please see <http://www.apache.org/>.
344 * This code is based on, and used with the permission of, the
345 * SIO stdio-replacement strx_* functions by Panos Tsirigotis
346 * <panos@alumni.cs.colorado.edu> for xinetd.
350 * Convert num to a base X number where X is a power of 2. nbits determines X.
351 * For example, if nbits is 3, we do base 8 conversion
352 * Return value:
353 * a pointer to a string containing the number
355 * The caller provides a buffer for the string: that is the buf_end argument
356 * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
357 * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
359 char * ap_php_conv_p2(register uint64_t num, register int nbits,
360 char format, char *buf_end, register int *len)
362 register int mask = (1 << nbits) - 1;
363 register char *p = buf_end;
364 static char low_digits[] = "0123456789abcdef";
365 static char upper_digits[] = "0123456789ABCDEF";
366 register char *digits = (format == 'X') ? upper_digits : low_digits;
368 do {
369 *--p = digits[num & mask];
370 num >>= nbits;
372 while (num);
374 *len = buf_end - p;
375 return (p);
379 * Convert num to its decimal format.
380 * Return value:
381 * - a pointer to a string containing the number (no sign)
382 * - len contains the length of the string
383 * - is_negative is set to TRUE or FALSE depending on the sign
384 * of the number (always set to FALSE if is_unsigned is TRUE)
386 * The caller provides a buffer for the string: that is the buf_end argument
387 * which is a pointer to the END of the buffer + 1 (i.e. if the buffer
388 * is declared as buf[ 100 ], buf_end should be &buf[ 100 ])
390 char * ap_php_conv_10(register int64_t num, register bool is_unsigned,
391 register int * is_negative, char *buf_end,
392 register int *len) {
393 register char *p = buf_end;
394 register uint64_t magnitude;
396 if (is_unsigned) {
397 magnitude = (uint64_t) num;
398 *is_negative = 0;
399 } else {
400 *is_negative = (num < 0);
403 * On a 2's complement machine, negating the most negative integer
404 * results in a number that cannot be represented as a signed integer.
405 * Here is what we do to obtain the number's magnitude:
406 * a. add 1 to the number
407 * b. negate it (becomes positive)
408 * c. convert it to unsigned
409 * d. add 1
411 if (*is_negative) {
412 int64_t t = num + 1;
413 magnitude = ((uint64_t) - t) + 1;
414 } else {
415 magnitude = (uint64_t) num;
420 * We use a do-while loop so that we write at least 1 digit
422 do {
423 register uint64_t new_magnitude = magnitude / 10;
425 *--p = (char)(magnitude - new_magnitude * 10 + '0');
426 magnitude = new_magnitude;
428 while (magnitude);
430 *len = buf_end - p;
431 return (p);
434 ///////////////////////////////////////////////////////////////////////////////
436 * Convert a floating point number to a string formats 'f', 'e' or 'E'.
437 * The result is placed in buf, and len denotes the length of the string
438 * The sign is returned in the is_negative argument (and is not placed
439 * in buf).
441 char * php_conv_fp(register char format, register double num,
442 bool add_dp, int precision, char dec_point,
443 int *is_negative, char *buf, int *len) {
444 register char *s = buf;
445 register char *p, *p_orig;
446 int decimal_point;
448 if (precision >= NDIG - 1) {
449 precision = NDIG - 2;
452 if (format == 'F') {
453 p_orig = p = php_fcvt(num, precision, &decimal_point, is_negative);
454 } else { // either e or E format
455 p_orig = p = php_ecvt(num, precision + 1, &decimal_point, is_negative);
458 // Check for Infinity and NaN
459 if (isalpha((int)*p)) {
460 *len = strlen(p);
461 memcpy(buf, p, *len + 1);
462 *is_negative = 0;
463 free(p_orig);
464 return (buf);
466 if (format == 'F') {
467 if (decimal_point <= 0) {
468 if (num != 0 || precision > 0) {
469 *s++ = '0';
470 if (precision > 0) {
471 *s++ = dec_point;
472 while (decimal_point++ < 0) {
473 *s++ = '0';
475 } else if (add_dp) {
476 *s++ = dec_point;
479 } else {
480 int addz = decimal_point >= NDIG ? decimal_point - NDIG + 1 : 0;
481 decimal_point -= addz;
482 while (decimal_point-- > 0) {
483 *s++ = *p++;
485 while (addz-- > 0) {
486 *s++ = '0';
488 if (precision > 0 || add_dp) {
489 *s++ = dec_point;
492 } else {
493 *s++ = *p++;
494 if (precision > 0 || add_dp) {
495 *s++ = '.';
499 // copy the rest of p, the NUL is NOT copied
500 while (*p) {
501 *s++ = *p++;
504 if (format != 'F') {
505 char temp[EXPONENT_LENGTH]; // for exponent conversion
506 int t_len;
507 int exponent_is_negative;
509 *s++ = format; // either e or E
510 decimal_point--;
511 if (decimal_point != 0) {
512 p = ap_php_conv_10((int64_t) decimal_point, false,
513 &exponent_is_negative, &temp[EXPONENT_LENGTH],
514 &t_len);
515 *s++ = exponent_is_negative ? '-' : '+';
517 // Make sure the exponent has at least 2 digits
518 while (t_len--) {
519 *s++ = *p++;
521 } else {
522 *s++ = '+';
523 *s++ = '0';
526 *len = s - buf;
527 free(p_orig);
528 return (buf);
531 ///////////////////////////////////////////////////////////////////////////////
533 inline static void appendchar(char **buffer, int *pos, int *size, char add) {
534 if ((*pos + 1) >= *size) {
535 *size <<= 1;
536 *buffer = (char*)realloc(*buffer, *size);
538 (*buffer)[(*pos)++] = add;
541 inline static void appendsimplestring(char **buffer, int *pos, int *size,
542 const char *add, int len) {
543 int req_size = *pos + len;
545 if (req_size > *size) {
546 while (req_size > *size) {
547 *size <<= 1;
549 *buffer = (char *)realloc(*buffer, *size);
551 memcpy(&(*buffer)[*pos], add, len);
552 *pos += len;
556 * Do format conversion placing the output in buffer
558 static int xbuf_format_converter(char **outbuf, const char *fmt, va_list ap)
560 register char *s = nullptr;
561 char *q;
562 int s_len;
564 register int min_width = 0;
565 int precision = 0;
566 enum {
567 LEFT, RIGHT
568 } adjust;
569 char pad_char;
570 char prefix_char;
572 double fp_num;
573 wide_int i_num = (wide_int) 0;
574 u_wide_int ui_num;
576 char num_buf[NUM_BUF_SIZE];
577 char char_buf[2]; /* for printing %% and %<unknown> */
579 #ifdef HAVE_LOCALE_H
580 struct lconv *lconv = nullptr;
581 #endif
584 * Flag variables
586 length_modifier_e modifier;
587 boolean_e alternate_form;
588 boolean_e print_sign;
589 boolean_e print_blank;
590 boolean_e adjust_precision;
591 boolean_e adjust_width;
592 int is_negative;
594 int size = 240;
595 char *result = (char *)malloc(size);
596 int outpos = 0;
598 while (*fmt) {
599 if (*fmt != '%') {
600 appendchar(&result, &outpos, &size, *fmt);
601 } else {
603 * Default variable settings
605 adjust = RIGHT;
606 alternate_form = print_sign = print_blank = NO;
607 pad_char = ' ';
608 prefix_char = NUL;
610 fmt++;
613 * Try to avoid checking for flags, width or precision
615 if (isascii((int)*fmt) && !islower((int)*fmt)) {
617 * Recognize flags: -, #, BLANK, +
619 for (;; fmt++) {
620 if (*fmt == '-')
621 adjust = LEFT;
622 else if (*fmt == '+')
623 print_sign = YES;
624 else if (*fmt == '#')
625 alternate_form = YES;
626 else if (*fmt == ' ')
627 print_blank = YES;
628 else if (*fmt == '0')
629 pad_char = '0';
630 else
631 break;
635 * Check if a width was specified
637 if (isdigit((int)*fmt)) {
638 STR_TO_DEC(fmt, min_width);
639 adjust_width = YES;
640 } else if (*fmt == '*') {
641 min_width = va_arg(ap, int);
642 fmt++;
643 adjust_width = YES;
644 if (min_width < 0) {
645 adjust = LEFT;
646 min_width = -min_width;
648 } else
649 adjust_width = NO;
652 * Check if a precision was specified
654 * XXX: an unreasonable amount of precision may be specified
655 * resulting in overflow of num_buf. Currently we
656 * ignore this possibility.
658 if (*fmt == '.') {
659 adjust_precision = YES;
660 fmt++;
661 if (isdigit((int)*fmt)) {
662 STR_TO_DEC(fmt, precision);
663 } else if (*fmt == '*') {
664 precision = va_arg(ap, int);
665 fmt++;
666 if (precision < 0)
667 precision = 0;
668 } else
669 precision = 0;
670 } else
671 adjust_precision = NO;
672 } else
673 adjust_precision = adjust_width = NO;
676 * Modifier check
678 switch (*fmt) {
679 case 'L':
680 fmt++;
681 modifier = LM_LONG_DOUBLE;
682 break;
683 case 'I':
684 fmt++;
685 #if SIZEOF_LONG_LONG
686 if (*fmt == '6' && *(fmt+1) == '4') {
687 fmt += 2;
688 modifier = LM_LONG_LONG;
689 } else
690 #endif
691 if (*fmt == '3' && *(fmt+1) == '2') {
692 fmt += 2;
693 modifier = LM_LONG;
694 } else {
695 #ifdef _WIN64
696 modifier = LM_LONG_LONG;
697 #else
698 modifier = LM_LONG;
699 #endif
701 break;
702 case 'l':
703 fmt++;
704 #if SIZEOF_LONG_LONG
705 if (*fmt == 'l') {
706 fmt++;
707 modifier = LM_LONG_LONG;
708 } else
709 #endif
710 modifier = LM_LONG;
711 break;
712 case 'z':
713 fmt++;
714 modifier = LM_SIZE_T;
715 break;
716 case 'j':
717 fmt++;
718 #if SIZEOF_INTMAX_T
719 modifier = LM_INTMAX_T;
720 #else
721 modifier = LM_SIZE_T;
722 #endif
723 break;
724 case 't':
725 fmt++;
726 #if SIZEOF_PTRDIFF_T
727 modifier = LM_PTRDIFF_T;
728 #else
729 modifier = LM_SIZE_T;
730 #endif
731 break;
732 case 'h':
733 fmt++;
734 if (*fmt == 'h') {
735 fmt++;
737 /* these are promoted to int, so no break */
738 default:
739 modifier = LM_STD;
740 break;
744 * Argument extraction and printing.
745 * First we determine the argument type.
746 * Then, we convert the argument to a string.
747 * On exit from the switch, s points to the string that
748 * must be printed, s_len has the length of the string
749 * The precision requirements, if any, are reflected in s_len.
751 * NOTE: pad_char may be set to '0' because of the 0 flag.
752 * It is reset to ' ' by non-numeric formats
754 switch (*fmt) {
755 case 'u':
756 switch(modifier) {
757 default:
758 i_num = (wide_int) va_arg(ap, unsigned int);
759 break;
760 case LM_LONG_DOUBLE:
761 goto fmt_error;
762 case LM_LONG:
763 i_num = (wide_int) va_arg(ap, unsigned long int);
764 break;
765 case LM_SIZE_T:
766 i_num = (wide_int) va_arg(ap, size_t);
767 break;
768 #if SIZEOF_LONG_LONG
769 case LM_LONG_LONG:
770 i_num = (wide_int) va_arg(ap, u_wide_int);
771 break;
772 #endif
773 #if SIZEOF_INTMAX_T
774 case LM_INTMAX_T:
775 i_num = (wide_int) va_arg(ap, uintmax_t);
776 break;
777 #endif
778 #if SIZEOF_PTRDIFF_T
779 case LM_PTRDIFF_T:
780 i_num = (wide_int) va_arg(ap, ptrdiff_t);
781 break;
782 #endif
785 * The rest also applies to other integer formats, so fall
786 * into that case.
788 case 'd':
789 case 'i':
791 * Get the arg if we haven't already.
793 if ((*fmt) != 'u') {
794 switch(modifier) {
795 default:
796 i_num = (wide_int) va_arg(ap, int);
797 break;
798 case LM_LONG_DOUBLE:
799 goto fmt_error;
800 case LM_LONG:
801 i_num = (wide_int) va_arg(ap, long int);
802 break;
803 case LM_SIZE_T:
804 #if SIZEOF_SSIZE_T
805 i_num = (wide_int) va_arg(ap, ssize_t);
806 #else
807 i_num = (wide_int) va_arg(ap, size_t);
808 #endif
809 break;
810 #if SIZEOF_LONG_LONG
811 case LM_LONG_LONG:
812 i_num = (wide_int) va_arg(ap, wide_int);
813 break;
814 #endif
815 #if SIZEOF_INTMAX_T
816 case LM_INTMAX_T:
817 i_num = (wide_int) va_arg(ap, intmax_t);
818 break;
819 #endif
820 #if SIZEOF_PTRDIFF_T
821 case LM_PTRDIFF_T:
822 i_num = (wide_int) va_arg(ap, ptrdiff_t);
823 break;
824 #endif
827 s = ap_php_conv_10(i_num, (*fmt) == 'u', &is_negative,
828 &num_buf[NUM_BUF_SIZE], &s_len);
829 FIX_PRECISION(adjust_precision, precision, s, s_len);
831 if (*fmt != 'u') {
832 if (is_negative)
833 prefix_char = '-';
834 else if (print_sign)
835 prefix_char = '+';
836 else if (print_blank)
837 prefix_char = ' ';
839 break;
842 case 'o':
843 switch(modifier) {
844 default:
845 ui_num = (u_wide_int) va_arg(ap, unsigned int);
846 break;
847 case LM_LONG_DOUBLE:
848 goto fmt_error;
849 case LM_LONG:
850 ui_num = (u_wide_int) va_arg(ap, unsigned long int);
851 break;
852 case LM_SIZE_T:
853 ui_num = (u_wide_int) va_arg(ap, size_t);
854 break;
855 #if SIZEOF_LONG_LONG
856 case LM_LONG_LONG:
857 ui_num = (u_wide_int) va_arg(ap, u_wide_int);
858 break;
859 #endif
860 #if SIZEOF_INTMAX_T
861 case LM_INTMAX_T:
862 ui_num = (u_wide_int) va_arg(ap, uintmax_t);
863 break;
864 #endif
865 #if SIZEOF_PTRDIFF_T
866 case LM_PTRDIFF_T:
867 ui_num = (u_wide_int) va_arg(ap, ptrdiff_t);
868 break;
869 #endif
871 s = ap_php_conv_p2(ui_num, 3, *fmt,
872 &num_buf[NUM_BUF_SIZE], &s_len);
873 FIX_PRECISION(adjust_precision, precision, s, s_len);
874 if (alternate_form && *s != '0') {
875 *--s = '0';
876 s_len++;
878 break;
881 case 'x':
882 case 'X':
883 switch(modifier) {
884 default:
885 ui_num = (u_wide_int) va_arg(ap, unsigned int);
886 break;
887 case LM_LONG_DOUBLE:
888 goto fmt_error;
889 case LM_LONG:
890 ui_num = (u_wide_int) va_arg(ap, unsigned long int);
891 break;
892 case LM_SIZE_T:
893 ui_num = (u_wide_int) va_arg(ap, size_t);
894 break;
895 #if SIZEOF_LONG_LONG
896 case LM_LONG_LONG:
897 ui_num = (u_wide_int) va_arg(ap, u_wide_int);
898 break;
899 #endif
900 #if SIZEOF_INTMAX_T
901 case LM_INTMAX_T:
902 ui_num = (u_wide_int) va_arg(ap, uintmax_t);
903 break;
904 #endif
905 #if SIZEOF_PTRDIFF_T
906 case LM_PTRDIFF_T:
907 ui_num = (u_wide_int) va_arg(ap, ptrdiff_t);
908 break;
909 #endif
911 s = ap_php_conv_p2(ui_num, 4, *fmt,
912 &num_buf[NUM_BUF_SIZE], &s_len);
913 FIX_PRECISION(adjust_precision, precision, s, s_len);
914 if (alternate_form && i_num != 0) {
915 *--s = *fmt; /* 'x' or 'X' */
916 *--s = '0';
917 s_len += 2;
919 break;
922 case 's':
923 case 'v':
924 s = va_arg(ap, char *);
925 if (s != nullptr) {
926 s_len = strlen(s);
927 if (adjust_precision && precision < s_len)
928 s_len = precision;
929 } else {
930 s = const_cast<char*>(s_null);
931 s_len = S_NULL_LEN;
933 pad_char = ' ';
934 break;
937 case 'f':
938 case 'F':
939 case 'e':
940 case 'E':
941 switch(modifier) {
942 case LM_LONG_DOUBLE:
943 fp_num = (double) va_arg(ap, long double);
944 break;
945 case LM_STD:
946 fp_num = va_arg(ap, double);
947 break;
948 default:
949 goto fmt_error;
952 if (std::isnan(fp_num)) {
953 s = const_cast<char*>("nan");
954 s_len = 3;
955 } else if (std::isinf(fp_num)) {
956 s = const_cast<char*>("inf");
957 s_len = 3;
958 } else {
959 #ifdef HAVE_LOCALE_H
960 if (!lconv) {
961 lconv = localeconv();
963 #endif
964 s = php_conv_fp((*fmt == 'f')?'F':*fmt, fp_num, alternate_form,
965 (adjust_precision == NO) ? FLOAT_DIGITS : precision,
966 (*fmt == 'f')?LCONV_DECIMAL_POINT:'.',
967 &is_negative, &num_buf[1], &s_len);
968 if (is_negative)
969 prefix_char = '-';
970 else if (print_sign)
971 prefix_char = '+';
972 else if (print_blank)
973 prefix_char = ' ';
975 break;
978 case 'g':
979 case 'k':
980 case 'G':
981 case 'H':
982 switch(modifier) {
983 case LM_LONG_DOUBLE:
984 fp_num = (double) va_arg(ap, long double);
985 break;
986 case LM_STD:
987 fp_num = va_arg(ap, double);
988 break;
989 default:
990 goto fmt_error;
993 if (std::isnan(fp_num)) {
994 s = const_cast<char*>("NAN");
995 s_len = 3;
996 break;
997 } else if (std::isinf(fp_num)) {
998 if (fp_num > 0) {
999 s = const_cast<char*>("INF");
1000 s_len = 3;
1001 } else {
1002 s = const_cast<char*>("-INF");
1003 s_len = 4;
1005 break;
1008 if (adjust_precision == NO)
1009 precision = FLOAT_DIGITS;
1010 else if (precision == 0)
1011 precision = 1;
1013 * * We use &num_buf[ 1 ], so that we have room for the sign
1015 #ifdef HAVE_LOCALE_H
1016 if (!lconv) {
1017 lconv = localeconv();
1019 #endif
1020 s = php_gcvt(fp_num, precision,
1021 (*fmt=='H' || *fmt == 'k') ? '.' : LCONV_DECIMAL_POINT,
1022 (*fmt == 'G' || *fmt == 'H')?'E':'e', &num_buf[1]);
1023 if (*s == '-')
1024 prefix_char = *s++;
1025 else if (print_sign)
1026 prefix_char = '+';
1027 else if (print_blank)
1028 prefix_char = ' ';
1030 s_len = strlen(s);
1032 if (alternate_form && (q = strchr(s, '.')) == nullptr)
1033 s[s_len++] = '.';
1034 break;
1037 case 'c':
1038 char_buf[0] = (char) (va_arg(ap, int));
1039 s = &char_buf[0];
1040 s_len = 1;
1041 pad_char = ' ';
1042 break;
1045 case '%':
1046 char_buf[0] = '%';
1047 s = &char_buf[0];
1048 s_len = 1;
1049 pad_char = ' ';
1050 break;
1053 case 'n':
1054 *(va_arg(ap, int *)) = outpos;
1055 goto skip_output;
1058 * Always extract the argument as a "char *" pointer. We
1059 * should be using "void *" but there are still machines
1060 * that don't understand it.
1061 * If the pointer size is equal to the size of an unsigned
1062 * integer we convert the pointer to a hex number, otherwise
1063 * we print "%p" to indicate that we don't handle "%p".
1065 case 'p':
1066 if (sizeof(char *) <= sizeof(u_wide_int)) {
1067 ui_num = (u_wide_int)((size_t) va_arg(ap, char *));
1068 s = ap_php_conv_p2(ui_num, 4, 'x',
1069 &num_buf[NUM_BUF_SIZE], &s_len);
1070 if (ui_num != 0) {
1071 *--s = 'x';
1072 *--s = '0';
1073 s_len += 2;
1075 } else {
1076 s = const_cast<char*>("%p");
1077 s_len = 2;
1079 pad_char = ' ';
1080 break;
1083 case NUL:
1085 * The last character of the format string was %.
1086 * We ignore it.
1088 continue;
1091 fmt_error:
1092 throw Exception("Illegal length modifier specified '%c'", *fmt);
1095 * The default case is for unrecognized %'s.
1096 * We print %<char> to help the user identify what
1097 * option is not understood.
1098 * This is also useful in case the user wants to pass
1099 * the output of format_converter to another function
1100 * that understands some other %<char> (like syslog).
1101 * Note that we can't point s inside fmt because the
1102 * unknown <char> could be preceded by width etc.
1104 default:
1105 char_buf[0] = '%';
1106 char_buf[1] = *fmt;
1107 s = char_buf;
1108 s_len = 2;
1109 pad_char = ' ';
1110 break;
1113 if (prefix_char != NUL) {
1114 *--s = prefix_char;
1115 s_len++;
1117 if (adjust_width && adjust == RIGHT && min_width > s_len) {
1118 if (pad_char == '0' && prefix_char != NUL) {
1119 appendchar(&result, &outpos, &size, *s);
1120 s++;
1121 s_len--;
1122 min_width--;
1124 for (int i = 0; i < min_width - s_len; i++) {
1125 appendchar(&result, &outpos, &size, pad_char);
1129 * Print the (for now) non-null terminated string s.
1131 appendsimplestring(&result, &outpos, &size, s, s_len);
1133 if (adjust_width && adjust == LEFT && min_width > s_len) {
1134 for (int i = 0; i < min_width - s_len; i++) {
1135 appendchar(&result, &outpos, &size, pad_char);
1139 skip_output:
1140 fmt++;
1143 * Add the terminating null here since it wasn't added incrementally above
1144 * once the whole string has been composed.
1146 appendchar(&result, &outpos, &size, NUL);
1147 *outbuf = result;
1148 return outpos - 1;
1152 * This is the general purpose conversion function.
1154 int vspprintf(char** pbuf, size_t /*max_len*/, const char* format, ...) {
1155 int len;
1156 va_list ap;
1157 va_start(ap, format);
1158 len = xbuf_format_converter(pbuf, format, ap);
1159 va_end(ap);
1160 return len;
1164 * Same as vspprintf but taking an va_list
1166 int vspprintf_ap(char** pbuf, size_t /*max_len*/, const char* format,
1167 va_list ap) {
1168 int len;
1169 len = xbuf_format_converter(pbuf, format, ap);
1170 return len;
1173 int spprintf(char **pbuf, size_t max_len, const char *format, ...)
1175 int cc;
1176 va_list ap;
1178 va_start(ap, format);
1179 cc = vspprintf(pbuf, max_len, format, ap);
1180 va_end(ap);
1181 return (cc);
1184 ///////////////////////////////////////////////////////////////////////////////