1
Fork 0
mirror of https://github.com/RGBCube/dix synced 2025-07-28 04:07:46 +00:00

util: switch to raw strings for VersionComponentIterator

This commit is contained in:
Dragyx 2025-05-07 13:09:39 +02:00 committed by Bloxx12
parent da723c704f
commit 697a72bcef

View file

@ -32,14 +32,14 @@ impl PartialOrd for VersionComponent {
// //
// a component is delimited by '-' or '.' and consists of just digits or letters // a component is delimited by '-' or '.' and consists of just digits or letters
struct VersionComponentIterator<'a> { struct VersionComponentIterator<'a> {
v: &'a str, v: &'a [u8],
pos: usize, pos: usize,
} }
impl<'a> VersionComponentIterator<'a> { impl<'a> VersionComponentIterator<'a> {
pub fn new<I: Into<&'a str>>(v: I) -> Self { pub fn new<I: Into<&'a str>>(v: I) -> Self {
Self { Self {
v: v.into(), v: v.into().as_bytes(),
pos: 0, pos: 0,
} }
} }
@ -49,29 +49,25 @@ impl Iterator for VersionComponentIterator<'_> {
type Item = VersionComponent; type Item = VersionComponent;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
// check if there is something left
if self.pos >= self.v.len() {
return None;
}
// skip all '-' and '.' in the beginning // skip all '-' and '.' in the beginning
let mut skipped_chars = 0; while let Some(b'.' | b'-') = self.v.get(self.pos) {
let mut chars = self.v[self.pos..].chars().peekable(); self.pos += 1;
while let Some('.' | '-') = chars.peek() {
skipped_chars += 1;
chars.next();
} }
// get the next character and decide if it is a digit or char // get the next character and decide if it is a digit or char
let c = chars.peek()?; let c = self.v.get(self.pos)?;
let is_digit = c.is_ascii_digit(); let is_digit = c.is_ascii_digit();
// based on this collect characters after this into the component // based on this collect characters after this into the component
let component: String = chars let component_len = self.v[self.pos..]
.take_while(|&c| c.is_ascii_digit() == is_digit && c != '.' && c != '-') .iter()
.collect(); .copied()
.take_while(|&c| c.is_ascii_digit() == is_digit && c != b'.' && c != b'-')
.count();
let component =
String::from_utf8_lossy(&self.v[self.pos..(self.pos + component_len)]).into_owned();
// remember what chars we used // remember what chars we used
self.pos += component.len() + skipped_chars; self.pos += component_len;
if component.is_empty() { if component.is_empty() {
None None