diff options
author | ThoFrank <thomas@franks-im-web.de> | 2022-09-17T07·23+0200 |
---|---|---|
committer | Thomas Frank <thomas@franks-im-web.de> | 2022-09-17T14·48+0000 |
commit | ef80d00b06f3d90e37a94d65ce8c56062b19a43a (patch) | |
tree | 9fcf1c54a5b6d3b50248fea5383e0a01f5a97ad1 /tvix/eval/src/builtins/versions.rs | |
parent | 1ee0f670b96de50c23f8b160993853af35e18038 (diff) |
feat(tvix_eval): Support builtins.compareVersions r/4885
Added an Iterator over &str wich yields the VersionParts. Change-Id: I8043d423127446a173d01d290aab10de0c24a6fc Reviewed-on: https://cl.tvl.fyi/c/depot/+/6619 Reviewed-by: tazjin <tazjin@tvl.su> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/builtins/versions.rs')
-rw-r--r-- | tvix/eval/src/builtins/versions.rs | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/tvix/eval/src/builtins/versions.rs b/tvix/eval/src/builtins/versions.rs new file mode 100644 index 000000000000..cc36ae5b6e0a --- /dev/null +++ b/tvix/eval/src/builtins/versions.rs @@ -0,0 +1,108 @@ +use std::ops::RangeInclusive; + +/// Version strings can be broken up into Parts. +/// One Part represents either a string of digits or characters. +/// '.' and '_' represent deviders between parts and are not included in any part. +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)] +pub enum VersionPart<'a> { + Word(&'a str), + Number(u64), +} + +/// Type used to hold information about a VersionPart during creation +enum InternalPart { + Number { range: RangeInclusive<usize> }, + Word { range: RangeInclusive<usize> }, + Break, +} + +/// An iterator which yields the parts of a version string. +/// +/// This can then be directly used to compare two versions +pub struct VersionPartsIter<'a> { + cached_part: InternalPart, + iter: std::str::CharIndices<'a>, + version: &'a str, +} + +impl<'a> VersionPartsIter<'a> { + pub fn new(version: &'a str) -> Self { + Self { + cached_part: InternalPart::Break, + iter: version.char_indices(), + version, + } + } +} + +impl<'a> Iterator for VersionPartsIter<'a> { + type Item = VersionPart<'a>; + + fn next(&mut self) -> Option<Self::Item> { + let char = self.iter.next(); + + if char.is_none() { + let cached_part = std::mem::replace(&mut self.cached_part, InternalPart::Break); + match cached_part { + InternalPart::Break => return None, + InternalPart::Number { range } => { + return Some(VersionPart::Number(self.version[range].parse().unwrap())) + } + InternalPart::Word { range } => { + return Some(VersionPart::Word(&self.version[range])) + } + } + } + + let (pos, char) = char.unwrap(); + match char { + // Divider encountered + '.' | '_' => { + let cached_part = std::mem::replace(&mut self.cached_part, InternalPart::Break); + match cached_part { + InternalPart::Number { range } => { + Some(VersionPart::Number(self.version[range].parse().unwrap())) + } + InternalPart::Word { range } => Some(VersionPart::Word(&self.version[range])), + InternalPart::Break => self.next(), + } + } + + // digit encountered + _ if char.is_ascii_digit() => { + let cached_part = std::mem::replace( + &mut self.cached_part, + InternalPart::Number { range: pos..=pos }, + ); + match cached_part { + InternalPart::Number { range } => { + self.cached_part = InternalPart::Number { + range: *range.start()..=*range.end() + 1, + }; + self.next() + } + InternalPart::Word { range } => Some(VersionPart::Word(&self.version[range])), + InternalPart::Break => self.next(), + } + } + + // char encountered + _ => { + let mut cached_part = InternalPart::Word { range: pos..=pos }; + std::mem::swap(&mut cached_part, &mut self.cached_part); + match cached_part { + InternalPart::Word { range } => { + self.cached_part = InternalPart::Word { + range: *range.start()..=*range.end() + char.len_utf8(), + }; + self.next() + } + InternalPart::Number { range } => { + Some(VersionPart::Number(self.version[range].parse().unwrap())) + } + InternalPart::Break => self.next(), + } + } + } + } +} |