-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVersionStringUtilities.cs
52 lines (48 loc) · 1.65 KB
/
VersionStringUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using ItzWarty;
using System;
using System.Text.RegularExpressions;
namespace Dargon {
public static class VersionStringUtilities {
public static string GetVersionString(uint n) {
var b0 = n & 0xFF;
var b1 = (n >> 8) & 0xFF;
var b2 = (n >> 16) & 0xFF;
var b3 = (n >> 24) & 0xFF;
return b3 + "." + b2 + "." + b1 + "." + b0;
}
/// <summary>
/// Gets the version string from the given path.
/// If no match is found
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string GetVersionString(string s) {
var matchResult = Regex.Match(s, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
if (matchResult.Success) return matchResult.Value;
else return "";
}
public static uint GetVersionNumber(string s) {
var parts = GetVersionString(s).Split(".");
if (parts.Length != 4)
return uint.MaxValue;
uint result = 0;
for (int i = 0; i < 4; i++)
result = (result << 8) | UInt32.Parse(parts[i]);
return result;
}
public static bool TryGetVersionNumber(string s, out uint versionNumber) {
var match = Regex.Match(s, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
if (!match.Success) {
versionNumber = uint.MaxValue;
return false;
} else {
var parts = match.Value.Split(".");
uint result = 0;
for (int i = 0; i < 4; i++)
result = (result << 8) | UInt32.Parse(parts[i]);
versionNumber = result;
return true;
}
}
}
}