hare

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

ztos.ha (577B)


      1 // License: MPL-2.0
      2 // (c) 2021 Drew DeVault <sir@cmpwn.com>
      3 
      4 fn bytes_reverse(b: []u8) void = {
      5 	if (len(b) == 0) {
      6 		return;
      7 	};
      8 	for (let s = 0z, e = len(b) - 1; s < e) {
      9 		let x = b[s];
     10 		b[s] = b[e];
     11 		b[e] = x;
     12 		s += 1;
     13 		e -= 1;
     14 	};
     15 };
     16 
     17 fn ztos(u: size) const str = {
     18 	static let buf: [20]u8 = [0...];
     19 	buf = [0...];
     20 
     21 	let s = struct {
     22 		b: *[*]u8 = &buf,
     23 		l: size = 0,
     24 		c: size = 0,
     25 	};
     26 
     27 	if (u == 0) {
     28 		s.b[s.l] = '0';
     29 		s.l += 1;
     30 	};
     31 
     32 	for (u > 0) {
     33 		s.b[s.l] = '0' + (u % 10): u8;
     34 		s.l += 1;
     35 		u /= 10;
     36 	};
     37 
     38 	bytes_reverse(s.b[..s.l]);
     39 	return *(&s: *str);
     40 };