| 1 | /**************************************************************************/ |
| 2 | /* semver.h */ |
| 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 | #pragma once |
| 32 | |
| 33 | #include "core/string/ustring.h" |
| 34 | |
| 35 | #include "modules/regex/regex.h" |
| 36 | |
| 37 | // <sys/sysmacros.h> is included somewhere, which defines major(dev) to gnu_dev_major(dev) |
| 38 | #if defined(major) |
| 39 | #undef major |
| 40 | #endif |
| 41 | #if defined(minor) |
| 42 | #undef minor |
| 43 | #endif |
| 44 | |
| 45 | namespace godotsharp { |
| 46 | |
| 47 | struct SemVer { |
| 48 | private: |
| 49 | static bool parse_digit_only_field(const String &p_field, uint64_t &r_result); |
| 50 | |
| 51 | static int cmp(const SemVer &p_a, const SemVer &p_b); |
| 52 | |
| 53 | public: |
| 54 | int major = 0; |
| 55 | int minor = 0; |
| 56 | int patch = 0; |
| 57 | String prerelease; |
| 58 | String build_metadata; |
| 59 | |
| 60 | bool operator==(const SemVer &b) const { |
| 61 | return cmp(*this, b) == 0; |
| 62 | } |
| 63 | |
| 64 | bool operator!=(const SemVer &b) const { |
| 65 | return !operator==(b); |
| 66 | } |
| 67 | |
| 68 | bool operator<(const SemVer &b) const { |
| 69 | return cmp(*this, b) < 0; |
| 70 | } |
| 71 | |
| 72 | bool operator>(const SemVer &b) const { |
| 73 | return cmp(*this, b) > 0; |
| 74 | } |
| 75 | |
| 76 | bool operator<=(const SemVer &b) const { |
| 77 | return cmp(*this, b) <= 0; |
| 78 | } |
| 79 | |
| 80 | bool operator>=(const SemVer &b) const { |
| 81 | return cmp(*this, b) >= 0; |
| 82 | } |
| 83 | |
| 84 | SemVer() {} |
| 85 | |
| 86 | SemVer(int p_major, int p_minor, int p_patch, |
| 87 | const String &p_prerelease, const String &p_build_metadata) : |
| 88 | major(p_major), |
| 89 | minor(p_minor), |
| 90 | patch(p_patch), |
| 91 | prerelease(p_prerelease), |
| 92 | build_metadata(p_build_metadata) { |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | struct SemVerParser { |
| 97 | private: |
| 98 | RegEx regex; |
| 99 | |
| 100 | public: |
| 101 | bool parse(const String &p_ver_text, SemVer &r_semver); |
| 102 | }; |
| 103 | |
| 104 | } //namespace godotsharp |