2002-04-10 09:09:22 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| PHP Version 5 |
|
2002-04-10 09:09:22 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2014-01-03 11:08:10 +08:00
|
|
|
| Copyright (c) 1997-2014 The PHP Group |
|
2002-04-10 09:09:22 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2002-04-10 09:09:22 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:51:34 +08:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2002-04-10 09:09:22 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: Marcus Boerger <helly@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
2003-02-19 16:40:19 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
/* This is the spprintf implementation.
|
|
|
|
* It has emerged from apache snprintf. See original header:
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright (c) 1995-1998 The Apache Group. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2008-02-07 20:47:44 +08:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2002-04-10 09:09:22 +08:00
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgment:
|
|
|
|
* "This product includes software developed by the Apache Group
|
|
|
|
* for use in the Apache HTTP server project (http://www.apache.org/)."
|
|
|
|
*
|
|
|
|
* 4. The names "Apache Server" and "Apache Group" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission.
|
|
|
|
*
|
|
|
|
* 5. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgment:
|
|
|
|
* "This product includes software developed by the Apache Group
|
|
|
|
* for use in the Apache HTTP server project (http://www.apache.org/)."
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many
|
|
|
|
* individuals on behalf of the Apache Group and was originally based
|
|
|
|
* on public domain software written at the National Center for
|
|
|
|
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
|
|
|
|
* For more information on the Apache Group and the Apache HTTP server
|
|
|
|
* project, please see <http://www.apache.org/>.
|
|
|
|
*
|
|
|
|
* This code is based on, and used with the permission of, the
|
|
|
|
* SIO stdio-replacement strx_* functions by Panos Tsirigotis
|
|
|
|
* <panos@alumni.cs.colorado.edu> for xinetd.
|
|
|
|
*/
|
2008-12-13 07:43:18 +08:00
|
|
|
#define _GNU_SOURCE
|
2002-04-10 09:09:22 +08:00
|
|
|
#include "php.h"
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
2003-10-21 15:36:43 +08:00
|
|
|
#ifdef HAVE_INTTYPES_H
|
|
|
|
#include <inttypes.h>
|
|
|
|
#endif
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
|
2006-12-19 19:54:38 +08:00
|
|
|
#ifdef HAVE_LOCALE_H
|
|
|
|
#include <locale.h>
|
2006-12-19 21:13:29 +08:00
|
|
|
#define LCONV_DECIMAL_POINT (*lconv->decimal_point)
|
|
|
|
#else
|
|
|
|
#define LCONV_DECIMAL_POINT '.'
|
2006-12-19 19:54:38 +08:00
|
|
|
#endif
|
|
|
|
|
2002-04-10 09:09:22 +08:00
|
|
|
#include "snprintf.h"
|
|
|
|
|
|
|
|
#define FALSE 0
|
|
|
|
#define TRUE 1
|
|
|
|
#define NUL '\0'
|
|
|
|
#define INT_NULL ((int *)0)
|
|
|
|
|
|
|
|
#define S_NULL "(null)"
|
|
|
|
#define S_NULL_LEN 6
|
|
|
|
|
|
|
|
#define FLOAT_DIGITS 6
|
|
|
|
#define EXPONENT_LENGTH 10
|
|
|
|
|
2003-02-13 03:38:10 +08:00
|
|
|
#include "ext/standard/php_smart_str.h"
|
|
|
|
|
2007-08-03 17:51:12 +08:00
|
|
|
/* {{{ macros */
|
|
|
|
|
2002-04-10 09:09:22 +08:00
|
|
|
/*
|
|
|
|
* NUM_BUF_SIZE is the size of the buffer used for arithmetic conversions
|
|
|
|
*
|
|
|
|
* XXX: this is a magic number; do not decrease it
|
2011-07-11 17:47:59 +08:00
|
|
|
* Emax = 1023
|
|
|
|
* NDIG = 320
|
|
|
|
* NUM_BUF_SIZE >= strlen("-") + Emax + strlrn(".") + NDIG + strlen("E+1023") + 1;
|
2002-04-10 09:09:22 +08:00
|
|
|
*/
|
2011-07-11 17:47:59 +08:00
|
|
|
#define NUM_BUF_SIZE 2048
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
/*
|
2003-02-13 03:38:10 +08:00
|
|
|
* The INS_CHAR macro inserts a character in the buffer.
|
2002-04-10 09:09:22 +08:00
|
|
|
*
|
2003-02-13 03:38:10 +08:00
|
|
|
* NOTE: Evaluation of the ch argument should not have any side-effects
|
2002-04-10 09:09:22 +08:00
|
|
|
*/
|
2003-02-13 03:38:10 +08:00
|
|
|
#define INS_CHAR_NR(xbuf, ch) do { \
|
|
|
|
smart_str_appendc(xbuf, ch); \
|
|
|
|
} while (0)
|
2003-02-12 04:30:37 +08:00
|
|
|
|
2003-02-13 03:38:10 +08:00
|
|
|
#define INS_STRING(xbuf, s, slen) do { \
|
|
|
|
smart_str_appendl(xbuf, s, slen); \
|
|
|
|
} while (0)
|
2008-02-07 20:47:44 +08:00
|
|
|
|
2003-02-13 03:38:10 +08:00
|
|
|
#define INS_CHAR(xbuf, ch) \
|
|
|
|
INS_CHAR_NR(xbuf, ch)
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Macro that does padding. The padding is done by printing
|
|
|
|
* the character ch.
|
|
|
|
*/
|
2014-02-18 17:42:46 +08:00
|
|
|
#define PAD(xbuf, count, ch) do { \
|
|
|
|
if ((count) > 0) { \
|
|
|
|
size_t newlen; \
|
|
|
|
smart_str_alloc(xbuf, (count), 0); \
|
|
|
|
memset(xbuf->s->val + xbuf->s->len, ch, (count)); \
|
|
|
|
xbuf->s->len += (count); \
|
|
|
|
} \
|
2003-02-13 03:38:10 +08:00
|
|
|
} while (0)
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
#define NUM(c) (c - '0')
|
|
|
|
|
2003-02-13 03:38:10 +08:00
|
|
|
#define STR_TO_DEC(str, num) do { \
|
|
|
|
num = NUM(*str++); \
|
|
|
|
while (isdigit((int)*str)) { \
|
|
|
|
num *= 10; \
|
|
|
|
num += NUM(*str++); \
|
2004-03-09 07:11:45 +08:00
|
|
|
if (num >= INT_MAX / 10) { \
|
2003-02-13 03:38:10 +08:00
|
|
|
while (isdigit((int)*str++)); \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This macro does zero padding so that the precision
|
|
|
|
* requirement is satisfied. The padding is done by
|
|
|
|
* adding '0's to the left of the string that is going
|
|
|
|
* to be printed.
|
|
|
|
*/
|
2003-02-13 03:38:10 +08:00
|
|
|
#define FIX_PRECISION(adjust, precision, s, s_len) do { \
|
|
|
|
if (adjust) \
|
|
|
|
while (s_len < precision) { \
|
|
|
|
*--s = '0'; \
|
|
|
|
s_len++; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2002-04-10 09:09:22 +08:00
|
|
|
|
2007-08-03 17:51:12 +08:00
|
|
|
/* }}} */
|
2002-04-10 09:09:22 +08:00
|
|
|
|
2008-12-13 07:43:18 +08:00
|
|
|
|
|
|
|
#if !HAVE_STRNLEN
|
|
|
|
static size_t strnlen(const char *s, size_t maxlen) {
|
|
|
|
char *r = memchr(s, '\0', maxlen);
|
|
|
|
return r ? r-s : maxlen;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-04-10 09:09:22 +08:00
|
|
|
/*
|
|
|
|
* Do format conversion placing the output in buffer
|
|
|
|
*/
|
2007-08-03 17:51:12 +08:00
|
|
|
static void xbuf_format_converter(smart_str *xbuf, const char *fmt, va_list ap) /* {{{ */
|
2002-04-10 09:09:22 +08:00
|
|
|
{
|
2008-02-08 02:41:35 +08:00
|
|
|
char *s = NULL;
|
|
|
|
int s_len, free_zcopy;
|
|
|
|
zval *zvp, zcopy;
|
2002-04-10 09:09:22 +08:00
|
|
|
|
2008-02-08 02:41:35 +08:00
|
|
|
int min_width = 0;
|
2002-04-10 09:09:22 +08:00
|
|
|
int precision = 0;
|
|
|
|
enum {
|
|
|
|
LEFT, RIGHT
|
|
|
|
} adjust;
|
|
|
|
char pad_char;
|
|
|
|
char prefix_char;
|
|
|
|
|
|
|
|
double fp_num;
|
|
|
|
wide_int i_num = (wide_int) 0;
|
2013-01-13 22:45:15 +08:00
|
|
|
u_wide_int ui_num = (u_wide_int) 0;
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
char num_buf[NUM_BUF_SIZE];
|
|
|
|
char char_buf[2]; /* for printing %% and %<unknown> */
|
|
|
|
|
2006-12-19 21:13:29 +08:00
|
|
|
#ifdef HAVE_LOCALE_H
|
2006-12-19 19:54:38 +08:00
|
|
|
struct lconv *lconv = NULL;
|
2006-12-19 21:13:29 +08:00
|
|
|
#endif
|
2006-12-19 19:54:38 +08:00
|
|
|
|
2002-04-10 09:09:22 +08:00
|
|
|
/*
|
|
|
|
* Flag variables
|
|
|
|
*/
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
length_modifier_e modifier;
|
2002-04-10 09:09:22 +08:00
|
|
|
boolean_e alternate_form;
|
|
|
|
boolean_e print_sign;
|
|
|
|
boolean_e print_blank;
|
|
|
|
boolean_e adjust_precision;
|
|
|
|
boolean_e adjust_width;
|
|
|
|
bool_int is_negative;
|
|
|
|
|
|
|
|
while (*fmt) {
|
|
|
|
if (*fmt != '%') {
|
2003-02-13 03:38:10 +08:00
|
|
|
INS_CHAR(xbuf, *fmt);
|
2002-04-10 09:09:22 +08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Default variable settings
|
|
|
|
*/
|
|
|
|
adjust = RIGHT;
|
|
|
|
alternate_form = print_sign = print_blank = NO;
|
|
|
|
pad_char = ' ';
|
|
|
|
prefix_char = NUL;
|
2008-02-08 02:41:35 +08:00
|
|
|
free_zcopy = 0;
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
fmt++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to avoid checking for flags, width or precision
|
|
|
|
*/
|
|
|
|
if (isascii((int)*fmt) && !islower((int)*fmt)) {
|
|
|
|
/*
|
|
|
|
* Recognize flags: -, #, BLANK, +
|
|
|
|
*/
|
|
|
|
for (;; fmt++) {
|
|
|
|
if (*fmt == '-')
|
|
|
|
adjust = LEFT;
|
|
|
|
else if (*fmt == '+')
|
|
|
|
print_sign = YES;
|
|
|
|
else if (*fmt == '#')
|
|
|
|
alternate_form = YES;
|
|
|
|
else if (*fmt == ' ')
|
|
|
|
print_blank = YES;
|
|
|
|
else if (*fmt == '0')
|
|
|
|
pad_char = '0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if a width was specified
|
|
|
|
*/
|
|
|
|
if (isdigit((int)*fmt)) {
|
|
|
|
STR_TO_DEC(fmt, min_width);
|
|
|
|
adjust_width = YES;
|
|
|
|
} else if (*fmt == '*') {
|
|
|
|
min_width = va_arg(ap, int);
|
|
|
|
fmt++;
|
|
|
|
adjust_width = YES;
|
|
|
|
if (min_width < 0) {
|
|
|
|
adjust = LEFT;
|
|
|
|
min_width = -min_width;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
adjust_width = NO;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if a precision was specified
|
|
|
|
*/
|
|
|
|
if (*fmt == '.') {
|
|
|
|
adjust_precision = YES;
|
|
|
|
fmt++;
|
|
|
|
if (isdigit((int)*fmt)) {
|
|
|
|
STR_TO_DEC(fmt, precision);
|
|
|
|
} else if (*fmt == '*') {
|
|
|
|
precision = va_arg(ap, int);
|
|
|
|
fmt++;
|
|
|
|
if (precision < 0)
|
|
|
|
precision = 0;
|
|
|
|
} else
|
|
|
|
precision = 0;
|
2011-02-21 14:53:24 +08:00
|
|
|
|
|
|
|
if (precision > FORMAT_CONV_MAX_PRECISION) {
|
|
|
|
precision = FORMAT_CONV_MAX_PRECISION;
|
|
|
|
}
|
2002-04-10 09:09:22 +08:00
|
|
|
} else
|
|
|
|
adjust_precision = NO;
|
|
|
|
} else
|
|
|
|
adjust_precision = adjust_width = NO;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Modifier check
|
|
|
|
*/
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
switch (*fmt) {
|
2003-09-14 17:50:36 +08:00
|
|
|
case 'L':
|
|
|
|
fmt++;
|
|
|
|
modifier = LM_LONG_DOUBLE;
|
|
|
|
break;
|
2007-08-03 17:48:58 +08:00
|
|
|
case 'I':
|
|
|
|
fmt++;
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
if (*fmt == '6' && *(fmt+1) == '4') {
|
|
|
|
fmt += 2;
|
|
|
|
modifier = LM_LONG_LONG;
|
|
|
|
} else
|
|
|
|
#endif
|
2007-08-03 22:31:28 +08:00
|
|
|
if (*fmt == '3' && *(fmt+1) == '2') {
|
|
|
|
fmt += 2;
|
|
|
|
modifier = LM_LONG;
|
|
|
|
} else {
|
|
|
|
#ifdef _WIN64
|
|
|
|
modifier = LM_LONG_LONG;
|
|
|
|
#else
|
|
|
|
modifier = LM_LONG;
|
|
|
|
#endif
|
|
|
|
}
|
2007-08-03 17:48:58 +08:00
|
|
|
break;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
case 'l':
|
|
|
|
fmt++;
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
if (*fmt == 'l') {
|
|
|
|
fmt++;
|
|
|
|
modifier = LM_LONG_LONG;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
modifier = LM_LONG;
|
|
|
|
break;
|
|
|
|
case 'z':
|
2003-09-14 17:50:36 +08:00
|
|
|
fmt++;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
modifier = LM_SIZE_T;
|
|
|
|
break;
|
|
|
|
case 'j':
|
2003-09-14 17:50:36 +08:00
|
|
|
fmt++;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
#if SIZEOF_INTMAX_T
|
|
|
|
modifier = LM_INTMAX_T;
|
|
|
|
#else
|
|
|
|
modifier = LM_SIZE_T;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 't':
|
2003-09-14 17:50:36 +08:00
|
|
|
fmt++;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
#if SIZEOF_PTRDIFF_T
|
|
|
|
modifier = LM_PTRDIFF_T;
|
|
|
|
#else
|
|
|
|
modifier = LM_SIZE_T;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
fmt++;
|
|
|
|
if (*fmt == 'h') {
|
|
|
|
fmt++;
|
|
|
|
}
|
|
|
|
/* these are promoted to int, so no break */
|
2008-02-07 20:47:44 +08:00
|
|
|
default:
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
modifier = LM_STD;
|
|
|
|
break;
|
2003-09-14 00:49:24 +08:00
|
|
|
}
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Argument extraction and printing.
|
|
|
|
* First we determine the argument type.
|
|
|
|
* Then, we convert the argument to a string.
|
|
|
|
* On exit from the switch, s points to the string that
|
|
|
|
* must be printed, s_len has the length of the string
|
|
|
|
* The precision requirements, if any, are reflected in s_len.
|
|
|
|
*
|
|
|
|
* NOTE: pad_char may be set to '0' because of the 0 flag.
|
|
|
|
* It is reset to ' ' by non-numeric formats
|
|
|
|
*/
|
|
|
|
switch (*fmt) {
|
2008-02-08 02:41:35 +08:00
|
|
|
case 'Z':
|
|
|
|
zvp = (zval*) va_arg(ap, zval*);
|
|
|
|
zend_make_printable_zval(zvp, &zcopy, &free_zcopy);
|
|
|
|
if (free_zcopy) {
|
|
|
|
zvp = &zcopy;
|
|
|
|
}
|
|
|
|
s_len = Z_STRLEN_P(zvp);
|
|
|
|
s = Z_STRVAL_P(zvp);
|
|
|
|
if (adjust_precision && precision < s_len) {
|
|
|
|
s_len = precision;
|
|
|
|
}
|
|
|
|
break;
|
2002-04-10 09:09:22 +08:00
|
|
|
case 'u':
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
switch(modifier) {
|
|
|
|
default:
|
|
|
|
i_num = (wide_int) va_arg(ap, unsigned int);
|
|
|
|
break;
|
2003-09-14 17:50:36 +08:00
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
goto fmt_error;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
case LM_LONG:
|
|
|
|
i_num = (wide_int) va_arg(ap, unsigned long int);
|
|
|
|
break;
|
|
|
|
case LM_SIZE_T:
|
|
|
|
i_num = (wide_int) va_arg(ap, size_t);
|
|
|
|
break;
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
case LM_LONG_LONG:
|
|
|
|
i_num = (wide_int) va_arg(ap, u_wide_int);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_INTMAX_T
|
|
|
|
case LM_INTMAX_T:
|
|
|
|
i_num = (wide_int) va_arg(ap, uintmax_t);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_PTRDIFF_T
|
|
|
|
case LM_PTRDIFF_T:
|
|
|
|
i_num = (wide_int) va_arg(ap, ptrdiff_t);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
2002-04-10 09:09:22 +08:00
|
|
|
/*
|
|
|
|
* The rest also applies to other integer formats, so fall
|
|
|
|
* into that case.
|
|
|
|
*/
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
/*
|
|
|
|
* Get the arg if we haven't already.
|
|
|
|
*/
|
|
|
|
if ((*fmt) != 'u') {
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
switch(modifier) {
|
|
|
|
default:
|
|
|
|
i_num = (wide_int) va_arg(ap, int);
|
|
|
|
break;
|
2003-09-14 17:50:36 +08:00
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
goto fmt_error;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
case LM_LONG:
|
|
|
|
i_num = (wide_int) va_arg(ap, long int);
|
|
|
|
break;
|
|
|
|
case LM_SIZE_T:
|
|
|
|
#if SIZEOF_SSIZE_T
|
|
|
|
i_num = (wide_int) va_arg(ap, ssize_t);
|
|
|
|
#else
|
|
|
|
i_num = (wide_int) va_arg(ap, size_t);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
case LM_LONG_LONG:
|
|
|
|
i_num = (wide_int) va_arg(ap, wide_int);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_INTMAX_T
|
|
|
|
case LM_INTMAX_T:
|
|
|
|
i_num = (wide_int) va_arg(ap, intmax_t);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_PTRDIFF_T
|
|
|
|
case LM_PTRDIFF_T:
|
|
|
|
i_num = (wide_int) va_arg(ap, ptrdiff_t);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2002-04-10 09:09:22 +08:00
|
|
|
s = ap_php_conv_10(i_num, (*fmt) == 'u', &is_negative,
|
|
|
|
&num_buf[NUM_BUF_SIZE], &s_len);
|
|
|
|
FIX_PRECISION(adjust_precision, precision, s, s_len);
|
|
|
|
|
|
|
|
if (*fmt != 'u') {
|
|
|
|
if (is_negative)
|
|
|
|
prefix_char = '-';
|
|
|
|
else if (print_sign)
|
|
|
|
prefix_char = '+';
|
|
|
|
else if (print_blank)
|
|
|
|
prefix_char = ' ';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'o':
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
switch(modifier) {
|
|
|
|
default:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, unsigned int);
|
|
|
|
break;
|
2003-09-14 17:50:36 +08:00
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
goto fmt_error;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
case LM_LONG:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, unsigned long int);
|
|
|
|
break;
|
|
|
|
case LM_SIZE_T:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, size_t);
|
|
|
|
break;
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
case LM_LONG_LONG:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, u_wide_int);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_INTMAX_T
|
|
|
|
case LM_INTMAX_T:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, uintmax_t);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_PTRDIFF_T
|
|
|
|
case LM_PTRDIFF_T:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, ptrdiff_t);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
2002-04-10 09:09:22 +08:00
|
|
|
s = ap_php_conv_p2(ui_num, 3, *fmt,
|
|
|
|
&num_buf[NUM_BUF_SIZE], &s_len);
|
|
|
|
FIX_PRECISION(adjust_precision, precision, s, s_len);
|
|
|
|
if (alternate_form && *s != '0') {
|
|
|
|
*--s = '0';
|
|
|
|
s_len++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
switch(modifier) {
|
|
|
|
default:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, unsigned int);
|
|
|
|
break;
|
2003-09-14 17:50:36 +08:00
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
goto fmt_error;
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
case LM_LONG:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, unsigned long int);
|
|
|
|
break;
|
|
|
|
case LM_SIZE_T:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, size_t);
|
|
|
|
break;
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
case LM_LONG_LONG:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, u_wide_int);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_INTMAX_T
|
|
|
|
case LM_INTMAX_T:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, uintmax_t);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_PTRDIFF_T
|
|
|
|
case LM_PTRDIFF_T:
|
|
|
|
ui_num = (u_wide_int) va_arg(ap, ptrdiff_t);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
2002-04-10 09:09:22 +08:00
|
|
|
s = ap_php_conv_p2(ui_num, 4, *fmt,
|
|
|
|
&num_buf[NUM_BUF_SIZE], &s_len);
|
|
|
|
FIX_PRECISION(adjust_precision, precision, s, s_len);
|
2013-01-13 22:45:15 +08:00
|
|
|
if (alternate_form && ui_num != 0) {
|
2002-04-10 09:09:22 +08:00
|
|
|
*--s = *fmt; /* 'x' or 'X' */
|
|
|
|
*--s = '0';
|
|
|
|
s_len += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 's':
|
2006-05-07 20:40:17 +08:00
|
|
|
case 'v':
|
2002-04-10 09:09:22 +08:00
|
|
|
s = va_arg(ap, char *);
|
|
|
|
if (s != NULL) {
|
2008-12-13 07:43:18 +08:00
|
|
|
if (!adjust_precision) {
|
|
|
|
s_len = strlen(s);
|
|
|
|
} else {
|
|
|
|
s_len = strnlen(s, precision);
|
|
|
|
}
|
2002-04-10 09:09:22 +08:00
|
|
|
} else {
|
|
|
|
s = S_NULL;
|
|
|
|
s_len = S_NULL_LEN;
|
|
|
|
}
|
|
|
|
pad_char = ' ';
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'f':
|
2006-12-19 19:54:38 +08:00
|
|
|
case 'F':
|
2002-04-10 09:09:22 +08:00
|
|
|
case 'e':
|
|
|
|
case 'E':
|
2003-09-14 17:50:36 +08:00
|
|
|
switch(modifier) {
|
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
fp_num = (double) va_arg(ap, long double);
|
|
|
|
break;
|
|
|
|
case LM_STD:
|
|
|
|
fp_num = va_arg(ap, double);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fmt_error;
|
|
|
|
}
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
if (zend_isnan(fp_num)) {
|
|
|
|
s = "nan";
|
|
|
|
s_len = 3;
|
|
|
|
} else if (zend_isinf(fp_num)) {
|
|
|
|
s = "inf";
|
|
|
|
s_len = 3;
|
|
|
|
} else {
|
2006-12-19 21:13:29 +08:00
|
|
|
#ifdef HAVE_LOCALE_H
|
2006-12-19 19:54:38 +08:00
|
|
|
if (!lconv) {
|
|
|
|
lconv = localeconv();
|
|
|
|
}
|
2006-12-19 21:13:29 +08:00
|
|
|
#endif
|
2006-12-19 19:54:38 +08:00
|
|
|
s = php_conv_fp((*fmt == 'f')?'F':*fmt, fp_num, alternate_form,
|
2006-12-06 17:52:51 +08:00
|
|
|
(adjust_precision == NO) ? FLOAT_DIGITS : precision,
|
2007-06-19 20:20:50 +08:00
|
|
|
(*fmt == 'f')?LCONV_DECIMAL_POINT:'.',
|
2002-04-10 09:09:22 +08:00
|
|
|
&is_negative, &num_buf[1], &s_len);
|
|
|
|
if (is_negative)
|
|
|
|
prefix_char = '-';
|
|
|
|
else if (print_sign)
|
|
|
|
prefix_char = '+';
|
|
|
|
else if (print_blank)
|
|
|
|
prefix_char = ' ';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'g':
|
2007-10-01 23:22:41 +08:00
|
|
|
case 'k':
|
2002-04-10 09:09:22 +08:00
|
|
|
case 'G':
|
2007-06-19 20:20:50 +08:00
|
|
|
case 'H':
|
2003-09-14 17:50:36 +08:00
|
|
|
switch(modifier) {
|
|
|
|
case LM_LONG_DOUBLE:
|
|
|
|
fp_num = (double) va_arg(ap, long double);
|
|
|
|
break;
|
|
|
|
case LM_STD:
|
|
|
|
fp_num = va_arg(ap, double);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fmt_error;
|
|
|
|
}
|
2003-09-29 09:07:55 +08:00
|
|
|
|
|
|
|
if (zend_isnan(fp_num)) {
|
|
|
|
s = "NAN";
|
|
|
|
s_len = 3;
|
|
|
|
break;
|
|
|
|
} else if (zend_isinf(fp_num)) {
|
|
|
|
if (fp_num > 0) {
|
|
|
|
s = "INF";
|
|
|
|
s_len = 3;
|
|
|
|
} else {
|
|
|
|
s = "-INF";
|
|
|
|
s_len = 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-04-10 09:09:22 +08:00
|
|
|
if (adjust_precision == NO)
|
|
|
|
precision = FLOAT_DIGITS;
|
|
|
|
else if (precision == 0)
|
|
|
|
precision = 1;
|
|
|
|
/*
|
|
|
|
* * We use &num_buf[ 1 ], so that we have room for the sign
|
|
|
|
*/
|
2006-12-19 21:13:29 +08:00
|
|
|
#ifdef HAVE_LOCALE_H
|
2006-12-19 19:54:38 +08:00
|
|
|
if (!lconv) {
|
|
|
|
lconv = localeconv();
|
|
|
|
}
|
2006-12-19 21:13:29 +08:00
|
|
|
#endif
|
2007-10-01 23:22:41 +08:00
|
|
|
s = php_gcvt(fp_num, precision, (*fmt=='H' || *fmt == 'k') ? '.' : LCONV_DECIMAL_POINT, (*fmt == 'G' || *fmt == 'H')?'E':'e', &num_buf[1]);
|
2002-04-10 09:09:22 +08:00
|
|
|
if (*s == '-')
|
|
|
|
prefix_char = *s++;
|
|
|
|
else if (print_sign)
|
|
|
|
prefix_char = '+';
|
|
|
|
else if (print_blank)
|
|
|
|
prefix_char = ' ';
|
|
|
|
|
|
|
|
s_len = strlen(s);
|
|
|
|
|
2011-08-09 10:37:02 +08:00
|
|
|
if (alternate_form && (strchr(s, '.')) == NULL)
|
2002-04-10 09:09:22 +08:00
|
|
|
s[s_len++] = '.';
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
char_buf[0] = (char) (va_arg(ap, int));
|
|
|
|
s = &char_buf[0];
|
|
|
|
s_len = 1;
|
|
|
|
pad_char = ' ';
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case '%':
|
|
|
|
char_buf[0] = '%';
|
|
|
|
s = &char_buf[0];
|
|
|
|
s_len = 1;
|
|
|
|
pad_char = ' ';
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'n':
|
2014-02-18 17:42:46 +08:00
|
|
|
*(va_arg(ap, int *)) = xbuf->s->len;
|
2006-01-25 04:59:46 +08:00
|
|
|
goto skip_output;
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
/*
|
2008-02-07 20:47:44 +08:00
|
|
|
* Always extract the argument as a "char *" pointer. We
|
|
|
|
* should be using "void *" but there are still machines
|
2002-04-10 09:09:22 +08:00
|
|
|
* that don't understand it.
|
|
|
|
* If the pointer size is equal to the size of an unsigned
|
2008-02-07 20:47:44 +08:00
|
|
|
* integer we convert the pointer to a hex number, otherwise
|
2002-04-10 09:09:22 +08:00
|
|
|
* we print "%p" to indicate that we don't handle "%p".
|
|
|
|
*/
|
|
|
|
case 'p':
|
2003-09-14 00:49:24 +08:00
|
|
|
if (sizeof(char *) <= sizeof(u_wide_int)) {
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
ui_num = (u_wide_int)((size_t) va_arg(ap, char *));
|
2008-02-07 20:47:44 +08:00
|
|
|
s = ap_php_conv_p2(ui_num, 4, 'x',
|
2003-09-14 00:49:24 +08:00
|
|
|
&num_buf[NUM_BUF_SIZE], &s_len);
|
2004-04-16 07:04:49 +08:00
|
|
|
if (ui_num != 0) {
|
2003-09-14 00:49:24 +08:00
|
|
|
*--s = 'x';
|
|
|
|
*--s = '0';
|
|
|
|
s_len += 2;
|
|
|
|
}
|
|
|
|
} else {
|
2002-04-10 09:09:22 +08:00
|
|
|
s = "%p";
|
|
|
|
s_len = 2;
|
|
|
|
}
|
|
|
|
pad_char = ' ';
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case NUL:
|
|
|
|
/*
|
|
|
|
* The last character of the format string was %.
|
|
|
|
* We ignore it.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
2003-09-14 17:50:36 +08:00
|
|
|
fmt_error:
|
|
|
|
php_error(E_ERROR, "Illegal length modifier specified '%c' in s[np]printf call", *fmt);
|
2002-04-10 09:09:22 +08:00
|
|
|
/*
|
|
|
|
* The default case is for unrecognized %'s.
|
|
|
|
* We print %<char> to help the user identify what
|
|
|
|
* option is not understood.
|
|
|
|
* This is also useful in case the user wants to pass
|
|
|
|
* the output of format_converter to another function
|
|
|
|
* that understands some other %<char> (like syslog).
|
|
|
|
* Note that we can't point s inside fmt because the
|
|
|
|
* unknown <char> could be preceded by width etc.
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
char_buf[0] = '%';
|
|
|
|
char_buf[1] = *fmt;
|
|
|
|
s = char_buf;
|
|
|
|
s_len = 2;
|
|
|
|
pad_char = ' ';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefix_char != NUL) {
|
|
|
|
*--s = prefix_char;
|
|
|
|
s_len++;
|
|
|
|
}
|
|
|
|
if (adjust_width && adjust == RIGHT && min_width > s_len) {
|
|
|
|
if (pad_char == '0' && prefix_char != NUL) {
|
2003-02-13 03:38:10 +08:00
|
|
|
INS_CHAR(xbuf, *s);
|
|
|
|
s++;
|
2002-04-10 09:09:22 +08:00
|
|
|
s_len--;
|
|
|
|
min_width--;
|
|
|
|
}
|
2003-02-13 03:38:10 +08:00
|
|
|
PAD(xbuf, min_width - s_len, pad_char);
|
2002-04-10 09:09:22 +08:00
|
|
|
}
|
|
|
|
/*
|
2008-02-07 20:47:44 +08:00
|
|
|
* Print the string s.
|
2002-04-10 09:09:22 +08:00
|
|
|
*/
|
2003-02-13 03:38:10 +08:00
|
|
|
INS_STRING(xbuf, s, s_len);
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
if (adjust_width && adjust == LEFT && min_width > s_len)
|
2003-02-13 03:38:10 +08:00
|
|
|
PAD(xbuf, min_width - s_len, pad_char);
|
2008-02-08 02:41:35 +08:00
|
|
|
if (free_zcopy) {
|
|
|
|
zval_dtor(&zcopy);
|
|
|
|
}
|
2002-04-10 09:09:22 +08:00
|
|
|
}
|
2006-01-25 04:59:46 +08:00
|
|
|
skip_output:
|
2002-04-10 09:09:22 +08:00
|
|
|
fmt++;
|
|
|
|
}
|
2003-02-13 03:38:10 +08:00
|
|
|
return;
|
2002-04-10 09:09:22 +08:00
|
|
|
}
|
2007-08-03 17:51:12 +08:00
|
|
|
/* }}} */
|
2002-04-10 09:09:22 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the general purpose conversion function.
|
|
|
|
*/
|
2007-08-03 17:51:12 +08:00
|
|
|
PHPAPI int vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap) /* {{{ */
|
2002-04-10 09:09:22 +08:00
|
|
|
{
|
2003-02-13 03:38:10 +08:00
|
|
|
smart_str xbuf = {0};
|
2014-02-18 17:42:46 +08:00
|
|
|
int result;
|
2002-04-10 09:09:22 +08:00
|
|
|
|
2003-02-13 03:38:10 +08:00
|
|
|
xbuf_format_converter(&xbuf, format, ap);
|
2008-02-07 20:47:44 +08:00
|
|
|
|
2014-02-18 17:42:46 +08:00
|
|
|
if (max_len && xbuf.s->len > max_len) {
|
|
|
|
xbuf.s->len = max_len;
|
2002-04-10 09:09:22 +08:00
|
|
|
}
|
2003-02-13 03:38:10 +08:00
|
|
|
smart_str_0(&xbuf);
|
2008-02-07 20:47:44 +08:00
|
|
|
|
2014-02-18 17:42:46 +08:00
|
|
|
*pbuf = estrndup(xbuf.s->val, xbuf.s->len);
|
|
|
|
result = xbuf.s->len;
|
|
|
|
smart_str_free(&xbuf);
|
2008-02-07 20:47:44 +08:00
|
|
|
|
2014-02-18 17:42:46 +08:00
|
|
|
return result;
|
2002-04-10 09:09:22 +08:00
|
|
|
}
|
2007-08-03 17:51:12 +08:00
|
|
|
/* }}} */
|
2002-04-10 09:09:22 +08:00
|
|
|
|
2007-08-03 17:51:12 +08:00
|
|
|
PHPAPI int spprintf(char **pbuf, size_t max_len, const char *format, ...) /* {{{ */
|
2002-04-10 09:09:22 +08:00
|
|
|
{
|
|
|
|
int cc;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2002-04-10 21:02:53 +08:00
|
|
|
cc = vspprintf(pbuf, max_len, format, ap);
|
2002-04-10 09:09:22 +08:00
|
|
|
va_end(ap);
|
|
|
|
return (cc);
|
|
|
|
}
|
2007-08-03 17:51:12 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2002-04-10 09:09:22 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
|
|
|
*/
|