mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 19:58:18 +00:00
Rename count_delimiters to count_segments, since we only count the delimiters in order to determine the number of segments
This commit is contained in:
parent
af3882c8e3
commit
ed4d637201
2 changed files with 13 additions and 44 deletions
|
@ -87,15 +87,13 @@ pub fn str_split_<'a>(
|
|||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn count_delimiters_(str: &[u8], delimiter: &[u8]) -> i64 {
|
||||
let mut count: i64 = 0;
|
||||
pub fn count_segments_(str: &[u8], delimiter: &[u8]) -> i64 {
|
||||
let mut count: i64 = 1;
|
||||
|
||||
let str_len = str.len();
|
||||
let delimiter_len = delimiter.len();
|
||||
|
||||
if delimiter_len > str_len {
|
||||
0
|
||||
} else {
|
||||
if str_len > delimiter_len {
|
||||
for str_index in 0..(str_len - delimiter_len) {
|
||||
let mut delimiter_index = 0;
|
||||
|
||||
|
@ -116,38 +114,9 @@ pub fn count_delimiters_(str: &[u8], delimiter: &[u8]) -> i64 {
|
|||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
count
|
||||
}
|
||||
}
|
||||
|
||||
pub fn measure_next_split_segment_length_(from_index: i64, str: &[u8], delimiter: &[u8]) -> i64 {
|
||||
let str_len = str.len() as i64;
|
||||
let delimiter_len = delimiter.len() as i64;
|
||||
|
||||
let mut str_index = from_index;
|
||||
|
||||
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 as usize];
|
||||
let str_char = str[(str_index + delimiter_index) as usize];
|
||||
|
||||
matches_delimiter = delimiter_char == str_char;
|
||||
|
||||
delimiter_index += 1;
|
||||
}
|
||||
|
||||
if matches_delimiter {
|
||||
return str_index - from_index;
|
||||
} else {
|
||||
str_index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
str_len - from_index
|
||||
count
|
||||
}
|
||||
|
||||
/// Adapted from Rust's core::num module, by the Rust core team,
|
||||
|
|
|
@ -3,21 +3,21 @@ extern crate pretty_assertions;
|
|||
|
||||
#[cfg(test)]
|
||||
mod bitcode {
|
||||
use roc_builtins_bitcode::{count_delimiters_, measure_next_split_segment_length_, str_split_};
|
||||
use roc_builtins_bitcode::{count_segments_, str_split_};
|
||||
|
||||
#[test]
|
||||
fn count_delimiters() {
|
||||
fn count_segments() {
|
||||
assert_eq!(
|
||||
count_delimiters_((&"hello there").as_bytes(), (&"hello").as_bytes()),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
count_delimiters_((&"a\nb\nc").as_bytes(), (&"\n").as_bytes()),
|
||||
count_segments_((&"hello there").as_bytes(), (&"hello").as_bytes()),
|
||||
2
|
||||
);
|
||||
assert_eq!(
|
||||
count_delimiters_((&"str").as_bytes(), (&"delimiter").as_bytes()),
|
||||
0
|
||||
count_segments_((&"a\nb\nc").as_bytes(), (&"\n").as_bytes()),
|
||||
3
|
||||
);
|
||||
assert_eq!(
|
||||
count_segments_((&"str").as_bytes(), (&"delimiter").as_bytes()),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue