]>
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 *buf
, char *end
, unsigned long long int num
, int base
, int size
, 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 */
230 if(!(type
& (ZEROPAD
+ LEFT
)))
246 /* "0x" / "0" prefix */
255 *buf
= digits
[16]; /* for arbitrary base: digits[33]; */
259 /* zero or space padding */
262 char c
= (type
& ZEROPAD
) ? '0' : ' ';
270 /* hmm even more zero padding? */
271 while(i
<= --precision
)
277 /* actual digits of result */
284 /* trailing space padding */
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
;
321 int flags
; /* flags to number() */
323 int field_width
; /* width of output field */
324 int precision
; /* min. # of digits for integers; max
325 number of chars for from string */
326 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
327 /* 'z' support added 23/7/1999 S.H. */
328 /* 'z' changed to 'Z' --davidm 1/25/99 */
329 /* 't' added for ptrdiff_t */
331 /* Reject out-of-range values early. Large positive sizes are
332 used for unknown buffer sizes. */
333 if(rb_unlikely((int)size
< 0))
341 /* Make sure end is always >= buf */
361 ++fmt
; /* this also skips first '%' */
381 /* get field width */
384 field_width
= skip_atoi(&fmt
);
388 /* it's the next argument */
389 field_width
= va_arg(args
, int);
392 field_width
= -field_width
;
397 /* get the precision */
403 precision
= skip_atoi(&fmt
);
407 /* it's the next argument */
408 precision
= va_arg(args
, int);
414 /* get the conversion qualifier */
416 if(*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
417 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't')
421 if(qualifier
== 'l' && *fmt
== 'l')
436 while(--field_width
> 0)
443 c
= (unsigned char)va_arg(args
, int);
447 while(--field_width
> 0)
456 s
= va_arg(args
, char *);
459 abort(); /* prefer blowing up vs corrupt data */
461 len
= rb_strnlen(s
, precision
);
465 while(len
< field_width
--)
472 for(i
= 0; i
< len
; ++i
)
479 while(len
< field_width
--)
488 if(field_width
== -1)
490 field_width
= 2 * sizeof(void *);
493 str
= number(str
, end
,
494 (unsigned long)va_arg(args
, void *),
495 16, field_width
, precision
, flags
);
501 * What does C99 say about the overflow case here? */
504 long *ip
= va_arg(args
, long *);
507 else if(qualifier
== 'Z' || qualifier
== 'z')
509 size_t *ip
= va_arg(args
, size_t *);
514 int *ip
= va_arg(args
, int *);
525 /* integer number formats - set up the flags and "break" */
559 num
= va_arg(args
, long long int);
560 else if(qualifier
== 'l')
562 num
= va_arg(args
, unsigned long);
564 num
= (signed long)num
;
566 else if(qualifier
== 'Z' || qualifier
== 'z')
568 num
= va_arg(args
, size_t);
570 else if(qualifier
== 't')
572 num
= va_arg(args
, ptrdiff_t);
574 else if(qualifier
== 'h')
576 num
= (unsigned short)va_arg(args
, int);
578 num
= (signed short)num
;
582 num
= va_arg(args
, unsigned int);
584 num
= (signed int)num
;
586 str
= number(str
, end
, num
, base
, field_width
, precision
, flags
);
595 /* the trailing null byte doesn't count towards the total */
600 * snprintf - Format a string and place it in a buffer
601 * @buf: The buffer to place the result into
602 * @size: The size of the buffer, including the trailing null space
603 * @fmt: The format string to use
604 * @...: Arguments for the format string
606 * The return value is the number of characters which would be
607 * generated for the given input, excluding the trailing null,
608 * as per ISO C99. If the return is greater than or equal to
609 * @size, the resulting string is truncated.
612 rb_snprintf(char *buf
, size_t size
, const char *fmt
, ...)
618 i
= rb_vsnprintf(buf
, size
, fmt
, args
);
624 * vsprintf - Format a string and place it in a buffer
625 * @buf: The buffer to place the result into
626 * @fmt: The format string to use
627 * @args: Arguments for the format string
629 * The function returns the number of characters written
630 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
633 * Call this function if you are already dealing with a va_list.
634 * You probably want sprintf() instead.
637 rb_vsprintf(char *buf
, const char *fmt
, va_list args
)
639 return rb_vsnprintf(buf
, INT_MAX
, fmt
, args
);
643 * sprintf - Format a string and place it in a buffer
644 * @buf: The buffer to place the result into
645 * @fmt: The format string to use
646 * @...: Arguments for the format string
648 * The function returns the number of characters written
649 * into @buf. Use snprintf() or scnprintf() in order to avoid
653 rb_sprintf(char *buf
, const char *fmt
, ...)
659 i
= rb_vsnprintf(buf
, INT_MAX
, fmt
, args
);
665 * rb_vsprintf_append()
666 * appends sprintf formatted string to the end of the buffer
670 rb_vsprintf_append(char *str
, const char *format
, va_list ap
)
672 size_t x
= strlen(str
);
673 return (rb_vsprintf(str
+ x
, format
, ap
) + x
);
677 * rb_sprintf_append()
678 * appends sprintf formatted string to the end of the buffer
681 rb_sprintf_append(char *str
, const char *format
, ...)
685 va_start(ap
, format
);
686 x
= rb_vsprintf_append(str
, format
, ap
);
692 * rb_vsnprintf_append()
693 * appends sprintf formatted string to the end of the buffer but not
698 rb_vsnprintf_append(char *str
, size_t len
, const char *format
, va_list ap
)
710 return (rb_vsnprintf(str
+ x
, len
- x
, format
, ap
) + x
);
714 * rb_snprintf_append()
715 * appends snprintf formatted string to the end of the buffer but not
720 rb_snprintf_append(char *str
, size_t len
, const char *format
, ...)
724 va_start(ap
, format
);
725 x
= rb_vsnprintf_append(str
, len
, format
, ap
);