Skip to content

Commit

Permalink
SemVer: Implement string compare
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Oct 11, 2024
1 parent ee22cf8 commit cbbdc0f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 40 deletions.
56 changes: 16 additions & 40 deletions include/SemVer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ namespace OpenShock {
std::string prerelease;
std::string build;

SemVer()
inline SemVer()
: major(0)
, minor(0)
, patch(0)
, prerelease()
, build()
{
}
SemVer(uint16_t major, uint16_t minor, uint16_t patch)
inline SemVer(uint16_t major, uint16_t minor, uint16_t patch)
: major(major)
, minor(minor)
, patch(patch)
, prerelease()
, build()
{
}
SemVer(uint16_t major, uint16_t minor, uint16_t patch, std::string_view prerelease, std::string_view build)
inline SemVer(uint16_t major, uint16_t minor, uint16_t patch, std::string_view prerelease, std::string_view build)
: major(major)
, minor(minor)
, patch(patch)
Expand All @@ -37,43 +37,19 @@ namespace OpenShock {
{
}

bool operator==(const SemVer& other) const { return major == other.major && minor == other.minor && patch == other.patch && prerelease == other.prerelease && build == other.build; }
bool operator!=(const SemVer& other) const { return !(*this == other); }
bool operator<(const SemVer& other) const
{
if (major < other.major) {
return true;
}
if (major > other.major) {
return false;
}

if (minor < other.minor) {
return true;
}
if (minor > other.minor) {
return false;
}

if (patch < other.patch) {
return true;
}
if (patch > other.patch) {
return false;
}

if (prerelease < other.prerelease) {
return true;
}
if (prerelease > other.prerelease) {
return false;
}

return build < other.build;
}
bool operator<=(const SemVer& other) const { return *this < other || *this == other; }
bool operator>(const SemVer& other) const { return !(*this <= other); }
bool operator>=(const SemVer& other) const { return !(*this < other); }
bool operator==(const SemVer& other) const;
inline bool operator!=(const SemVer& other) const { return !(*this == other); }
bool operator<(const SemVer& other) const;
inline bool operator<=(const SemVer& other) const { return *this < other || *this == other; }
inline bool operator>(const SemVer& other) const { return !(*this <= other); }
inline bool operator>=(const SemVer& other) const { return !(*this < other); }

bool operator==(const std::string_view& other) const;
inline bool operator!=(const std::string_view& other) const { return !(*this == other); }
bool operator<(const std::string_view& other) const;
inline bool operator<=(const std::string_view& other) const { return *this < other || *this == other; }
inline bool operator>(const std::string_view& other) const { return !(*this <= other); }
inline bool operator>=(const std::string_view& other) const { return !(*this < other); }

bool isValid() const;

Expand Down
58 changes: 58 additions & 0 deletions src/SemVer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,64 @@ std::string SemVer::toString() const
return str;
}

bool SemVer::operator==(const SemVer& other) const
{
return major == other.major && minor == other.minor && patch == other.patch && prerelease == other.prerelease && build == other.build;
}

bool SemVer::operator<(const SemVer& other) const
{
if (major < other.major) {
return true;
}
if (major > other.major) {
return false;
}

if (minor < other.minor) {
return true;
}
if (minor > other.minor) {
return false;
}

if (patch < other.patch) {
return true;
}
if (patch > other.patch) {
return false;
}

if (prerelease < other.prerelease) {
return true;
}
if (prerelease > other.prerelease) {
return false;
}

return build < other.build;
}

bool SemVer::operator==(const std::string_view& other) const
{
SemVer otherSemVer;
if (!OpenShock::TryParseSemVer(other, otherSemVer)) {
return false;
}

return *this == otherSemVer;
}

bool SemVer::operator<(const std::string_view& other) const
{
SemVer otherSemVer;
if (!OpenShock::TryParseSemVer(other, otherSemVer)) {
return false;
}

return *this < otherSemVer;
}

bool OpenShock::TryParseSemVer(std::string_view semverStr, SemVer& semver)
{
std::string_view parts[3];
Expand Down

0 comments on commit cbbdc0f

Please sign in to comment.