WGet.NET 4.4.1
A WinGet wrapper library for .Net.
Loading...
Searching...
No Matches
VersionParser.cs
1//--------------------------------------------------//
2// Created by basicx-StrgV //
3// https://github.com/basicx-StrgV/ //
4//--------------------------------------------------//
5using System;
6using System.Text;
7
9{
13 internal static class VersionParser
14 {
22 public static bool CheckPreviewStatus(string input)
23 {
24 if (string.IsNullOrWhiteSpace(input) || input.Length < 7)
25 {
26 // Return if the string is null, empty or to short for a preview version
27 return false;
28 }
29
30 if (input.EndsWith("PREVIEW", StringComparison.OrdinalIgnoreCase))
31 {
32 return true;
33 }
34
35 return false;
36 }
37
45 public static Version Parse(string input)
46 {
47 if (string.IsNullOrWhiteSpace(input))
48 {
49 return new Version(0, 0);
50 }
51
52 if (Version.TryParse(input, out Version? result))
53 {
54 return result;
55 }
56
57 return CleanParse(input);
58 }
59
70 private static Version CleanParse(string input)
71 {
72 string[] versionParts = input.Split('.');
73
74 int major = 0;
75 int minor = 0;
76 int build = -1;
77 int revision = -1;
78
79 if (versionParts.Length >= 1)
80 {
81 major = ParseToInt(versionParts[0]);
82 }
83
84 if (versionParts.Length >= 2)
85 {
86 minor = ParseToInt(versionParts[1]);
87 }
88
89 if (versionParts.Length >= 3)
90 {
91 build = ParseToInt(versionParts[2], -1);
92 }
93
94 if (versionParts.Length >= 4)
95 {
96 revision = ParseToInt(versionParts[3], -1);
97 }
98
99 if (build < 0)
100 {
101 return new Version(major, minor);
102 }
103 else if (revision < 0)
104 {
105 return new Version(major, minor, build);
106 }
107 else
108 {
109 return new Version(major, minor, build, revision);
110 }
111 }
112
121 private static int ParseToInt(string input, int defaultValue = 0)
122 {
123 if (int.TryParse(input, out int result))
124 {
125 return result;
126 }
127
128 return CleanParseToInt(input, defaultValue);
129 }
130
143 private static int CleanParseToInt(string input, int defaultValue = 0)
144 {
145 string cleanInput = CleanupNumberString(input);
146
147 if (int.TryParse(cleanInput, out int result))
148 {
149 return result;
150 }
151
152 return defaultValue;
153 }
154
165 private static string CleanupNumberString(string input)
166 {
167 // Remove appendix (e.g. 123456-preview1) becaue it could contain a mix of numbers and letters,
168 // which is not considert by the next cleanup.
169 input = RemoveAppendix(input);
170
171 char[] parts = input.ToCharArray();
172
173 StringBuilder cleanString = new();
174 bool lastCharWasALetter = false;
175 for (int i = 0; i < parts.Length; i++)
176 {
177 if (char.IsDigit(parts[i]) && !lastCharWasALetter)
178 {
179 cleanString.Append(parts[i]);
180 }
181 else if (char.IsDigit(parts[i]) && lastCharWasALetter)
182 {
183 // Dont clenup a string that is a mix of numbers and letters (e.g. 123ABC456),
184 // only strings that have letters at the end (e.g. 123456ABC).
185 return string.Empty;
186 }
187 else
188 {
189 lastCharWasALetter = true;
190 }
191 }
192
193 return cleanString.ToString();
194 }
195
206 private static string RemoveAppendix(string input)
207 {
208 string[] parts = input.Split('-');
209
210 if (parts.Length >= 1)
211 {
212 return parts[0];
213 }
214
215 return input;
216 }
217 }
218}