* don't ever print sign for unsigned conversions
* don't break when right justifying a number past the end of the buffer * handle zero precision and the value zero more correctly git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@12413 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
@@ -41,6 +41,7 @@ RCSID("$Id$");
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <roken.h>
|
#include <roken.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
enum format_flags {
|
enum format_flags {
|
||||||
minus_flag = 1,
|
minus_flag = 1,
|
||||||
@@ -123,14 +124,24 @@ typedef unsigned long u_longest;
|
|||||||
typedef long longest;
|
typedef long longest;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
|
||||||
* is # supposed to do anything?
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
pad(struct snprintf_state *state, int width, char c)
|
||||||
|
{
|
||||||
|
int len = 0;
|
||||||
|
while(width-- > 0){
|
||||||
|
(*state->append_char)(state, c);
|
||||||
|
++len;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* return true if we should use alternatve hex form */
|
||||||
static int
|
static int
|
||||||
use_alternative (int flags, u_longest num, unsigned base)
|
use_alternative (int flags, u_longest num, unsigned base)
|
||||||
{
|
{
|
||||||
return flags & alternate_flag && (base == 16 || base == 8) && num != 0;
|
return (flags & alternate_flag) && base == 16 && num != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@@ -139,77 +150,108 @@ append_number(struct snprintf_state *state,
|
|||||||
int width, int prec, int flags, int minusp)
|
int width, int prec, int flags, int minusp)
|
||||||
{
|
{
|
||||||
int len = 0;
|
int len = 0;
|
||||||
int i;
|
|
||||||
u_longest n = num;
|
u_longest n = num;
|
||||||
|
char nstr[64]; /* enough for <192 bit octal integers */
|
||||||
|
int nstart, nlen;
|
||||||
|
char signchar;
|
||||||
|
|
||||||
/* given precision, ignore zero flag */
|
/* given precision, ignore zero flag */
|
||||||
if(prec != -1)
|
if(prec != -1)
|
||||||
flags &= ~zero_flag;
|
flags &= ~zero_flag;
|
||||||
else
|
else
|
||||||
prec = 1;
|
prec = 1;
|
||||||
/* zero value with zero precision -> "" */
|
|
||||||
if(prec == 0 && n == 0)
|
/* format number as string */
|
||||||
return 0;
|
nstart = sizeof(nstr);
|
||||||
|
nlen = 0;
|
||||||
|
nstr[--nstart] = '\0';
|
||||||
do {
|
do {
|
||||||
(*state->append_char)(state, rep[n % base]);
|
assert(nstart > 0);
|
||||||
++len;
|
nstr[--nstart] = rep[n % base];
|
||||||
|
++nlen;
|
||||||
n /= base;
|
n /= base;
|
||||||
} while(n);
|
} while(n);
|
||||||
prec -= len;
|
|
||||||
/* pad with prec zeros */
|
/* zero value with zero precision should produce no digits */
|
||||||
while(prec-- > 0){
|
if(prec == 0 && num == 0) {
|
||||||
(*state->append_char)(state, '0');
|
nlen--;
|
||||||
++len;
|
nstart++;
|
||||||
}
|
}
|
||||||
/* add length of alternate prefix (added later) to len */
|
|
||||||
|
/* figure out what char to use for sign */
|
||||||
|
if(minusp)
|
||||||
|
signchar = '-';
|
||||||
|
else if((flags & plus_flag))
|
||||||
|
signchar = '+';
|
||||||
|
else if((flags & space_flag))
|
||||||
|
signchar = ' ';
|
||||||
|
else
|
||||||
|
signchar = '\0';
|
||||||
|
|
||||||
|
if((flags & alternate_flag) && base == 8) {
|
||||||
|
/* if necessary, increase the precision to
|
||||||
|
make first digit a zero */
|
||||||
|
|
||||||
|
/* XXX C99 claims (regarding # and %o) that "if the value and
|
||||||
|
precision are both 0, a single 0 is printed", but there is
|
||||||
|
no such wording for %x. This would mean that %#.o would
|
||||||
|
output "0", but %#.x "". This does not make sense, and is
|
||||||
|
also not what other printf implementations are doing. */
|
||||||
|
|
||||||
|
if(prec <= nlen && nstr[nstart] != '0' && nstr[nstart] != '\0')
|
||||||
|
prec = nlen + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* possible formats:
|
||||||
|
pad | sign | alt | zero | digits
|
||||||
|
sign | alt | zero | digits | pad minus_flag
|
||||||
|
sign | alt | zero | digits zero_flag */
|
||||||
|
|
||||||
|
/* if not right justifying or padding with zeros, we need to
|
||||||
|
compute the length of the rest of the string, and then pad with
|
||||||
|
spaces */
|
||||||
|
if(!(flags & (minus_flag | zero_flag))) {
|
||||||
|
if(prec > nlen)
|
||||||
|
width -= prec;
|
||||||
|
else
|
||||||
|
width -= nlen;
|
||||||
|
|
||||||
if(use_alternative(flags, num, base))
|
if(use_alternative(flags, num, base))
|
||||||
len += base / 8;
|
width -= 2;
|
||||||
/* pad with zeros */
|
|
||||||
if(flags & zero_flag){
|
if(signchar != '\0')
|
||||||
width -= len;
|
|
||||||
if(minusp || (flags & space_flag) || (flags & plus_flag))
|
|
||||||
width--;
|
width--;
|
||||||
while(width-- > 0){
|
|
||||||
(*state->append_char)(state, '0');
|
/* pad to width */
|
||||||
len++;
|
len += pad(state, width, ' ');
|
||||||
}
|
}
|
||||||
|
if(signchar != '\0') {
|
||||||
|
(*state->append_char)(state, signchar);
|
||||||
|
++len;
|
||||||
}
|
}
|
||||||
/* add alternate prefix */
|
|
||||||
if(use_alternative(flags, num, base)) {
|
if(use_alternative(flags, num, base)) {
|
||||||
if(base == 16)
|
|
||||||
(*state->append_char)(state, rep[10] + 23); /* XXX */
|
|
||||||
(*state->append_char)(state, '0');
|
(*state->append_char)(state, '0');
|
||||||
|
(*state->append_char)(state, rep[10] + 23); /* XXX */
|
||||||
|
len += 2;
|
||||||
}
|
}
|
||||||
/* add sign */
|
if(flags & zero_flag) {
|
||||||
if(minusp){
|
/* pad to width with zeros */
|
||||||
(*state->append_char)(state, '-');
|
if(prec - nlen > width - len - nlen)
|
||||||
++len;
|
len += pad(state, prec - nlen, '0');
|
||||||
} else if(flags & plus_flag) {
|
else
|
||||||
(*state->append_char)(state, '+');
|
len += pad(state, width - len - nlen, '0');
|
||||||
++len;
|
} else
|
||||||
} else if(flags & space_flag) {
|
/* pad to prec with zeros */
|
||||||
(*state->append_char)(state, ' ');
|
len += pad(state, prec - nlen, '0');
|
||||||
|
|
||||||
|
while(nstr[nstart] != '\0') {
|
||||||
|
(*state->append_char)(state, nstr[nstart++]);
|
||||||
++len;
|
++len;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(flags & minus_flag)
|
if(flags & minus_flag)
|
||||||
/* swap before padding with spaces */
|
len += pad(state, width - len, ' ');
|
||||||
for(i = 0; i < len / 2; i++){
|
|
||||||
char c = state->s[-i-1];
|
|
||||||
state->s[-i-1] = state->s[-len+i];
|
|
||||||
state->s[-len+i] = c;
|
|
||||||
}
|
|
||||||
width -= len;
|
|
||||||
while(width-- > 0){
|
|
||||||
(*state->append_char)(state, ' ');
|
|
||||||
++len;
|
|
||||||
}
|
|
||||||
if(!(flags & minus_flag))
|
|
||||||
/* swap after padding with spaces */
|
|
||||||
for(i = 0; i < len / 2; i++){
|
|
||||||
char c = state->s[-i-1];
|
|
||||||
state->s[-i-1] = state->s[-len+i];
|
|
||||||
state->s[-len+i] = c;
|
|
||||||
}
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,10 +276,8 @@ append_string (struct snprintf_state *state,
|
|||||||
else
|
else
|
||||||
width -= strlen((const char *)arg);
|
width -= strlen((const char *)arg);
|
||||||
if(!(flags & minus_flag))
|
if(!(flags & minus_flag))
|
||||||
while(width-- > 0) {
|
len += pad(state, width, ' ');
|
||||||
(*state->append_char) (state, ' ');
|
|
||||||
++len;
|
|
||||||
}
|
|
||||||
if (prec != -1) {
|
if (prec != -1) {
|
||||||
while (*arg && prec--) {
|
while (*arg && prec--) {
|
||||||
(*state->append_char) (state, *arg++);
|
(*state->append_char) (state, *arg++);
|
||||||
@@ -250,10 +290,7 @@ append_string (struct snprintf_state *state,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(flags & minus_flag)
|
if(flags & minus_flag)
|
||||||
while(width-- > 0) {
|
len += pad(state, width, ' ');
|
||||||
(*state->append_char) (state, ' ');
|
|
||||||
++len;
|
|
||||||
}
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,6 +427,9 @@ xyzprintf (struct snprintf_state *state, const char *char_format, va_list ap)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(c != 'd' && c != 'i')
|
||||||
|
flags &= ~(plus_flag | space_flag);
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'c' :
|
case 'c' :
|
||||||
append_char(state, va_arg(ap, int), width, flags);
|
append_char(state, va_arg(ap, int), width, flags);
|
||||||
|
Reference in New Issue
Block a user