mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
add Str.fromInt
This commit is contained in:
parent
82e87d04e3
commit
4b0b5126a1
13 changed files with 124 additions and 4 deletions
|
@ -50,9 +50,23 @@ const RocStr = extern struct {
|
|||
|
||||
return ret_small_str;
|
||||
} else {
|
||||
var new_bytes: [*]u8 = @ptrCast([*]u8, malloc(length));
|
||||
var result = allocate_str(u64, InPlace.Clone, length);
|
||||
|
||||
@memcpy(new_bytes, bytes, length);
|
||||
@memcpy(@ptrCast([*]u8, result.str_bytes), bytes, length);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// This takes ownership of the pointed-to bytes if they won't fit in a
|
||||
// small string, and returns a (pointer, len) tuple which points to them.
|
||||
pub fn withCapacity(length: usize) RocStr {
|
||||
const rocStrSize = @sizeOf(RocStr);
|
||||
|
||||
if (length < rocStrSize) {
|
||||
return RocStr.empty();
|
||||
} else {
|
||||
var new_bytes: [*]u8 = @ptrCast([*]u8, malloc(length));
|
||||
|
||||
return RocStr{
|
||||
.str_bytes = new_bytes,
|
||||
|
@ -210,6 +224,28 @@ pub fn strNumberOfBytes(string: RocStr) callconv(.C) u64 {
|
|||
return string.len();
|
||||
}
|
||||
|
||||
// Str.fromInt
|
||||
|
||||
pub fn strFromInt(int: i64) callconv(.C) RocStr {
|
||||
// prepare for having multiple integer types in the future
|
||||
return strFromIntHelp(i64, int);
|
||||
}
|
||||
|
||||
fn strFromIntHelp(comptime T: type, int: T) RocStr {
|
||||
// determine maximum size for this T
|
||||
comptime const size = comptime blk: {
|
||||
// the string representation of the minimum i128 value uses at most 40 characters
|
||||
var buf: [40]u8 = undefined;
|
||||
var result = std.fmt.bufPrint(&buf, "{}", .{std.math.minInt(T)}) catch unreachable;
|
||||
break :blk result.len;
|
||||
};
|
||||
|
||||
var buf: [size]u8 = undefined;
|
||||
const result = std.fmt.bufPrint(&buf, "{}", .{int}) catch unreachable;
|
||||
|
||||
return RocStr.init(&buf, result.len);
|
||||
}
|
||||
|
||||
// Str.split
|
||||
|
||||
pub fn strSplitInPlace(array: [*]RocStr, array_len: usize, string: RocStr, delimiter: RocStr) callconv(.C) void {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue