mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
Merge pull request #1903 from rtfeldman/fix-str-from-int
Implement Str.fromInt for any integer type
This commit is contained in:
commit
0a347456ef
5 changed files with 41 additions and 41 deletions
|
@ -4,6 +4,7 @@ const utils = @import("utils.zig");
|
|||
|
||||
const ROC_BUILTINS = "roc_builtins";
|
||||
const NUM = "num";
|
||||
const STR = "str";
|
||||
|
||||
// Dec Module
|
||||
const dec = @import("dec.zig");
|
||||
|
@ -116,7 +117,6 @@ comptime {
|
|||
exportStrFn(str.strConcatC, "concat");
|
||||
exportStrFn(str.strJoinWithC, "joinWith");
|
||||
exportStrFn(str.strNumberOfBytes, "number_of_bytes");
|
||||
exportStrFn(str.strFromIntC, "from_int");
|
||||
exportStrFn(str.strFromFloatC, "from_float");
|
||||
exportStrFn(str.strEqual, "equal");
|
||||
exportStrFn(str.strToUtf8C, "to_utf8");
|
||||
|
@ -124,6 +124,10 @@ comptime {
|
|||
exportStrFn(str.fromUtf8RangeC, "from_utf8_range");
|
||||
exportStrFn(str.repeat, "repeat");
|
||||
exportStrFn(str.strTrim, "trim");
|
||||
|
||||
inline for (INTEGERS) |T| {
|
||||
str.exportFromInt(T, ROC_BUILTINS ++ "." ++ STR ++ ".from_int.");
|
||||
}
|
||||
}
|
||||
|
||||
// Utils
|
||||
|
|
|
@ -413,9 +413,14 @@ pub fn strNumberOfBytes(string: RocStr) callconv(.C) usize {
|
|||
}
|
||||
|
||||
// Str.fromInt
|
||||
pub fn strFromIntC(int: i64) callconv(.C) RocStr {
|
||||
// prepare for having multiple integer types in the future
|
||||
return @call(.{ .modifier = always_inline }, strFromIntHelp, .{ i64, int });
|
||||
pub fn exportFromInt(comptime T: type, comptime name: []const u8) void {
|
||||
comptime var f = struct {
|
||||
fn func(int: T) callconv(.C) RocStr {
|
||||
return @call(.{ .modifier = always_inline }, strFromIntHelp, .{ T, int });
|
||||
}
|
||||
}.func;
|
||||
|
||||
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
|
||||
}
|
||||
|
||||
fn strFromIntHelp(comptime T: type, int: T) RocStr {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue