-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of libversion algorithm implementation in rust
This is my first code in rust, it's partially naive rewrite from C, partially switch to obvious rust features sich as bitfields, string slices and iterator. The code compiles and passes minimal tests.
- Loading branch information
0 parents
commit ef97a52
Showing
8 changed files
with
572 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "libversion-rs" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
bitflags = "2.4.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::component::Component; | ||
use crate::string::{is_alpha, to_lower}; | ||
|
||
pub fn compare_components(a: &Component, b: &Component) -> i8 { | ||
// precedence has highest priority | ||
if a.precedence < b.precedence { | ||
return -1; | ||
} | ||
if a.precedence > b.precedence { | ||
return 1; | ||
} | ||
|
||
// empty strings come before everything | ||
if a.value.is_empty() && b.value.is_empty() { | ||
return 0; | ||
} | ||
if a.value.is_empty() { | ||
return -1; | ||
} | ||
if b.value.is_empty() { | ||
return 1; | ||
} | ||
|
||
// alpha come before numbers | ||
let a_first = a.value.chars().nth(0).unwrap(); | ||
let b_first = b.value.chars().nth(0).unwrap(); | ||
let a_is_alpha = is_alpha(a_first); | ||
let b_is_alpha = is_alpha(b_first); | ||
|
||
if a_is_alpha && b_is_alpha { | ||
if to_lower(a_first) < to_lower(b_first) { | ||
return -1; | ||
} | ||
if to_lower(a_first) > to_lower(b_first) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
if a_is_alpha { | ||
return -1; | ||
} | ||
if b_is_alpha { | ||
return 1; | ||
} | ||
|
||
// numeric comparison (note that leading zeroes are already trimmed here) | ||
if a.value.len() < b.value.len() { | ||
return -1; | ||
} | ||
if a.value.len() > b.value.len() { | ||
return 1; | ||
} | ||
|
||
if a.value < b.value { | ||
return -1; | ||
} | ||
if a.value > b.value { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
//assert_eq!(version_compare2("1.0", "1.0"), 0); | ||
//assert_eq!(version_compare2("1.0", "1.1"), -1); | ||
//assert_eq!(version_compare2("1.1", "1.0"), 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#[derive(PartialEq, PartialOrd)] | ||
pub enum ComponentPrecedence { | ||
LowerBound, | ||
PreRelease, | ||
Zero, | ||
PostRelease, | ||
NonZero, | ||
LetterSuffix, | ||
UpperBound, | ||
} | ||
|
||
pub struct Component<'a> { | ||
pub precedence: ComponentPrecedence, | ||
pub value: &'a str, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::mem; | ||
use crate::Flags; | ||
use crate::component::Component; | ||
use crate::parse::{SomeComponents, get_next_version_component}; | ||
|
||
pub enum IteratedComponent { | ||
|
||
} | ||
|
||
pub struct VersionComponentIterator<'a> { | ||
rest_of_version: &'a str, | ||
needs_trailing_component: bool, | ||
carried_component: Option<Component<'a>>, | ||
flags: Flags, | ||
} | ||
|
||
impl VersionComponentIterator<'_> { | ||
pub fn new<'a>(version: &'a str, flags: Flags) -> VersionComponentIterator<'a> { | ||
return VersionComponentIterator{ | ||
rest_of_version: version, | ||
needs_trailing_component: flags.contains(Flags::LowerBound | Flags::UpperBound), | ||
carried_component: None, | ||
flags | ||
}; | ||
} | ||
|
||
pub fn next(&mut self) -> Component { | ||
if let Some(component) = mem::take(&mut self.carried_component) { | ||
return component; | ||
} | ||
|
||
let (components, rest_of_version) = get_next_version_component(self.rest_of_version, self.flags); | ||
|
||
self.rest_of_version = rest_of_version; | ||
|
||
match components { | ||
SomeComponents::One(component) => { | ||
return component; | ||
}, | ||
SomeComponents::Two(component1, component2) => { | ||
self.carried_component = Some(component2); | ||
return component1; | ||
} | ||
} | ||
} | ||
|
||
pub fn is_exhausted(&self) -> bool { | ||
return self.rest_of_version.is_empty(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use bitflags::bitflags; | ||
use crate::iter::VersionComponentIterator; | ||
use crate::compare::compare_components; | ||
|
||
mod string; | ||
mod parse; | ||
mod component; | ||
mod compare; | ||
mod iter; | ||
|
||
bitflags! { | ||
#[derive(Clone, Copy)] | ||
pub struct Flags: u32 { | ||
const PIsPatch = 0b00000001; | ||
const AnyIsPatch = 0b00000010; | ||
const LowerBound = 0b00000100; | ||
const UpperBound = 0b00001000; | ||
} | ||
} | ||
|
||
pub fn version_compare4(v1: &str, v2: &str, v1_flags: Flags, v2_flags: Flags) -> i8 { | ||
let mut v1_it = VersionComponentIterator::new(v1, v1_flags); | ||
let mut v2_it = VersionComponentIterator::new(v2, v2_flags); | ||
|
||
let mut v1_need_extra_component = v1_flags.contains(Flags::LowerBound | Flags::UpperBound); | ||
let mut v2_need_extra_component = v2_flags.contains(Flags::LowerBound | Flags::UpperBound); | ||
|
||
loop { | ||
let v1_comp = v1_it.next(); | ||
let v2_comp = v2_it.next(); | ||
|
||
let res = compare_components(&v1_comp, &v2_comp); | ||
if res != 0 { | ||
return res; | ||
} | ||
|
||
if v1_it.is_exhausted() && v2_it.is_exhausted() { | ||
if !v1_need_extra_component && !v2_need_extra_component { | ||
return 0; | ||
} | ||
if v1_need_extra_component { | ||
v1_need_extra_component = false; | ||
} | ||
if v2_need_extra_component { | ||
v2_need_extra_component = false; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
pub fn version_compare2(v1: &str, v2: &str) -> i8 { | ||
return version_compare4(v1, v2, Flags::empty(), Flags::empty()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_version_compare() { | ||
assert_eq!(version_compare2("1.0", "1.0"), 0); | ||
assert_eq!(version_compare2("1.0", "1.1"), -1); | ||
assert_eq!(version_compare2("1.1", "1.0"), 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
use crate::string::*; | ||
use crate::component::*; | ||
use crate::Flags; | ||
|
||
#[derive(PartialEq, Debug)] | ||
pub enum KeywordClass { | ||
Unknown, | ||
PreRelease, | ||
PostRelease, | ||
} | ||
|
||
pub fn classify_keyword(s: &str, flags: Flags) -> KeywordClass { | ||
if strings_are_equal_ci(s, "alpha") { | ||
return KeywordClass::PreRelease; | ||
} else if strings_are_equal_ci(s, "beta") { | ||
return KeywordClass::PreRelease; | ||
} else if strings_are_equal_ci(s, "rc") { | ||
return KeywordClass::PreRelease; | ||
} else if string_has_prefix_ci(s, "pre") { | ||
return KeywordClass::PreRelease; | ||
} else if string_has_prefix_ci(s, "post") { | ||
return KeywordClass::PostRelease; | ||
} else if string_has_prefix_ci(s, "patch") { | ||
return KeywordClass::PostRelease; | ||
} else if strings_are_equal_ci(s, "pl") { // patchlevel | ||
return KeywordClass::PostRelease; | ||
} else if strings_are_equal_ci(s, "errata") { | ||
return KeywordClass::PostRelease; | ||
} else if flags.contains(Flags::PIsPatch) && strings_are_equal_ci(s, "p") { | ||
return KeywordClass::PostRelease; | ||
} | ||
return KeywordClass::Unknown; | ||
} | ||
|
||
pub fn parse_token_to_component(s: &str, flags: Flags) -> (Component, &str) { | ||
if is_alpha(s.chars().nth(0).unwrap()) { | ||
let (alpha, rest) = split_alpha(s); | ||
return ( | ||
Component{ | ||
precedence: match classify_keyword(alpha, flags) { | ||
KeywordClass::Unknown => if flags.contains(Flags::AnyIsPatch) { ComponentPrecedence::PostRelease } else { ComponentPrecedence::PreRelease }, | ||
KeywordClass::PreRelease => ComponentPrecedence::PreRelease, | ||
KeywordClass::PostRelease => ComponentPrecedence::PostRelease, | ||
}, | ||
value: alpha, | ||
}, | ||
rest | ||
) | ||
} else { | ||
let s = skip_zeroes(s); | ||
let (number, rest) = split_number(s); | ||
return ( | ||
Component{ | ||
precedence: if s.is_empty() { ComponentPrecedence::Zero } else { ComponentPrecedence::NonZero }, | ||
value: number, | ||
}, | ||
rest | ||
) | ||
} | ||
} | ||
|
||
pub fn make_default_component(flags: Flags) -> Component<'static> { | ||
return Component{ | ||
precedence: | ||
if flags.contains(Flags::LowerBound) { | ||
ComponentPrecedence::LowerBound | ||
} else if flags.contains(Flags::UpperBound) { | ||
ComponentPrecedence::UpperBound | ||
} else { | ||
ComponentPrecedence::Zero | ||
}, | ||
value: "", | ||
}; | ||
} | ||
|
||
pub enum SomeComponents<'a> { | ||
One(Component<'a>), | ||
Two(Component<'a>, Component<'a>), | ||
} | ||
|
||
pub fn get_next_version_component(s: &str, flags: Flags) -> (SomeComponents, &str) { | ||
let s = skip_separator(s); | ||
|
||
if s.is_empty() { | ||
return (SomeComponents::One(make_default_component(flags)), s); | ||
} | ||
|
||
let (component, rest) = parse_token_to_component(s, flags); | ||
|
||
let (alpha, rest_after_alpha) = split_alpha(rest); | ||
|
||
if !alpha.is_empty() && !rest_after_alpha.chars().nth(0).is_some_and(|c| is_number(c)) { | ||
return ( | ||
SomeComponents::Two( | ||
component, | ||
Component{ | ||
precedence: match classify_keyword(alpha, flags) { | ||
KeywordClass::Unknown => ComponentPrecedence::LetterSuffix, | ||
KeywordClass::PreRelease => ComponentPrecedence::PreRelease, | ||
KeywordClass::PostRelease => ComponentPrecedence::PostRelease, | ||
}, | ||
value: alpha | ||
}, | ||
), | ||
rest_after_alpha | ||
); | ||
} | ||
|
||
return (SomeComponents::One(component), rest); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_classify_keyword() { | ||
assert_eq!(classify_keyword("ALPHA", Flags::empty()), KeywordClass::PreRelease); | ||
assert_eq!(classify_keyword("ALPHABET", Flags::empty()), KeywordClass::Unknown); | ||
assert_eq!(classify_keyword("BETA", Flags::empty()), KeywordClass::PreRelease); | ||
assert_eq!(classify_keyword("BETAKE", Flags::empty()), KeywordClass::Unknown); | ||
assert_eq!(classify_keyword("RC", Flags::empty()), KeywordClass::PreRelease); | ||
assert_eq!(classify_keyword("PRE", Flags::empty()), KeywordClass::PreRelease); | ||
assert_eq!(classify_keyword("PRERELEASE", Flags::empty()), KeywordClass::PreRelease); | ||
assert_eq!(classify_keyword("POST", Flags::empty()), KeywordClass::PostRelease); | ||
assert_eq!(classify_keyword("POSTRELEASE", Flags::empty()), KeywordClass::PostRelease); | ||
assert_eq!(classify_keyword("PATCH", Flags::empty()), KeywordClass::PostRelease); | ||
assert_eq!(classify_keyword("PATCHLEVEL", Flags::empty()), KeywordClass::PostRelease); | ||
assert_eq!(classify_keyword("PL", Flags::empty()), KeywordClass::PostRelease); | ||
assert_eq!(classify_keyword("ERRATA", Flags::empty()), KeywordClass::PostRelease); | ||
|
||
assert_eq!(classify_keyword("FOOBAR", Flags::empty()), KeywordClass::Unknown); | ||
} | ||
} |
Oops, something went wrong.