Support Num.toStr for f32, f64

This commit is contained in:
Ayaz Hafiz 2022-07-13 12:13:01 -04:00
parent 6ac9c37e06
commit b7c312d449
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
7 changed files with 79 additions and 14 deletions

View file

@ -152,7 +152,6 @@ comptime {
exportStrFn(str.strConcatC, "concat");
exportStrFn(str.strJoinWithC, "joinWith");
exportStrFn(str.strNumberOfBytes, "number_of_bytes");
exportStrFn(str.strFromFloatC, "from_float");
exportStrFn(str.strEqual, "equal");
exportStrFn(str.substringUnsafe, "substring_unsafe");
exportStrFn(str.getUnsafe, "get_unsafe");
@ -173,6 +172,7 @@ comptime {
}
inline for (FLOATS) |T| {
str.exportFromFloat(T, ROC_BUILTINS ++ "." ++ STR ++ ".from_float.");
num.exportParseFloat(T, ROC_BUILTINS ++ "." ++ STR ++ ".to_float.");
}
}

View file

@ -749,13 +749,19 @@ fn strFromIntHelp(comptime T: type, int: T) RocStr {
}
// Str.fromFloat
pub fn strFromFloatC(float: f64) callconv(.C) RocStr {
return @call(.{ .modifier = always_inline }, strFromFloatHelp, .{ f64, float });
pub fn exportFromFloat(comptime T: type, comptime name: []const u8) void {
comptime var f = struct {
fn func(float: T) callconv(.C) RocStr {
return @call(.{ .modifier = always_inline }, strFromFloatHelp, .{ T, float });
}
}.func;
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
}
fn strFromFloatHelp(comptime T: type, float: T) RocStr {
var buf: [100]u8 = undefined;
const result = std.fmt.bufPrint(&buf, "{d}", .{float}) catch unreachable;
const result = std.fmt.bufPrint(&buf, "{}", .{float}) catch unreachable;
return RocStr.init(&buf, result.len);
}