hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

itos.ha (2620B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use bytes;
      5 use strings;
      6 use types;
      7 
      8 // Converts an i64 to a string. The return value is statically allocated and
      9 // will be overwritten on subsequent calls; see [[strings::dup]] to duplicate
     10 // the result.
     11 export fn i64tos(i: i64, b: base = base::DEC) const str = {
     12 	static assert(types::I64_MAX == 9223372036854775807);
     13 	if (b == base::DEFAULT) {
     14 		b = base::DEC;
     15 	};
     16 
     17 	if (i >= 0) return u64tos(i: u64, b);
     18 
     19 	static let buf: [65]u8 = [0...]; // 64 binary digits plus -
     20 
     21 	let s = types::string { data = &buf, ... };
     22 
     23 	buf[0] = '-';
     24 	s.length = 1;
     25 
     26 	let u = strings::toutf8(u64tos((-i): u64, b));
     27 	assert(len(u) < len(buf));
     28 
     29 	buf[1..len(u) + 1] = u[..];
     30 	s.length += len(u);
     31 
     32 	return *(&s: *str);
     33 };
     34 
     35 // Converts an i32 to a string. The return value is statically allocated and
     36 // will be overwritten on subsequent calls; see [[strings::dup]] to duplicate
     37 // the result.
     38 export fn i32tos(i: i32, b: base = base::DEC) const str = i64tos(i, b);
     39 
     40 // Converts an i16 to a string. The return value is statically allocated and
     41 // will be overwritten on subsequent calls; see [[strings::dup]] to duplicate
     42 // the result.
     43 export fn i16tos(i: i16, b: base = base::DEC) const str = i64tos(i, b);
     44 
     45 // Converts an i8 to a string. The return value is statically allocated and will
     46 // be overwritten on subsequent calls; see [[strings::dup]] to duplicate the
     47 // result.
     48 export fn i8tos(i: i8, b: base = base::DEC) const str = i64tos(i, b);
     49 
     50 // Converts an int to a string. The return value is statically allocated and
     51 // will be overwritten on subsequent calls; see [[strings::dup]] to duplicate
     52 // the result.
     53 export fn itos(i: int, b: base = base::DEC) const str = i64tos(i, b);
     54 
     55 @test fn itos_bases() void = {
     56 	assert("11010" == i64tos(0b11010, base::BIN));
     57 	assert("1234567" == i64tos(0o1234567, base::OCT));
     58 	assert("123456789" == i64tos(123456789, base::DEC));
     59 	assert("123456789ABCDEF" == i64tos(0x123456789ABCDEF, base::HEX));
     60 	assert("123456789ABCDEF" == i64tos(0x123456789ABCDEF, base::HEX_UPPER));
     61 	assert("123456789abcdef" == i64tos(0x123456789ABCDEF, base::HEX_LOWER));
     62 	assert("-1000000000000000000000000000000000000000000000000000000000000000"
     63 		== i64tos(types::I64_MIN, base::BIN));
     64 };
     65 
     66 @test fn itos() void = {
     67 	const samples: [_]i64 = [
     68 		1234,
     69 		4321,
     70 		-1337,
     71 		0,
     72 		types::I64_MAX,
     73 		types::I64_MIN,
     74 	];
     75 	const expected = [
     76 		"1234",
     77 		"4321",
     78 		"-1337",
     79 		"0",
     80 		"9223372036854775807",
     81 		"-9223372036854775808",
     82 	];
     83 
     84 	for (let i = 0z; i < len(samples); i += 1) {
     85 		const s = i64tos(samples[i]);
     86 		assert(s == expected[i]);
     87 	};
     88 };