Don't use <regex>, because it causes problems on GCC 4.8

This commit is contained in:
Sebastian Messmer 2016-03-03 11:49:23 +01:00
parent 5736a5ea37
commit 61611d03b2

View File

@ -1,15 +1,14 @@
#include "parser.h" #include "parser.h"
#include <regex> #include <sstream>
using std::string; using std::string;
using std::pair; using std::pair;
using std::tuple; using std::tuple;
using std::tie; using std::tie;
using std::regex;
using std::smatch;
using std::regex_match;
using boost::optional; using boost::optional;
using boost::none; using boost::none;
using std::istringstream;
using std::getline;
namespace gitversion { namespace gitversion {
@ -30,55 +29,53 @@ namespace gitversion {
} }
pair<string, optional<string>> Parser::_splitAtPlusSign(const string &versionString) { pair<string, optional<string>> Parser::_splitAtPlusSign(const string &versionString) {
regex splitRegex("^([^+]+)(\\+([^+]+))?$"); istringstream stream(versionString);
smatch match; string versionNumber;
regex_match(versionString, match, splitRegex); getline(stream, versionNumber, '+');
if(match[0] != versionString) { if (!stream.good()) {
throw std::logic_error("First match group should be whole string"); return std::make_pair(versionNumber, none);
}
if(match.size() != 4) {
throw std::logic_error("Wrong number of match groups");
}
if (match[2].matched) {
return std::make_pair(match[1], optional<string>(match[3]));
} else { } else {
return std::make_pair(match[1], none); string versionInfo;
getline(stream, versionInfo);
return std::make_pair(versionNumber, versionInfo);
} }
}; };
tuple<string, string, string> Parser::_extractMajorMinorTag(const string &versionNumber) { tuple<string, string, string> Parser::_extractMajorMinorTag(const string &versionNumber) {
regex splitRegex("^([0-9]+)(\\.([0-9]+))?(\\.[0-9\\.]*)?(-(.*))?$"); istringstream stream(versionNumber);
smatch match; string major, minor, hotfix, tag;
regex_match(versionNumber, match, splitRegex); getline(stream, major, '.');
if(match[0] != versionNumber) { if (!stream.good()) {
throw std::logic_error("First match group should be whole string"); minor = "0";
} else {
getline(stream, minor, '.');
} }
if(match.size() != 7) { if (!stream.good()) {
throw std::logic_error("Wrong number of match groups"); hotfix = "0";
} else {
getline(stream, hotfix, '-');
} }
string major = match[1]; if (!stream.good()) {
string minor = "0"; tag = "";
if (match[3].matched) { } else {
minor = match[3]; getline(stream, tag);
}
string tag = "";
if (match[6].matched) {
tag = match[6];
} }
return std::make_tuple(major, minor, tag); return std::make_tuple(major, minor, tag);
}; };
string Parser::_extractGitCommitId(const string &versionInfo) { string Parser::_extractGitCommitId(const string &versionInfo) {
regex extractRegex("^[0-9]+\\.g([0-9a-f]+)(\\..*)?$"); istringstream stream(versionInfo);
smatch match; string commitsSinceTag;
regex_match(versionInfo, match, extractRegex); getline(stream, commitsSinceTag, '.');
if(match[0] != versionInfo) { if (!stream.good()) {
throw std::logic_error("First match group should be whole string"); throw std::logic_error("Invalid version information: Missing delimiter after commitsSinceTag.");
} }
if(match.size() != 3) { string gitCommitId;
throw std::logic_error("Wrong number of match groups"); getline(stream, gitCommitId, '.');
if (gitCommitId[0] != 'g') {
throw std::logic_error("Invalid version information: Git commit id component doesn't start with 'g'.");
} }
return match[1]; return gitCommitId.substr(1);
} }
} }