Update SemanticVersion.cs

AI-optimized code
This commit is contained in:
2dust 2025-03-06 10:47:31 +08:00
parent d35f65f86d
commit 1229c967ba

View file

@ -1,29 +1,29 @@
namespace ServiceLib.Common namespace ServiceLib.Common
{ {
public class SemanticVersion public class SemanticVersion
{ {
private int major; private readonly int major;
private int minor; private readonly int minor;
private int patch; private readonly int patch;
private string version; private readonly string version;
public SemanticVersion(int major, int minor, int patch) public SemanticVersion(int major, int minor, int patch)
{ {
this.major = major; this.major = major;
this.minor = minor; this.minor = minor;
this.patch = patch; this.patch = patch;
this.version = $"{major}.{minor}.{patch}"; version = $"{major}.{minor}.{patch}";
} }
public SemanticVersion(string? version) public SemanticVersion(string? version)
{ {
try try
{ {
if (version.IsNullOrEmpty()) if (string.IsNullOrEmpty(version))
{ {
this.major = 0; major = 0;
this.minor = 0; minor = 0;
this.patch = 0; patch = 0;
return; return;
} }
this.version = version.RemovePrefix('v'); this.version = version.RemovePrefix('v');
@ -31,15 +31,15 @@
var parts = this.version.Split('.'); var parts = this.version.Split('.');
if (parts.Length == 2) if (parts.Length == 2)
{ {
this.major = int.Parse(parts.First()); major = int.Parse(parts.First());
this.minor = int.Parse(parts.Last()); minor = int.Parse(parts.Last());
this.patch = 0; patch = 0;
} }
else if (parts.Length is 3 or 4) else if (parts.Length is 3 or 4)
{ {
this.major = int.Parse(parts[0]); major = int.Parse(parts[0]);
this.minor = int.Parse(parts[1]); minor = int.Parse(parts[1]);
this.patch = int.Parse(parts[2]); patch = int.Parse(parts[2]);
} }
else else
{ {
@ -48,9 +48,9 @@
} }
catch catch
{ {
this.major = 0; major = 0;
this.minor = 0; minor = 0;
this.patch = 0; patch = 0;
} }
} }
@ -58,7 +58,7 @@
{ {
if (obj is SemanticVersion other) if (obj is SemanticVersion other)
{ {
return this.major == other.major && this.minor == other.minor && this.patch == other.patch; return major == other.major && minor == other.minor && patch == other.patch;
} }
else else
{ {
@ -68,7 +68,7 @@
public override int GetHashCode() public override int GetHashCode()
{ {
return this.major.GetHashCode() ^ this.minor.GetHashCode() ^ this.patch.GetHashCode(); return major.GetHashCode() ^ minor.GetHashCode() ^ patch.GetHashCode();
} }
/// <summary> /// <summary>
@ -77,18 +77,18 @@
/// <returns>major.minor.patch</returns> /// <returns>major.minor.patch</returns>
public override string ToString() public override string ToString()
{ {
return this.version; return version;
} }
public string ToVersionString(string? prefix = null) public string ToVersionString(string? prefix = null)
{ {
if (prefix == null) if (prefix == null)
{ {
return this.version; return version;
} }
else else
{ {
return $"{prefix}{this.version}"; return $"{prefix}{version}";
} }
} }
@ -108,31 +108,31 @@
private bool GreaterEquals(SemanticVersion other) private bool GreaterEquals(SemanticVersion other)
{ {
if (this.major < other.major) if (major < other.major)
{ {
return false; return false;
} }
else if (this.major > other.major) else if (major > other.major)
{ {
return true; return true;
} }
else else
{ {
if (this.minor < other.minor) if (minor < other.minor)
{ {
return false; return false;
} }
else if (this.minor > other.minor) else if (minor > other.minor)
{ {
return true; return true;
} }
else else
{ {
if (this.patch < other.patch) if (patch < other.patch)
{ {
return false; return false;
} }
else if (this.patch > other.patch) else if (patch > other.patch)
{ {
return true; return true;
} }
@ -146,31 +146,31 @@
private bool LessEquals(SemanticVersion other) private bool LessEquals(SemanticVersion other)
{ {
if (this.major < other.major) if (major < other.major)
{ {
return true; return true;
} }
else if (this.major > other.major) else if (major > other.major)
{ {
return false; return false;
} }
else else
{ {
if (this.minor < other.minor) if (minor < other.minor)
{ {
return true; return true;
} }
else if (this.minor > other.minor) else if (minor > other.minor)
{ {
return false; return false;
} }
else else
{ {
if (this.patch < other.patch) if (patch < other.patch)
{ {
return true; return true;
} }
else if (this.patch > other.patch) else if (patch > other.patch)
{ {
return false; return false;
} }
@ -184,4 +184,4 @@
#endregion Private #endregion Private
} }
} }