hare

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

numeric.ha (3035B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use types;
      5 
      6 // Converts any [[types::signed]] to a string. The return value is statically
      7 // allocated and will be overwritten on subsequent calls; see [[strings::dup]]
      8 // to duplicate the result. If base is not specified, base 10 is used.
      9 export fn signedtos(n: types::signed, b: base = base::DEC) const str = {
     10 	match (n) {
     11 	case let i: int =>
     12 		return itos(i, b);
     13 	case let i: i8 =>
     14 		return i8tos(i, b);
     15 	case let i: i16 =>
     16 		return i16tos(i, b);
     17 	case let i: i32 =>
     18 		return i32tos(i, b);
     19 	case let i: i64 =>
     20 		return i64tos(i, b);
     21 	};
     22 };
     23 
     24 // Converts any [[types::unsigned]] to a string. The return value is statically
     25 // allocated and will be overwritten on subsequent calls; see [[strings::dup]]
     26 // to duplicate the result. If base is not specified, base 10 is used.
     27 export fn unsignedtos(n: types::unsigned, b: base = base::DEC) const str = {
     28 	match (n) {
     29 	case let u: size =>
     30 		return ztos(u, b);
     31 	case let u: uint =>
     32 		return utos(u, b);
     33 	case let u: u8 =>
     34 		return u8tos(u, b);
     35 	case let u: u16 =>
     36 		return u16tos(u, b);
     37 	case let u: u32 =>
     38 		return u32tos(u, b);
     39 	case let u: u64 =>
     40 		return u64tos(u, b);
     41 	};
     42 };
     43 
     44 // Converts any [[types::integer]] to a string. The return value is statically
     45 // allocated and will be overwritten on subsequent calls; see [[strings::dup]]
     46 // to duplicate the result. If base is not specified, base 10 is used.
     47 export fn integertos(n: types::integer, b: base = base::DEC) const str = {
     48 	match (n) {
     49 	case let s: types::signed =>
     50 		return signedtos(s, b);
     51 	case let u: types::unsigned =>
     52 		return unsignedtos(u, b);
     53 	};
     54 };
     55 
     56 // Converts any [[types::floating]] to a string. The return value is statically
     57 // allocated and will be overwritten on subsequent calls; see [[strings::dup]]
     58 // to duplicate the result. If base is not specified, base 10 is used.
     59 export fn floatingtos(n: types::floating, b: base = base::DEC) const str = {
     60 	if (b == base::DEFAULT) {
     61 		b = base::DEC;
     62 	};
     63 	assert(b == base::DEC);
     64 	match (n) {
     65 	case let f: f32 =>
     66 		return f32tos(f);
     67 	case let f: f64 =>
     68 		return f64tos(f);
     69 	};
     70 };
     71 
     72 // Converts any [[types::numeric]] to a string. The return value is statically
     73 // allocated and will be overwritten on subsequent calls; see [[strings::dup]]
     74 // to duplicate the result. If base is not specified, base 10 is used.
     75 export fn numerictos(n: types::numeric, b: base = base::DEC) const str = {
     76 	match (n) {
     77 	case let i: types::integer =>
     78 		return integertos(i, b);
     79 	case let f: types::floating =>
     80 		return floatingtos(f, b);
     81 	};
     82 };
     83 
     84 @test fn numeric() void = {
     85 	const cases: [_]types::numeric = [
     86 		42u8, 1337u16, 1337u32, 1337u64, 42i8, -42i8, 1337i16, -1337i16,
     87 		1337i32, -1337i32, 1337i64, -1337i64, 1337i, -1337i, 1337u,
     88 		-1337i,
     89 	];
     90 	const expected = [
     91 		"42", "1337", "1337", "1337", "42", "-42", "1337", "-1337",
     92 		"1337", "-1337", "1337", "-1337", "1337", "-1337", "1337",
     93 		"-1337",
     94 	];
     95 	for (let i = 0z; i < len(cases); i += 1) {
     96 		assert(numerictos(cases[i]) == expected[i]);
     97 	};
     98 };