Refactored UriHelper.Reduce to use string.Split.
authorMarcos Henrich <marcos.henrich@xamarin.com>
Fri, 25 Jul 2014 12:31:57 +0000 (13:31 +0100)
committerMarcos Henrich <marcos.henrich@xamarin.com>
Fri, 25 Jul 2014 12:31:57 +0000 (13:31 +0100)
mcs/class/System/System/UriHelper.cs

index 2b1bdcf0ec7cc68e72054c6c194bb3eff73cd871..78cdca8fbe8d22a9fadf8a3c1879d834b5d1c026 100644 (file)
@@ -497,42 +497,38 @@ namespace System {
 
                        List<string> result = new List<string> ();
 
-                       bool begin = true;
-                       for (int startpos = 0; startpos < path.Length; ) {
-                               endWithSlash = true;
-
-                               int endpos = path.IndexOf ('/', startpos);
-                               if (endpos == -1)
-                                       endpos = path.Length;
-                               string current = path.Substring (startpos, endpos-startpos);
-                               startpos = endpos + 1;
-                               if (begin && current.Length == 0) {
-                                       begin = false;
+                       string[] segments = path.Split ('/');
+                       int lastSegmentIndex = segments.Length - 1;
+                       for (var i = 0; i <= lastSegmentIndex; i++) {
+                               string segment = segments [i];
+
+                               if (i == lastSegmentIndex &&
+                                       (segment.Length == 0 || segment == ".." || segment == "."))
+                                       endWithSlash = true;
+
+                               if ((i == 0 || i == lastSegmentIndex) && segment.Length == 0)
                                        continue;
-                               }
 
-                               begin = false;
-                               if (current == "..") {
+                               if (segment == "..") {
                                        int resultCount = result.Count;
                                        // in 2.0 profile, skip leading ".." parts
-                                       if (resultCount == 0) {
+                                       if (resultCount == 0)
                                                continue;
-                                       }
 
                                        result.RemoveAt (resultCount - 1);
                                        continue;
                                }
 
-                               if (current == "." ||
-                                       (trimDots && current.EndsWith("."))) {
-                                       current = current.TrimEnd('.');
-                                       if (current == "" && endpos < path.Length)
+                               if (segment == "." ||
+                                       (trimDots && segment.EndsWith (".", StringComparison.Ordinal))) {
+                                       segment = segment.TrimEnd ('.');
+                                       if (segment == "" && i < lastSegmentIndex)
                                                continue;
                                }
 
                                endWithSlash = false;
 
-                               result.Add (current);
+                               result.Add (segment);
                        }
 
                        if (result.Count == 0)