mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 19:58:18 +00:00
PR: rename to List.concatUtf8
This commit is contained in:
parent
33e8a7a439
commit
f7bec802c0
18 changed files with 81 additions and 89 deletions
|
@ -80,7 +80,7 @@ It's one thing to actually write these functions, it's _another_ thing to let th
|
|||
|
||||
## Specifying how we pass args to the function
|
||||
|
||||
### builtins/mono/src/borrow.rs
|
||||
### builtins/mono/src/inc_dec.rs
|
||||
|
||||
After we have all of this, we need to specify if the arguments we're passing are owned, borrowed or irrelevant. Towards the bottom of this file, add a new case for your builtin and specify each arg. Be sure to read the comment, as it explains this in more detail.
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const std = @import("std");
|
||||
const utils = @import("utils.zig");
|
||||
const str = @import("str.zig");
|
||||
const UpdateMode = utils.UpdateMode;
|
||||
const mem = std.mem;
|
||||
const math = std.math;
|
||||
|
@ -1033,3 +1034,34 @@ test "listConcat: non-unique with unique overlapping" {
|
|||
|
||||
try expect(concatted.eql(wanted));
|
||||
}
|
||||
|
||||
pub fn listConcatUtf8(
|
||||
list: RocList,
|
||||
string: str.RocStr,
|
||||
) callconv(.C) RocList {
|
||||
if (string.len() == 0) {
|
||||
return list;
|
||||
} else {
|
||||
const combined_length = list.len() + string.len();
|
||||
|
||||
// List U8 has alignment 1 and element_width 1
|
||||
var result = list.reallocate(1, combined_length, 1);
|
||||
// We just allocated combined_length, which is > 0 because string.len() > 0
|
||||
var bytes = result.bytes orelse unreachable;
|
||||
@memcpy(bytes[list.len()..combined_length], string.asU8ptr()[0..string.len()]);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
test "listConcatUtf8" {
|
||||
const list = RocList.fromSlice(u8, &[_]u8{ 1, 2, 3, 4 });
|
||||
defer list.decref(1);
|
||||
const string_bytes = "🐦";
|
||||
const string = str.RocStr.init(string_bytes, string_bytes.len);
|
||||
defer string.decref();
|
||||
const ret = listConcatUtf8(list, string);
|
||||
const expected = RocList.fromSlice(u8, &[_]u8{ 1, 2, 3, 4, 240, 159, 144, 166 });
|
||||
defer expected.decref(1);
|
||||
try expect(ret.eql(expected));
|
||||
}
|
||||
|
|
|
@ -85,6 +85,7 @@ comptime {
|
|||
exportListFn(list.listCapacity, "capacity");
|
||||
exportListFn(list.listAllocationPtr, "allocation_ptr");
|
||||
exportListFn(list.listReleaseExcessCapacity, "release_excess_capacity");
|
||||
exportListFn(list.listConcatUtf8, "concat_utf8");
|
||||
}
|
||||
|
||||
// Num Module
|
||||
|
@ -210,7 +211,6 @@ comptime {
|
|||
exportStrFn(str.withCapacityC, "with_capacity");
|
||||
exportStrFn(str.strAllocationPtr, "allocation_ptr");
|
||||
exportStrFn(str.strReleaseExcessCapacity, "release_excess_capacity");
|
||||
exportStrFn(str.strConcatUtf8, "concat_utf8");
|
||||
|
||||
inline for (INTEGERS) |T| {
|
||||
str.exportFromInt(T, ROC_BUILTINS ++ "." ++ STR ++ ".from_int.");
|
||||
|
|
|
@ -2390,34 +2390,3 @@ pub fn strReleaseExcessCapacity(
|
|||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn strConcatUtf8(
|
||||
list: RocList,
|
||||
string: RocStr,
|
||||
) callconv(.C) RocList {
|
||||
if (string.len() == 0) {
|
||||
return list;
|
||||
} else {
|
||||
const combined_length = list.len() + string.len();
|
||||
|
||||
// XXX: I assume List U8 has alignment 1 and element_width 1?
|
||||
var result = list.reallocate(1, combined_length, 1);
|
||||
// We just allocated combined_length, which is > 0 because string.len() > 0
|
||||
var bytes = result.bytes orelse unreachable;
|
||||
@memcpy(bytes[list.len()..combined_length], string.asU8ptr()[0..string.len()]);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
test "strConcatUtf8" {
|
||||
const list = RocList.fromSlice(u8, &[_]u8{ 1, 2, 3, 4 });
|
||||
defer list.decref(1);
|
||||
const string_bytes = "🐦";
|
||||
const string = RocStr.init(string_bytes, string_bytes.len);
|
||||
defer string.decref();
|
||||
const ret = strConcatUtf8(list, string);
|
||||
const expected = RocList.fromSlice(u8, &[_]u8{ 1, 2, 3, 4, 240, 159, 144, 166 });
|
||||
defer expected.decref(1);
|
||||
try expect(ret.eql(expected));
|
||||
}
|
||||
|
|
|
@ -69,11 +69,12 @@ module [
|
|||
walkBackwardsUntil,
|
||||
countIf,
|
||||
chunksOf,
|
||||
concatUtf8,
|
||||
]
|
||||
|
||||
import Bool exposing [Bool, Eq]
|
||||
import Result exposing [Result]
|
||||
import Num exposing [U64, Num]
|
||||
import Num exposing [U64, Num, U8]
|
||||
|
||||
## ## Types
|
||||
##
|
||||
|
@ -1324,3 +1325,12 @@ iterBackwardsHelp = \list, state, f, prevIndex ->
|
|||
Break b -> Break b
|
||||
else
|
||||
Continue state
|
||||
|
||||
## Concatenates the bytes of a string encoded as utf8 to a list of bytes.
|
||||
## ```roc
|
||||
## expect (List.concatUtf8 [1, 2, 3, 4] "🐦") == [1, 2, 3, 4, 240, 159, 144, 166]
|
||||
## ```
|
||||
concatUtf8 : List U8, Str -> List U8
|
||||
|
||||
expect (List.concatUtf8 [1, 2, 3, 4] "🐦") == [1, 2, 3, 4, 240, 159, 144, 166]
|
||||
|
||||
|
|
|
@ -367,7 +367,6 @@ module [
|
|||
withCapacity,
|
||||
withPrefix,
|
||||
contains,
|
||||
concatUtf8,
|
||||
]
|
||||
|
||||
import Bool exposing [Bool]
|
||||
|
@ -561,14 +560,6 @@ FromUtf8Result : {
|
|||
|
||||
fromUtf8Lowlevel : List U8 -> FromUtf8Result
|
||||
|
||||
## Concatenates the bytes of a string encoded as utf8 to a list of bytes.
|
||||
## ```roc
|
||||
## expect (Str.concatUtf8 [1, 2, 3, 4] "🐦") == [1, 2, 3, 4, 240, 159, 144, 166]
|
||||
## ```
|
||||
concatUtf8 : List U8, Str -> List U8
|
||||
|
||||
expect (Str.concatUtf8 [1, 2, 3, 4] "🐦") == [1, 2, 3, 4, 240, 159, 144, 166]
|
||||
|
||||
## Check if the given [Str] starts with a value.
|
||||
## ```roc
|
||||
## expect Str.startsWith "ABC" "A" == Bool.true
|
||||
|
|
|
@ -369,7 +369,6 @@ pub const STR_CLONE_TO: &str = "roc_builtins.str.clone_to";
|
|||
pub const STR_WITH_CAPACITY: &str = "roc_builtins.str.with_capacity";
|
||||
pub const STR_ALLOCATION_PTR: &str = "roc_builtins.str.allocation_ptr";
|
||||
pub const STR_RELEASE_EXCESS_CAPACITY: &str = "roc_builtins.str.release_excess_capacity";
|
||||
pub const STR_CONCAT_UTF8: &str = "roc_builtins.str.concat_utf8";
|
||||
|
||||
pub const LIST_MAP: &str = "roc_builtins.list.map";
|
||||
pub const LIST_MAP2: &str = "roc_builtins.list.map2";
|
||||
|
@ -391,6 +390,7 @@ pub const LIST_RESERVE: &str = "roc_builtins.list.reserve";
|
|||
pub const LIST_CAPACITY: &str = "roc_builtins.list.capacity";
|
||||
pub const LIST_ALLOCATION_PTR: &str = "roc_builtins.list.allocation_ptr";
|
||||
pub const LIST_RELEASE_EXCESS_CAPACITY: &str = "roc_builtins.list.release_excess_capacity";
|
||||
pub const LIST_CONCAT_UTF8: &str = "roc_builtins.list.concat_utf8";
|
||||
|
||||
pub const DEC_ABS: &str = "roc_builtins.dec.abs";
|
||||
pub const DEC_ACOS: &str = "roc_builtins.dec.acos";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue