| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Runtime.CompilerServices; |
| | 6 | |
|
| | 7 | | namespace GDX.Developer |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// A Semantic Versioning structure. |
| | 11 | | /// </summary> |
| | 12 | | /// <remarks>https://semver.org/</remarks> |
| | 13 | | public readonly struct SemanticVersion |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// An array of <see cref="char" /> used to split versions. |
| | 17 | | /// </summary> |
| 1 | 18 | | static readonly char[] k_VersionIndicators = { '.', ',', '_', 'f' }; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Major Version. |
| | 22 | | /// </summary> |
| | 23 | | /// <remarks>Is incremented when you make incompatible API changes.</remarks> |
| | 24 | | public readonly int Major; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Minor Version. |
| | 28 | | /// </summary> |
| | 29 | | /// <remarks>Is incremented when you add functionality in a backwards-compatible manner.</remarks> |
| | 30 | | public readonly int Minor; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Patch Version |
| | 34 | | /// </summary> |
| | 35 | | /// <remarks>Is incremented when you make backwards-compatible fixes.</remarks> |
| | 36 | | public readonly int Patch; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Create a <see cref="SemanticVersion" /> based on a formatted <see cref="string" />. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="version">A formatted version semantic version string (2020.1.0).</param> |
| | 42 | | public SemanticVersion(string version) |
| 49 | 43 | | { |
| 49 | 44 | | string[] split = version.Split(k_VersionIndicators); |
| 49 | 45 | | switch (split.Length) |
| | 46 | | { |
| | 47 | | case 4: |
| 6 | 48 | | int.TryParse(split[0], out Major); |
| 6 | 49 | | int.TryParse(split[1], out Minor); |
| 6 | 50 | | int.TryParse(split[2], out Patch); |
| 6 | 51 | | break; |
| | 52 | | case 3: |
| 40 | 53 | | int.TryParse(split[0], out Major); |
| 40 | 54 | | int.TryParse(split[1], out Minor); |
| 40 | 55 | | int.TryParse(split[2], out Patch); |
| 40 | 56 | | break; |
| | 57 | | case 2: |
| 1 | 58 | | int.TryParse(split[0], out Major); |
| 1 | 59 | | int.TryParse(split[1], out Minor); |
| 1 | 60 | | Patch = 0; |
| 1 | 61 | | break; |
| | 62 | | case 1: |
| 1 | 63 | | int.TryParse(split[0], out Major); |
| 1 | 64 | | Minor = 0; |
| 1 | 65 | | Patch = 0; |
| 1 | 66 | | break; |
| | 67 | | default: |
| 1 | 68 | | Major = 0; |
| 1 | 69 | | Minor = 0; |
| 1 | 70 | | Patch = 0; |
| 1 | 71 | | break; |
| | 72 | | } |
| 49 | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Determine if <see cref="SemanticVersion" /> is greater than another <see cref="SemanticVersion" />. |
| | 77 | | /// </summary> |
| | 78 | | /// <param name="lhs">Left-hand side <see cref="SemanticVersion" />.</param> |
| | 79 | | /// <param name="rhs">Right-hand side <see cref="SemanticVersion" />.</param> |
| | 80 | | /// <returns>Returns the result of a GREATER THAN operation on two <see cref="SemanticVersion" /> values.</retur |
| | 81 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 82 | | public static bool operator >(SemanticVersion lhs, SemanticVersion rhs) |
| 3 | 83 | | { |
| 3 | 84 | | if (lhs.Major > rhs.Major) |
| 1 | 85 | | { |
| 1 | 86 | | return true; |
| | 87 | | } |
| | 88 | |
|
| 2 | 89 | | if (lhs.Major == rhs.Major && lhs.Minor > rhs.Minor) |
| 1 | 90 | | { |
| 1 | 91 | | return true; |
| | 92 | | } |
| | 93 | |
|
| 1 | 94 | | return lhs.Major == rhs.Major && lhs.Minor == rhs.Minor && lhs.Patch > rhs.Patch; |
| 3 | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Determine if <see cref="SemanticVersion" /> is greater than or equal to another <see cref="SemanticVersi |
| | 99 | | /// </summary> |
| | 100 | | /// <param name="lhs">Left-hand side <see cref="SemanticVersion" />.</param> |
| | 101 | | /// <param name="rhs">Right-hand side <see cref="SemanticVersion" />.</param> |
| | 102 | | /// <returns> |
| | 103 | | /// Returns the result of a GREATER THAN OR EQUAL operation on two <see cref="SemanticVersion" /> |
| | 104 | | /// values. |
| | 105 | | /// </returns> |
| | 106 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 107 | | public static bool operator >=(SemanticVersion lhs, SemanticVersion rhs) |
| 6 | 108 | | { |
| 6 | 109 | | if (lhs.Major > rhs.Major) |
| 1 | 110 | | { |
| 1 | 111 | | return true; |
| | 112 | | } |
| | 113 | |
|
| 5 | 114 | | if (lhs.Major == rhs.Major && lhs.Minor > rhs.Minor) |
| 1 | 115 | | { |
| 1 | 116 | | return true; |
| | 117 | | } |
| | 118 | |
|
| 4 | 119 | | if (lhs.Major == rhs.Major && lhs.Minor == rhs.Minor && lhs.Patch > rhs.Patch) |
| 1 | 120 | | { |
| 1 | 121 | | return true; |
| | 122 | | } |
| | 123 | |
|
| 3 | 124 | | return lhs.Major == rhs.Major && lhs.Minor == rhs.Minor && lhs.Patch == rhs.Patch; |
| 6 | 125 | | } |
| | 126 | |
|
| | 127 | | /// <summary> |
| | 128 | | /// Determine if <see cref="SemanticVersion" /> is less than or equal to another <see cref="SemanticVersion" |
| | 129 | | /// </summary> |
| | 130 | | /// <param name="lhs">Left-hand side <see cref="SemanticVersion" />.</param> |
| | 131 | | /// <param name="rhs">Right-hand side <see cref="SemanticVersion" />.</param> |
| | 132 | | /// <returns> |
| | 133 | | /// Returns the result of a LESS THAN OR EQUAL operation on two <see cref="SemanticVersion" /> |
| | 134 | | /// values. |
| | 135 | | /// </returns> |
| | 136 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 137 | | public static bool operator <=(SemanticVersion lhs, SemanticVersion rhs) |
| 6 | 138 | | { |
| 6 | 139 | | if (lhs.Major < rhs.Major) |
| 1 | 140 | | { |
| 1 | 141 | | return true; |
| | 142 | | } |
| | 143 | |
|
| 5 | 144 | | if (lhs.Major == rhs.Major && lhs.Minor < rhs.Minor) |
| 1 | 145 | | { |
| 1 | 146 | | return true; |
| | 147 | | } |
| | 148 | |
|
| 4 | 149 | | if (lhs.Major == rhs.Major && lhs.Minor == rhs.Minor && lhs.Patch < rhs.Patch) |
| 1 | 150 | | { |
| 1 | 151 | | return true; |
| | 152 | | } |
| | 153 | |
|
| 3 | 154 | | return lhs.Major == rhs.Major && lhs.Minor == rhs.Minor && lhs.Patch == rhs.Patch; |
| 6 | 155 | | } |
| | 156 | |
|
| | 157 | | /// <summary> |
| | 158 | | /// Determine if <see cref="SemanticVersion" /> is less than another <see cref="SemanticVersion" />. |
| | 159 | | /// </summary> |
| | 160 | | /// <param name="lhs">Left-hand side <see cref="SemanticVersion" />.</param> |
| | 161 | | /// <param name="rhs">Right-hand side <see cref="SemanticVersion" />.</param> |
| | 162 | | /// <returns>Returns the result of a LESS THAN operation on two <see cref="SemanticVersion" /> values.</returns> |
| | 163 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 164 | | public static bool operator <(SemanticVersion lhs, SemanticVersion rhs) |
| 3 | 165 | | { |
| 3 | 166 | | if (lhs.Major < rhs.Major) |
| 1 | 167 | | { |
| 1 | 168 | | return true; |
| | 169 | | } |
| | 170 | |
|
| 2 | 171 | | if (lhs.Major == rhs.Major && lhs.Minor < rhs.Minor) |
| 1 | 172 | | { |
| 1 | 173 | | return true; |
| | 174 | | } |
| | 175 | |
|
| 1 | 176 | | return lhs.Major == rhs.Major && lhs.Minor == rhs.Minor && lhs.Patch < rhs.Patch; |
| 3 | 177 | | } |
| | 178 | |
|
| | 179 | | /// <summary> |
| | 180 | | /// Determine if <see cref="SemanticVersion" /> is equal to another <see cref="SemanticVersion" />. |
| | 181 | | /// </summary> |
| | 182 | | /// <param name="lhs">Left-hand side <see cref="SemanticVersion" />.</param> |
| | 183 | | /// <param name="rhs">Right-hand side <see cref="SemanticVersion" />.</param> |
| | 184 | | /// <returns>Returns the result of a EQUALITY operation on two <see cref="SemanticVersion" /> values.</returns> |
| | 185 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 186 | | public static bool operator ==(SemanticVersion lhs, SemanticVersion rhs) |
| 1 | 187 | | { |
| 1 | 188 | | return lhs.Major == rhs.Major && lhs.Minor == rhs.Minor && lhs.Patch == rhs.Patch; |
| 1 | 189 | | } |
| | 190 | |
|
| | 191 | | /// <summary> |
| | 192 | | /// Determine if <see cref="SemanticVersion" /> does not equal than another <see cref="SemanticVersion" />. |
| | 193 | | /// </summary> |
| | 194 | | /// <param name="lhs">Left-hand side <see cref="SemanticVersion" />.</param> |
| | 195 | | /// <param name="rhs">Right-hand side <see cref="SemanticVersion" />.</param> |
| | 196 | | /// <returns>Returns the result of a NOT EQUAL operation on two <see cref="SemanticVersion" /> values.</returns> |
| | 197 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 198 | | public static bool operator !=(SemanticVersion lhs, SemanticVersion rhs) |
| 1 | 199 | | { |
| 1 | 200 | | return lhs.Major != rhs.Major || lhs.Minor != rhs.Minor || lhs.Patch != rhs.Patch; |
| 1 | 201 | | } |
| | 202 | |
|
| | 203 | | /// <summary> |
| | 204 | | /// Does the <paramref name="obj" /> equal this <see cref="SemanticVersion" />. |
| | 205 | | /// </summary> |
| | 206 | | /// <param name="obj">An <see cref="object" /> to compare against.</param> |
| | 207 | | /// <returns>Returns the result of an EQUALITY operation.</returns> |
| | 208 | | public override bool Equals(object obj) |
| 1 | 209 | | { |
| 1 | 210 | | return obj is SemanticVersion other && Equals(other); |
| 1 | 211 | | } |
| | 212 | |
|
| | 213 | | /// <summary> |
| | 214 | | /// Get the hash code of the <see cref="SemanticVersion" />. |
| | 215 | | /// </summary> |
| | 216 | | /// <returns>A <see cref="int" /> value.</returns> |
| | 217 | | public override int GetHashCode() |
| 1 | 218 | | { |
| | 219 | | unchecked |
| 1 | 220 | | { |
| 1 | 221 | | int hashCode = Major; |
| 1 | 222 | | hashCode = (hashCode * 397) ^ Minor; |
| 1 | 223 | | hashCode = (hashCode * 397) ^ Patch; |
| 1 | 224 | | return hashCode; |
| | 225 | | } |
| 1 | 226 | | } |
| | 227 | |
|
| | 228 | | /// <summary> |
| | 229 | | /// Does the <paramref name="otherSemanticVersion" /> equal the <see cref="SemanticVersion" />. |
| | 230 | | /// </summary> |
| | 231 | | /// <param name="otherSemanticVersion"></param> |
| | 232 | | /// <returns> |
| | 233 | | /// The results of checking the <see cref="SemanticVersion.Major" />/<see cref="SemanticVersion.Minor" />/ |
| | 234 | | /// <see cref="SemanticVersion.Patch" /> for equality. |
| | 235 | | /// </returns> |
| | 236 | | bool Equals(SemanticVersion otherSemanticVersion) |
| 1 | 237 | | { |
| 1 | 238 | | return Major == otherSemanticVersion.Major && |
| | 239 | | Minor == otherSemanticVersion.Minor && |
| | 240 | | Patch == otherSemanticVersion.Patch; |
| 1 | 241 | | } |
| | 242 | | } |
| | 243 | | } |