mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
Str Split bitcode
This commit is contained in:
parent
bf81e67a89
commit
626d49d7b0
3 changed files with 91 additions and 12 deletions
|
@ -39,6 +39,53 @@ pub fn pow_int_(mut base: i64, mut exp: i64) -> i64 {
|
||||||
acc
|
acc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub fn str_split_<'a>(
|
||||||
|
list: &'a mut [&'a [u8]],
|
||||||
|
str_: &'a [u8],
|
||||||
|
delimiter: &[u8],
|
||||||
|
) -> &'a [&'a [u8]] {
|
||||||
|
let mut ret_list_index = 0;
|
||||||
|
|
||||||
|
let str_len = str_.len();
|
||||||
|
let delimiter_len = delimiter.len();
|
||||||
|
|
||||||
|
let mut slice_start_index = 0;
|
||||||
|
|
||||||
|
let mut str_index = 0;
|
||||||
|
|
||||||
|
if str_len > delimiter_len {
|
||||||
|
while str_index <= (str_len - delimiter_len) {
|
||||||
|
let mut delimiter_index = 0;
|
||||||
|
let mut matches_delimiter = true;
|
||||||
|
|
||||||
|
while matches_delimiter && delimiter_index < delimiter_len {
|
||||||
|
let delimiter_char = delimiter[delimiter_index];
|
||||||
|
let str_char = str_[str_index + delimiter_index];
|
||||||
|
|
||||||
|
if delimiter_char != str_char {
|
||||||
|
matches_delimiter = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
delimiter_index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches_delimiter {
|
||||||
|
list[ret_list_index] = &str_[slice_start_index..str_index];
|
||||||
|
slice_start_index = str_index + delimiter_len;
|
||||||
|
ret_list_index += 1;
|
||||||
|
str_index += delimiter_len;
|
||||||
|
} else {
|
||||||
|
str_index += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
list[ret_list_index] = &str_[slice_start_index..str_len];
|
||||||
|
|
||||||
|
list
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub fn count_delimiters_(str: &[u8], delimiter: &[u8]) -> i64 {
|
pub fn count_delimiters_(str: &[u8], delimiter: &[u8]) -> i64 {
|
||||||
let mut count: i64 = 0;
|
let mut count: i64 = 0;
|
||||||
|
|
|
@ -3,7 +3,7 @@ extern crate pretty_assertions;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod bitcode {
|
mod bitcode {
|
||||||
use roc_builtins_bitcode::{count_delimiters_, measure_next_split_segment_length_};
|
use roc_builtins_bitcode::{count_delimiters_, measure_next_split_segment_length_, str_split_};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn count_delimiters() {
|
fn count_delimiters() {
|
||||||
|
@ -21,6 +21,47 @@ mod bitcode {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn str_split() {
|
||||||
|
fn splits_to(string: &str, delimiter: &str, expectation: &[&[u8]]) {
|
||||||
|
assert_eq!(
|
||||||
|
str_split_(
|
||||||
|
&mut [(&"").as_bytes()].repeat(expectation.len()),
|
||||||
|
&string.as_bytes(),
|
||||||
|
&delimiter.as_bytes()
|
||||||
|
),
|
||||||
|
expectation
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
splits_to(
|
||||||
|
"a!b!c",
|
||||||
|
"!",
|
||||||
|
&[(&"a").as_bytes(), (&"b").as_bytes(), (&"c").as_bytes()],
|
||||||
|
);
|
||||||
|
|
||||||
|
splits_to(
|
||||||
|
"a!?b!?c!?",
|
||||||
|
"!?",
|
||||||
|
&[
|
||||||
|
(&"a").as_bytes(),
|
||||||
|
(&"b").as_bytes(),
|
||||||
|
(&"c").as_bytes(),
|
||||||
|
(&"").as_bytes(),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
splits_to("abc", "!", &[(&"abc").as_bytes()]);
|
||||||
|
|
||||||
|
splits_to(
|
||||||
|
"tttttghittttt",
|
||||||
|
"ttttt",
|
||||||
|
&[(&"").as_bytes(), (&"ghi").as_bytes(), (&"").as_bytes()],
|
||||||
|
);
|
||||||
|
|
||||||
|
splits_to("def", "!!!!!!", &[(&"def").as_bytes()]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn measure_next_split_segment() {
|
fn measure_next_split_segment() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
|
@ -64,18 +64,9 @@ pub fn str_split<'a, 'ctx, 'env>(
|
||||||
|
|
||||||
let ret_list_ptr = allocate_list(env, inplace, &CHAR_LAYOUT, ret_list_len);
|
let ret_list_ptr = allocate_list(env, inplace, &CHAR_LAYOUT, ret_list_len);
|
||||||
|
|
||||||
let ret_list_loop = |index| {};
|
let ret_list: BasicValueEnum<'ctx> = panic!("Get return list");
|
||||||
|
|
||||||
incrementing_index_loop(
|
call_bitcode_fn(env, &[ret_list, str, delimiter], "str_split_")
|
||||||
builder,
|
|
||||||
ctx,
|
|
||||||
parent,
|
|
||||||
ret_list_len,
|
|
||||||
"str_split_ret_list_len_index",
|
|
||||||
ret_list_loop,
|
|
||||||
);
|
|
||||||
|
|
||||||
empty_list(env)
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue