]>
jfr.im git - solanum.git/blob - libratbox/src/snprintf.c
3 * Modified and hacked into libratbox by Aaron Sethman <androsyn@ratbox.org>
4 * The original headers are below..
5 * Note that this implementation does not process floating point numbers so
6 * you will likely need to fall back to using sprintf yourself to do those...
7 * $Id: snprintf.c 26092 2008-09-19 15:13:52Z androsyn $
11 * linux/lib/vsprintf.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
16 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
18 * Wirzenius wrote this portably, Torvalds fucked it up :-)
22 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
23 * - changed to provide snprintf and vsnprintf functions
24 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
25 * - scnprintf and vscnprintf
27 #include <libratbox_config.h>
28 #include <ratbox_lib.h>
31 skip_atoi(const char **s
)
36 i
= i
* 10 + *((*s
)++) - '0';
40 /* Decimal conversion is by far the most typical, and is used
41 * for /proc and /sys data. This directly impacts e.g. top performance
42 * with many processes running. We optimize it for speed
44 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
45 * (with permission from the author, Douglas W. Jones). */
47 /* Formats correctly any integer in [0,99999].
48 * Outputs from one to five digits depending on input.
49 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
51 put_dec_trunc(char *buf
, unsigned q
)
53 unsigned d3
, d2
, d1
, d0
;
58 d0
= 6 * (d3
+ d2
+ d1
) + (q
& 0xf);
59 q
= (d0
* 0xcd) >> 11;
61 *buf
++ = d0
+ '0'; /* least significant digit */
62 d1
= q
+ 9 * d3
+ 5 * d2
+ d1
;
65 q
= (d1
* 0xcd) >> 11;
67 *buf
++ = d1
+ '0'; /* next digit */
70 if((d2
!= 0) || (d3
!= 0))
74 *buf
++ = d2
+ '0'; /* next digit */
79 q
= (d3
* 0xcd) >> 11;
81 *buf
++ = d3
+ '0'; /* next digit */
83 *buf
++ = q
+ '0'; /* most sign. digit */
90 /* Same with if's removed. Always emits five digits */
92 put_dec_full(char *buf
, unsigned q
)
94 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
95 /* but anyway, gcc produces better code with full-sized ints */
96 unsigned d3
, d2
, d1
, d0
;
101 /* Possible ways to approx. divide by 10 */
102 /* gcc -O2 replaces multiply with shifts and adds */
103 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
104 // (x * 0x67) >> 10: 1100111
105 // (x * 0x34) >> 9: 110100 - same
106 // (x * 0x1a) >> 8: 11010 - same
107 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
109 d0
= 6 * (d3
+ d2
+ d1
) + (q
& 0xf);
110 q
= (d0
* 0xcd) >> 11;
113 d1
= q
+ 9 * d3
+ 5 * d2
+ d1
;
114 q
= (d1
* 0xcd) >> 11;
124 q
= (d3
* 0xcd) >> 11; /* - shorter code */
125 /* q = (d3 * 0x67) >> 10; - would also work */
133 put_dec(char *buf
, unsigned long long int num
)
139 return put_dec_trunc(buf
, num
);
142 buf
= put_dec_full(buf
, rem
);
146 #define ZEROPAD 1 /* pad with zero */
147 #define SIGN 2 /* unsigned/signed long */
148 #define PLUS 4 /* show plus */
149 #define SPACE 8 /* space if plus */
150 #define LEFT 16 /* left justified */
151 #define SPECIAL 32 /* 0x */
152 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
155 number(char *const buf
, const size_t size
, size_t idx
, unsigned long long int num
, int base
, int field_width
, int precision
,
160 /* we are called with base 8, 10 or 16, only, thus don't need "g..." */
161 static const char small_digits
[] = "0123456789abcdefx"; /* "ghijklmnopqrstuvwxyz"; */
162 static const char large_digits
[] = "0123456789ABCDEFX"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
163 int need_pfx
= ((type
& SPECIAL
) && base
!= 10);
166 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
169 if(base
< 2 || base
> 36)
174 if((signed long long int)num
< 0)
177 num
= -(signed long long int)num
;
185 else if(type
& SPACE
)
198 /* generate full string in tmp[], in reverse order */
202 /* Generic code, for any base:
204 tmp[i++] = digits[do_div(num,base)];
215 tmp
[i
++] = digits
[((unsigned char)num
) & mask
];
222 i
= put_dec(tmp
, num
) - tmp
;
225 /* printing 100 using %2d gives "100", not "00" */
228 /* leading space padding */
229 field_width
-= precision
;
230 if(!(type
& (ZEROPAD
+ LEFT
)))
232 while(--field_width
>= 0)
246 /* "0x" / "0" prefix */
255 buf
[idx
] = digits
[16]; /* for arbitrary base: digits[33]; */
259 /* zero or space padding */
262 char c
= (type
& ZEROPAD
) ? '0' : ' ';
263 while(--field_width
>= 0)
270 /* hmm even more zero padding? */
271 while(i
<= --precision
)
277 /* actual digits of result */
284 /* trailing space padding */
285 while(--field_width
>= 0)
295 * vsnprintf - Format a string and place it in a buffer
296 * @buf: The buffer to place the result into
297 * @size: The size of the buffer, including the trailing null space
298 * @fmt: The format string to use
299 * @args: Arguments for the format string
301 * The return value is the number of characters which would
302 * be generated for the given input, excluding the trailing
303 * '\0', as per ISO C99. If you want to have the exact
304 * number of characters written into @buf as return value
305 * (not including the trailing '\0'), use vscnprintf(). If the
306 * return is greater than or equal to @size, the resulting
307 * string is truncated.
309 * Call this function if you are already dealing with a va_list.
310 * You probably want snprintf() instead.
313 rb_vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
316 unsigned long long int num
;
322 int flags
; /* flags to number() */
324 int field_width
; /* width of output field */
325 int precision
; /* min. # of digits for integers; max
326 number of chars for from string */
327 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
328 /* 'z' support added 23/7/1999 S.H. */
329 /* 'z' changed to 'Z' --davidm 1/25/99 */
330 /* 't' added for ptrdiff_t */
332 /* Reject out-of-range values early. Large positive sizes are
333 used for unknown buffer sizes. */
334 if(rb_unlikely(size
> INT_MAX
))
354 ++fmt
; /* this also skips first '%' */
374 /* get field width */
377 field_width
= skip_atoi(&fmt
);
381 /* it's the next argument */
382 field_width
= va_arg(args
, int);
385 field_width
= -field_width
;
390 /* get the precision */
396 precision
= skip_atoi(&fmt
);
400 /* it's the next argument */
401 precision
= va_arg(args
, int);
407 /* get the conversion qualifier */
409 if(*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
410 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't')
414 if(qualifier
== 'l' && *fmt
== 'l')
429 while(--field_width
> 0)
436 c
= (unsigned char)va_arg(args
, int);
440 while(--field_width
> 0)
449 s
= va_arg(args
, char *);
452 abort(); /* prefer blowing up vs corrupt data */
454 len
= rb_strnlen(s
, precision
);
458 while(len
< field_width
--)
465 for(i
= 0; i
< len
; ++i
)
472 while(len
< field_width
--)
481 if(field_width
== -1)
483 field_width
= 2 * sizeof(void *);
486 idx
= number(buf
, size
, idx
,
487 (unsigned long)va_arg(args
, void *),
488 16, field_width
, precision
, flags
);
494 * What does C99 say about the overflow case here? */
497 long *ip
= va_arg(args
, long *);
500 else if(qualifier
== 'Z' || qualifier
== 'z')
502 size_t *ip
= va_arg(args
, size_t *);
507 int *ip
= va_arg(args
, int *);
518 /* integer number formats - set up the flags and "break" */
552 num
= va_arg(args
, long long int);
553 else if(qualifier
== 'l')
555 num
= va_arg(args
, unsigned long);
557 num
= (signed long)num
;
559 else if(qualifier
== 'Z' || qualifier
== 'z')
561 num
= va_arg(args
, size_t);
563 else if(qualifier
== 't')
565 num
= va_arg(args
, ptrdiff_t);
567 else if(qualifier
== 'h')
569 num
= (unsigned short)va_arg(args
, int);
571 num
= (signed short)num
;
575 num
= va_arg(args
, unsigned int);
577 num
= (signed int)num
;
579 idx
= number(buf
, size
, idx
, num
, base
, field_width
, precision
, flags
);
586 buf
[size
- 1] = '\0';
588 /* the trailing null byte doesn't count towards the total */
593 * snprintf - Format a string and place it in a buffer
594 * @buf: The buffer to place the result into
595 * @size: The size of the buffer, including the trailing null space
596 * @fmt: The format string to use
597 * @...: Arguments for the format string
599 * The return value is the number of characters which would be
600 * generated for the given input, excluding the trailing null,
601 * as per ISO C99. If the return is greater than or equal to
602 * @size, the resulting string is truncated.
605 rb_snprintf(char *buf
, size_t size
, const char *fmt
, ...)
611 i
= rb_vsnprintf(buf
, size
, fmt
, args
);
617 * vsprintf - Format a string and place it in a buffer
618 * @buf: The buffer to place the result into
619 * @fmt: The format string to use
620 * @args: Arguments for the format string
622 * The function returns the number of characters written
623 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
626 * Call this function if you are already dealing with a va_list.
627 * You probably want sprintf() instead.
630 rb_vsprintf(char *buf
, const char *fmt
, va_list args
)
632 return rb_vsnprintf(buf
, INT_MAX
, fmt
, args
);
636 * sprintf - Format a string and place it in a buffer
637 * @buf: The buffer to place the result into
638 * @fmt: The format string to use
639 * @...: Arguments for the format string
641 * The function returns the number of characters written
642 * into @buf. Use snprintf() or scnprintf() in order to avoid
646 rb_sprintf(char *buf
, const char *fmt
, ...)
652 i
= rb_vsnprintf(buf
, INT_MAX
, fmt
, args
);
658 * rb_vsprintf_append()
659 * appends sprintf formatted string to the end of the buffer
663 rb_vsprintf_append(char *str
, const char *format
, va_list ap
)
665 size_t x
= strlen(str
);
666 return (rb_vsprintf(str
+ x
, format
, ap
) + x
);
670 * rb_sprintf_append()
671 * appends sprintf formatted string to the end of the buffer
674 rb_sprintf_append(char *str
, const char *format
, ...)
678 va_start(ap
, format
);
679 x
= rb_vsprintf_append(str
, format
, ap
);
685 * rb_vsnprintf_append()
686 * appends sprintf formatted string to the end of the buffer but not
691 rb_vsnprintf_append(char *str
, size_t len
, const char *format
, va_list ap
)
703 return (rb_vsnprintf(str
+ x
, len
- x
, format
, ap
) + x
);
707 * rb_snprintf_append()
708 * appends snprintf formatted string to the end of the buffer but not
713 rb_snprintf_append(char *str
, size_t len
, const char *format
, ...)
717 va_start(ap
, format
);
718 x
= rb_vsnprintf_append(str
, len
, format
, ap
);