Str.reserve

This commit is contained in:
Folkert 2022-07-04 14:57:20 +02:00
parent f7b8094dfb
commit ab721dd3c1
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
10 changed files with 32 additions and 0 deletions

View file

@ -157,6 +157,7 @@ comptime {
exportStrFn(str.strEqual, "equal");
exportStrFn(str.substringUnsafe, "substring_unsafe");
exportStrFn(str.getUnsafe, "get_unsafe");
exportStrFn(str.reserve, "reserve");
exportStrFn(str.strToUtf8C, "to_utf8");
exportStrFn(str.fromUtf8C, "from_utf8");
exportStrFn(str.fromUtf8RangeC, "from_utf8_range");

View file

@ -2446,3 +2446,11 @@ test "appendScalar: big 😀" {
try expect(actual.eq(expected));
}
pub fn reserve(string: RocStr, capacity: usize) callconv(.C) RocStr {
if (capacity > string.capacity()) {
return string.reallocate(capacity);
} else {
return string;
}
}

View file

@ -37,6 +37,7 @@ interface Str
splitFirst,
splitLast,
walkUtf8WithIndex,
reserve
]
imports [Bool.{ Bool }, Result.{ Result }]
@ -341,3 +342,6 @@ walkUtf8WithIndexHelp = \string, state, step, index, length ->
walkUtf8WithIndexHelp string newState step (index + 1) length
else
state
## Make sure at least some number of bytes fit in this string without reallocating
reserve : Str, Nat -> Str

View file

@ -333,6 +333,7 @@ pub const STR_TRIM: &str = "roc_builtins.str.trim";
pub const STR_TRIM_LEFT: &str = "roc_builtins.str.trim_left";
pub const STR_TRIM_RIGHT: &str = "roc_builtins.str.trim_right";
pub const STR_GET_UNSAFE: &str = "roc_builtins.str.get_unsafe";
pub const STR_RESERVE: &str = "roc_builtins.str.reserve";
pub const DICT_HASH: &str = "roc_builtins.dict.hash";
pub const DICT_HASH_STR: &str = "roc_builtins.dict.hash_str";