25 #include "graphar/version_parser.h"
30 bool is_whitespace(
char ch) {
31 return ch ==
' ' || ch ==
'\t' || ch ==
'\r' || ch ==
'\n';
34 void trim(std::string& s) {
36 for (
auto it = s.begin(); it != s.end(); ++it) {
37 if (!is_whitespace(*it)) {
43 if (trim_left == s.size()) {
46 size_t trim_right = 0;
47 for (
auto it = s.rbegin(); it != s.rend(); ++it) {
48 if (!is_whitespace(*it)) {
54 if (trim_left > 0 || trim_right > 0) {
56 s.resize(s.size() - trim_right);
58 std::string copy(s.c_str() + trim_left,
59 s.size() - trim_left - trim_right);
66 int parserVersionImpl(
const std::string& version_str) {
68 const std::regex version_regex(
"gar/v(\\d+).*");
69 if (std::regex_match(version_str, match, version_regex)) {
70 if (match.size() != 2) {
71 throw std::runtime_error(
"Invalid version string: " + version_str);
73 return std::stoi(match[1].str());
75 throw std::runtime_error(
"Invalid version string: " + version_str);
79 std::vector<std::string> parseUserDefineTypesImpl(
80 const std::string& version_str) {
82 std::vector<std::string> user_define_types;
83 const std::regex user_define_types_regex(
"gar/v\\d+ *\\((.*)\\).*");
84 if (std::regex_match(version_str, match, user_define_types_regex)) {
85 if (match.size() != 2) {
86 throw std::runtime_error(
"Invalid version string: " + version_str);
88 std::string types_str = match[1].str();
90 while (pos != std::string::npos) {
91 size_t next_pos = types_str.find(
',', pos);
92 std::string type = types_str.substr(pos, next_pos - pos);
95 user_define_types.push_back(type);
97 if (next_pos != std::string::npos) {
104 return user_define_types;
108 const std::string& version_str) noexcept {
109 std::shared_ptr<InfoVersion> version;
111 version = std::make_shared<InfoVersion>(
112 parserVersionImpl(version_str), parseUserDefineTypesImpl(version_str));
113 }
catch (
const std::exception& e) {
static Result< std::shared_ptr< const InfoVersion > > Parse(const std::string &str) noexcept
static Status Invalid(Args &&... args)