3 ms·
> 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 functio
by strager 4y 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/f8d31341f5cac5d0e676ead553a3610284d47a3e/cpp/src/quick-lint-js/i18n/locale.cpp#L38-L43 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/f8d31341f5cac5d0e676ead553a3610284d47a3e/rust/libs/i18n/src/locale.rs#L53-L64 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) {