mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
Measure next split segment length bitcode function
This commit is contained in:
parent
415a37a891
commit
a11f0ec25a
2 changed files with 75 additions and 1 deletions
|
@ -74,6 +74,35 @@ pub fn count_delimiters_(str: &[u8], delimiter: &[u8]) -> i64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
/// Adapted from Rust's core::num module, by the Rust core team,
|
/// Adapted from Rust's core::num module, by the Rust core team,
|
||||||
/// licensed under the Apache License, version 2.0 - https://www.apache.org/licenses/LICENSE-2.0
|
/// licensed under the Apache License, version 2.0 - https://www.apache.org/licenses/LICENSE-2.0
|
||||||
///
|
///
|
||||||
|
|
|
@ -3,7 +3,7 @@ extern crate pretty_assertions;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod bitcode {
|
mod bitcode {
|
||||||
use roc_builtins_bitcode::count_delimiters_;
|
use roc_builtins_bitcode::{count_delimiters_, measure_next_split_segment_length_};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn count_delimiters() {
|
fn count_delimiters() {
|
||||||
|
@ -20,4 +20,49 @@ mod bitcode {
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn measure_next_split_segment() {
|
||||||
|
assert_eq!(
|
||||||
|
measure_next_split_segment_length_(
|
||||||
|
0,
|
||||||
|
(&"de!!!de!!!de").as_bytes(),
|
||||||
|
(&"!!!").as_bytes()
|
||||||
|
),
|
||||||
|
2
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
measure_next_split_segment_length_(
|
||||||
|
5,
|
||||||
|
(&"de!!!abcde!!!de").as_bytes(),
|
||||||
|
(&"!!!").as_bytes()
|
||||||
|
),
|
||||||
|
5
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
measure_next_split_segment_length_(
|
||||||
|
13,
|
||||||
|
(&"de!!!abcde!!!de").as_bytes(),
|
||||||
|
(&"!!!").as_bytes()
|
||||||
|
),
|
||||||
|
2
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
measure_next_split_segment_length_(0, (&"!!!").as_bytes(), (&"!!!").as_bytes()),
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
measure_next_split_segment_length_(0, (&"a!!b!!!").as_bytes(), (&"!!!").as_bytes()),
|
||||||
|
4
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
measure_next_split_segment_length_(0, (&"abcde!!!").as_bytes(), (&"!!!").as_bytes()),
|
||||||
|
5
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue