public static string canonicalizePath(string path, bool discardRelativePrefix){int segmentStart = 0;int deletableSegments = 0;{for (int i = 0; i <= path.Length; ){int nextSegmentStart;if (i == path.Length){nextSegmentStart = i;}else{if (path[i] == '/'){nextSegmentStart = i + 1;}else{i++;continue;}}if (i == segmentStart + 1 && Sharpen.StringHelper.RegionMatches(path, segmentStart, ".", 0, 1)){path = Sharpen.StringHelper.Substring(path, 0, segmentStart) + Sharpen.StringHelper.Substring(path, nextSegmentStart);i = segmentStart;}else{if (i == segmentStart + 2 && Sharpen.StringHelper.RegionMatches(path, segmentStart, "..", 0, 2)){if (deletableSegments > 0 || discardRelativePrefix){deletableSegments--;int prevSegmentStart = path.LastIndexOf('/', segmentStart - 2) + 1;path = Sharpen.StringHelper.Substring(path, 0, prevSegmentStart) + Sharpen.StringHelper.Substring(path, nextSegmentStart);i = segmentStart = prevSegmentStart;}else{i++;segmentStart = i;}}else{if (i > 0){deletableSegments++;}i++;segmentStart = i;}}}}return path;}