semver.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "semver.h"
  31. bool godotsharp::SemVer::parse_digit_only_field(const String &p_field, uint64_t &r_result) {
  32. if (p_field.is_empty()) {
  33. return false;
  34. }
  35. int64_t integer = 0;
  36. for (int i = 0; i < p_field.length(); i++) {
  37. char32_t c = p_field[i];
  38. if (is_digit(c)) {
  39. bool overflow = ((uint64_t)integer > UINT64_MAX / 10) || ((uint64_t)integer == UINT64_MAX / 10 && c > '5');
  40. ERR_FAIL_COND_V_MSG(overflow, false, "Cannot represent '" + p_field + "' as a 64-bit unsigned integer, since the value is too large.");
  41. integer *= 10;
  42. integer += c - '0';
  43. } else {
  44. return false;
  45. }
  46. }
  47. r_result = (uint64_t)integer;
  48. return true;
  49. }
  50. int godotsharp::SemVer::cmp(const godotsharp::SemVer &p_a, const godotsharp::SemVer &p_b) {
  51. if (p_a.major != p_b.major) {
  52. return p_a.major > p_b.major ? 1 : -1;
  53. }
  54. if (p_a.minor != p_b.minor) {
  55. return p_a.minor > p_b.minor ? 1 : -1;
  56. }
  57. if (p_a.patch != p_b.patch) {
  58. return p_a.patch > p_b.patch ? 1 : -1;
  59. }
  60. if (p_a.prerelease.is_empty() && p_b.prerelease.is_empty()) {
  61. return 0;
  62. }
  63. if (p_a.prerelease.is_empty() || p_b.prerelease.is_empty()) {
  64. return p_a.prerelease.is_empty() ? 1 : -1;
  65. }
  66. if (p_a.prerelease != p_b.prerelease) {
  67. // This could be optimized, but I'm too lazy
  68. Vector<String> a_field_set = p_a.prerelease.split(".");
  69. Vector<String> b_field_set = p_b.prerelease.split(".");
  70. int a_field_count = a_field_set.size();
  71. int b_field_count = b_field_set.size();
  72. int min_field_count = MIN(a_field_count, b_field_count);
  73. for (int i = 0; i < min_field_count; i++) {
  74. const String &a_field = a_field_set[i];
  75. const String &b_field = b_field_set[i];
  76. if (a_field == b_field) {
  77. continue;
  78. }
  79. uint64_t a_num;
  80. bool a_is_digit_only = parse_digit_only_field(a_field, a_num);
  81. uint64_t b_num;
  82. bool b_is_digit_only = parse_digit_only_field(b_field, b_num);
  83. if (a_is_digit_only && b_is_digit_only) {
  84. // Identifiers consisting of only digits are compared numerically.
  85. if (a_num == b_num) {
  86. continue;
  87. }
  88. return a_num > b_num ? 1 : -1;
  89. }
  90. if (a_is_digit_only || b_is_digit_only) {
  91. // Numeric identifiers always have lower precedence than non-numeric identifiers.
  92. return b_is_digit_only ? 1 : -1;
  93. }
  94. // Identifiers with letters or hyphens are compared lexically in ASCII sort order.
  95. return a_field > b_field ? 1 : -1;
  96. }
  97. if (a_field_count != b_field_count) {
  98. // A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal.
  99. return a_field_count > b_field_count ? 1 : -1;
  100. }
  101. }
  102. return 0;
  103. }
  104. bool godotsharp::SemVerParser::parse(const String &p_ver_text, godotsharp::SemVer &r_semver) {
  105. if (!regex.is_valid() && regex.get_pattern().is_empty()) {
  106. 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-]+)*))?$");
  107. ERR_FAIL_COND_V(!regex.is_valid(), false);
  108. }
  109. Ref<RegExMatch> match = regex.search(p_ver_text);
  110. if (match.is_valid()) {
  111. r_semver = SemVer(
  112. match->get_string("major").to_int(),
  113. match->get_string("minor").to_int(),
  114. match->get_string("patch").to_int(),
  115. match->get_string("prerelease"),
  116. match->get_string("buildmetadata"));
  117. return true;
  118. }
  119. return false;
  120. }