Refactor beast::SemanticVersion (RIPD-199)

This commit is contained in:
Nik Bougalis
2014-08-06 23:33:20 -07:00
parent bf403a6142
commit 0857c6350d
2 changed files with 300 additions and 227 deletions

View File

@@ -20,8 +20,9 @@
#ifndef BEAST_SEMANTICVERSION_H_INCLUDED
#define BEAST_SEMANTICVERSION_H_INCLUDED
#include <beast/strings/String.h>
#include <beast/module/core/text/StringArray.h>
#include <vector>
#include <string>
#include <beast/utility/noexcept.h>
namespace beast {
@@ -36,47 +37,79 @@ namespace beast {
class SemanticVersion
{
public:
typedef std::vector<std::string> identifier_list;
int majorVersion;
int minorVersion;
int patchVersion;
StringArray preReleaseIdentifiers;
StringArray metaData;
identifier_list preReleaseIdentifiers;
identifier_list metaData;
SemanticVersion ();
SemanticVersion (std::string const& version);
/** Parse a semantic version string.
The parsing is as strict as possible.
@return `true` if the string was parsed.
*/
bool parse (String input);
bool parse (std::string const& input, bool debug = false);
/** Produce a string from semantic version components. */
String print () const;
std::string print () const;
inline bool isRelease () const noexcept { return preReleaseIdentifiers.size () <= 0; }
inline bool isPreRelease () const noexcept { return ! isRelease (); }
/** Compare this against another version.
The comparison follows the rules as per the specification.
*/
int compare (SemanticVersion const& rhs) const noexcept;
inline bool operator== (SemanticVersion const& other) const noexcept { return compare (other) == 0; }
inline bool operator!= (SemanticVersion const& other) const noexcept { return compare (other) != 0; }
inline bool operator>= (SemanticVersion const& other) const noexcept { return compare (other) >= 0; }
inline bool operator<= (SemanticVersion const& other) const noexcept { return compare (other) <= 0; }
inline bool operator> (SemanticVersion const& other) const noexcept { return compare (other) > 0; }
inline bool operator< (SemanticVersion const& other) const noexcept { return compare (other) < 0; }
private:
static bool isNumeric (String const& s);
static String printIdentifiers (StringArray const& list);
static bool chop (String const& what, String& input);
static bool chopUInt (int* value, int limit, String& input);
static bool chopIdentifier (String* value, bool allowLeadingZeroes, String& input);
static bool chopIdentifiers (StringArray* value, bool preRelease, String& input);
inline bool isRelease () const noexcept
{
return preReleaseIdentifiers.empty();
}
inline bool isPreRelease () const noexcept
{
return !isRelease ();
}
};
/** Compare two SemanticVersions against each other.
The comparison follows the rules as per the specification.
*/
int compare (SemanticVersion const& lhs, SemanticVersion const& rhs);
inline bool
operator== (SemanticVersion const& lhs, SemanticVersion const& rhs)
{
return compare (lhs, rhs) == 0;
}
inline bool
operator!= (SemanticVersion const& lhs, SemanticVersion const& rhs)
{
return compare (lhs, rhs) != 0;
}
inline bool
operator>= (SemanticVersion const& lhs, SemanticVersion const& rhs)
{
return compare (lhs, rhs) >= 0;
}
inline bool
operator<= (SemanticVersion const& lhs, SemanticVersion const& rhs)
{
return compare (lhs, rhs) <= 0;
}
inline bool
operator> (SemanticVersion const& lhs, SemanticVersion const& rhs)
{
return compare (lhs, rhs) > 0;
}
inline bool
operator< (SemanticVersion const& lhs, SemanticVersion const& rhs)
{
return compare (lhs, rhs) < 0;
}
} // beast
#endif