Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/univers/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,11 @@ class NoneVersionRange(VersionRange):
version_class = versions.NoneVersion


class LexicographicVersionRange(VersionRange):
scheme = "lexicographic"
version_class = versions.LexicographicVersion


def from_gitlab_native(gitlab_scheme, string):
purl_scheme = gitlab_scheme
if gitlab_scheme not in PURL_TYPE_BY_GITLAB_SCHEME.values():
Expand Down Expand Up @@ -1446,6 +1451,7 @@ def build_range_from_snyk_advisory_string(scheme: str, string: Union[str, List])
"all": AllVersionRange,
"none": NoneVersionRange,
"intdot": IntdotVersionRange,
"lexicographic": LexicographicVersionRange,
}

PURL_TYPE_BY_GITLAB_SCHEME = {
Expand Down
24 changes: 24 additions & 0 deletions src/univers/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ def is_valid(cls, string):
return string == "vers:none/*"


class LexicographicVersion(Version):
@classmethod
def build_value(cls, string):
return str(string)

"""
Create a string, even if, e.g., an integer is given
"""

@classmethod
def normalize(cls, string):
return remove_spaces(str(string))

def __lt__(self, other):
return self.value.encode("utf-8") < other.value.encode("utf-8")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some reference docs that explain how two UTF-8 byte strings are compared, so we can get that working beyond Python?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this conforms with rfc 3629 which states

UTF-8 encodes UCS characters as a varying number of octets, where the number of octets, and the value of each, depend on the integer value assigned to the character in ISO/IEC 10646 (the character number, a.k.a. code position, code point or Unicode scalar value). This encoding form has the following characteristics (all values are in hexadecimal):
...

  • The byte-value lexicographic sorting order of UTF-8 strings is the same as if ordered by character numbers. Of course this is of limited interest since a sort order based on character numbers is almost never culturally valid.

https://www.rfc-editor.org/rfc/rfc3629.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for checking!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np. Thank you for making the PR and following this through, it seems like a good general purpose version scheme to have available. 👍


def __gt__(self, other):
return self.value.encode("utf-8") > other.value.encode("utf-8")

def __eq__(self, other):
return self.value.encode("utf-8") == other.value.encode("utf-8")


class IntdotVersion(Version):
@classmethod
def build_value(cls, string):
Expand Down Expand Up @@ -726,4 +749,5 @@ def bump(self, index):
LegacyOpensslVersion,
AlpineLinuxVersion,
IntdotVersion,
LexicographicVersion,
]
10 changes: 10 additions & 0 deletions tests/test_version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from univers.version_range import build_range_from_snyk_advisory_string
from univers.version_range import from_gitlab_native
from univers.versions import IntdotVersion
from univers.versions import LexicographicVersion
from univers.versions import OpensslVersion
from univers.versions import PypiVersion
from univers.versions import SemverVersion
Expand Down Expand Up @@ -366,3 +367,12 @@ def test_version_range_intdot():
assert IntdotVersion("1.3.3alpha") in intdot_range
assert IntdotVersion("1.2.2.pre") not in intdot_range
assert IntdotVersion("1010.23.234203.0") in IntdotVersionRange.from_string("vers:intdot/*")


def test_version_range_lexicographic():
assert LexicographicVersion("1.2.3") in VersionRange.from_string(
"vers:lexicographic/<1.2.4|>0.9"
)
assert LexicographicVersion(-123) in VersionRange.from_string("vers:lexicographic/<~")
assert LexicographicVersion(None) in VersionRange.from_string("vers:lexicographic/*")
assert LexicographicVersion("ABC") in VersionRange.from_string("vers:lexicographic/>abc|<=None")
11 changes: 11 additions & 0 deletions tests/test_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from univers.versions import GentooVersion
from univers.versions import GolangVersion
from univers.versions import IntdotVersion
from univers.versions import LexicographicVersion
from univers.versions import MavenVersion
from univers.versions import NginxVersion
from univers.versions import NugetVersion
Expand Down Expand Up @@ -230,3 +231,13 @@ def test_intdot_version():
assert IntdotVersion("1.2.3.4.6-pre") <= IntdotVersion("2.2.3.4.5.pre")
assert IntdotVersion("1.2.3.4.6-pre") <= IntdotVersion("2.2.3.4.5-10")
assert IntdotVersion("1.2.3.4.6-pre") <= IntdotVersion("2.2.3.4.5-10")


def test_lexicographic_version():
assert LexicographicVersion("abc") == LexicographicVersion("abc")
assert LexicographicVersion(" abc") == LexicographicVersion("abc")
assert LexicographicVersion("123") == LexicographicVersion(123)
assert LexicographicVersion("abc") > LexicographicVersion(None)
assert LexicographicVersion("Abc") < LexicographicVersion(None)
assert LexicographicVersion("123") < LexicographicVersion("bbc")
assert LexicographicVersion("2.3.4") > LexicographicVersion("1.2.3")