* 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 <ctype.h>
|
||||
#include <roken.h>
|
||||
#include <assert.h>
|
||||
|
||||
enum format_flags {
|
||||
minus_flag = 1,
|
||||
@@ -123,14 +124,24 @@ typedef unsigned long u_longest;
|
||||
typedef long longest;
|
||||
#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
|
||||
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
|
||||
@@ -139,77 +150,108 @@ append_number(struct snprintf_state *state,
|
||||
int width, int prec, int flags, int minusp)
|
||||
{
|
||||
int len = 0;
|
||||
int i;
|
||||
u_longest n = num;
|
||||
char nstr[64]; /* enough for <192 bit octal integers */
|
||||
int nstart, nlen;
|
||||
char signchar;
|
||||
|
||||
/* given precision, ignore zero flag */
|
||||
if(prec != -1)
|
||||
flags &= ~zero_flag;
|
||||
else
|
||||
prec = 1;
|
||||
/* zero value with zero precision -> "" */
|
||||
if(prec == 0 && n == 0)
|
||||
return 0;
|
||||
do{
|
||||
(*state->append_char)(state, rep[n % base]);
|
||||
++len;
|
||||
|
||||
/* format number as string */
|
||||
nstart = sizeof(nstr);
|
||||
nlen = 0;
|
||||
nstr[--nstart] = '\0';
|
||||
do {
|
||||
assert(nstart > 0);
|
||||
nstr[--nstart] = rep[n % base];
|
||||
++nlen;
|
||||
n /= base;
|
||||
} while(n);
|
||||
prec -= len;
|
||||
/* pad with prec zeros */
|
||||
while(prec-- > 0){
|
||||
(*state->append_char)(state, '0');
|
||||
++len;
|
||||
|
||||
/* zero value with zero precision should produce no digits */
|
||||
if(prec == 0 && num == 0) {
|
||||
nlen--;
|
||||
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))
|
||||
len += base / 8;
|
||||
/* pad with zeros */
|
||||
if(flags & zero_flag){
|
||||
width -= len;
|
||||
if(minusp || (flags & space_flag) || (flags & plus_flag))
|
||||
width -= 2;
|
||||
|
||||
if(signchar != '\0')
|
||||
width--;
|
||||
while(width-- > 0){
|
||||
|
||||
/* pad to width */
|
||||
len += pad(state, width, ' ');
|
||||
}
|
||||
if(signchar != '\0') {
|
||||
(*state->append_char)(state, signchar);
|
||||
++len;
|
||||
}
|
||||
if(use_alternative(flags, num, base)) {
|
||||
(*state->append_char)(state, '0');
|
||||
len++;
|
||||
}
|
||||
}
|
||||
/* add alternate prefix */
|
||||
if(use_alternative(flags, num, base)){
|
||||
if(base == 16)
|
||||
(*state->append_char)(state, rep[10] + 23); /* XXX */
|
||||
(*state->append_char)(state, '0');
|
||||
len += 2;
|
||||
}
|
||||
/* add sign */
|
||||
if(minusp){
|
||||
(*state->append_char)(state, '-');
|
||||
++len;
|
||||
} else if(flags & plus_flag) {
|
||||
(*state->append_char)(state, '+');
|
||||
++len;
|
||||
} else if(flags & space_flag) {
|
||||
(*state->append_char)(state, ' ');
|
||||
if(flags & zero_flag) {
|
||||
/* pad to width with zeros */
|
||||
if(prec - nlen > width - len - nlen)
|
||||
len += pad(state, prec - nlen, '0');
|
||||
else
|
||||
len += pad(state, width - len - nlen, '0');
|
||||
} else
|
||||
/* pad to prec with zeros */
|
||||
len += pad(state, prec - nlen, '0');
|
||||
|
||||
while(nstr[nstart] != '\0') {
|
||||
(*state->append_char)(state, nstr[nstart++]);
|
||||
++len;
|
||||
}
|
||||
|
||||
if(flags & minus_flag)
|
||||
/* swap before 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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
len += pad(state, width - len, ' ');
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -234,10 +276,8 @@ append_string (struct snprintf_state *state,
|
||||
else
|
||||
width -= strlen((const char *)arg);
|
||||
if(!(flags & minus_flag))
|
||||
while(width-- > 0) {
|
||||
(*state->append_char) (state, ' ');
|
||||
++len;
|
||||
}
|
||||
len += pad(state, width, ' ');
|
||||
|
||||
if (prec != -1) {
|
||||
while (*arg && prec--) {
|
||||
(*state->append_char) (state, *arg++);
|
||||
@@ -250,10 +290,7 @@ append_string (struct snprintf_state *state,
|
||||
}
|
||||
}
|
||||
if(flags & minus_flag)
|
||||
while(width-- > 0) {
|
||||
(*state->append_char) (state, ' ');
|
||||
++len;
|
||||
}
|
||||
len += pad(state, width, ' ');
|
||||
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) {
|
||||
case 'c' :
|
||||
append_char(state, va_arg(ap, int), width, flags);
|
||||
|
Reference in New Issue
Block a user