|
|
|
|
|
by strager
1265 days ago
|
|
> I wonder if OP's C++ port doesn't use iterators that much, and how idiomatic it is. I think I only used iterators in places where there's no built-in function on slices like C++'s strchr and strspn. (I think Rust's str has these, but not [u8].) For example: C++: https://github.com/quick-lint/cpp-vs-rust/blob/f8d31341f5cac... std::size_t length = std::strcspn(c, separators);
if (c[length] == '\0') {
return found_separator{.length = length,
.which_separator = static_cast<std::size_t>(-1)};
}
const char* separator = std::strchr(separators, c[length]);
Rust: https://github.com/quick-lint/cpp-vs-rust/blob/f8d31341f5cac... match s
.as_bytes()
.iter()
.position(|c: &u8| separators.contains(c))
{
None => FoundSeparator {
length: s.len(),
which_separator: INVALID_WHICH_SEPARATOR,
},
Some(length) => {
let found_separator: u8 = unsafe { *s.as_bytes().get_unchecked(length) };
match separators.iter().position(|c: &u8| *c == found_separator) {
|
|