| 1 | /**************************************************************************/ |
| 2 | /* semver.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "semver.h" |
| 32 | |
| 33 | bool godotsharp::SemVer::parse_digit_only_field(const String &p_field, uint64_t &r_result) { |
| 34 | if (p_field.is_empty()) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | int64_t integer = 0; |
| 39 | |
| 40 | for (int i = 0; i < p_field.length(); i++) { |
| 41 | char32_t c = p_field[i]; |
| 42 | if (is_digit(c)) { |
| 43 | bool overflow = ((uint64_t)integer > UINT64_MAX / 10) || ((uint64_t)integer == UINT64_MAX / 10 && c > '5'); |
| 44 | ERR_FAIL_COND_V_MSG(overflow, false, "Cannot represent '" + p_field + "' as a 64-bit unsigned integer, since the value is too large."); |
| 45 | integer *= 10; |
| 46 | integer += c - '0'; |
| 47 | } else { |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | r_result = (uint64_t)integer; |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | int godotsharp::SemVer::cmp(const godotsharp::SemVer &p_a, const godotsharp::SemVer &p_b) { |
| 57 | if (p_a.major != p_b.major) { |
| 58 | return p_a.major > p_b.major ? 1 : -1; |
| 59 | } |
| 60 | |
| 61 | if (p_a.minor != p_b.minor) { |
| 62 | return p_a.minor > p_b.minor ? 1 : -1; |
| 63 | } |
| 64 | |
| 65 | if (p_a.patch != p_b.patch) { |
| 66 | return p_a.patch > p_b.patch ? 1 : -1; |
| 67 | } |
| 68 | |
| 69 | if (p_a.prerelease.is_empty() && p_b.prerelease.is_empty()) { |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | if (p_a.prerelease.is_empty() || p_b.prerelease.is_empty()) { |
| 74 | return p_a.prerelease.is_empty() ? 1 : -1; |
| 75 | } |
| 76 | |
| 77 | if (p_a.prerelease != p_b.prerelease) { |
| 78 | // This could be optimized, but I'm too lazy |
| 79 | |
| 80 | Vector<String> a_field_set = p_a.prerelease.split("."); |
| 81 | Vector<String> b_field_set = p_b.prerelease.split("."); |
| 82 | |
| 83 | int a_field_count = a_field_set.size(); |
| 84 | int b_field_count = b_field_set.size(); |
| 85 | |
| 86 | int min_field_count = MIN(a_field_count, b_field_count); |
| 87 | |
| 88 | for (int i = 0; i < min_field_count; i++) { |
| 89 | const String &a_field = a_field_set[i]; |
| 90 | const String &b_field = b_field_set[i]; |
| 91 | |
| 92 | if (a_field == b_field) { |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | uint64_t a_num; |
| 97 | bool a_is_digit_only = parse_digit_only_field(a_field, a_num); |
| 98 | |
| 99 | uint64_t b_num; |
| 100 | bool b_is_digit_only = parse_digit_only_field(b_field, b_num); |
| 101 | |
| 102 | if (a_is_digit_only && b_is_digit_only) { |
| 103 | // Identifiers consisting of only digits are compared numerically. |
| 104 | |
| 105 | if (a_num == b_num) { |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | return a_num > b_num ? 1 : -1; |
| 110 | } |
| 111 | |
| 112 | if (a_is_digit_only || b_is_digit_only) { |
| 113 | // Numeric identifiers always have lower precedence than non-numeric identifiers. |
| 114 | return b_is_digit_only ? 1 : -1; |
| 115 | } |
| 116 | |
| 117 | // Identifiers with letters or hyphens are compared lexically in ASCII sort order. |
| 118 | return a_field > b_field ? 1 : -1; |
| 119 | } |
| 120 | |
| 121 | if (a_field_count != b_field_count) { |
| 122 | // A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal. |
| 123 | return a_field_count > b_field_count ? 1 : -1; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | bool godotsharp::SemVerParser::parse(const String &p_ver_text, godotsharp::SemVer &r_semver) { |
| 131 | if (!regex.is_valid() && regex.get_pattern().is_empty()) { |
| 132 | regex.compile("^(?P<major>0|[1-9]\\d*)\\.(?P<minor>0|[1-9]\\d*)\\.(?P<patch>0|[1-9]\\d*)(?:-(?P<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"); |
| 133 | ERR_FAIL_COND_V(!regex.is_valid(), false); |
| 134 | } |
| 135 | |
| 136 | Ref<RegExMatch> match = regex.search(p_ver_text); |
| 137 | |
| 138 | if (match.is_valid()) { |
| 139 | r_semver = SemVer( |
| 140 | match->get_string("major").to_int(), |
| 141 | match->get_string("minor").to_int(), |
| 142 | match->get_string("patch").to_int(), |
| 143 | match->get_string("prerelease"), |
| 144 | match->get_string("buildmetadata")); |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | return false; |
| 149 | } |