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
|
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>
use strings;
use types;
// Converts an i64 to a string. The return value is statically allocated and
// will be overwritten on subsequent calls; see [[strings::dup]] to duplicate
// the result.
export fn i64tos(i: i64, b: base = base::DEC) const str = {
static assert(types::I64_MAX == 9223372036854775807);
if (b == base::DEFAULT) {
b = base::DEC;
};
if (i >= 0) return u64tos(i: u64, b);
static let buf: [65]u8 = [0...]; // 64 binary digits plus -
let s = types::string { data = &buf, ... };
buf[0] = '-';
s.length = 1;
let u = strings::toutf8(u64tos((-i): u64, b));
assert(len(u) < len(buf));
buf[1..len(u) + 1] = u[..];
s.length += len(u);
return *(&s: *str);
};
// Converts an i32 to a string. The return value is statically allocated and
// will be overwritten on subsequent calls; see [[strings::dup]] to duplicate
// the result.
export fn i32tos(i: i32, b: base = base::DEC) const str = i64tos(i, b);
// Converts an i16 to a string. The return value is statically allocated and
// will be overwritten on subsequent calls; see [[strings::dup]] to duplicate
// the result.
export fn i16tos(i: i16, b: base = base::DEC) const str = i64tos(i, b);
// Converts an i8 to a string. The return value is statically allocated and will
// be overwritten on subsequent calls; see [[strings::dup]] to duplicate the
// result.
export fn i8tos(i: i8, b: base = base::DEC) const str = i64tos(i, b);
// Converts an int to a string. The return value is statically allocated and
// will be overwritten on subsequent calls; see [[strings::dup]] to duplicate
// the result.
export fn itos(i: int, b: base = base::DEC) const str = i64tos(i, b);
@test fn itos_bases() void = {
assert("11010" == i64tos(0b11010, base::BIN));
assert("1234567" == i64tos(0o1234567, base::OCT));
assert("123456789" == i64tos(123456789, base::DEC));
assert("123456789ABCDEF" == i64tos(0x123456789ABCDEF, base::HEX));
assert("123456789ABCDEF" == i64tos(0x123456789ABCDEF, base::HEX_UPPER));
assert("123456789abcdef" == i64tos(0x123456789ABCDEF, base::HEX_LOWER));
assert("-1000000000000000000000000000000000000000000000000000000000000000"
== i64tos(types::I64_MIN, base::BIN));
};
@test fn itos() void = {
const samples: [_]i64 = [
1234,
4321,
-1337,
0,
types::I64_MAX,
types::I64_MIN,
];
const expected = [
"1234",
"4321",
"-1337",
"0",
"9223372036854775807",
"-9223372036854775808",
];
for (let i = 0z; i < len(samples); i += 1) {
const s = i64tos(samples[i]);
assert(s == expected[i]);
};
};
|