~sircmpwn/hare

ref: c66099085ccde84906ac368ee82214a7422bfd42 hare/strconv/numeric.ha -rw-r--r-- 3.7 KiB
c6609908Eyal Sawady README.md: fix typo 1 year, 3 months ago
                                                                                
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use types;

// Converts any [types::signed] to a string in a given base. The return value is
// statically allocated and will be overwritten on subsequent calls; see
// [strings::dup] to duplicate the result.
export fn signedtosb(n: types::signed, b: base) const str = {
	return match (n) {
		i: int => itosb(i, b),
		i: i8  => i8tosb(i, b),
		i: i16 => i16tosb(i, b),
		i: i32 => i32tosb(i, b),
		i: i64 => i64tosb(i, b),
	};
};

// Converts any [types::signed] to a string in base 10. The return value is
// statically allocated and will be overwritten on subsequent calls; see
// [strings::dup] to duplicate the result.
export fn signedtos(n: types::signed) const str = signedtosb(n, base::DEC);

// Converts any [types::unsigned] to a string in a given base. The return value
// is statically allocated and will be overwritten on subsequent calls; see
// [strings::dup] to duplicate the result.
export fn unsignedtosb(n: types::unsigned, b: base) const str = {
	return match (n) {
		u: size => ztosb(u, b),
		u: uint => utosb(u, b),
		u: u8   => u8tosb(u, b),
		u: u16  => u16tosb(u, b),
		u: u32  => u32tosb(u, b),
		u: u64  => u64tosb(u, b),
	};
};

// Converts any [types::unsigned] to a string in base 10. The return value is
// statically allocated and will be overwritten on subsequent calls; see
// [strings::dup] to duplicate the result.
export fn unsignedtos(n: types::unsigned) const str = unsignedtosb(n, base::DEC);

// Converts any [types::integer] to a string in a given base, which must be 2,
// 8, 10, or 16. The return value is statically allocated and will be
// overwritten on subsequent calls; see [strings::dup] to duplicate the result.
export fn integertosb(n: types::integer, b: base) const str = {
	return match (n) {
		s: types::signed   => signedtosb(s, b),
		u: types::unsigned => unsignedtosb(u, b),
	};
};

// Converts any [types::integer] to a string in base 10. The return value is
// statically allocated and will be overwritten on subsequent calls; see
// [strings::dup] to duplicate the result.
export fn integertos(n: types::integer) const str = integertosb(n, base::DEC);

// Converts any [types::floating] to a string in a given base. The return value
// is statically allocated and will be overwritten on subsequent calls; see
// [strings::dup] to duplicate the result.
export fn floatingtosb(n: types::floating, b: base) const str = {
	abort(); // TODO
};

// Converts any [types::floating] to a string in base 10. The return value is
// statically allocated and will be overwritten on subsequent calls; see
// [strings::dup] to duplicate the result.
export fn floatingtos(n: types::floating) const str = floatingtosb(n, base::DEC);

// Converts any [types::numeric] to a string in a given base. The return value
// is statically allocated and will be overwritten on subsequent calls; see
// [strings::dup] to duplicate the result.
export fn numerictosb(n: types::numeric, b: base) const str = {
	return match (n) {
		i: types::integer  => integertosb(i, b),
		f: types::floating => floatingtosb(f, b),
	};
};

// Converts any [types::numeric] to a string in base 10. The return value is
// statically allocated and will be overwritten on subsequent calls; see
// [strings::dup] to duplicate the result.
export fn numerictos(n: types::numeric) const str = numerictosb(n, base::DEC);

@test fn numeric() void = {
	const cases: [_]types::numeric = [
		42u8, 1337u16, 1337u32, 1337u64, 42i8, -42i8, 1337i16, -1337i16,
		1337i32, -1337i32, 1337i64, -1337i64, 1337i, -1337i, 1337u,
		-1337i,
	];
	const expected = [
		"42", "1337", "1337", "1337", "42", "-42", "1337", "-1337",
		"1337", "-1337", "1337", "-1337", "1337", "-1337", "1337",
		"-1337",
	];
	for (let i = 0z; i < len(cases); i += 1) {
		assert(numerictos(cases[i]) == expected[i]);
	};
};