*
* File_PathJoin --
*
- * Join the dirName and baseName together to create a (full) path but
- * don't add a DIRSEPS unless necessary.
+ * Join the dirName and baseName together to create a (full) path.
+ *
+ * This code concatenates two strings together and omits a redundant
+ * directory separator between the two. It does not treat a fully qualified
+ * baseName the way one would expect:
+ *
+ * File_PathJoin("", "b") -> "/b"
+ * File_PathJoin("/", "b") -> "/b"
+ * File_PathJoin("a", "b") -> "a/b"
+ * File_PathJoin("a/", "b") -> "a/b"
+ * File_PathJoin("a/////", "b") -> "a/b"
+ * File_PathJoin("a", "") -> "a/"
+ * File_PathJoin("a", "/") -> "a/" (only posix)
+ * File_PathJoin("a", "/b") -> "a/b" (only posix)
+ * File_PathJoin("a", "/////b") -> "a/b" (only posix)
+ * File_PathJoin("a/", "/b") -> "a/b" (only posix)
+ * File_PathJoin("a/////", "/////b") -> "a/b" (only posix)
*
- * File_PathJoin("a", "b") -> "a/b"
- * File_PathJoin("a/", "b") -> "a/b"
- *
- * Results:
+ * Results:
* The constructed path which must be freed by the caller.
*
- * Side effects:
+ * Side effects:
* None
*
*---------------------------------------------------------------------------
ConstUnicode baseName) // IN:
{
Unicode result;
+ Unicode newDir = NULL;
ASSERT(dirName);
ASSERT(baseName);
- if (Unicode_EndsWith(dirName, DIRSEPS)) {
- result = Unicode_Append(dirName, baseName);
- } else {
- result = Unicode_Join(dirName, DIRSEPS, baseName, NULL);
+#if defined(_WIN32)
+ ASSERT(!Unicode_StartsWith(baseName, DIRSEPS));
+ ASSERT(!Unicode_StartsWith(baseName, "/"));
+ ASSERT(Unicode_LengthInCodePoints(baseName) < 2 ||
+ Unicode_FindSubstrInRange(baseName, 1, 1, ":", 0, 1) ==
+ UNICODE_INDEX_NOT_FOUND);
+#else
+ /*
+ * Remove ALL directory separators from baseName begin.
+ */
+ while(Unicode_StartsWith(baseName, DIRSEPS)) {
+ baseName++;
}
+#endif
+
+ /*
+ * Remove ALL directory separators from dirName end.
+ */
+ newDir = File_StripSlashes(dirName);
+
+ result = Unicode_Join(newDir, DIRSEPS, baseName, NULL);
+ Unicode_Free(newDir);
return result;
}