1
1
#include " version_weaver.h"
2
2
namespace version_weaver {
3
- bool validate (std::string_view version) { return true ; }
3
+ bool validate (std::string_view version) {
4
+ return std::holds_alternative<Version>(parse (version));
5
+ }
6
+
4
7
bool gt (std::string_view version1, std::string_view version2) { return true ; }
5
8
bool lt (std::string_view version1, std::string_view version2) { return true ; }
6
9
bool satisfies (std::string_view version, std::string_view range) { return true ; }
7
10
std::string coerce (std::string_view version) { return " " ; }
8
11
std::string minimum (std::string_view range) { return " " ; }
9
12
std::string clean (std::string_view range) { return " " ; }
13
+
14
+ std::variant<Version, ParseError> parse (std::string_view input) {
15
+ if (input.size () > MAX_VERSION_LENGTH) {
16
+ return ParseError::VERSION_LARGER_THAN_MAX_LENGTH;
17
+ }
18
+
19
+ std::string_view input_copy = input;
20
+ // TODO: Trim leading and trailing whitespace
21
+
22
+ auto dot_iterator = input_copy.find (' .' );
23
+ if (dot_iterator == std::string_view::npos) {
24
+ // Only major exists. No minor or patch.
25
+ return ParseError::INVALID_INPUT;
26
+ }
27
+ Version version;
28
+ auto major = input_copy.substr (0 , dot_iterator);
29
+
30
+ if (major.empty () || major.front () == ' 0' ) {
31
+ // Version components can not have leading zeroes.
32
+ return ParseError::INVALID_INPUT;
33
+ }
34
+ version.major = major;
35
+ input_copy = input_copy.substr (dot_iterator + 1 );
36
+ dot_iterator = input_copy.find (' .' );
37
+ if (dot_iterator == std::string_view::npos) {
38
+ // Only major and minor exists. No patch.
39
+ return ParseError::INVALID_INPUT;
40
+ }
41
+
42
+ auto minor = input_copy.substr (0 , dot_iterator);
43
+ if (minor.empty () || minor.front () == ' 0' ) {
44
+ // Version components can not have leading zeroes.
45
+ return ParseError::INVALID_INPUT;
46
+ }
47
+ version.minor = minor;
48
+ input_copy = input_copy.substr (dot_iterator + 1 );
49
+ dot_iterator = input_copy.find (' .' );
50
+ if (dot_iterator == std::string_view::npos) {
51
+ // Only major, minor and patch exists.
52
+ return ParseError::INVALID_INPUT;
53
+ }
54
+
55
+ auto patch = input_copy.substr (0 , dot_iterator);
56
+ if (patch.empty () || patch.front () == ' 0' ) {
57
+ // Version components can not have leading zeroes.
58
+ return ParseError::INVALID_INPUT;
59
+ }
60
+ version.patch = patch;
61
+ input_copy = input_copy.substr (dot_iterator + 1 );
62
+
63
+ auto pre_release_iterator = input_copy.find (' -' );
64
+ if (pre_release_iterator != std::string_view::npos) {
65
+ version.pre_release = input_copy.substr (0 , pre_release_iterator);
66
+ input_copy = input_copy.substr (pre_release_iterator + 1 );
67
+ if (input_copy.find (' .' ) != std::string_view::npos) {
68
+ // Build metadata exists.
69
+ auto build_iterator = input_copy.find (' .' );
70
+ version.build = input_copy.substr (0 , build_iterator);
71
+ }
72
+ }
73
+
74
+ return version;
75
+ }
10
76
}
0 commit comments