]>
jfr.im git - irc/rqf/shadowircd.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...
10 * linux/lib/vsprintf.c
12 * Copyright (C) 1991, 1992 Linus Torvalds
15 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
17 * Wirzenius wrote this portably, Torvalds fucked it up :-)
21 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
22 * - changed to provide snprintf and vsnprintf functions
23 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
24 * - scnprintf and vscnprintf
26 #include <libratbox_config.h>
27 #include <ratbox_lib.h>
30 skip_atoi(const char **s
)
35 i
= i
* 10 + *((*s
)++) - '0';
39 /* Decimal conversion is by far the most typical, and is used
40 * for /proc and /sys data. This directly impacts e.g. top performance
41 * with many processes running. We optimize it for speed
43 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
44 * (with permission from the author, Douglas W. Jones). */
46 /* Formats correctly any integer in [0,99999].
47 * Outputs from one to five digits depending on input.
48 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
50 put_dec_trunc(char *buf
, unsigned q
)
52 unsigned d3
, d2
, d1
, d0
;
57 d0
= 6 * (d3
+ d2
+ d1
) + (q
& 0xf);
58 q
= (d0
* 0xcd) >> 11;
60 *buf
++ = d0
+ '0'; /* least significant digit */
61 d1
= q
+ 9 * d3
+ 5 * d2
+ d1
;
64 q
= (d1
* 0xcd) >> 11;
66 *buf
++ = d1
+ '0'; /* next digit */
69 if((d2
!= 0) || (d3
!= 0))
73 *buf
++ = d2
+ '0'; /* next digit */
78 q
= (d3
* 0xcd) >> 11;
80 *buf
++ = d3
+ '0'; /* next digit */
82 *buf
++ = q
+ '0'; /* most sign. digit */
89 /* Same with if's removed. Always emits five digits */
91 put_dec_full(char *buf
, unsigned q
)
93 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
94 /* but anyway, gcc produces better code with full-sized ints */
95 unsigned d3
, d2
, d1
, d0
;
100 /* Possible ways to approx. divide by 10 */
101 /* gcc -O2 replaces multiply with shifts and adds */
102 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
103 // (x * 0x67) >> 10: 1100111
104 // (x * 0x34) >> 9: 110100 - same
105 // (x * 0x1a) >> 8: 11010 - same
106 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
108 d0
= 6 * (d3
+ d2
+ d1
) + (q
& 0xf);
109 q
= (d0
* 0xcd) >> 11;
112 d1
= q
+ 9 * d3
+ 5 * d2
+ d1
;
113 q
= (d1
* 0xcd) >> 11;
123 q
= (d3
* 0xcd) >> 11; /* - shorter code */
124 /* q = (d3 * 0x67) >> 10; - would also work */
132 put_dec(char *buf
, unsigned long long int num
)
138 return put_dec_trunc(buf
, num
);
141 buf
= put_dec_full(buf
, rem
);
145 #define ZEROPAD 1 /* pad with zero */
146 #define SIGN 2 /* unsigned/signed long */
147 #define PLUS 4 /* show plus */
148 #define SPACE 8 /* space if plus */
149 #define LEFT 16 /* left justified */
150 #define SPECIAL 32 /* 0x */
151 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
154 number(char *buf
, char *end
, unsigned long long int num
, int base
, int size
, int precision
,
159 /* we are called with base 8, 10 or 16, only, thus don't need "g..." */
160 static const char small_digits
[] = "0123456789abcdefx"; /* "ghijklmnopqrstuvwxyz"; */
161 static const char large_digits
[] = "0123456789ABCDEFX"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
162 int need_pfx
= ((type
& SPECIAL
) && base
!= 10);
165 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
168 if(base
< 2 || base
> 36)
173 if((signed long long int)num
< 0)
176 num
= -(signed long long int)num
;
184 else if(type
& SPACE
)
197 /* generate full string in tmp[], in reverse order */
201 /* Generic code, for any base:
203 tmp[i++] = digits[do_div(num,base)];
214 tmp
[i
++] = digits
[((unsigned char)num
) & mask
];
221 i
= put_dec(tmp
, num
) - tmp
;
224 /* printing 100 using %2d gives "100", not "00" */
227 /* leading space padding */
229 if(!(type
& (ZEROPAD
+ LEFT
)))
245 /* "0x" / "0" prefix */
254 *buf
= digits
[16]; /* for arbitrary base: digits[33]; */
258 /* zero or space padding */
261 char c
= (type
& ZEROPAD
) ? '0' : ' ';
269 /* hmm even more zero padding? */
270 while(i
<= --precision
)
276 /* actual digits of result */
283 /* trailing space padding */
294 * vsnprintf - Format a string and place it in a buffer
295 * @buf: The buffer to place the result into
296 * @size: The size of the buffer, including the trailing null space
297 * @fmt: The format string to use
298 * @args: Arguments for the format string
300 * The return value is the number of characters which would
301 * be generated for the given input, excluding the trailing
302 * '\0', as per ISO C99. If you want to have the exact
303 * number of characters written into @buf as return value
304 * (not including the trailing '\0'), use vscnprintf(). If the
305 * return is greater than or equal to @size, the resulting
306 * string is truncated.
308 * Call this function if you are already dealing with a va_list.
309 * You probably want snprintf() instead.
312 rb_vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
315 unsigned long long int num
;
320 int flags
; /* flags to number() */
322 int field_width
; /* width of output field */
323 int precision
; /* min. # of digits for integers; max
324 number of chars for from string */
325 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
326 /* 'z' support added 23/7/1999 S.H. */
327 /* 'z' changed to 'Z' --davidm 1/25/99 */
328 /* 't' added for ptrdiff_t */
330 /* Reject out-of-range values early. Large positive sizes are
331 used for unknown buffer sizes. */
332 if(rb_unlikely((int)size
< 0))
340 /* Make sure end is always >= buf */
360 ++fmt
; /* this also skips first '%' */
380 /* get field width */
383 field_width
= skip_atoi(&fmt
);
387 /* it's the next argument */
388 field_width
= va_arg(args
, int);
391 field_width
= -field_width
;
396 /* get the precision */
402 precision
= skip_atoi(&fmt
);
406 /* it's the next argument */
407 precision
= va_arg(args
, int);
413 /* get the conversion qualifier */
415 if(*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
416 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't')
420 if(qualifier
== 'l' && *fmt
== 'l')
435 while(--field_width
> 0)
442 c
= (unsigned char)va_arg(args
, int);
446 while(--field_width
> 0)
455 s
= va_arg(args
, char *);
458 abort(); /* prefer blowing up vs corrupt data */
460 len
= rb_strnlen(s
, precision
);
464 while(len
< field_width
--)
471 for(i
= 0; i
< len
; ++i
)
478 while(len
< field_width
--)
487 if(field_width
== -1)
489 field_width
= 2 * sizeof(void *);
492 str
= number(str
, end
,
493 (unsigned long)va_arg(args
, void *),
494 16, field_width
, precision
, flags
);
500 * What does C99 say about the overflow case here? */
503 long *ip
= va_arg(args
, long *);
506 else if(qualifier
== 'Z' || qualifier
== 'z')
508 size_t *ip
= va_arg(args
, size_t *);
513 int *ip
= va_arg(args
, int *);
524 /* integer number formats - set up the flags and "break" */
558 num
= va_arg(args
, long long int);
559 else if(qualifier
== 'l')
561 num
= va_arg(args
, unsigned long);
563 num
= (signed long)num
;
565 else if(qualifier
== 'Z' || qualifier
== 'z')
567 num
= va_arg(args
, size_t);
569 else if(qualifier
== 't')
571 num
= va_arg(args
, ptrdiff_t);
573 else if(qualifier
== 'h')
575 num
= (unsigned short)va_arg(args
, int);
577 num
= (signed short)num
;
581 num
= va_arg(args
, unsigned int);
583 num
= (signed int)num
;
585 str
= number(str
, end
, num
, base
, field_width
, precision
, flags
);
594 /* the trailing null byte doesn't count towards the total */
599 * snprintf - Format a string and place it in a buffer
600 * @buf: The buffer to place the result into
601 * @size: The size of the buffer, including the trailing null space
602 * @fmt: The format string to use
603 * @...: Arguments for the format string
605 * The return value is the number of characters which would be
606 * generated for the given input, excluding the trailing null,
607 * as per ISO C99. If the return is greater than or equal to
608 * @size, the resulting string is truncated.
611 rb_snprintf(char *buf
, size_t size
, const char *fmt
, ...)
617 i
= rb_vsnprintf(buf
, size
, fmt
, args
);
623 * vsprintf - Format a string and place it in a buffer
624 * @buf: The buffer to place the result into
625 * @fmt: The format string to use
626 * @args: Arguments for the format string
628 * The function returns the number of characters written
629 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
632 * Call this function if you are already dealing with a va_list.
633 * You probably want sprintf() instead.
636 rb_vsprintf(char *buf
, const char *fmt
, va_list args
)
638 return rb_vsnprintf(buf
, INT_MAX
, fmt
, args
);
642 * sprintf - Format a string and place it in a buffer
643 * @buf: The buffer to place the result into
644 * @fmt: The format string to use
645 * @...: Arguments for the format string
647 * The function returns the number of characters written
648 * into @buf. Use snprintf() or scnprintf() in order to avoid
652 rb_sprintf(char *buf
, const char *fmt
, ...)
658 i
= rb_vsnprintf(buf
, INT_MAX
, fmt
, args
);
664 * rb_vsprintf_append()
665 * appends sprintf formatted string to the end of the buffer
669 rb_vsprintf_append(char *str
, const char *format
, va_list ap
)
671 size_t x
= strlen(str
);
672 return (rb_vsprintf(str
+ x
, format
, ap
) + x
);
676 * rb_sprintf_append()
677 * appends sprintf formatted string to the end of the buffer
680 rb_sprintf_append(char *str
, const char *format
, ...)
684 va_start(ap
, format
);
685 x
= rb_vsprintf_append(str
, format
, ap
);
691 * rb_vsnprintf_append()
692 * appends sprintf formatted string to the end of the buffer but not
697 rb_vsnprintf_append(char *str
, size_t len
, const char *format
, va_list ap
)
709 return (rb_vsnprintf(str
+ x
, len
- x
, format
, ap
) + x
);
713 * rb_snprintf_append()
714 * appends snprintf formatted string to the end of the buffer but not
719 rb_snprintf_append(char *str
, size_t len
, const char *format
, ...)
723 va_start(ap
, format
);
724 x
= rb_vsnprintf_append(str
, len
, format
, ap
);