Moved constexpr string manipulations to cpputils::const_string
This commit is contained in:
parent
3fd5cb7afc
commit
4cf1237bb8
@ -2,13 +2,14 @@
|
||||
#define MESSMER_CRYFS_VERSION_H
|
||||
|
||||
#include <stdexcept>
|
||||
#include <messmer/cpp-utils/constexpr/const_string.h>
|
||||
|
||||
namespace version {
|
||||
enum class VersionTag : unsigned char {
|
||||
ALPHA, BETA, RC1, FINAL
|
||||
};
|
||||
|
||||
constexpr const char *VersionTagToString(VersionTag tag) {
|
||||
constexpr cpputils::const_string VersionTagToString(VersionTag tag) {
|
||||
return (tag == VersionTag::ALPHA) ? "alpha" :
|
||||
(tag == VersionTag::BETA) ? "beta" :
|
||||
(tag == VersionTag::RC1) ? "rc1" :
|
||||
@ -46,7 +47,7 @@ namespace version {
|
||||
}
|
||||
|
||||
std::string toString() const {
|
||||
return std::to_string(_major) + "." + std::to_string(_minor) + VersionTagToString(_tag);
|
||||
return std::to_string(_major) + "." + std::to_string(_minor) + VersionTagToString(_tag).toStdString();
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -2,75 +2,38 @@
|
||||
#define MESSMER_CRYFS_VERSIONPARSER_H
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include "Version.h"
|
||||
#include <messmer/cpp-utils/constexpr/const_string.h>
|
||||
|
||||
namespace version {
|
||||
class VersionParser {
|
||||
public:
|
||||
static constexpr Version parse(const char *input) {
|
||||
static constexpr Version parse(const cpputils::const_string &input) {
|
||||
return Version(VersionParser::extractMajor(input),
|
||||
VersionParser::extractMinor(input),
|
||||
parseTag(extractTag(input)));
|
||||
}
|
||||
|
||||
static constexpr unsigned int extractMajor(const char *input) {
|
||||
return parseNumber(input);
|
||||
static constexpr unsigned int extractMajor(const cpputils::const_string &input) {
|
||||
return input.parseUIntPrefix();
|
||||
}
|
||||
|
||||
static constexpr unsigned int extractMinor(const char *input) {
|
||||
return (skipNumber(input)[0] == '.') ? parseNumber(skipNumber(input) + 1)
|
||||
static constexpr unsigned int extractMinor(const cpputils::const_string &input) {
|
||||
return (input.dropUIntPrefix()[0] == '.') ? input.dropUIntPrefix().dropPrefix(1).parseUIntPrefix()
|
||||
: throw std::logic_error("Minor version should be separated by a dot");
|
||||
}
|
||||
|
||||
static constexpr unsigned int parseNumber(const char *input) {
|
||||
return (isDigit(input[0])) ? parseNumberBackwards(input + numDigits(input) - 1, numDigits(input))
|
||||
: throw std::logic_error("Not a valid number");
|
||||
static constexpr cpputils::const_string extractTag(const cpputils::const_string &input) {
|
||||
return input.dropUIntPrefix().dropPrefix(1).dropUIntPrefix();
|
||||
}
|
||||
|
||||
static constexpr unsigned int numDigits(const char *input) {
|
||||
return (isDigit(input[0])) ? (1 + numDigits(input + 1)) : 0;
|
||||
}
|
||||
|
||||
static constexpr bool isDigit(char digit) {
|
||||
return digit == '0' || digit == '1' || digit == '2' || digit == '3' || digit == '4' || digit == '5' ||
|
||||
digit == '6' || digit == '7' || digit == '8' || digit == '9';
|
||||
}
|
||||
|
||||
static constexpr unsigned char parseDigit(char digit) {
|
||||
return (digit == '0') ? 0 :
|
||||
(digit == '1') ? 1 :
|
||||
(digit == '2') ? 2 :
|
||||
(digit == '3') ? 3 :
|
||||
(digit == '4') ? 4 :
|
||||
(digit == '5') ? 5 :
|
||||
(digit == '6') ? 6 :
|
||||
(digit == '7') ? 7 :
|
||||
(digit == '8') ? 8 :
|
||||
(digit == '9') ? 9 :
|
||||
throw std::logic_error("Not a valid digit");
|
||||
}
|
||||
|
||||
static constexpr const char *extractTag(const char *input) {
|
||||
return skipNumber(skipNumber(input) + 1);
|
||||
}
|
||||
|
||||
static constexpr VersionTag parseTag(const char *input) {
|
||||
return (0 == strcmp(VersionTagToString(VersionTag::ALPHA), input)) ? VersionTag::ALPHA :
|
||||
(0 == strcmp(VersionTagToString(VersionTag::BETA), input)) ? VersionTag::BETA :
|
||||
(0 == strcmp(VersionTagToString(VersionTag::RC1), input)) ? VersionTag::RC1 :
|
||||
(0 == strcmp(VersionTagToString(VersionTag::FINAL), input)) ? VersionTag::FINAL :
|
||||
static constexpr VersionTag parseTag(const cpputils::const_string &input) {
|
||||
return (VersionTagToString(VersionTag::ALPHA) == input) ? VersionTag::ALPHA :
|
||||
(VersionTagToString(VersionTag::BETA) == input) ? VersionTag::BETA :
|
||||
(VersionTagToString(VersionTag::RC1) == input) ? VersionTag::RC1 :
|
||||
(VersionTagToString(VersionTag::FINAL) == input) ? VersionTag::FINAL :
|
||||
throw std::logic_error("Not a valid version tag");
|
||||
}
|
||||
|
||||
static constexpr const char *skipNumber(const char *input) {
|
||||
return (isDigit(input[0])) ? skipNumber(input + 1) : input;
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr unsigned int parseNumberBackwards(const char *input, unsigned int numDigits) {
|
||||
return (numDigits == 0) ? 0 : (parseDigit(*input) + 10 * parseNumberBackwards(input - 1, numDigits - 1));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -4,11 +4,12 @@
|
||||
#include <google/gtest/gtest.h>
|
||||
|
||||
using namespace version;
|
||||
using cpputils::const_string;
|
||||
|
||||
static_assert(0 == strcmp("alpha", VersionTagToString(VersionTag::ALPHA)), "VersionTag::ALPHA toString");
|
||||
static_assert(0 == strcmp("beta", VersionTagToString(VersionTag::BETA)), "VersionTag::BETA toString");
|
||||
static_assert(0 == strcmp("rc1", VersionTagToString(VersionTag::RC1)), "VersionTag::RC1 toString");
|
||||
static_assert(0 == strlen(VersionTagToString(VersionTag::FINAL)), "VersionTag::FINAL toString");
|
||||
static_assert(const_string("alpha") == VersionTagToString(VersionTag::ALPHA), "VersionTag::ALPHA toString");
|
||||
static_assert(const_string("beta") == VersionTagToString(VersionTag::BETA), "VersionTag::BETA toString");
|
||||
static_assert(const_string("rc1") == VersionTagToString(VersionTag::RC1), "VersionTag::RC1 toString");
|
||||
static_assert(const_string("") == VersionTagToString(VersionTag::FINAL), "VersionTag::FINAL toString");
|
||||
|
||||
static_assert(Version(1, 0, VersionTag::ALPHA) == Version(1, 0, VersionTag::ALPHA), "Equality for equals");
|
||||
static_assert(!(Version(1, 0, VersionTag::ALPHA) != Version(1, 0, VersionTag::ALPHA)), "Inequality for equals");
|
||||
|
@ -2,58 +2,7 @@
|
||||
#include <cstring>
|
||||
|
||||
using namespace version;
|
||||
|
||||
static_assert(VersionParser::isDigit('0'), "'0' should be recognized as a digit");
|
||||
static_assert(VersionParser::isDigit('1'), "'1' should be recognized as a digit");
|
||||
static_assert(VersionParser::isDigit('2'), "'2' should be recognized as a digit");
|
||||
static_assert(VersionParser::isDigit('3'), "'3' should be recognized as a digit");
|
||||
static_assert(VersionParser::isDigit('4'), "'4' should be recognized as a digit");
|
||||
static_assert(VersionParser::isDigit('5'), "'5' should be recognized as a digit");
|
||||
static_assert(VersionParser::isDigit('6'), "'6' should be recognized as a digit");
|
||||
static_assert(VersionParser::isDigit('7'), "'7' should be recognized as a digit");
|
||||
static_assert(VersionParser::isDigit('8'), "'8' should be recognized as a digit");
|
||||
static_assert(VersionParser::isDigit('9'), "'9' should be recognized as a digit");
|
||||
static_assert(!VersionParser::isDigit('a'), "'a' should not be recognized as a digit");
|
||||
static_assert(!VersionParser::isDigit('.'), "'.' should not be recognized as a digit");
|
||||
static_assert(!VersionParser::isDigit('-'), "'-' should not be recognized as a digit");
|
||||
static_assert(!VersionParser::isDigit('/'), "'/' should not be recognized as a digit");
|
||||
static_assert(!VersionParser::isDigit(' '), "' ' should not be recognized as a digit");
|
||||
|
||||
static_assert(0 == VersionParser::parseDigit('0'), "'0' should be correctly parsed");
|
||||
static_assert(1 == VersionParser::parseDigit('1'), "'1' should be correctly parsed");
|
||||
static_assert(2 == VersionParser::parseDigit('2'), "'2' should be correctly parsed");
|
||||
static_assert(3 == VersionParser::parseDigit('3'), "'3' should be correctly parsed");
|
||||
static_assert(4 == VersionParser::parseDigit('4'), "'4' should be correctly parsed");
|
||||
static_assert(5 == VersionParser::parseDigit('5'), "'5' should be correctly parsed");
|
||||
static_assert(6 == VersionParser::parseDigit('6'), "'6' should be correctly parsed");
|
||||
static_assert(7 == VersionParser::parseDigit('7'), "'7' should be correctly parsed");
|
||||
static_assert(8 == VersionParser::parseDigit('8'), "'8' should be correctly parsed");
|
||||
static_assert(9 == VersionParser::parseDigit('9'), "'9' should be correctly parsed");
|
||||
|
||||
static_assert(0 == VersionParser::numDigits("ab"), "\"ab\" doesn't have any digits");
|
||||
static_assert(0 == VersionParser::numDigits(""), "\"\" doesn't have any digits");
|
||||
static_assert(1 == VersionParser::numDigits("5"), "\"5\" has 1 digit");
|
||||
static_assert(1 == VersionParser::numDigits("5a"), "\"5\" has 1 digit");
|
||||
static_assert(10 == VersionParser::numDigits("5594839203a"), "\"5594839203a\" has 1 digit");
|
||||
static_assert(10 == VersionParser::numDigits("5594839203"), "\"5594839203\" has 1 digit");
|
||||
|
||||
static_assert(0 == VersionParser::parseNumber("0"), "\"0\" should be correctly parsed");
|
||||
static_assert(0 == VersionParser::parseNumber("0a"), "\"0a\" should be correctly parsed");
|
||||
static_assert(0 == VersionParser::parseNumber("0."), "\"0.\" should be correctly parsed");
|
||||
static_assert(3 == VersionParser::parseNumber("3"), "\"3\" should be correctly parsed");
|
||||
static_assert(12 == VersionParser::parseNumber("12"), "\"12\" should be correctly parsed");
|
||||
static_assert(123 == VersionParser::parseNumber("123"), "\"123\" should be correctly parsed");
|
||||
static_assert(123 == VersionParser::parseNumber("0123"), "\"0123\" should be correctly parsed");
|
||||
static_assert(1 == VersionParser::parseNumber("001a"), "\"001a\" should be correctly parsed");
|
||||
static_assert(1230 == VersionParser::parseNumber("1230"), "\"1230\" should be correctly parsed");
|
||||
static_assert(1230 == VersionParser::parseNumber("1230beta"), "\"1230beta\" should be correctly parsed");
|
||||
static_assert(357532 == VersionParser::parseNumber("357532"), "\"357532\" should be correctly parsed");
|
||||
static_assert(357532 == VersionParser::parseNumber("357532alpha"), "\"357532alpha\" should be correctly parsed");
|
||||
static_assert(357532 == VersionParser::parseNumber("357532.4"), "\"357532.4\" should be correctly parsed");
|
||||
|
||||
static_assert(0 == strcmp("bla", VersionParser::skipNumber("bla")), "\"bla\" has no number to skip");
|
||||
static_assert(0 == strcmp("alpha", VersionParser::skipNumber("0alpha")), "\"0alpha\" should skip the 0");
|
||||
static_assert(0 == strcmp(".3alpha", VersionParser::skipNumber("12.3alpha")), "\"12.3alpha\" should skip the 12");
|
||||
using cpputils::const_string;
|
||||
|
||||
static_assert(0 == VersionParser::extractMajor("0.8"), "\"0.8\" has major version 0");
|
||||
static_assert(0 == VersionParser::extractMajor("0.8alpha"), "\"0.8alpha\" has major version 0");
|
||||
@ -68,12 +17,12 @@ static_assert(1 == VersionParser::extractMinor("0.01"), "\"0.01\" has minor vers
|
||||
static_assert(34 == VersionParser::extractMinor("12.34"), "\"12.34\" has minor version 34");
|
||||
static_assert(34 == VersionParser::extractMinor("12.34alpha"), "\"12.34alpha\" has minor version 34");
|
||||
|
||||
static_assert(0 == strlen(VersionParser::extractTag("0.0")), "\"0.0\" has no version tag");
|
||||
static_assert(0 == strlen(VersionParser::extractTag("0.01")), "\"0.01\" has no version tag");
|
||||
static_assert(0 == strlen(VersionParser::extractTag("12.34")), "\"12.34\" has no version tag");
|
||||
static_assert(0 == strcmp("alpha", VersionParser::extractTag("12.34alpha")), "\"12.34alpha\" has alpha version tag");
|
||||
static_assert(0 == strcmp("rc1", VersionParser::extractTag("12.34rc1")), "\"12.34rc1\" has rc1 version tag");
|
||||
static_assert(0 == strcmp("rc1", VersionParser::extractTag("1.0rc1")), "\"1.0rc1\" has rc1 version tag");
|
||||
static_assert(const_string("") == VersionParser::extractTag("0.0"), "\"0.0\" has no version tag");
|
||||
static_assert(const_string("") == VersionParser::extractTag("0.01"), "\"0.01\" has no version tag");
|
||||
static_assert(const_string("") == VersionParser::extractTag("12.34"), "\"12.34\" has no version tag");
|
||||
static_assert(const_string("alpha") == VersionParser::extractTag("12.34alpha"), "\"12.34alpha\" has alpha version tag");
|
||||
static_assert(const_string("rc1") == VersionParser::extractTag("12.34rc1"), "\"12.34rc1\" has rc1 version tag");
|
||||
static_assert(const_string("rc1") == VersionParser::extractTag("1.0rc1"), "\"1.0rc1\" has rc1 version tag");
|
||||
|
||||
static_assert(VersionTag::ALPHA == VersionParser::parseTag("alpha"), "alpha version tag should be parseable");
|
||||
static_assert(VersionTag::BETA == VersionParser::parseTag("beta"), "beta version tag should be parseable");
|
||||
|
Loading…
Reference in New Issue
Block a user