annotate src/libsys/sprintf/integer.c @ 149:bd1301884216

l1_small.c: BIG_SMALL_SLEEP support like in Magnetite
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 06 Feb 2019 23:21:43 +0000
parents 425ab6d987f3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
86
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Embedded [v]sprintf() implementation by Mychaela Falconia,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * loosely based on the 4.3BSD-Tahoe version.
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 *
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * This module contains the integer conversion functions.
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/types.h>
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <ctype.h>
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include "defs.h"
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 extern u_char * _sprintf_field(u_char *op, int width, int flags, int sign,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 u_char *body, int size, int dprec, int fpprec);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 static const char lcdigits[] = "0123456789abcdef";
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 static const char ucdigits[] = "0123456789ABCDEF";
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 u_char *
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 _sprintf_integer(u_char *op, int width, int flags, int sign,
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 unsigned number, int base, int prec)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 const char *digits;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 char buf[12];
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 char *t, *endp;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 * ``... diouXx conversions ... if a precision is
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 * specified, the 0 flag will be ignored.''
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 * -- ANSI X3J11
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (prec >= 0)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 flags &= ~ZEROPAD;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 if (flags & UPPERCASE)
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 digits = ucdigits;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 else
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 digits = lcdigits;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 /*
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 * ``The result of converting a zero value with an
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 * explicit precision of zero is no characters.''
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 * -- ANSI X3J11
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 t = endp = buf + sizeof(buf);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (number != 0 || prec != 0) {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 do {
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 *--t = digits[number % base];
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 number /= base;
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 } while (number);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (flags & ALT && base == 8 && *t != '0')
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 *--t = '0'; /* octal leading 0 */
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 return _sprintf_field(op, width, flags, sign, t, endp - t, prec, 0);
425ab6d987f3 src/libsys: pieced together from Citrine
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 }