/// <summary> /// A Debian-like parsing of version numbers that encodes the /// operation into the string. For example, "> 2.3.4" would be /// true if the ExtendedVersion object was 2.3.5 but not 2.3.3 or 2.3.4. /// /// The following operations are allowed: /// "<", "<=", "=", ">", ">=" /// /// There may be any number of spaces between the op and the version. /// </summary> public bool Compare(string operation) { // Pull out the parts using substrings. string op; string ver; if (operation.StartsWith(">=")) { op = ">="; ver = operation.Substring(2).Trim(); } else if (operation.StartsWith("<=")) { op = "<="; ver = operation.Substring(2).Trim(); } else if (operation.StartsWith("<")) { op = "<"; ver = operation.Substring(1).Trim(); } else if (operation.StartsWith(">")) { op = ">"; ver = operation.Substring(1).Trim(); } else if (operation.StartsWith("=")) { op = "="; ver = operation.Substring(1).Trim(); } else { return(false); } // Check the op var v = new ExtendedVersion(ver); switch (op) { case "<": return(this < v); case "<=": return(this <= v); case "=": return(this == v); case ">": return(this > v); case ">=": return(this >= v); default: throw new Exception("Cannot identify operation: " + op); } }
/// <summary> /// Determines if the first is less than the second one. There are /// some conditions where a version is neither less than or greater /// than another version, specifcally with version parts that have /// text in it. /// </summary> public static bool operator <(ExtendedVersion v1, ExtendedVersion v2) { // Make sure v1 has the less parts, for simplicicity. bool swapped = false; if (v1.numerics.Length > v2.numerics.Length) { ExtendedVersion v3 = v2; v2 = v1; v1 = v3; swapped = true; } // Go through the various parts for (int i = 0; i < v1.numerics.Length; i++) { // Get the parts int num1 = v1.numerics[i]; string str1 = v1.strings[i]; int num2 = v2.numerics[i]; string str2 = v2.strings[i]; // Make sure strings match. If they do not, then the versions // will never match. if (str1 != str2) { return(swapped); } // Compare the numbers. If num1 is less than num2, then the // rest of the version will be less. If it is the reverse, // return it. if (num1 < num2) { return(!swapped); } if (num1 > num2) { return(swapped); } } // We never got something that explicitly was less or invalid, // so assume false (equals). return(swapped); }