mirror of
https://github.com/roc-lang/roc.git
synced 2025-11-01 21:40:58 +00:00
Support Num.toStr for f32, f64
This commit is contained in:
parent
6ac9c37e06
commit
b7c312d449
7 changed files with 79 additions and 14 deletions
|
|
@ -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.");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue