hare

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

unknown_errno.ha (883B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 fn unknown_errno(err: errno) str = {
      5 	static let buf: [27]u8 = [0...];
      6 	const s = *(&"[unknown errno ": *[]u8);
      7 	buf[..len(s)] = s;
      8 	if (err < 0) {
      9 		buf[len(s)] = '-';
     10 		const s2 = &u64tos(-err: u64): *string;
     11 		buf[len(s) + 1..s2.length + len(s) + 1] = s2.data[..s2.length];
     12 		buf[s2.length + len(s) + 1] = ']';
     13 		return *(&buf[..s2.length + len(s) + 2]: *str);
     14 	} else {
     15 		const s2 = &u64tos(err: u64): *string;
     16 		buf[len(s)..s2.length + len(s)] = s2.data[..s2.length];
     17 		buf[s2.length + len(s)] = ']';
     18 		return *(&buf[..s2.length + len(s) + 1]: *str);
     19 	};
     20 };
     21 
     22 @test fn unknown_errno() void = {
     23 	let err: errno = -1;
     24 	assert(strerror(err) == "[unknown errno -1]");
     25 	err = 0;
     26 	assert(strerror(err) == "[unknown errno 0]");
     27 	err = 2147483647;
     28 	assert(strerror(err) == "[unknown errno 2147483647]");
     29 };