if (path1.IndexOfAny (_InvalidPathChars) != -1)
throw new ArgumentException ("Illegal characters in path", "path1");
- int len = path1.Length + 1;
+ int len = path1.Length;
+ int start = -1;
for (int i = 0; i < paths.Length; ++i) {
if (paths [i] == null)
- throw new ArgumentNullException ("paths");
+ throw new ArgumentNullException ("paths[" + i + "]");
+ if (paths [i].IndexOfAny (_InvalidPathChars) != -1)
+ throw new ArgumentException ("Illegal characters in path", "paths[" + i + "]");
+ if (IsPathRooted (paths [i])) {
+ len = 0;
+ start = i;
+ }
len += paths [i].Length + 1;
}
StringBuilder sb = new StringBuilder (len);
- sb.Append (path1);
- for (int i = 0; i < paths.Length; ++i)
+ if (start == -1) {
+ sb.Append (path1);
+ start = 0;
+ }
+ for (int i = start; i < paths.Length; ++i)
Combine (sb, paths [i]);
return sb.ToString ();
}
private static void Combine (StringBuilder path, string part)
{
- if (part.IndexOfAny (_InvalidPathChars) != -1)
- throw new ArgumentException ("Illegal characters in path", "path1");
- char end = path [path.Length-1];
- if (end != DirectorySeparatorChar &&
- end != AltDirectorySeparatorChar &&
- end != VolumeSeparatorChar)
- path.Append (DirectorySeparatorChar);
+ if (path.Length > 0 && part.Length > 0) {
+ char end = path [path.Length-1];
+ if (end != DirectorySeparatorChar &&
+ end != AltDirectorySeparatorChar &&
+ end != VolumeSeparatorChar)
+ path.Append (DirectorySeparatorChar);
+ }
path.Append (part);
}
--- /dev/null
+//
+// Mono.Unix.UnixPath Test Cases
+//
+// Authors:
+// Jonathan Pryor (jonpryor@vt.edu)
+//
+// (c) 2006 Jonathan Pryor
+//
+
+using NUnit.Framework;
+using System.IO;
+using System;
+using System.Text;
+using Mono.Unix;
+
+namespace MonoTests.Mono.Unix
+{
+
+ [TestFixture, Category ("NotDotNet")]
+ public class UnixPathTest {
+
+ private static readonly char DSC = UnixPath.DirectorySeparatorChar;
+
+ [Test]
+ public void Combine ()
+ {
+ string path, expected;
+ string current = UnixDirectoryInfo.GetCurrentDirectory ();
+
+ path = UnixPath.Combine ("/etc", "init.d");
+ Assert.AreEqual ("/etc/init.d", path);
+
+ path = UnixPath.Combine ("one", "");
+ Assert.AreEqual ("one", path);
+
+ path = UnixPath.Combine ("", "one");
+ Assert.AreEqual ("one", path);
+
+ path = UnixPath.Combine (current, "one");
+ expected = current + DSC + "one";
+ Assert.AreEqual (expected, path);
+
+ path = UnixPath.Combine ("one", current);
+ Assert.AreEqual (current, path);
+
+ path = UnixPath.Combine (current, expected);
+ Assert.AreEqual (expected, path);
+
+ path = DSC + "one";
+ path = UnixPath.Combine (path, "two" + DSC);
+ expected = DSC + "one" + DSC + "two" + DSC;
+ Assert.AreEqual (expected, path);
+
+ path = "one" + DSC;
+ path = UnixPath.Combine (path, DSC + "two");
+ expected = DSC + "two";
+ Assert.AreEqual (expected, path);
+
+ path = "one" + DSC;
+ path = UnixPath.Combine (path, "two" + DSC);
+ expected = "one" + DSC + "two" + DSC;
+ Assert.AreEqual (expected, path);
+
+ path = UnixPath.Combine ("/a", "b", "c", "/d", "e");
+ expected = "/d/e";
+ Assert.AreEqual (expected, path);
+
+ try {
+ path = UnixPath.Combine ("one", null);
+ Assert.Fail ("Combine Fail #01");
+ }
+ catch (Exception e) {
+ Assert.AreEqual (typeof (ArgumentNullException), e.GetType ());
+ }
+
+ try {
+ path = UnixPath.Combine (null, "one");
+ Assert.Fail ("Combine Fail #02");
+ }
+ catch (Exception e) {
+ Assert.AreEqual (typeof (ArgumentNullException), e.GetType ());
+ }
+ }
+ }
+}
+