mirror of
https://github.com/php/php-src.git
synced 2025-08-20 09:24:05 +02:00
Add '+' modifier.
+ A sign (+ or -) always be placed before a number produced by a signed conversion. By default a sign is used only for negative numbers. A + overrides a space if both are used.
This commit is contained in:
parent
bcadd534ee
commit
30f038be18
1 changed files with 23 additions and 8 deletions
|
@ -200,7 +200,8 @@ php_sprintf_appendstring(char **buffer, int *pos, int *size, char *add,
|
||||||
|
|
||||||
inline static void
|
inline static void
|
||||||
php_sprintf_appendint(char **buffer, int *pos, int *size, long number,
|
php_sprintf_appendint(char **buffer, int *pos, int *size, long number,
|
||||||
int width, char padding, int alignment)
|
int width, char padding, int alignment,
|
||||||
|
int always_sign)
|
||||||
{
|
{
|
||||||
char numbuf[NUM_BUF_SIZE];
|
char numbuf[NUM_BUF_SIZE];
|
||||||
register unsigned long magn, nmagn;
|
register unsigned long magn, nmagn;
|
||||||
|
@ -229,6 +230,8 @@ php_sprintf_appendint(char **buffer, int *pos, int *size, long number,
|
||||||
while (magn > 0 && i > 0);
|
while (magn > 0 && i > 0);
|
||||||
if (neg) {
|
if (neg) {
|
||||||
numbuf[--i] = '-';
|
numbuf[--i] = '-';
|
||||||
|
} else if (always_sign) {
|
||||||
|
numbuf[--i] = '+';
|
||||||
}
|
}
|
||||||
PRINTF_DEBUG(("sprintf: appending %d as \"%s\", i=%d\n",
|
PRINTF_DEBUG(("sprintf: appending %d as \"%s\", i=%d\n",
|
||||||
number, &numbuf[i], i));
|
number, &numbuf[i], i));
|
||||||
|
@ -240,7 +243,7 @@ php_sprintf_appendint(char **buffer, int *pos, int *size, long number,
|
||||||
inline static void
|
inline static void
|
||||||
php_sprintf_appenduint(char **buffer, int *pos, int *size,
|
php_sprintf_appenduint(char **buffer, int *pos, int *size,
|
||||||
unsigned long number,
|
unsigned long number,
|
||||||
int width, char padding, int alignment)
|
int width, char padding, int alignment, int always_sign)
|
||||||
{
|
{
|
||||||
char numbuf[NUM_BUF_SIZE];
|
char numbuf[NUM_BUF_SIZE];
|
||||||
register unsigned long magn, nmagn;
|
register unsigned long magn, nmagn;
|
||||||
|
@ -260,8 +263,10 @@ php_sprintf_appenduint(char **buffer, int *pos, int *size,
|
||||||
|
|
||||||
numbuf[--i] = (unsigned char)(magn - (nmagn * 10)) + '0';
|
numbuf[--i] = (unsigned char)(magn - (nmagn * 10)) + '0';
|
||||||
magn = nmagn;
|
magn = nmagn;
|
||||||
}
|
} while (magn > 0 && i > 0);
|
||||||
while (magn > 0 && i > 0);
|
|
||||||
|
if (always_sign)
|
||||||
|
numbuf[--i] = '+';
|
||||||
PRINTF_DEBUG(("sprintf: appending %d as \"%s\", i=%d\n", number, &numbuf[i], i));
|
PRINTF_DEBUG(("sprintf: appending %d as \"%s\", i=%d\n", number, &numbuf[i], i));
|
||||||
php_sprintf_appendstring(buffer, pos, size, &numbuf[i], width, 0,
|
php_sprintf_appendstring(buffer, pos, size, &numbuf[i], width, 0,
|
||||||
padding, alignment, (NUM_BUF_SIZE - 1) - i, 0, 0);
|
padding, alignment, (NUM_BUF_SIZE - 1) - i, 0, 0);
|
||||||
|
@ -272,7 +277,8 @@ php_sprintf_appenddouble(char **buffer, int *pos,
|
||||||
int *size, double number,
|
int *size, double number,
|
||||||
int width, char padding,
|
int width, char padding,
|
||||||
int alignment, int precision,
|
int alignment, int precision,
|
||||||
int adjust, char fmt)
|
int adjust, char fmt,
|
||||||
|
int always_sign)
|
||||||
{
|
{
|
||||||
char numbuf[NUM_BUF_SIZE];
|
char numbuf[NUM_BUF_SIZE];
|
||||||
char *cvt;
|
char *cvt;
|
||||||
|
@ -313,6 +319,8 @@ php_sprintf_appenddouble(char **buffer, int *pos,
|
||||||
|
|
||||||
if (sign) {
|
if (sign) {
|
||||||
numbuf[i++] = '-';
|
numbuf[i++] = '-';
|
||||||
|
} else if (always_sign) {
|
||||||
|
numbuf[i++] = '+';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fmt == 'f') {
|
if (fmt == 'f') {
|
||||||
|
@ -405,6 +413,7 @@ php_sprintf_getnumber(char *buffer, int *pos)
|
||||||
* "-" left adjusted field
|
* "-" left adjusted field
|
||||||
* n field size
|
* n field size
|
||||||
* "."n precision (floats only)
|
* "."n precision (floats only)
|
||||||
|
* "+" Always place a sign (+ or -) in front of a number
|
||||||
*
|
*
|
||||||
* Type specifiers:
|
* Type specifiers:
|
||||||
*
|
*
|
||||||
|
@ -426,6 +435,7 @@ php_formatted_print(int ht, int *len, int use_array TSRMLS_DC)
|
||||||
int argc, size = 240, inpos = 0, outpos = 0, temppos;
|
int argc, size = 240, inpos = 0, outpos = 0, temppos;
|
||||||
int alignment, width, precision, currarg, adjusting, argnum;
|
int alignment, width, precision, currarg, adjusting, argnum;
|
||||||
char *format, *result, padding;
|
char *format, *result, padding;
|
||||||
|
int always_sign;
|
||||||
|
|
||||||
argc = ZEND_NUM_ARGS();
|
argc = ZEND_NUM_ARGS();
|
||||||
|
|
||||||
|
@ -482,6 +492,7 @@ php_formatted_print(int ht, int *len, int use_array TSRMLS_DC)
|
||||||
alignment = ALIGN_RIGHT;
|
alignment = ALIGN_RIGHT;
|
||||||
adjusting = 0;
|
adjusting = 0;
|
||||||
padding = ' ';
|
padding = ' ';
|
||||||
|
always_sign = 0;
|
||||||
inpos++; /* skip the '%' */
|
inpos++; /* skip the '%' */
|
||||||
|
|
||||||
PRINTF_DEBUG(("sprintf: first looking at '%c', inpos=%d\n",
|
PRINTF_DEBUG(("sprintf: first looking at '%c', inpos=%d\n",
|
||||||
|
@ -521,6 +532,8 @@ php_formatted_print(int ht, int *len, int use_array TSRMLS_DC)
|
||||||
} else if (format[inpos] == '-') {
|
} else if (format[inpos] == '-') {
|
||||||
alignment = ALIGN_LEFT;
|
alignment = ALIGN_LEFT;
|
||||||
/* space padding, the default */
|
/* space padding, the default */
|
||||||
|
} else if (format[inpos] == '+') {
|
||||||
|
always_sign = 1;
|
||||||
} else if (format[inpos] == '\'') {
|
} else if (format[inpos] == '\'') {
|
||||||
padding = format[++inpos];
|
padding = format[++inpos];
|
||||||
} else {
|
} else {
|
||||||
|
@ -583,14 +596,16 @@ php_formatted_print(int ht, int *len, int use_array TSRMLS_DC)
|
||||||
convert_to_long_ex(args[argnum]);
|
convert_to_long_ex(args[argnum]);
|
||||||
php_sprintf_appendint(&result, &outpos, &size,
|
php_sprintf_appendint(&result, &outpos, &size,
|
||||||
Z_LVAL_PP(args[argnum]),
|
Z_LVAL_PP(args[argnum]),
|
||||||
width, padding, alignment);
|
width, padding, alignment,
|
||||||
|
always_sign);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'u':
|
case 'u':
|
||||||
convert_to_long_ex(args[argnum]);
|
convert_to_long_ex(args[argnum]);
|
||||||
php_sprintf_appenduint(&result, &outpos, &size,
|
php_sprintf_appenduint(&result, &outpos, &size,
|
||||||
Z_LVAL_PP(args[argnum]),
|
Z_LVAL_PP(args[argnum]),
|
||||||
width, padding, alignment);
|
width, padding, alignment,
|
||||||
|
always_sign);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'e':
|
case 'e':
|
||||||
|
@ -601,7 +616,7 @@ php_formatted_print(int ht, int *len, int use_array TSRMLS_DC)
|
||||||
Z_DVAL_PP(args[argnum]),
|
Z_DVAL_PP(args[argnum]),
|
||||||
width, padding, alignment,
|
width, padding, alignment,
|
||||||
precision, adjusting,
|
precision, adjusting,
|
||||||
format[inpos]);
|
format[inpos], always_sign);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'c':
|
case 'c':
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue