Skip to content

Instantly share code, notes, and snippets.

@mikdusan
Created April 27, 2020 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikdusan/d81b26e434620d6938a47e59ce2cbc16 to your computer and use it in GitHub Desktop.
Save mikdusan/d81b26e434620d6938a47e59ce2cbc16 to your computer and use it in GitHub Desktop.
const std = @import("std");
fn stringForUnsigned(comptime value: usize) []const u8 {
if (value == 0) return "0";
const base = 10;
const base_digits = "0123456789";
const ndigits = 1 + @floatToInt(
usize,
std.math.floor(std.math.log2(@intToFloat(f64, value)) / std.math.log2(@intToFloat(f64, base))),
);
comptime var buffer: [ndigits]u8 = undefined;
comptime var i = buffer.len;
comptime var a = value;
inline while (i > 0 and a != 0) {
i -= 1;
const digit = a % base;
buffer[i] = base_digits[digit];
a /= base;
}
return &buffer;
}
export fn foo() void {
@compileError("whups: " ++ stringForUnsigned(98137123));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment