Created
April 27, 2020 08:30
-
-
Save mikdusan/d81b26e434620d6938a47e59ce2cbc16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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