aboutsummaryrefslogtreecommitdiffstats
path: root/src/zstr.c
blob: 81a967486a0079b6c330b4279f8785047279ea9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* See LICENSE file for copyright and license details. */
#include "internals.h"

#include <stdio.h>

#define num  libzahl_tmp_str_num
#define rem  libzahl_tmp_str_rem

/* All 19 you see here is derived from that 10¹⁹ is the largest
 * power of than < 2⁶⁴, and 64 is the number of bits in
 * zahl_char_t. If zahl_char_t is chanced, the value 19, and
 * the cast to unsigned long long must be changed accordingly. */


char *
zstr(z_t a, char *b)
{
	char buf[19 + 1];
	size_t n, len;
	char overridden = 0;
	int neg;

	if (zzero(a)) {
		if (!b) {
			b = malloc(2);
			if (!b)
				FAILURE(errno);
		}
		b[0] = '0';
		b[1] = 0;
		return b;
	}

	n = zstr_length(a, 10);

	if (!b) {
		b = malloc(n + 1);
		if (!b)
			FAILURE(errno);
	}

	neg = zsignum(a) < 0;
	zabs(num, a);
	b[0] = '-';
	b += neg;
	n -= neg;
	n = n > 19 ? (n - 19) : 0;

	for (;;) {
		zdivmod(num, rem, num, libzahl_const_1e19);
		if (!zzero(num)) {
			sprintf(b + n, "%019llu", zzero(rem) ? 0ULL : (unsigned long long)(rem->chars[0]));
			b[n + 19] = overridden;
			overridden = b[n];
			n = n > 19 ? (n - 19) : 0;
		} else {
			len = (size_t)sprintf(buf, "%llu", (unsigned long long)(rem->chars[0]));
			if (overridden)
				buf[len] = b[n + len];
			memcpy(b + n, buf, len + 1);
			break;
		}
	}

	return b - neg;
}