public static string StripAccelerators(string input, char accessSpecifier) { if (string.IsNullOrEmpty(input) || input.IndexOf(accessSpecifier) < 0) { return(input); } using (ReusableResourceHolder <StringBuilder> reusableResourceHolder = ReusableStringBuilder.AcquireDefault(input.Length)) { StringBuilder resource = reusableResourceHolder.Resource; for (int index1 = 0; index1 < input.Length; ++index1) { if (input[index1] == accessSpecifier) { ++index1; } else if (input[index1] == 40) { bool flag = false; int num = -1; for (int index2 = index1 + 1; index2 < input.Length; ++index2) { if (input[index2] == 41) { num = index2; break; } if (input[index2] == accessSpecifier) { if (index2 == input.Length - 1 || input[index2 + 1] == accessSpecifier) { ++index2; } else { flag = true; } } } if (flag && num >= 0) { index1 = num; continue; } } if (index1 < input.Length) { resource.Append(input[index1]); } } return(resource.ToString().TrimEnd(Array.Empty <char>())); } }
public static string GetCommonPathPrefix(string path1, string path2) { Validate.IsNotNull(path1, "path1"); Validate.IsNotNull(path2, "path2"); if (path1.Length == 0 || path2.Length == 0) { return(string.Empty); } if (ArePathsEqual(path1, path2)) { return(path1); } bool flag = path1.StartsWith("\\\\"); if (flag != path2.StartsWith("\\\\")) { return(string.Empty); } using (ReusableResourceHolder <StringBuilder> reusableResourceHolder = ReusableStringBuilder.AcquireDefault(260)) { StringBuilder resource = reusableResourceHolder.Resource; PathParser pathParser = new PathParser(path1); PathParser other = new PathParser(path2); while (pathParser.MoveNext() && other.MoveNext() && (pathParser.CurrentLength == other.CurrentLength && pathParser.CompareCurrentSegment(other) == 0)) { if (resource.Length == 0 & flag) { resource.Append("\\\\"); } else if (resource.Length > 0) { resource.Append(Path.DirectorySeparatorChar); } resource.Append(path1, pathParser.CurrentStartIndex, pathParser.CurrentLength); } if (resource.Length == 2 && resource[1] == Path.VolumeSeparatorChar) { resource.Append('\\'); } return(resource.ToString()); } }
public static string Normalize(string path) { if (string.IsNullOrEmpty(path)) { return(path); } if (path.IndexOfAny(Path.GetInvalidPathChars()) != -1) { throw new ArgumentException(nameof(path)); } int index1 = 0; while (index1 < path.Length && char.IsWhiteSpace(path[index1])) { ++index1; } if (index1 == path.Length) { return(string.Empty); } int index2 = path.Length - 1; while (index2 >= index1 && char.IsWhiteSpace(path[index2])) { --index2; } int num = index2 - index1 + 1; using (var reusableResourceHolder = ReusableStringBuilder.AcquireDefault(260)) { StringBuilder resource = reusableResourceHolder.Resource; bool flag1 = false; bool flag2 = false; for (int index3 = index1; index3 <= index2; ++index3) { char c = path[index3]; if (c == Path.AltDirectorySeparatorChar) { c = Path.DirectorySeparatorChar; flag2 = true; } if (c == Path.DirectorySeparatorChar) { if (flag1 && index3 > index1 + 1) { flag2 = true; continue; } } else if (char.IsUpper(c)) { c = char.ToLower(c); flag2 = true; } flag1 = c == Path.DirectorySeparatorChar; resource.Append(c); } if (flag1 && resource.Length > 3) { resource.Remove(resource.Length - 1, 1); flag2 = true; } if (!flag2 && num == path.Length) { return(path); } return(resource.ToString()); } }