2
0

Version.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Globalization;
  5. using System.Diagnostics;
  6. using System.Text;
  7. namespace System
  8. {
  9. // A Version object contains four hierarchical numeric components: major, minor,
  10. // build and revision. Build and revision may be unspecified, which is represented
  11. // internally as a -1. By definition, an unspecified component matches anything
  12. // (both unspecified and specified), and an unspecified component is "less than" any
  13. // specified component.
  14. [Serializable]
  15. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  16. public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>, ISpanFormattable
  17. {
  18. // AssemblyName depends on the order staying the same
  19. private readonly int _Major; // Do not rename (binary serialization)
  20. private readonly int _Minor; // Do not rename (binary serialization)
  21. private readonly int _Build = -1; // Do not rename (binary serialization)
  22. private readonly int _Revision = -1; // Do not rename (binary serialization)
  23. public Version(int major, int minor, int build, int revision)
  24. {
  25. if (major < 0)
  26. throw new ArgumentOutOfRangeException(nameof(major), SR.ArgumentOutOfRange_Version);
  27. if (minor < 0)
  28. throw new ArgumentOutOfRangeException(nameof(minor), SR.ArgumentOutOfRange_Version);
  29. if (build < 0)
  30. throw new ArgumentOutOfRangeException(nameof(build), SR.ArgumentOutOfRange_Version);
  31. if (revision < 0)
  32. throw new ArgumentOutOfRangeException(nameof(revision), SR.ArgumentOutOfRange_Version);
  33. _Major = major;
  34. _Minor = minor;
  35. _Build = build;
  36. _Revision = revision;
  37. }
  38. public Version(int major, int minor, int build)
  39. {
  40. if (major < 0)
  41. throw new ArgumentOutOfRangeException(nameof(major), SR.ArgumentOutOfRange_Version);
  42. if (minor < 0)
  43. throw new ArgumentOutOfRangeException(nameof(minor), SR.ArgumentOutOfRange_Version);
  44. if (build < 0)
  45. throw new ArgumentOutOfRangeException(nameof(build), SR.ArgumentOutOfRange_Version);
  46. _Major = major;
  47. _Minor = minor;
  48. _Build = build;
  49. }
  50. public Version(int major, int minor)
  51. {
  52. if (major < 0)
  53. throw new ArgumentOutOfRangeException(nameof(major), SR.ArgumentOutOfRange_Version);
  54. if (minor < 0)
  55. throw new ArgumentOutOfRangeException(nameof(minor), SR.ArgumentOutOfRange_Version);
  56. _Major = major;
  57. _Minor = minor;
  58. }
  59. public Version(string version)
  60. {
  61. Version v = Version.Parse(version);
  62. _Major = v.Major;
  63. _Minor = v.Minor;
  64. _Build = v.Build;
  65. _Revision = v.Revision;
  66. }
  67. public Version()
  68. {
  69. _Major = 0;
  70. _Minor = 0;
  71. }
  72. private Version(Version version)
  73. {
  74. Debug.Assert(version != null);
  75. _Major = version._Major;
  76. _Minor = version._Minor;
  77. _Build = version._Build;
  78. _Revision = version._Revision;
  79. }
  80. public object Clone()
  81. {
  82. return new Version(this);
  83. }
  84. // Properties for setting and getting version numbers
  85. public int Major
  86. {
  87. get { return _Major; }
  88. }
  89. public int Minor
  90. {
  91. get { return _Minor; }
  92. }
  93. public int Build
  94. {
  95. get { return _Build; }
  96. }
  97. public int Revision
  98. {
  99. get { return _Revision; }
  100. }
  101. public short MajorRevision
  102. {
  103. get { return (short)(_Revision >> 16); }
  104. }
  105. public short MinorRevision
  106. {
  107. get { return (short)(_Revision & 0xFFFF); }
  108. }
  109. public int CompareTo(object version)
  110. {
  111. if (version == null)
  112. {
  113. return 1;
  114. }
  115. Version v = version as Version;
  116. if (v == null)
  117. {
  118. throw new ArgumentException(SR.Arg_MustBeVersion);
  119. }
  120. return CompareTo(v);
  121. }
  122. public int CompareTo(Version value)
  123. {
  124. return
  125. object.ReferenceEquals(value, this) ? 0 :
  126. value is null ? 1 :
  127. _Major != value._Major ? (_Major > value._Major ? 1 : -1) :
  128. _Minor != value._Minor ? (_Minor > value._Minor ? 1 : -1) :
  129. _Build != value._Build ? (_Build > value._Build ? 1 : -1) :
  130. _Revision != value._Revision ? (_Revision > value._Revision ? 1 : -1) :
  131. 0;
  132. }
  133. public override bool Equals(object obj)
  134. {
  135. return Equals(obj as Version);
  136. }
  137. public bool Equals(Version obj)
  138. {
  139. return object.ReferenceEquals(obj, this) ||
  140. (!(obj is null) &&
  141. _Major == obj._Major &&
  142. _Minor == obj._Minor &&
  143. _Build == obj._Build &&
  144. _Revision == obj._Revision);
  145. }
  146. public override int GetHashCode()
  147. {
  148. // Let's assume that most version numbers will be pretty small and just
  149. // OR some lower order bits together.
  150. int accumulator = 0;
  151. accumulator |= (_Major & 0x0000000F) << 28;
  152. accumulator |= (_Minor & 0x000000FF) << 20;
  153. accumulator |= (_Build & 0x000000FF) << 12;
  154. accumulator |= (_Revision & 0x00000FFF);
  155. return accumulator;
  156. }
  157. public override string ToString() =>
  158. ToString(DefaultFormatFieldCount);
  159. public string ToString(int fieldCount) =>
  160. fieldCount == 0 ? string.Empty :
  161. fieldCount == 1 ? _Major.ToString() :
  162. StringBuilderCache.GetStringAndRelease(ToCachedStringBuilder(fieldCount));
  163. public bool TryFormat(Span<char> destination, out int charsWritten) =>
  164. TryFormat(destination, DefaultFormatFieldCount, out charsWritten);
  165. public bool TryFormat(Span<char> destination, int fieldCount, out int charsWritten)
  166. {
  167. if (fieldCount == 0)
  168. {
  169. charsWritten = 0;
  170. return true;
  171. }
  172. else if (fieldCount == 1)
  173. {
  174. return _Major.TryFormat(destination, out charsWritten);
  175. }
  176. StringBuilder sb = ToCachedStringBuilder(fieldCount);
  177. if (sb.Length <= destination.Length)
  178. {
  179. sb.CopyTo(0, destination, sb.Length);
  180. StringBuilderCache.Release(sb);
  181. charsWritten = sb.Length;
  182. return true;
  183. }
  184. StringBuilderCache.Release(sb);
  185. charsWritten = 0;
  186. return false;
  187. }
  188. bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
  189. {
  190. // format and provider are ignored.
  191. return TryFormat(destination, out charsWritten);
  192. }
  193. private int DefaultFormatFieldCount =>
  194. _Build == -1 ? 2 :
  195. _Revision == -1 ? 3 :
  196. 4;
  197. private StringBuilder ToCachedStringBuilder(int fieldCount)
  198. {
  199. // Note: As we always have positive numbers then it is safe to convert the number to string
  200. // regardless of the current culture as we'll not have any punctuation marks in the number.
  201. if (fieldCount == 2)
  202. {
  203. StringBuilder sb = StringBuilderCache.Acquire();
  204. sb.Append(_Major);
  205. sb.Append('.');
  206. sb.Append(_Minor);
  207. return sb;
  208. }
  209. else
  210. {
  211. if (_Build == -1)
  212. {
  213. throw new ArgumentException(SR.Format(SR.ArgumentOutOfRange_Bounds_Lower_Upper, "0", "2"), nameof(fieldCount));
  214. }
  215. if (fieldCount == 3)
  216. {
  217. StringBuilder sb = StringBuilderCache.Acquire();
  218. sb.Append(_Major);
  219. sb.Append('.');
  220. sb.Append(_Minor);
  221. sb.Append('.');
  222. sb.Append(_Build);
  223. return sb;
  224. }
  225. if (_Revision == -1)
  226. {
  227. throw new ArgumentException(SR.Format(SR.ArgumentOutOfRange_Bounds_Lower_Upper, "0", "3"), nameof(fieldCount));
  228. }
  229. if (fieldCount == 4)
  230. {
  231. StringBuilder sb = StringBuilderCache.Acquire();
  232. sb.Append(_Major);
  233. sb.Append('.');
  234. sb.Append(_Minor);
  235. sb.Append('.');
  236. sb.Append(_Build);
  237. sb.Append('.');
  238. sb.Append(_Revision);
  239. return sb;
  240. }
  241. throw new ArgumentException(SR.Format(SR.ArgumentOutOfRange_Bounds_Lower_Upper, "0", "4"), nameof(fieldCount));
  242. }
  243. }
  244. public static Version Parse(string input)
  245. {
  246. if (input == null)
  247. {
  248. throw new ArgumentNullException(nameof(input));
  249. }
  250. return ParseVersion(input.AsSpan(), throwOnFailure: true);
  251. }
  252. public static Version Parse(ReadOnlySpan<char> input) =>
  253. ParseVersion(input, throwOnFailure: true);
  254. public static bool TryParse(string input, out Version result)
  255. {
  256. if (input == null)
  257. {
  258. result = null;
  259. return false;
  260. }
  261. return (result = ParseVersion(input.AsSpan(), throwOnFailure: false)) != null;
  262. }
  263. public static bool TryParse(ReadOnlySpan<char> input, out Version result) =>
  264. (result = ParseVersion(input, throwOnFailure: false)) != null;
  265. private static Version ParseVersion(ReadOnlySpan<char> input, bool throwOnFailure)
  266. {
  267. // Find the separator between major and minor. It must exist.
  268. int majorEnd = input.IndexOf('.');
  269. if (majorEnd < 0)
  270. {
  271. if (throwOnFailure) throw new ArgumentException(SR.Arg_VersionString, nameof(input));
  272. return null;
  273. }
  274. // Find the ends of the optional minor and build portions.
  275. // We musn't have any separators after build.
  276. int buildEnd = -1;
  277. int minorEnd = input.Slice(majorEnd + 1).IndexOf('.');
  278. if (minorEnd != -1)
  279. {
  280. minorEnd += (majorEnd + 1);
  281. buildEnd = input.Slice(minorEnd + 1).IndexOf('.');
  282. if (buildEnd != -1)
  283. {
  284. buildEnd += (minorEnd + 1);
  285. if (input.Slice(buildEnd + 1).Contains('.'))
  286. {
  287. if (throwOnFailure) throw new ArgumentException(SR.Arg_VersionString, nameof(input));
  288. return null;
  289. }
  290. }
  291. }
  292. int minor, build, revision;
  293. // Parse the major version
  294. if (!TryParseComponent(input.Slice(0, majorEnd), nameof(input), throwOnFailure, out int major))
  295. {
  296. return null;
  297. }
  298. if (minorEnd != -1)
  299. {
  300. // If there's more than a major and minor, parse the minor, too.
  301. if (!TryParseComponent(input.Slice(majorEnd + 1, minorEnd - majorEnd - 1), nameof(input), throwOnFailure, out minor))
  302. {
  303. return null;
  304. }
  305. if (buildEnd != -1)
  306. {
  307. // major.minor.build.revision
  308. return
  309. TryParseComponent(input.Slice(minorEnd + 1, buildEnd - minorEnd - 1), nameof(build), throwOnFailure, out build) &&
  310. TryParseComponent(input.Slice(buildEnd + 1), nameof(revision), throwOnFailure, out revision) ?
  311. new Version(major, minor, build, revision) :
  312. null;
  313. }
  314. else
  315. {
  316. // major.minor.build
  317. return TryParseComponent(input.Slice(minorEnd + 1), nameof(build), throwOnFailure, out build) ?
  318. new Version(major, minor, build) :
  319. null;
  320. }
  321. }
  322. else
  323. {
  324. // major.minor
  325. return TryParseComponent(input.Slice(majorEnd + 1), nameof(input), throwOnFailure, out minor) ?
  326. new Version(major, minor) :
  327. null;
  328. }
  329. }
  330. private static bool TryParseComponent(ReadOnlySpan<char> component, string componentName, bool throwOnFailure, out int parsedComponent)
  331. {
  332. if (throwOnFailure)
  333. {
  334. if ((parsedComponent = int.Parse(component, NumberStyles.Integer, CultureInfo.InvariantCulture)) < 0)
  335. {
  336. throw new ArgumentOutOfRangeException(componentName, SR.ArgumentOutOfRange_Version);
  337. }
  338. return true;
  339. }
  340. return int.TryParse(component, NumberStyles.Integer, CultureInfo.InvariantCulture, out parsedComponent) && parsedComponent >= 0;
  341. }
  342. public static bool operator ==(Version v1, Version v2)
  343. {
  344. if (v1 is null)
  345. {
  346. return v2 is null;
  347. }
  348. return v1.Equals(v2);
  349. }
  350. public static bool operator !=(Version v1, Version v2)
  351. {
  352. return !(v1 == v2);
  353. }
  354. public static bool operator <(Version v1, Version v2)
  355. {
  356. if ((object)v1 == null)
  357. throw new ArgumentNullException(nameof(v1));
  358. return (v1.CompareTo(v2) < 0);
  359. }
  360. public static bool operator <=(Version v1, Version v2)
  361. {
  362. if ((object)v1 == null)
  363. throw new ArgumentNullException(nameof(v1));
  364. return (v1.CompareTo(v2) <= 0);
  365. }
  366. public static bool operator >(Version v1, Version v2)
  367. {
  368. return (v2 < v1);
  369. }
  370. public static bool operator >=(Version v1, Version v2)
  371. {
  372. return (v2 <= v1);
  373. }
  374. }
  375. }