PageRenderTime 215ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 1ms

/std/path.d

http://github.com/jcd/phobos
D | 2926 lines | 1991 code | 275 blank | 660 comment | 658 complexity | c3e22674ee5c1e4ccc9289b6acb496e0 MD5 | raw file
  1. // Written in the D programming language.
  2. /** This module is used to manipulate _path strings.
  3. All functions, with the exception of $(LREF expandTilde) (and in some
  4. cases $(LREF absolutePath) and $(LREF relativePath)), are pure
  5. string manipulation functions; they don't depend on any state outside
  6. the program, nor do they perform any actual file system actions.
  7. This has the consequence that the module does not make any distinction
  8. between a _path that points to a directory and a _path that points to a
  9. file, and it does not know whether or not the object pointed to by the
  10. _path actually exists in the file system.
  11. To differentiate between these cases, use $(XREF file,isDir) and
  12. $(XREF file,exists).
  13. Note that on Windows, both the backslash ($(D `\`)) and the slash ($(D `/`))
  14. are in principle valid directory separators. This module treats them
  15. both on equal footing, but in cases where a $(I new) separator is
  16. added, a backslash will be used. Furthermore, the $(LREF buildNormalizedPath)
  17. function will replace all slashes with backslashes on that platform.
  18. In general, the functions in this module assume that the input paths
  19. are well-formed. (That is, they should not contain invalid characters,
  20. they should follow the file system's _path format, etc.) The result
  21. of calling a function on an ill-formed _path is undefined. When there
  22. is a chance that a _path or a file name is invalid (for instance, when it
  23. has been input by the user), it may sometimes be desirable to use the
  24. $(LREF isValidFilename) and $(LREF isValidPath) functions to check
  25. this.
  26. Most functions do not perform any memory allocations, and if a string is
  27. returned, it is usually a slice of an input string. If a function
  28. allocates, this is explicitly mentioned in the documentation.
  29. Authors:
  30. Lars Tandle Kyllingstad,
  31. $(WEB digitalmars.com, Walter Bright),
  32. Grzegorz Adam Hankiewicz,
  33. Thomas Kühne,
  34. $(WEB erdani.org, Andrei Alexandrescu)
  35. Copyright:
  36. Copyright (c) 2000–2011, the authors. All rights reserved.
  37. License:
  38. $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0)
  39. Source:
  40. $(PHOBOSSRC std/_path.d)
  41. Macros:
  42. WIKI = Phobos/StdPath
  43. */
  44. module std.path;
  45. import std.algorithm;
  46. import std.array;
  47. import std.conv;
  48. import std.file: getcwd;
  49. import std.range;
  50. import std.string;
  51. import std.traits;
  52. version(Posix)
  53. {
  54. import core.exception;
  55. import core.stdc.errno;
  56. import core.sys.posix.pwd;
  57. import core.sys.posix.stdlib;
  58. private import core.exception : onOutOfMemoryError;
  59. }
  60. /** String used to separate directory names in a path. Under
  61. POSIX this is a slash, under Windows a backslash.
  62. */
  63. version(Posix) enum string dirSeparator = "/";
  64. else version(Windows) enum string dirSeparator = "\\";
  65. else static assert (0, "unsupported platform");
  66. /** Path separator string. A colon under POSIX, a semicolon
  67. under Windows.
  68. */
  69. version(Posix) enum string pathSeparator = ":";
  70. else version(Windows) enum string pathSeparator = ";";
  71. else static assert (0, "unsupported platform");
  72. /** Determines whether the given character is a directory separator.
  73. On Windows, this includes both $(D `\`) and $(D `/`).
  74. On POSIX, it's just $(D `/`).
  75. */
  76. bool isDirSeparator(dchar c) @safe pure nothrow
  77. {
  78. if (c == '/') return true;
  79. version(Windows) if (c == '\\') return true;
  80. return false;
  81. }
  82. /* Determines whether the given character is a drive separator.
  83. On Windows, this is true if c is the ':' character that separates
  84. the drive letter from the rest of the path. On POSIX, this always
  85. returns false.
  86. */
  87. private bool isDriveSeparator(dchar c) @safe pure nothrow
  88. {
  89. version(Windows) return c == ':';
  90. else return false;
  91. }
  92. /* Combines the isDirSeparator and isDriveSeparator tests. */
  93. version(Windows) private bool isSeparator(dchar c) @safe pure nothrow
  94. {
  95. return isDirSeparator(c) || isDriveSeparator(c);
  96. }
  97. version(Posix) private alias isDirSeparator isSeparator;
  98. /* Helper function that determines the position of the last
  99. drive/directory separator in a string. Returns -1 if none
  100. is found.
  101. */
  102. private ptrdiff_t lastSeparator(C)(in C[] path) @safe pure nothrow
  103. if (isSomeChar!C)
  104. {
  105. auto i = (cast(ptrdiff_t) path.length) - 1;
  106. while (i >= 0 && !isSeparator(path[i])) --i;
  107. return i;
  108. }
  109. version (Windows)
  110. {
  111. private bool isUNC(C)(in C[] path) @safe pure nothrow if (isSomeChar!C)
  112. {
  113. return path.length >= 3 && isDirSeparator(path[0]) && isDirSeparator(path[1])
  114. && !isDirSeparator(path[2]);
  115. }
  116. private ptrdiff_t uncRootLength(C)(in C[] path) @safe pure nothrow if (isSomeChar!C)
  117. in { assert (isUNC(path)); }
  118. body
  119. {
  120. ptrdiff_t i = 3;
  121. while (i < path.length && !isDirSeparator(path[i])) ++i;
  122. if (i < path.length)
  123. {
  124. auto j = i;
  125. do { ++j; } while (j < path.length && isDirSeparator(path[j]));
  126. if (j < path.length)
  127. {
  128. do { ++j; } while (j < path.length && !isDirSeparator(path[j]));
  129. i = j;
  130. }
  131. }
  132. return i;
  133. }
  134. private bool hasDrive(C)(in C[] path) @safe pure nothrow if (isSomeChar!C)
  135. {
  136. return path.length >= 2 && isDriveSeparator(path[1]);
  137. }
  138. private bool isDriveRoot(C)(in C[] path) @safe pure nothrow if (isSomeChar!C)
  139. {
  140. return path.length >= 3 && isDriveSeparator(path[1])
  141. && isDirSeparator(path[2]);
  142. }
  143. }
  144. /* Helper functions that strip leading/trailing slashes and backslashes
  145. from a path.
  146. */
  147. private inout(C)[] ltrimDirSeparators(C)(inout(C)[] path) @safe pure nothrow
  148. if (isSomeChar!C)
  149. {
  150. int i = 0;
  151. while (i < path.length && isDirSeparator(path[i])) ++i;
  152. return path[i .. $];
  153. }
  154. private inout(C)[] rtrimDirSeparators(C)(inout(C)[] path) @safe pure nothrow
  155. if (isSomeChar!C)
  156. {
  157. auto i = (cast(ptrdiff_t) path.length) - 1;
  158. while (i >= 0 && isDirSeparator(path[i])) --i;
  159. return path[0 .. i+1];
  160. }
  161. private inout(C)[] trimDirSeparators(C)(inout(C)[] path) @safe pure nothrow
  162. if (isSomeChar!C)
  163. {
  164. return ltrimDirSeparators(rtrimDirSeparators(path));
  165. }
  166. /** This $(D enum) is used as a template argument to functions which
  167. compare file names, and determines whether the comparison is
  168. case sensitive or not.
  169. */
  170. enum CaseSensitive : bool
  171. {
  172. /// File names are case insensitive
  173. no = false,
  174. /// File names are case sensitive
  175. yes = true,
  176. /** The default (or most common) setting for the current platform.
  177. That is, $(D no) on Windows and Mac OS X, and $(D yes) on all
  178. POSIX systems except OS X (Linux, *BSD, etc.).
  179. */
  180. osDefault = osDefaultCaseSensitivity
  181. }
  182. version (Windows) private enum osDefaultCaseSensitivity = false;
  183. else version (OSX) private enum osDefaultCaseSensitivity = false;
  184. else version (Posix) private enum osDefaultCaseSensitivity = true;
  185. else static assert (0);
  186. /** Returns the name of a file, without any leading directory
  187. and with an optional suffix chopped off.
  188. If $(D suffix) is specified, it will be compared to $(D path)
  189. using $(D filenameCmp!cs),
  190. where $(D cs) is an optional template parameter determining whether
  191. the comparison is case sensitive or not. See the
  192. $(LREF filenameCmp) documentation for details.
  193. Examples:
  194. ---
  195. assert (baseName("dir/file.ext") == "file.ext");
  196. assert (baseName("dir/file.ext", ".ext") == "file");
  197. assert (baseName("dir/file.ext", ".xyz") == "file.ext");
  198. assert (baseName("dir/filename", "name") == "file");
  199. assert (baseName("dir/subdir/") == "subdir");
  200. version (Windows)
  201. {
  202. assert (baseName(`d:file.ext`) == "file.ext");
  203. assert (baseName(`d:\dir\file.ext`) == "file.ext");
  204. }
  205. ---
  206. Note:
  207. This function $(I only) strips away the specified suffix, which
  208. doesn't necessarily have to represent an extension. If you want
  209. to remove the extension from a path, regardless of what the extension
  210. is, use $(LREF stripExtension).
  211. If you want the filename without leading directories and without
  212. an extension, combine the functions like this:
  213. ---
  214. assert (baseName(stripExtension("dir/file.ext")) == "file");
  215. ---
  216. Standards:
  217. This function complies with
  218. $(LINK2 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/basename.html,
  219. the POSIX requirements for the 'basename' shell utility)
  220. (with suitable adaptations for Windows paths).
  221. */
  222. inout(C)[] baseName(C)(inout(C)[] path)
  223. @trusted pure //TODO: nothrow (BUG 5700)
  224. if (isSomeChar!C)
  225. {
  226. auto p1 = stripDrive(path);
  227. if (p1.empty)
  228. {
  229. version (Windows) if (isUNC(path))
  230. {
  231. return cast(typeof(return)) dirSeparator.dup;
  232. }
  233. return null;
  234. }
  235. auto p2 = rtrimDirSeparators(p1);
  236. if (p2.empty) return p1[0 .. 1];
  237. return p2[lastSeparator(p2)+1 .. $];
  238. }
  239. /// ditto
  240. inout(C)[] baseName(CaseSensitive cs = CaseSensitive.osDefault, C, C1)
  241. (inout(C)[] path, in C1[] suffix)
  242. @safe pure //TODO: nothrow (because of filenameCmp())
  243. if (isSomeChar!C && isSomeChar!C1)
  244. {
  245. auto p = baseName(path);
  246. if (p.length > suffix.length
  247. && filenameCmp!cs(p[$-suffix.length .. $], suffix) == 0)
  248. {
  249. return p[0 .. $-suffix.length];
  250. }
  251. else return p;
  252. }
  253. unittest
  254. {
  255. assert (baseName("").empty);
  256. assert (baseName("file.ext"w) == "file.ext");
  257. assert (baseName("file.ext"d, ".ext") == "file");
  258. assert (baseName("file", "file"w.dup) == "file");
  259. assert (baseName("dir/file.ext"d.dup) == "file.ext");
  260. assert (baseName("dir/file.ext", ".ext"d) == "file");
  261. assert (baseName("dir/file"w, "file"d) == "file");
  262. assert (baseName("dir///subdir////") == "subdir");
  263. assert (baseName("dir/subdir.ext/", ".ext") == "subdir");
  264. assert (baseName("dir/subdir/".dup, "subdir") == "subdir");
  265. assert (baseName("/"w.dup) == "/");
  266. assert (baseName("//"d.dup) == "/");
  267. assert (baseName("///") == "/");
  268. assert (baseName!(CaseSensitive.yes)("file.ext", ".EXT") == "file.ext");
  269. assert (baseName!(CaseSensitive.no)("file.ext", ".EXT") == "file");
  270. version (Windows)
  271. {
  272. assert (baseName(`dir\file.ext`) == `file.ext`);
  273. assert (baseName(`dir\file.ext`, `.ext`) == `file`);
  274. assert (baseName(`dir\file`, `file`) == `file`);
  275. assert (baseName(`d:file.ext`) == `file.ext`);
  276. assert (baseName(`d:file.ext`, `.ext`) == `file`);
  277. assert (baseName(`d:file`, `file`) == `file`);
  278. assert (baseName(`dir\\subdir\\\`) == `subdir`);
  279. assert (baseName(`dir\subdir.ext\`, `.ext`) == `subdir`);
  280. assert (baseName(`dir\subdir\`, `subdir`) == `subdir`);
  281. assert (baseName(`\`) == `\`);
  282. assert (baseName(`\\`) == `\`);
  283. assert (baseName(`\\\`) == `\`);
  284. assert (baseName(`d:\`) == `\`);
  285. assert (baseName(`d:`).empty);
  286. assert (baseName(`\\server\share\file`) == `file`);
  287. assert (baseName(`\\server\share\`) == `\`);
  288. assert (baseName(`\\server\share`) == `\`);
  289. }
  290. assert (baseName(stripExtension("dir/file.ext")) == "file");
  291. static assert (baseName("dir/file.ext") == "file.ext");
  292. static assert (baseName("dir/file.ext", ".ext") == "file");
  293. }
  294. /** Returns the directory part of a path. On Windows, this
  295. includes the drive letter if present.
  296. This function performs a memory allocation if and only if $(D path)
  297. is mutable and does not have a directory (in which case a new mutable
  298. string is needed to hold the returned current-directory symbol,
  299. $(D ".")).
  300. Examples:
  301. ---
  302. assert (dirName("file") == ".");
  303. assert (dirName("dir/file") == "dir");
  304. assert (dirName("/file") == "/");
  305. assert (dirName("dir/subdir/") == "dir");
  306. version (Windows)
  307. {
  308. assert (dirName("d:file") == "d:");
  309. assert (dirName(`d:\dir\file`) == `d:\dir`);
  310. assert (dirName(`d:\file`) == `d:\`);
  311. assert (dirName(`dir\subdir\`) == `dir`);
  312. }
  313. ---
  314. Standards:
  315. This function complies with
  316. $(LINK2 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/dirname.html,
  317. the POSIX requirements for the 'dirname' shell utility)
  318. (with suitable adaptations for Windows paths).
  319. */
  320. C[] dirName(C)(C[] path)
  321. //TODO: @safe (BUG 6169) pure nothrow (because of to())
  322. if (isSomeChar!C)
  323. {
  324. if (path.empty) return to!(typeof(return))(".");
  325. auto p = rtrimDirSeparators(path);
  326. if (p.empty) return path[0 .. 1];
  327. version (Windows)
  328. {
  329. if (isUNC(p) && uncRootLength(p) == p.length)
  330. return p;
  331. if (p.length == 2 && isDriveSeparator(p[1]) && path.length > 2)
  332. return path[0 .. 3];
  333. }
  334. auto i = lastSeparator(p);
  335. if (i == -1) return to!(typeof(return))(".");
  336. if (i == 0) return p[0 .. 1];
  337. version (Windows)
  338. {
  339. // If the directory part is either d: or d:\, don't
  340. // chop off the last symbol.
  341. if (isDriveSeparator(p[i]) || isDriveSeparator(p[i-1]))
  342. return p[0 .. i+1];
  343. }
  344. // Remove any remaining trailing (back)slashes.
  345. return rtrimDirSeparators(p[0 .. i]);
  346. }
  347. unittest
  348. {
  349. assert (dirName("") == ".");
  350. assert (dirName("file"w) == ".");
  351. assert (dirName("dir/"d) == ".");
  352. assert (dirName("dir///") == ".");
  353. assert (dirName("dir/file"w.dup) == "dir");
  354. assert (dirName("dir///file"d.dup) == "dir");
  355. assert (dirName("dir/subdir/") == "dir");
  356. assert (dirName("/dir/file"w) == "/dir");
  357. assert (dirName("/file"d) == "/");
  358. assert (dirName("/") == "/");
  359. assert (dirName("///") == "/");
  360. version (Windows)
  361. {
  362. assert (dirName(`dir\`) == `.`);
  363. assert (dirName(`dir\\\`) == `.`);
  364. assert (dirName(`dir\file`) == `dir`);
  365. assert (dirName(`dir\\\file`) == `dir`);
  366. assert (dirName(`dir\subdir\`) == `dir`);
  367. assert (dirName(`\dir\file`) == `\dir`);
  368. assert (dirName(`\file`) == `\`);
  369. assert (dirName(`\`) == `\`);
  370. assert (dirName(`\\\`) == `\`);
  371. assert (dirName(`d:`) == `d:`);
  372. assert (dirName(`d:file`) == `d:`);
  373. assert (dirName(`d:\`) == `d:\`);
  374. assert (dirName(`d:\file`) == `d:\`);
  375. assert (dirName(`d:\dir\file`) == `d:\dir`);
  376. assert (dirName(`\\server\share\dir\file`) == `\\server\share\dir`);
  377. assert (dirName(`\\server\share\file`) == `\\server\share`);
  378. assert (dirName(`\\server\share\`) == `\\server\share`);
  379. assert (dirName(`\\server\share`) == `\\server\share`);
  380. }
  381. static assert (dirName("dir/file") == "dir");
  382. }
  383. /** Returns the root directory of the specified path, or $(D null) if the
  384. path is not rooted.
  385. Examples:
  386. ---
  387. assert (rootName("foo") is null);
  388. assert (rootName("/foo") == "/");
  389. version (Windows)
  390. {
  391. assert (rootName(`\foo`) == `\`);
  392. assert (rootName(`c:\foo`) == `c:\`);
  393. assert (rootName(`\\server\share\foo`) == `\\server\share`);
  394. }
  395. ---
  396. */
  397. inout(C)[] rootName(C)(inout(C)[] path) @safe pure nothrow if (isSomeChar!C)
  398. {
  399. if (path.empty) return null;
  400. version (Posix)
  401. {
  402. if (isDirSeparator(path[0])) return path[0 .. 1];
  403. }
  404. else version (Windows)
  405. {
  406. if (isDirSeparator(path[0]))
  407. {
  408. if (isUNC(path)) return path[0 .. uncRootLength(path)];
  409. else return path[0 .. 1];
  410. }
  411. else if (path.length >= 3 && isDriveSeparator(path[1]) &&
  412. isDirSeparator(path[2]))
  413. {
  414. return path[0 .. 3];
  415. }
  416. }
  417. else static assert (0, "unsupported platform");
  418. assert (!isRooted(path));
  419. return null;
  420. }
  421. unittest
  422. {
  423. assert (rootName("") is null);
  424. assert (rootName("foo") is null);
  425. assert (rootName("/") == "/");
  426. assert (rootName("/foo/bar") == "/");
  427. version (Windows)
  428. {
  429. assert (rootName("d:foo") is null);
  430. assert (rootName(`d:\foo`) == `d:\`);
  431. assert (rootName(`\\server\share\foo`) == `\\server\share`);
  432. assert (rootName(`\\server\share`) == `\\server\share`);
  433. }
  434. }
  435. /** Returns the drive of a path, or $(D null) if the drive
  436. is not specified. In the case of UNC paths, the network share
  437. is returned.
  438. Always returns $(D null) on POSIX.
  439. Examples:
  440. ---
  441. version (Windows)
  442. {
  443. assert (driveName(`d:\file`) == "d:");
  444. assert (driveName(`\\server\share\file`) == `\\server\share`);
  445. assert (driveName(`dir\file`).empty);
  446. }
  447. ---
  448. */
  449. inout(C)[] driveName(C)(inout(C)[] path) @safe pure nothrow
  450. if (isSomeChar!C)
  451. {
  452. version (Windows)
  453. {
  454. if (hasDrive(path))
  455. return path[0 .. 2];
  456. else if (isUNC(path))
  457. return path[0 .. uncRootLength(path)];
  458. }
  459. return null;
  460. }
  461. unittest
  462. {
  463. version (Posix) assert (driveName("c:/foo").empty);
  464. version (Windows)
  465. {
  466. assert (driveName(`dir\file`).empty);
  467. assert (driveName(`d:file`) == "d:");
  468. assert (driveName(`d:\file`) == "d:");
  469. assert (driveName("d:") == "d:");
  470. assert (driveName(`\\server\share\file`) == `\\server\share`);
  471. assert (driveName(`\\server\share\`) == `\\server\share`);
  472. assert (driveName(`\\server\share`) == `\\server\share`);
  473. static assert (driveName(`d:\file`) == "d:");
  474. }
  475. }
  476. /** Strips the drive from a Windows path. On POSIX, the path is returned
  477. unaltered.
  478. Example:
  479. ---
  480. version (Windows)
  481. {
  482. assert (stripDrive(`d:\dir\file`) == `\dir\file`);
  483. assert (stripDrive(`\\server\share\dir\file`) == `\dir\file`);
  484. }
  485. ---
  486. */
  487. inout(C)[] stripDrive(C)(inout(C)[] path) @safe pure nothrow if (isSomeChar!C)
  488. {
  489. version(Windows)
  490. {
  491. if (hasDrive(path)) return path[2 .. $];
  492. else if (isUNC(path)) return path[uncRootLength(path) .. $];
  493. }
  494. return path;
  495. }
  496. unittest
  497. {
  498. version(Windows)
  499. {
  500. assert (stripDrive(`d:\dir\file`) == `\dir\file`);
  501. assert (stripDrive(`\\server\share\dir\file`) == `\dir\file`);
  502. static assert (stripDrive(`d:\dir\file`) == `\dir\file`);
  503. }
  504. version(Posix)
  505. {
  506. assert (stripDrive(`d:\dir\file`) == `d:\dir\file`);
  507. }
  508. }
  509. /* Helper function that returns the position of the filename/extension
  510. separator dot in path. If not found, returns -1.
  511. */
  512. private ptrdiff_t extSeparatorPos(C)(in C[] path) @safe pure nothrow
  513. if (isSomeChar!C)
  514. {
  515. auto i = (cast(ptrdiff_t) path.length) - 1;
  516. while (i >= 0 && !isSeparator(path[i]))
  517. {
  518. if (path[i] == '.' && i > 0 && !isSeparator(path[i-1])) return i;
  519. --i;
  520. }
  521. return -1;
  522. }
  523. /** Returns the _extension part of a file name, including the dot.
  524. If there is no _extension, $(D null) is returned.
  525. Examples:
  526. ---
  527. assert (extension("file").empty);
  528. assert (extension("file.ext") == ".ext");
  529. assert (extension("file.ext1.ext2") == ".ext2");
  530. assert (extension("file.") == ".");
  531. assert (extension(".file").empty);
  532. assert (extension(".file.ext") == ".ext");
  533. ---
  534. */
  535. inout(C)[] extension(C)(inout(C)[] path) @safe pure nothrow if (isSomeChar!C)
  536. {
  537. auto i = extSeparatorPos(path);
  538. if (i == -1) return null;
  539. else return path[i .. $];
  540. }
  541. unittest
  542. {
  543. assert (extension("file").empty);
  544. assert (extension("file.") == ".");
  545. assert (extension("file.ext"w) == ".ext");
  546. assert (extension("file.ext1.ext2"d) == ".ext2");
  547. assert (extension(".foo".dup).empty);
  548. assert (extension(".foo.ext"w.dup) == ".ext");
  549. assert (extension("dir/file"d.dup).empty);
  550. assert (extension("dir/file.") == ".");
  551. assert (extension("dir/file.ext") == ".ext");
  552. assert (extension("dir/file.ext1.ext2"w) == ".ext2");
  553. assert (extension("dir/.foo"d).empty);
  554. assert (extension("dir/.foo.ext".dup) == ".ext");
  555. version(Windows)
  556. {
  557. assert (extension(`dir\file`).empty);
  558. assert (extension(`dir\file.`) == ".");
  559. assert (extension(`dir\file.ext`) == `.ext`);
  560. assert (extension(`dir\file.ext1.ext2`) == `.ext2`);
  561. assert (extension(`dir\.foo`).empty);
  562. assert (extension(`dir\.foo.ext`) == `.ext`);
  563. assert (extension(`d:file`).empty);
  564. assert (extension(`d:file.`) == ".");
  565. assert (extension(`d:file.ext`) == `.ext`);
  566. assert (extension(`d:file.ext1.ext2`) == `.ext2`);
  567. assert (extension(`d:.foo`).empty);
  568. assert (extension(`d:.foo.ext`) == `.ext`);
  569. }
  570. static assert (extension("file").empty);
  571. static assert (extension("file.ext") == ".ext");
  572. }
  573. /** Returns the path with the extension stripped off.
  574. Examples:
  575. ---
  576. assert (stripExtension("file") == "file");
  577. assert (stripExtension("file.ext") == "file");
  578. assert (stripExtension("file.ext1.ext2") == "file.ext1");
  579. assert (stripExtension("file.") == "file");
  580. assert (stripExtension(".file") == ".file");
  581. assert (stripExtension(".file.ext") == ".file");
  582. assert (stripExtension("dir/file.ext") == "dir/file");
  583. ---
  584. */
  585. inout(C)[] stripExtension(C)(inout(C)[] path) @safe pure nothrow
  586. if (isSomeChar!C)
  587. {
  588. auto i = extSeparatorPos(path);
  589. if (i == -1) return path;
  590. else return path[0 .. i];
  591. }
  592. unittest
  593. {
  594. assert (stripExtension("file") == "file");
  595. assert (stripExtension("file.ext"w) == "file");
  596. assert (stripExtension("file.ext1.ext2"d) == "file.ext1");
  597. assert (stripExtension(".foo".dup) == ".foo");
  598. assert (stripExtension(".foo.ext"w.dup) == ".foo");
  599. assert (stripExtension("dir/file"d.dup) == "dir/file");
  600. assert (stripExtension("dir/file.ext") == "dir/file");
  601. assert (stripExtension("dir/file.ext1.ext2"w) == "dir/file.ext1");
  602. assert (stripExtension("dir/.foo"d) == "dir/.foo");
  603. assert (stripExtension("dir/.foo.ext".dup) == "dir/.foo");
  604. version(Windows)
  605. {
  606. assert (stripExtension("dir\\file") == "dir\\file");
  607. assert (stripExtension("dir\\file.ext") == "dir\\file");
  608. assert (stripExtension("dir\\file.ext1.ext2") == "dir\\file.ext1");
  609. assert (stripExtension("dir\\.foo") == "dir\\.foo");
  610. assert (stripExtension("dir\\.foo.ext") == "dir\\.foo");
  611. assert (stripExtension("d:file") == "d:file");
  612. assert (stripExtension("d:file.ext") == "d:file");
  613. assert (stripExtension("d:file.ext1.ext2") == "d:file.ext1");
  614. assert (stripExtension("d:.foo") == "d:.foo");
  615. assert (stripExtension("d:.foo.ext") == "d:.foo");
  616. }
  617. static assert (stripExtension("file") == "file");
  618. static assert (stripExtension("file.ext"w) == "file");
  619. }
  620. /** Returns a string containing the _path given by $(D path), but where
  621. the extension has been set to $(D ext).
  622. If the filename already has an extension, it is replaced. If not, the
  623. extension is simply appended to the filename. Including a leading dot
  624. in $(D ext) is optional.
  625. If the extension is empty, this function is equivalent to
  626. $(LREF stripExtension).
  627. This function normally allocates a new string (the possible exception
  628. being the case when path is immutable and doesn't already have an
  629. extension).
  630. Examples:
  631. ---
  632. assert (setExtension("file", "ext") == "file.ext");
  633. assert (setExtension("file", ".ext") == "file.ext");
  634. assert (setExtension("file.old", "") == "file");
  635. assert (setExtension("file.old", "new") == "file.new");
  636. assert (setExtension("file.old", ".new") == "file.new");
  637. ---
  638. */
  639. immutable(Unqual!C1)[] setExtension(C1, C2)(in C1[] path, in C2[] ext)
  640. @trusted pure nothrow
  641. if (isSomeChar!C1 && !is(C1 == immutable) && is(Unqual!C1 == Unqual!C2))
  642. {
  643. if (ext.length > 0 && ext[0] != '.')
  644. return cast(typeof(return))(stripExtension(path)~'.'~ext);
  645. else
  646. return cast(typeof(return))(stripExtension(path)~ext);
  647. }
  648. ///ditto
  649. immutable(C1)[] setExtension(C1, C2)(immutable(C1)[] path, const(C2)[] ext)
  650. @trusted pure nothrow
  651. if (isSomeChar!C1 && is(Unqual!C1 == Unqual!C2))
  652. {
  653. if (ext.length == 0)
  654. return stripExtension(path);
  655. // Optimised for the case where path is immutable and has no extension
  656. if (ext.length > 0 && ext[0] == '.') ext = ext[1 .. $];
  657. auto i = extSeparatorPos(path);
  658. if (i == -1)
  659. {
  660. path ~= '.';
  661. path ~= ext;
  662. return path;
  663. }
  664. else if (i == path.length - 1)
  665. {
  666. path ~= ext;
  667. return path;
  668. }
  669. else
  670. {
  671. return cast(typeof(return))(path[0 .. i+1] ~ ext);
  672. }
  673. }
  674. unittest
  675. {
  676. assert (setExtension("file", "ext") == "file.ext");
  677. assert (setExtension("file"w, ".ext"w) == "file.ext");
  678. assert (setExtension("file."d, "ext"d) == "file.ext");
  679. assert (setExtension("file.", ".ext") == "file.ext");
  680. assert (setExtension("file.old"w, "new"w) == "file.new");
  681. assert (setExtension("file.old"d, ".new"d) == "file.new");
  682. assert (setExtension("file"w.dup, "ext"w) == "file.ext");
  683. assert (setExtension("file"w.dup, ".ext"w) == "file.ext");
  684. assert (setExtension("file."w, "ext"w.dup) == "file.ext");
  685. assert (setExtension("file."w, ".ext"w.dup) == "file.ext");
  686. assert (setExtension("file.old"d.dup, "new"d) == "file.new");
  687. assert (setExtension("file.old"d.dup, ".new"d) == "file.new");
  688. static assert (setExtension("file", "ext") == "file.ext");
  689. static assert (setExtension("file.old", "new") == "file.new");
  690. static assert (setExtension("file"w.dup, "ext"w) == "file.ext");
  691. static assert (setExtension("file.old"d.dup, "new"d) == "file.new");
  692. // Issue 10601
  693. assert (setExtension("file", "") == "file");
  694. assert (setExtension("file.ext", "") == "file");
  695. }
  696. /** Returns the _path given by $(D path), with the extension given by
  697. $(D ext) appended if the path doesn't already have one.
  698. Including the dot in the extension is optional.
  699. This function always allocates a new string, except in the case when
  700. path is immutable and already has an extension.
  701. Examples:
  702. ---
  703. assert (defaultExtension("file", "ext") == "file.ext");
  704. assert (defaultExtension("file", ".ext") == "file.ext");
  705. assert (defaultExtension("file.", "ext") == "file.");
  706. assert (defaultExtension("file.old", "new") == "file.old");
  707. assert (defaultExtension("file.old", ".new") == "file.old");
  708. ---
  709. */
  710. immutable(Unqual!C1)[] defaultExtension(C1, C2)(in C1[] path, in C2[] ext)
  711. @trusted pure // TODO: nothrow (because of to())
  712. if (isSomeChar!C1 && is(Unqual!C1 == Unqual!C2))
  713. {
  714. auto i = extSeparatorPos(path);
  715. if (i == -1)
  716. {
  717. if (ext.length > 0 && ext[0] == '.')
  718. return cast(typeof(return))(path~ext);
  719. else
  720. return cast(typeof(return))(path~'.'~ext);
  721. }
  722. else return to!(typeof(return))(path);
  723. }
  724. unittest
  725. {
  726. assert (defaultExtension("file", "ext") == "file.ext");
  727. assert (defaultExtension("file", ".ext") == "file.ext");
  728. assert (defaultExtension("file.", "ext") == "file.");
  729. assert (defaultExtension("file.old", "new") == "file.old");
  730. assert (defaultExtension("file.old", ".new") == "file.old");
  731. assert (defaultExtension("file"w.dup, "ext"w) == "file.ext");
  732. assert (defaultExtension("file.old"d.dup, "new"d) == "file.old");
  733. static assert (defaultExtension("file", "ext") == "file.ext");
  734. static assert (defaultExtension("file.old", "new") == "file.old");
  735. static assert (defaultExtension("file"w.dup, "ext"w) == "file.ext");
  736. static assert (defaultExtension("file.old"d.dup, "new"d) == "file.old");
  737. }
  738. /** Combines one or more path segments.
  739. This function takes a set of path segments, given as an input
  740. range of string elements or as a set of string arguments,
  741. and concatenates them with each other. Directory separators
  742. are inserted between segments if necessary. If any of the
  743. path segments are absolute (as defined by $(LREF isAbsolute)), the
  744. preceding segments will be dropped.
  745. On Windows, if one of the path segments are rooted, but not absolute
  746. (e.g. $(D `\foo`)), all preceding path segments down to the previous
  747. root will be dropped. (See below for an example.)
  748. This function always allocates memory to hold the resulting path.
  749. The variadic overload is guaranteed to only perform a single
  750. allocation, as is the range version if $(D paths) is a forward
  751. range.
  752. */
  753. immutable(ElementEncodingType!(ElementType!Range))[]
  754. buildPath(Range)(Range segments)
  755. if (isInputRange!Range && isSomeString!(ElementType!Range))
  756. {
  757. if (segments.empty) return null;
  758. // If this is a forward range, we can pre-calculate a maximum length.
  759. static if (isForwardRange!Range)
  760. {
  761. auto segments2 = segments.save;
  762. size_t precalc = 0;
  763. foreach (segment; segments2) precalc += segment.length + 1;
  764. }
  765. // Otherwise, just venture a guess and resize later if necessary.
  766. else size_t precalc = 255;
  767. auto buf = new Unqual!(ElementEncodingType!(ElementType!Range))[](precalc);
  768. size_t pos = 0;
  769. foreach (segment; segments)
  770. {
  771. if (segment.empty) continue;
  772. static if (!isForwardRange!Range)
  773. {
  774. immutable neededLength = pos + segment.length + 1;
  775. if (buf.length < neededLength)
  776. buf.length = reserve(buf, neededLength + buf.length/2);
  777. }
  778. if (pos > 0)
  779. {
  780. if (isRooted(segment))
  781. {
  782. version (Posix)
  783. {
  784. pos = 0;
  785. }
  786. else version (Windows)
  787. {
  788. if (isAbsolute(segment))
  789. pos = 0;
  790. else
  791. {
  792. pos = rootName(buf[0 .. pos]).length;
  793. if (pos > 0 && isDirSeparator(buf[pos-1])) --pos;
  794. }
  795. }
  796. }
  797. else if (!isDirSeparator(buf[pos-1]))
  798. buf[pos++] = dirSeparator[0];
  799. }
  800. buf[pos .. pos + segment.length] = segment[];
  801. pos += segment.length;
  802. }
  803. static U trustedCast(U, V)(V v) @trusted pure nothrow { return cast(U) v; }
  804. return trustedCast!(typeof(return))(buf[0 .. pos]);
  805. }
  806. /// ditto
  807. immutable(C)[] buildPath(C)(const(C[])[] paths...)
  808. @safe pure nothrow
  809. if (isSomeChar!C)
  810. {
  811. return buildPath!(typeof(paths))(paths);
  812. }
  813. ///
  814. unittest
  815. {
  816. version (Posix)
  817. {
  818. assert (buildPath("foo", "bar", "baz") == "foo/bar/baz");
  819. assert (buildPath("/foo/", "bar/baz") == "/foo/bar/baz");
  820. assert (buildPath("/foo", "/bar") == "/bar");
  821. }
  822. version (Windows)
  823. {
  824. assert (buildPath("foo", "bar", "baz") == `foo\bar\baz`);
  825. assert (buildPath(`c:\foo`, `bar\baz`) == `c:\foo\bar\baz`);
  826. assert (buildPath("foo", `d:\bar`) == `d:\bar`);
  827. assert (buildPath("foo", `\bar`) == `\bar`);
  828. assert (buildPath(`c:\foo`, `\bar`) == `c:\bar`);
  829. }
  830. }
  831. unittest // non-documented
  832. {
  833. // ir() wraps an array in a plain (i.e. non-forward) input range, so that
  834. // we can test both code paths
  835. InputRange!(C[]) ir(C)(C[][] p...) { return inputRangeObject(p); }
  836. version (Posix)
  837. {
  838. assert (buildPath("foo") == "foo");
  839. assert (buildPath("/foo/") == "/foo/");
  840. assert (buildPath("foo", "bar") == "foo/bar");
  841. assert (buildPath("foo", "bar", "baz") == "foo/bar/baz");
  842. assert (buildPath("foo/".dup, "bar") == "foo/bar");
  843. assert (buildPath("foo///", "bar".dup) == "foo///bar");
  844. assert (buildPath("/foo"w, "bar"w) == "/foo/bar");
  845. assert (buildPath("foo"w.dup, "/bar"w) == "/bar");
  846. assert (buildPath("foo"w, "bar/"w.dup) == "foo/bar/");
  847. assert (buildPath("/"d, "foo"d) == "/foo");
  848. assert (buildPath(""d.dup, "foo"d) == "foo");
  849. assert (buildPath("foo"d, ""d.dup) == "foo");
  850. assert (buildPath("foo", "bar".dup, "baz") == "foo/bar/baz");
  851. assert (buildPath("foo"w, "/bar"w, "baz"w.dup) == "/bar/baz");
  852. static assert (buildPath("foo", "bar", "baz") == "foo/bar/baz");
  853. static assert (buildPath("foo", "/bar", "baz") == "/bar/baz");
  854. // The following are mostly duplicates of the above, except that the
  855. // range version does not accept mixed constness.
  856. assert (buildPath(ir("foo")) == "foo");
  857. assert (buildPath(ir("/foo/")) == "/foo/");
  858. assert (buildPath(ir("foo", "bar")) == "foo/bar");
  859. assert (buildPath(ir("foo", "bar", "baz")) == "foo/bar/baz");
  860. assert (buildPath(ir("foo/".dup, "bar".dup)) == "foo/bar");
  861. assert (buildPath(ir("foo///".dup, "bar".dup)) == "foo///bar");
  862. assert (buildPath(ir("/foo"w, "bar"w)) == "/foo/bar");
  863. assert (buildPath(ir("foo"w.dup, "/bar"w.dup)) == "/bar");
  864. assert (buildPath(ir("foo"w.dup, "bar/"w.dup)) == "foo/bar/");
  865. assert (buildPath(ir("/"d, "foo"d)) == "/foo");
  866. assert (buildPath(ir(""d.dup, "foo"d.dup)) == "foo");
  867. assert (buildPath(ir("foo"d, ""d)) == "foo");
  868. assert (buildPath(ir("foo", "bar", "baz")) == "foo/bar/baz");
  869. assert (buildPath(ir("foo"w.dup, "/bar"w.dup, "baz"w.dup)) == "/bar/baz");
  870. }
  871. version (Windows)
  872. {
  873. assert (buildPath("foo") == "foo");
  874. assert (buildPath(`\foo/`) == `\foo/`);
  875. assert (buildPath("foo", "bar", "baz") == `foo\bar\baz`);
  876. assert (buildPath("foo", `\bar`) == `\bar`);
  877. assert (buildPath(`c:\foo`, "bar") == `c:\foo\bar`);
  878. assert (buildPath("foo"w, `d:\bar`w.dup) == `d:\bar`);
  879. assert (buildPath(`c:\foo\bar`, `\baz`) == `c:\baz`);
  880. assert (buildPath(`\\foo\bar\baz`d, `foo`d, `\bar`d) == `\\foo\bar\bar`d);
  881. static assert (buildPath("foo", "bar", "baz") == `foo\bar\baz`);
  882. static assert (buildPath("foo", `c:\bar`, "baz") == `c:\bar\baz`);
  883. assert (buildPath(ir("foo")) == "foo");
  884. assert (buildPath(ir(`\foo/`)) == `\foo/`);
  885. assert (buildPath(ir("foo", "bar", "baz")) == `foo\bar\baz`);
  886. assert (buildPath(ir("foo", `\bar`)) == `\bar`);
  887. assert (buildPath(ir(`c:\foo`, "bar")) == `c:\foo\bar`);
  888. assert (buildPath(ir("foo"w.dup, `d:\bar`w.dup)) == `d:\bar`);
  889. assert (buildPath(ir(`c:\foo\bar`, `\baz`)) == `c:\baz`);
  890. assert (buildPath(ir(`\\foo\bar\baz`d, `foo`d, `\bar`d)) == `\\foo\bar\bar`d);
  891. }
  892. // Test that allocation works as it should.
  893. auto manyShort = "aaa".repeat(1000).array();
  894. auto manyShortCombined = join(manyShort, dirSeparator);
  895. assert (buildPath(manyShort) == manyShortCombined);
  896. assert (buildPath(ir(manyShort)) == manyShortCombined);
  897. auto fewLong = 'b'.repeat(500).array().repeat(10).array();
  898. auto fewLongCombined = join(fewLong, dirSeparator);
  899. assert (buildPath(fewLong) == fewLongCombined);
  900. assert (buildPath(ir(fewLong)) == fewLongCombined);
  901. }
  902. unittest
  903. {
  904. // Test for issue 7397
  905. string[] ary = ["a", "b"];
  906. version (Posix)
  907. {
  908. assert (buildPath(ary) == "a/b");
  909. }
  910. else version (Windows)
  911. {
  912. assert (buildPath(ary) == `a\b`);
  913. }
  914. }
  915. /** Performs the same task as $(LREF buildPath),
  916. while at the same time resolving current/parent directory
  917. symbols ($(D ".") and $(D "..")) and removing superfluous
  918. directory separators.
  919. On Windows, slashes are replaced with backslashes.
  920. Note that this function does not resolve symbolic links.
  921. This function always allocates memory to hold the resulting path.
  922. Examples:
  923. ---
  924. version (Posix)
  925. {
  926. assert (buildNormalizedPath("/foo/./bar/..//baz/") == "/foo/baz");
  927. assert (buildNormalizedPath("../foo/.") == "../foo");
  928. assert (buildNormalizedPath("/foo", "bar/baz/") == "/foo/bar/baz");
  929. assert (buildNormalizedPath("/foo", "/bar/..", "baz") == "/baz");
  930. assert (buildNormalizedPath("foo/./bar", "../../", "../baz") == "../baz");
  931. assert (buildNormalizedPath("/foo/./bar", "../../baz") == "/baz");
  932. }
  933. version (Windows)
  934. {
  935. assert (buildNormalizedPath(`c:\foo\.\bar/..\\baz\`) == `c:\foo\baz`);
  936. assert (buildNormalizedPath(`..\foo\.`) == `..\foo`);
  937. assert (buildNormalizedPath(`c:\foo`, `bar\baz\`) == `c:\foo\bar\baz`);
  938. assert (buildNormalizedPath(`c:\foo`, `bar/..`) == `c:\foo`);
  939. assert (buildNormalizedPath(`\\server\share\foo`, `..\bar`) == `\\server\share\bar`);
  940. }
  941. ---
  942. */
  943. immutable(C)[] buildNormalizedPath(C)(const(C[])[] paths...)
  944. @trusted pure nothrow
  945. if (isSomeChar!C)
  946. {
  947. import std.c.stdlib;
  948. auto paths2 = new const(C)[][](paths.length);
  949. //(cast(const(C)[]*)alloca((const(C)[]).sizeof * paths.length))[0 .. paths.length];
  950. // Check whether the resulting path will be absolute or rooted,
  951. // calculate its maximum length, and discard segments we won't use.
  952. typeof(paths[0][0])[] rootElement;
  953. int numPaths = 0;
  954. bool seenAbsolute;
  955. size_t segmentLengthSum = 0;
  956. foreach (i; 0 .. paths.length)
  957. {
  958. auto p = paths[i];
  959. if (p.empty) continue;
  960. else if (isRooted(p))
  961. {
  962. immutable thisIsAbsolute = isAbsolute(p);
  963. if (thisIsAbsolute || !seenAbsolute)
  964. {
  965. if (thisIsAbsolute) seenAbsolute = true;
  966. rootElement = rootName(p);
  967. paths2[0] = p[rootElement.length .. $];
  968. numPaths = 1;
  969. segmentLengthSum = paths2[0].length;
  970. }
  971. else
  972. {
  973. paths2[0] = p;
  974. numPaths = 1;
  975. segmentLengthSum = p.length;
  976. }
  977. }
  978. else
  979. {
  980. paths2[numPaths++] = p;
  981. segmentLengthSum += p.length;
  982. }
  983. }
  984. if (rootElement.length + segmentLengthSum == 0) return null;
  985. paths2 = paths2[0 .. numPaths];
  986. immutable rooted = !rootElement.empty;
  987. assert (rooted || !seenAbsolute); // absolute => rooted
  988. // Allocate memory for the resulting path, including room for
  989. // extra dir separators
  990. auto fullPath = new C[rootElement.length + segmentLengthSum + paths2.length];
  991. // Copy the root element into fullPath, and let relPart be
  992. // the remaining slice.
  993. typeof(fullPath) relPart;
  994. if (rooted)
  995. {
  996. // For Windows, we also need to perform normalization on
  997. // the root element.
  998. version (Posix)
  999. {
  1000. fullPath[0 .. rootElement.length] = rootElement[];
  1001. }
  1002. else version (Windows)
  1003. {
  1004. foreach (i, c; rootElement)
  1005. {
  1006. if (isDirSeparator(c))
  1007. {
  1008. static assert (dirSeparator.length == 1);
  1009. fullPath[i] = dirSeparator[0];
  1010. }
  1011. else fullPath[i] = c;
  1012. }
  1013. }
  1014. else static assert (0);
  1015. // If the root element doesn't end with a dir separator,
  1016. // we add one.
  1017. if (!isDirSeparator(rootElement[$-1]))
  1018. {
  1019. static assert (dirSeparator.length == 1);
  1020. fullPath[rootElement.length] = dirSeparator[0];
  1021. relPart = fullPath[rootElement.length + 1 .. $];
  1022. }
  1023. else
  1024. {
  1025. relPart = fullPath[rootElement.length .. $];
  1026. }
  1027. }
  1028. else relPart = fullPath;
  1029. // Now, we have ensured that all segments in path are relative to the
  1030. // root we found earlier.
  1031. bool hasParents = rooted;
  1032. ptrdiff_t i;
  1033. foreach (path; paths2)
  1034. {
  1035. path = trimDirSeparators(path);
  1036. enum Prev { nonSpecial, dirSep, dot, doubleDot }
  1037. Prev prev = Prev.dirSep;
  1038. foreach (j; 0 .. path.length+1)
  1039. {
  1040. // Fake a dir separator between path segments
  1041. immutable c = (j == path.length ? dirSeparator[0] : path[j]);
  1042. if (isDirSeparator(c))
  1043. {
  1044. final switch (prev)
  1045. {
  1046. case Prev.doubleDot:
  1047. if (hasParents)
  1048. {
  1049. while (i > 0 && !isDirSeparator(relPart[i-1])) --i;
  1050. if (i > 0) --i; // skip the dir separator
  1051. while (i > 0 && !isDirSeparator(relPart[i-1])) --i;
  1052. if (i == 0) hasParents = rooted;
  1053. }
  1054. else
  1055. {
  1056. relPart[i++] = '.';
  1057. relPart[i++] = '.';
  1058. static assert (dirSeparator.length == 1);
  1059. relPart[i++] = dirSeparator[0];
  1060. }
  1061. break;
  1062. case Prev.dot:
  1063. while (i > 0 && !isDirSeparator(relPart[i-1])) --i;
  1064. break;
  1065. case Prev.nonSpecial:
  1066. static assert (dirSeparator.length == 1);
  1067. relPart[i++] = dirSeparator[0];
  1068. hasParents = true;
  1069. break;
  1070. case Prev.dirSep:
  1071. break;
  1072. }
  1073. prev = Prev.dirSep;
  1074. }
  1075. else if (c == '.')
  1076. {
  1077. final switch (prev)
  1078. {
  1079. case Prev.dirSep:
  1080. prev = Prev.dot;
  1081. break;
  1082. case Prev.dot:
  1083. prev = Prev.doubleDot;
  1084. break;
  1085. case Prev.doubleDot:
  1086. prev = Prev.nonSpecial;
  1087. relPart[i .. i+3] = "...";
  1088. i += 3;
  1089. break;
  1090. case Prev.nonSpecial:
  1091. relPart[i] = '.';
  1092. ++i;
  1093. break;
  1094. }
  1095. }
  1096. else
  1097. {
  1098. final switch (prev)
  1099. {
  1100. case Prev.doubleDot:
  1101. relPart[i] = '.';
  1102. ++i;
  1103. goto case;
  1104. case Prev.dot:
  1105. relPart[i] = '.';
  1106. ++i;
  1107. break;
  1108. case Prev.dirSep: break;
  1109. case Prev.nonSpecial: break;
  1110. }
  1111. relPart[i] = c;
  1112. ++i;
  1113. prev = Prev.nonSpecial;
  1114. }
  1115. }
  1116. }
  1117. // Return path, including root element and excluding the
  1118. // final dir separator.
  1119. immutable len = (relPart.ptr - fullPath.ptr) + (i > 0 ? i - 1 : 0);
  1120. fullPath = fullPath[0 .. len];
  1121. version (Windows)
  1122. {
  1123. // On Windows, if the path is on the form `\\server\share`,
  1124. // with no further segments, normalization will have turned it
  1125. // into `\\server\share\`. If so, we need to remove the final
  1126. // backslash.
  1127. if (isUNC(fullPath) && uncRootLength(fullPath) == fullPath.length - 1)
  1128. fullPath = fullPath[0 .. $-1];
  1129. }
  1130. return cast(typeof(return)) fullPath;
  1131. }
  1132. unittest
  1133. {
  1134. assert (buildNormalizedPath("") is null);
  1135. assert (buildNormalizedPath("foo") == "foo");
  1136. version (Posix)
  1137. {
  1138. assert (buildNormalizedPath("/", "foo", "bar") == "/foo/bar");
  1139. assert (buildNormalizedPath("foo", "bar", "baz") == "foo/bar/baz");
  1140. assert (buildNormalizedPath("foo", "bar/baz") == "foo/bar/baz");
  1141. assert (buildNormalizedPath("foo", "bar//baz///") == "foo/bar/baz");
  1142. assert (buildNormalizedPath("/foo", "bar/baz") == "/foo/bar/baz");
  1143. assert (buildNormalizedPath("/foo", "/bar/baz") == "/bar/baz");
  1144. assert (buildNormalizedPath("/foo/..", "/bar/./baz") == "/bar/baz");
  1145. assert (buildNormalizedPath("/foo/..", "bar/baz") == "/bar/baz");
  1146. assert (buildNormalizedPath("/foo/../../", "bar/baz") == "/bar/baz");
  1147. assert (buildNormalizedPath("/foo/bar", "../baz") == "/foo/baz");
  1148. assert (buildNormalizedPath("/foo/bar", "../../baz") == "/baz");
  1149. assert (buildNormalizedPath("/foo/bar", ".././/baz/..", "wee/") == "/foo/wee");
  1150. assert (buildNormalizedPath("//foo/bar", "baz///wee") == "/foo/bar/baz/wee");
  1151. static assert (buildNormalizedPath("/foo/..", "/bar/./baz") == "/bar/baz");
  1152. // Examples in docs:
  1153. assert (buildNormalizedPath("/foo", "bar/baz/") == "/foo/bar/baz");
  1154. assert (buildNormalizedPath("/foo", "/bar/..", "baz") == "/baz");
  1155. assert (buildNormalizedPath("foo/./bar", "../../", "../baz") == "../baz");
  1156. assert (buildNormalizedPath("/foo/./bar", "../../baz") == "/baz");
  1157. }
  1158. else version (Windows)
  1159. {
  1160. assert (buildNormalizedPath(`\`, `foo`, `bar`) == `\foo\bar`);
  1161. assert (buildNormalizedPath(`foo`, `bar`, `baz`) == `foo\bar\baz`);
  1162. assert (buildNormalizedPath(`foo`, `bar\baz`) == `foo\bar\baz`);
  1163. assert (buildNormalizedPath(`foo`, `bar\\baz\\\`) == `foo\bar\baz`);
  1164. assert (buildNormalizedPath(`\foo`, `bar\baz`) == `\foo\bar\baz`);
  1165. assert (buildNormalizedPath(`\foo`, `\bar\baz`) == `\bar\baz`);
  1166. assert (buildNormalizedPath(`\foo\..`, `\bar\.\baz`) == `\bar\baz`);
  1167. assert (buildNormalizedPath(`\foo\..`, `bar\baz`) == `\bar\baz`);
  1168. assert (buildNormalizedPath(`\foo\..\..\`, `bar\baz`) == `\bar\baz`);
  1169. assert (buildNormalizedPath(`\foo\bar`, `..\baz`) == `\foo\baz`);
  1170. assert (buildNormalizedPath(`\foo\bar`, `../../baz`) == `\baz`);
  1171. assert (buildNormalizedPath(`\foo\bar`, `..\.\/baz\..`, `wee\`) == `\foo\wee`);
  1172. assert (buildNormalizedPath(`c:\`, `foo`, `bar`) == `c:\foo\bar`);
  1173. assert (buildNormalizedPath(`c:foo`, `bar`, `baz`) == `c:foo\bar\baz`);
  1174. assert (buildNormalizedPath(`c:foo`, `bar\baz`) == `c:foo\bar\baz`);
  1175. assert (buildNormalizedPath(`c:foo`, `bar\\baz\\\`) == `c:foo\bar\baz`);
  1176. assert (buildNormalizedPath(`c:\foo`, `bar\baz`) == `c:\foo\bar\baz`);
  1177. assert (buildNormalizedPath(`c:\foo`, `\bar\baz`) == `c:\bar\baz`);
  1178. assert (buildNormalizedPath(`c:\foo\..`, `\bar\.\baz`) == `c:\bar\baz`);
  1179. assert (buildNormalizedPath(`c:\foo\..`, `bar\baz`) == `c:\bar\baz`);
  1180. assert (buildNormalizedPath(`c:\foo\..\..\`, `bar\baz`) == `c:\bar\baz`);
  1181. assert (buildNormalizedPath(`c:\foo\bar`, `..\baz`) == `c:\foo\baz`);
  1182. assert (buildNormalizedPath(`c:\foo\bar`, `..\..\baz`) == `c:\baz`);
  1183. assert (buildNormalizedPath(`c:\foo\bar`, `..\.\\baz\..`, `wee\`) == `c:\foo\wee`);
  1184. assert (buildNormalizedPath(`\\server\share`, `foo`, `bar`) == `\\server\share\foo\bar`);
  1185. assert (buildNormalizedPath(`\\server\share\`, `foo`, `bar`) == `\\server\share\foo\bar`);
  1186. assert (buildNormalizedPath(`\\server\share\foo`, `bar\baz`) == `\\server\share\foo\bar\baz`);
  1187. assert (buildNormalizedPath(`\\server\share\foo`, `\bar\baz`) == `\\server\share\bar\baz`);
  1188. assert (buildNormalizedPath(`\\server\share\foo\..`, `\bar\.\baz`) == `\\server\share\bar\baz`);
  1189. assert (buildNormalizedPath(`\\server\share\foo\..`, `bar\baz`) == `\\server\share\bar\baz`);
  1190. assert (buildNormalizedPath(`\\server\share\foo\..\..\`, `bar\baz`) == `\\server\share\bar\baz`);
  1191. assert (buildNormalizedPath(`\\server\share\foo\bar`, `..\baz`) == `\\server\share\foo\baz`);
  1192. assert (buildNormalizedPath(`\\server\share\foo\bar`, `..\..\baz`) == `\\server\share\baz`);
  1193. assert (buildNormalizedPath(`\\server\share\foo\bar`, `..\.\\baz\..`, `wee\`) == `\\server\share\foo\wee`);
  1194. static assert (buildNormalizedPath(`\foo\..\..\`, `bar\baz`) == `\bar\baz`);
  1195. // Examples in docs:
  1196. assert (buildNormalizedPath(`c:\foo`, `bar\baz\`) == `c:\foo\bar\baz`);
  1197. assert (buildNormalizedPath(`c:\foo`, `bar/..`) == `c:\foo`);
  1198. assert (buildNormalizedPath(`\\server\share\foo`, `..\bar`) == `\\server\share\bar`);
  1199. }
  1200. else static assert (0);
  1201. }
  1202. unittest
  1203. {
  1204. version (Posix)
  1205. {
  1206. // Trivial
  1207. assert (buildNormalizedPath("").empty);
  1208. assert (buildNormalizedPath("foo/bar") == "foo/bar");
  1209. // Correct handling of leading slashes
  1210. assert (buildNormalizedPath("/") == "/");
  1211. assert (buildNormalizedPath("///") == "/");
  1212. assert (buildNormalizedPath("////") == "/");
  1213. assert (buildNormalizedPath("/foo/bar") == "/foo/bar");
  1214. assert (buildNormalizedPath("//foo/bar") == "/foo/bar");
  1215. assert (buildNormalizedPath("///foo/bar") == "/foo/bar");
  1216. assert (buildNormalizedPath("////foo/bar") == "/foo/bar");
  1217. // Correct handling of single-dot symbol (current directory)
  1218. assert (buildNormalizedPath("/./foo") == "/foo");
  1219. assert (buildNormalizedPath("/foo/./bar") == "/foo/bar");
  1220. assert (buildNormalizedPath("./foo") == "foo");
  1221. assert (buildNormalizedPath("././foo") == "foo");
  1222. assert (buildNormalizedPath("foo/././bar") == "foo/bar");
  1223. // Correct handling of double-dot symbol (previous directory)
  1224. assert (buildNormalizedPath("/foo/../bar") == "/bar");
  1225. assert (buildNormalizedPath("/foo/../../bar") == "/bar");
  1226. assert (buildNormalizedPath("/../foo") == "/foo");
  1227. assert (buildNormalizedPath("/../../foo") == "/foo");
  1228. assert (buildNormalizedPath("/foo/..") == "/");
  1229. assert (buildNormalizedPath("/foo/../..") == "/");
  1230. assert (buildNormalizedPath("foo/../bar") == "bar");
  1231. assert (buildNormalizedPath("foo/../../bar") == "../bar");
  1232. assert (buildNormalizedPath("../foo") == "../foo");
  1233. assert (buildNormalizedPath("../../foo") == "../../foo");
  1234. assert (buildNormalizedPath("../foo/../bar") == "../bar");
  1235. assert (buildNormalizedPath(".././../foo") == "../../foo");
  1236. assert (buildNormalizedPath("foo/bar/..") == "foo");
  1237. assert (buildNormalizedPath("/foo/../..") == "/");
  1238. // The ultimate path
  1239. assert (buildNormalizedPath("/foo/../bar//./../...///baz//") == "/.../baz");
  1240. static assert (buildNormalizedPath("/foo/../bar//./../...///baz//") == "/.../baz");
  1241. }
  1242. else version (Windows)
  1243. {
  1244. // Trivial
  1245. assert (buildNormalizedPath("").empty);
  1246. assert (buildNormalizedPath(`foo\bar`) == `foo\bar`);
  1247. assert (buildNormalizedPath("foo/bar") == `foo\bar`);
  1248. // Correct handling of absolute paths
  1249. assert (buildNormalizedPath("/") == `\`);
  1250. assert (buildNormalizedPath(`\`) == `\`);
  1251. assert (buildNormalizedPath(`\\\`) == `\`);
  1252. assert (buildNormalizedPath(`\\\\`) == `\`);
  1253. assert (buildNormalizedPath(`\foo\bar`) == `\foo\bar`);
  1254. assert (buildNormalizedPath(`\\foo`) == `\\foo`);
  1255. assert (buildNormalizedPath(`\\foo\\`) == `\\foo`);
  1256. assert (buildNormalizedPath(`\\foo/bar`) == `\\foo\bar`);
  1257. assert (buildNormalizedPath(`\\\foo\bar`) == `\foo\bar`);
  1258. assert (buildNormalizedPath(`\\\\foo\bar`) == `\foo\bar`);
  1259. assert (buildNormalizedPath(`c:\`) == `c:\`);
  1260. assert (buildNormalizedPath(`c:\foo\bar`) == `c:\foo\bar`);
  1261. assert (buildNormalizedPath(`c:\\foo\bar`) == `c:\foo\bar`);
  1262. // Correct handling of single-dot symbol (current directory)
  1263. assert (buildNormalizedPath(`\./foo`) == `\foo`);
  1264. assert (buildNormalizedPath(`\foo/.\bar`) == `\foo\bar`);
  1265. assert (buildNormalizedPath(`.\foo`) == `foo`);
  1266. assert (buildNormalizedPath(`./.\foo`) == `foo`);
  1267. assert (buildNormalizedPath(`foo\.\./bar`) == `foo\bar`);
  1268. // Correct handling of double-dot symbol (previous directory)
  1269. assert (buildNormalizedPath(`\foo\..\bar`) == `\bar`);
  1270. assert (buildNormalizedPath(`\foo\../..\bar`) == `\bar`);
  1271. assert (buildNormalizedPath(`\..\foo`) == `\foo`);
  1272. assert (buildNormalizedPath(`\..\..\foo`) == `\foo`);
  1273. assert (buildNormalizedPath(`\foo\..`) == `\`);
  1274. assert (buildNormalizedPath(`\foo\../..`) == `\`);
  1275. assert (buildNormalizedPath(`foo\..\bar`) == `bar`);
  1276. assert (buildNormalizedPath(`foo\..\../bar`) == `..\bar`);
  1277. assert (buildNormalizedPath(`..\foo`) == `..\foo`);
  1278. assert (buildNormalizedPath(`..\..\foo`) == `..\..\foo`);
  1279. assert (buildNormalizedPath(`..\foo\..\bar`) == `..\bar`);
  1280. assert (buildNormalizedPath(`..\.\..\foo`) == `..\..\foo`);
  1281. assert (buildNormalizedPath(`foo\bar\..`) == `foo`);
  1282. assert (buildNormalizedPath(`\foo\..\..`) == `\`);
  1283. assert (buildNormalizedPath(`c:\foo\..\..`) == `c:\`);
  1284. // Correct handling of non-root path with drive specifier
  1285. assert (buildNormalizedPath(`c:foo`) == `c:foo`);
  1286. assert (buildNormalizedPath(`c:..\foo\.\..\bar`) == `c:..\bar`);
  1287. // The ultimate path
  1288. assert (buildNormalizedPath(`c:\foo\..\bar\\.\..\...\\\baz\\`) == `c:\...\baz`);
  1289. static assert (buildNormalizedPath(`c:\foo\..\bar\\.\..\...\\\baz\\`) == `c:\...\baz`);
  1290. }
  1291. else static assert (false);
  1292. }
  1293. unittest
  1294. {
  1295. // Test for issue 7397
  1296. string[] ary = ["a", "b"];
  1297. version (Posix)
  1298. {
  1299. assert (buildNormalizedPath(ary) == "a/b");
  1300. }
  1301. else version (Windows)
  1302. {
  1303. assert (buildNormalizedPath(ary) == `a\b`);
  1304. }
  1305. }
  1306. /** Returns a bidirectional range that iterates over the elements of a path.
  1307. Examples:
  1308. ---
  1309. assert (equal(pathSplitter("/"), ["/"]));
  1310. assert (equal(pathSplitter("/foo/bar"), ["/", "foo", "bar"]));
  1311. assert (equal(pathSplitter("//foo/bar"), ["//foo", "bar"]));
  1312. assert (equal(pathSplitter("foo/../bar//./"), ["foo", "..", "bar", "."]));
  1313. version (Windows)
  1314. {
  1315. assert (equal(pathSplitter(`foo\..\bar\/.\`), ["foo", "..", "bar", "."]));
  1316. assert (equal(pathSplitter("c:"), ["c:"]));
  1317. assert (equal(pathSplitter(`c:\foo\bar`), [`c:\`, "foo", "bar"]));
  1318. assert (equal(pathSplitter(`c:foo\bar`), ["c:foo", "bar"]));
  1319. }
  1320. ---
  1321. */
  1322. auto pathSplitter(C)(const(C)[] path) @safe pure nothrow
  1323. if (isSomeChar!C)
  1324. {
  1325. static struct PathSplitter
  1326. {
  1327. @safe pure nothrow:
  1328. @property bool empty() const { return _empty; }
  1329. @property const(C)[] front() const
  1330. {
  1331. assert (!empty, "PathSplitter: called front() on empty range");
  1332. return _front;
  1333. }
  1334. void popFront()
  1335. {
  1336. assert (!empty, "PathSplitter: called popFront() on empty range");
  1337. if (_path.empty)
  1338. {
  1339. if (_front is _back)
  1340. {
  1341. _empty = true;
  1342. _front = null;
  1343. _back = null;
  1344. }
  1345. else
  1346. {
  1347. _front = _back;
  1348. }
  1349. }
  1350. else
  1351. {
  1352. ptrdiff_t i = 0;
  1353. while (i < _path.length && !isDirSeparator(_path[i])) ++i;
  1354. _front = _path[0 .. i];
  1355. _path = ltrimDirSeparators(_path[i .. $]);
  1356. }
  1357. }
  1358. @property const(C)[] back() const
  1359. {
  1360. assert (!empty, "PathSplitter: called back() on empty range");
  1361. return _back;
  1362. }
  1363. void popBack()
  1364. {
  1365. assert (!empty, "PathSplitter: called popBack() on empty range");
  1366. if (_path.empty)
  1367. {
  1368. if (_front is _back)
  1369. {
  1370. _empty = true;
  1371. _front = null;
  1372. _back = null;
  1373. }
  1374. else
  1375. {
  1376. _back = _front;
  1377. }
  1378. }
  1379. else
  1380. {
  1381. auto i = (cast(ptrdiff_t) _path.length) - 1;
  1382. while (i >= 0 && !isDirSeparator(_path[i])) --i;
  1383. _back = _path[i + 1 .. $];
  1384. _path = rtrimDirSeparators(_path[0 .. i+1]);
  1385. }
  1386. }
  1387. @property auto save() { return this; }
  1388. private:
  1389. typeof(path) _path, _front, _back;
  1390. bool _empty;
  1391. this(typeof(path) p)
  1392. {
  1393. if (p.empty)
  1394. {
  1395. _empty = true;
  1396. return;
  1397. }
  1398. _path = p;
  1399. // If path is rooted, first element is special
  1400. version (Windows)
  1401. {
  1402. if (isUNC(_path))
  1403. {
  1404. auto i = uncRootLength(_path);
  1405. _front = _path[0 .. i];
  1406. _path = ltrimDirSeparators(_path[i .. $]);
  1407. }
  1408. else if (isDriveRoot(_path))
  1409. {
  1410. _front = _path[0 .. 3];
  1411. _path = ltrimDirSeparators(_path[3 .. $]);
  1412. }
  1413. else if (_path.length >= 1 && isDirSeparator(_path[0]))
  1414. {
  1415. _front = _path[0 .. 1];
  1416. _path = ltrimDirSeparators(_path[1 .. $]);
  1417. }
  1418. else
  1419. {
  1420. assert (!isRooted(_path));
  1421. popFront();
  1422. }
  1423. }
  1424. else version (Posix)
  1425. {
  1426. if (_path.length >= 1 && isDirSeparator(_path[0]))
  1427. {
  1428. _front = _path[0 .. 1];
  1429. _path = ltrimDirSeparators(_path[1 .. $]);
  1430. }
  1431. else
  1432. {
  1433. popFront();
  1434. }
  1435. }
  1436. else static assert (0);
  1437. if (_path.empty) _back = _front;
  1438. else
  1439. {
  1440. _path = rtrimDirSeparators(_path);
  1441. popBack();
  1442. }
  1443. }
  1444. }
  1445. return PathSplitter(path);
  1446. }
  1447. unittest
  1448. {
  1449. // equal2 verifies that the range is the same both ways, i.e.
  1450. // through front/popFront and back/popBack.
  1451. import std.range;
  1452. bool equal2(R1, R2)(R1 r1, R2 r2)
  1453. {
  1454. static assert (isBidirectionalRange!R1);
  1455. return equal(r1, r2) && equal(retro(r1), retro(r2));
  1456. }
  1457. assert (pathSplitter("").empty);
  1458. // Root directories
  1459. assert (equal2(pathSplitter("/"), ["/"]));
  1460. assert (equal2(pathSplitter("//"), ["/"]));
  1461. assert (equal2(pathSplitter("///"w), ["/"w]));
  1462. // Absolute paths
  1463. assert (equal2(pathSplitter("/foo/bar".dup), ["/", "foo", "bar"]));
  1464. // General
  1465. assert (equal2(pathSplitter("foo/bar"d.dup), ["foo"d, "bar"d]));
  1466. assert (equal2(pathSplitter("foo//bar"), ["foo", "bar"]));
  1467. assert (equal2(pathSplitter("foo/bar//"w), ["foo"w, "bar"w]));
  1468. assert (equal2(pathSplitter("foo/../bar//./"d), ["foo"d, ".."d, "bar"d, "."d]));
  1469. // save()
  1470. auto ps1 = pathSplitter("foo/bar/baz");
  1471. auto ps2 = ps1.save;
  1472. ps1.popFront();
  1473. assert (equal2(ps1, ["bar", "baz"]));
  1474. assert (equal2(ps2, ["foo", "bar", "baz"]));
  1475. // Platform specific
  1476. version (Posix)
  1477. {
  1478. assert (equal2(pathSplitter("//foo/bar"w.dup), ["/"w, "foo"w, "bar"w]));
  1479. }
  1480. version (Windows)
  1481. {
  1482. assert (equal2(pathSplitter(`\`), [`\`]));
  1483. assert (equal2(pathSplitter(`foo\..\bar\/.\`), ["foo", "..", "bar", "."]));
  1484. assert (equal2(pathSplitter("c:"), ["c:"]));
  1485. assert (equal2(pathSplitter(`c:\foo\bar`), [`c:\`, "foo", "bar"]));
  1486. assert (equal2(pathSplitter(`c:foo\bar`), ["c:foo", "bar"]));
  1487. assert (equal2(pathSplitter(`\\foo\bar`), [`\\foo\bar`]));
  1488. assert (equal2(pathSplitter(`\\foo\bar\\`), [`\\foo\bar`]));
  1489. assert (equal2(pathSplitter(`\\foo\bar\baz`), [`\\foo\bar`, "baz"]));
  1490. }
  1491. import std.exception;
  1492. assertCTFEable!(
  1493. {
  1494. assert (equal(pathSplitter("/foo/bar".dup), ["/", "foo", "bar"]));
  1495. });
  1496. // Bugzilla 11691
  1497. // front should return a mutable array of const elements
  1498. static assert(is(typeof(pathSplitter!char(null).front) == const(char)[]));
  1499. }
  1500. /** Determines whether a path starts at a root directory.
  1501. On POSIX, this function returns true if and only if the path starts
  1502. with a slash (/).
  1503. ---
  1504. version (Posix)
  1505. {
  1506. assert (isRooted("/"));
  1507. assert (isRooted("/foo"));
  1508. assert (!isRooted("foo"));
  1509. assert (!isRooted("../foo"));
  1510. }
  1511. ---
  1512. On Windows, this function returns true if the path starts at
  1513. the root directory of the current drive, of some other drive,
  1514. or of a network drive.
  1515. ---
  1516. version (Windows)
  1517. {
  1518. assert (isRooted(`\`));
  1519. assert (isRooted(`\foo`));
  1520. assert (isRooted(`d:\foo`));
  1521. assert (isRooted(`\\foo\bar`));
  1522. assert (!isRooted("foo"));
  1523. assert (!isRooted("d:foo"));
  1524. }
  1525. ---
  1526. */
  1527. bool isRooted(C)(in C[] path) @safe pure nothrow if (isSomeChar!C)
  1528. {
  1529. if (path.length >= 1 && isDirSeparator(path[0])) return true;
  1530. version (Posix) return false;
  1531. else version (Windows) return isAbsolute(path);
  1532. }
  1533. unittest
  1534. {
  1535. assert (isRooted("/"));
  1536. assert (isRooted("/foo"));
  1537. assert (!isRooted("foo"));
  1538. assert (!isRooted("../foo"));
  1539. version (Windows)
  1540. {
  1541. assert (isRooted(`\`));
  1542. assert (isRooted(`\foo`));
  1543. assert (isRooted(`d:\foo`));
  1544. assert (isRooted(`\\foo\bar`));
  1545. assert (!isRooted("foo"));
  1546. assert (!isRooted("d:foo"));
  1547. }
  1548. static assert (isRooted("/foo"));
  1549. static assert (!isRooted("foo"));
  1550. }
  1551. /** Determines whether a path is absolute or not.
  1552. Examples:
  1553. On POSIX, an absolute path starts at the root directory.
  1554. (In fact, $(D _isAbsolute) is just an alias for $(LREF isRooted).)
  1555. ---
  1556. version (Posix)
  1557. {
  1558. assert (isAbsolute("/"));
  1559. assert (isAbsolute("/foo"));
  1560. assert (!isAbsolute("foo"));
  1561. assert (!isAbsolute("../foo"));
  1562. }
  1563. ---
  1564. On Windows, an absolute path starts at the root directory of
  1565. a specific drive. Hence, it must start with $(D `d:\`) or $(D `d:/`),
  1566. where $(D d) is the drive letter. Alternatively, it may be a
  1567. network path, i.e. a path starting with a double (back)slash.
  1568. ---
  1569. version (Windows)
  1570. {
  1571. assert (isAbsolute(`d:\`));
  1572. assert (isAbsolute(`d:\foo`));
  1573. assert (isAbsolute(`\\foo\bar`));
  1574. assert (!isAbsolute(`\`));
  1575. assert (!isAbsolute(`\foo`));
  1576. assert (!isAbsolute("d:foo"));
  1577. }
  1578. ---
  1579. */
  1580. version (StdDdoc) bool isAbsolute(C)(in C[] path) @safe pure nothrow
  1581. if (isSomeChar!C);
  1582. else version (Windows) bool isAbsolute(C)(in C[] path) @safe pure nothrow
  1583. if (isSomeChar!C)
  1584. {
  1585. return isDriveRoot(path) || isUNC(path);
  1586. }
  1587. else version (Posix) alias isRooted isAbsolute;
  1588. unittest
  1589. {
  1590. assert (!isAbsolute("foo"));
  1591. assert (!isAbsolute("../foo"w));
  1592. static assert (!isAbsolute("foo"));
  1593. version (Posix)
  1594. {
  1595. assert (isAbsolute("/"d));
  1596. assert (isAbsolute("/foo".dup));
  1597. static assert (isAbsolute("/foo"));
  1598. }
  1599. version (Windows)
  1600. {
  1601. assert (isAbsolute("d:\\"w));
  1602. assert (isAbsolute("d:\\foo"d));
  1603. assert (isAbsolute("\\\\foo\\bar"));
  1604. assert (!isAbsolute("\\"w.dup));
  1605. assert (!isAbsolute("\\foo"d.dup));
  1606. assert (!isAbsolute("d:"));
  1607. assert (!isAbsolute("d:foo"));
  1608. static assert (isAbsolute(`d:\foo`));
  1609. }
  1610. }
  1611. /** Translates $(D path) into an absolute _path.
  1612. The following algorithm is used:
  1613. $(OL
  1614. $(LI If $(D path) is empty, return $(D null).)
  1615. $(LI If $(D path) is already absolute, return it.)
  1616. $(LI Otherwise, append $(D path) to $(D base) and return
  1617. the result. If $(D base) is not specified, the current
  1618. working directory is used.)
  1619. )
  1620. The function allocates memory if and only if it gets to the third stage
  1621. of this algorithm.
  1622. Examples:
  1623. ---
  1624. version (Posix)
  1625. {
  1626. assert (absolutePath("some/file", "/foo/bar") == "/foo/bar/some/file");
  1627. assert (absolutePath("../file", "/foo/bar") == "/foo/bar/../file");
  1628. assert (absolutePath("/some/file", "/foo/bar") == "/some/file");
  1629. }
  1630. version (Windows)
  1631. {
  1632. assert (absolutePath(`some\file`, `c:\foo\bar`) == `c:\foo\bar\some\file`);
  1633. assert (absolutePath(`..\file`, `c:\foo\bar`) == `c:\foo\bar\..\file`);
  1634. assert (absolutePath(`c:\some\file`, `c:\foo\bar`) == `c:\some\file`);
  1635. assert (absolutePath(`\file`, `c:\foo\bar`) == `c:\file`);
  1636. }
  1637. ---
  1638. Throws:
  1639. $(D Exception) if the specified _base directory is not absolute.
  1640. */
  1641. string absolutePath(string path, lazy string base = getcwd())
  1642. @safe pure
  1643. {
  1644. if (path.empty) return null;
  1645. if (isAbsolute(path)) return path;
  1646. immutable baseVar = base;
  1647. if (!isAbsolute(baseVar)) throw new Exception("Base directory must be absolute");
  1648. return buildPath(baseVar, path);
  1649. }
  1650. unittest
  1651. {
  1652. version (Posix)
  1653. {
  1654. assert (absolutePath("some/file", "/foo/bar") == "/foo/bar/some/file");
  1655. assert (absolutePath("../file", "/foo/bar") == "/foo/bar/../file");
  1656. assert (absolutePath("/some/file", "/foo/bar") == "/some/file");
  1657. static assert (absolutePath("some/file", "/foo/bar") == "/foo/bar/some/file");
  1658. }
  1659. version (Windows)
  1660. {
  1661. assert (absolutePath(`some\file`, `c:\foo\bar`) == `c:\foo\bar\some\file`);
  1662. assert (absolutePath(`..\file`, `c:\foo\bar`) == `c:\foo\bar\..\file`);
  1663. assert (absolutePath(`c:\some\file`, `c:\foo\bar`) == `c:\some\file`);
  1664. assert (absolutePath(`\`, `c:\`) == `c:\`);
  1665. assert (absolutePath(`\some\file`, `c:\foo\bar`) == `c:\some\file`);
  1666. static assert (absolutePath(`some\file`, `c:\foo\bar`) == `c:\foo\bar\some\file`);
  1667. }
  1668. import std.exception;
  1669. assertThrown(absolutePath("bar", "foo"));
  1670. }
  1671. /** Translates $(D path) into a relative _path.
  1672. The returned _path is relative to $(D base), which is by default
  1673. taken to be the current working directory. If specified,
  1674. $(D base) must be an absolute _path, and it is always assumed
  1675. to refer to a directory. If $(D path) and $(D base) refer to
  1676. the same directory, the function returns $(D `.`).
  1677. The following algorithm is used:
  1678. $(OL
  1679. $(LI If $(D path) is a relative directory, return it unaltered.)
  1680. $(LI Find a common root between $(D path) and $(D base).
  1681. If there is no common root, return $(D path) unaltered.)
  1682. $(LI Prepare a string with as many $(D `../`) or $(D `..\`) as
  1683. necessary to reach the common root from base path.)
  1684. $(LI Append the remaining segments of $(D path) to the string
  1685. and return.)
  1686. )
  1687. In the second step, path components are compared using $(D filenameCmp!cs),
  1688. where $(D cs) is an optional template parameter determining whether
  1689. the comparison is case sensitive or not. See the
  1690. $(LREF filenameCmp) documentation for details.
  1691. The function allocates memory if and only if it reaches the third stage
  1692. of the above algorithm.
  1693. Examples:
  1694. ---
  1695. assert (relativePath("foo") == "foo");
  1696. version (Posix)
  1697. {
  1698. assert (relativePath("foo", "/bar") == "foo");
  1699. assert (relativePath("/foo/bar", "/foo/bar") == ".");
  1700. assert (relativePath("/foo/bar", "/foo/baz") == "../bar");
  1701. assert (relativePath("/foo/bar/baz", "/foo/woo/wee") == "../../bar/baz");
  1702. assert (relativePath("/foo/bar/baz", "/foo/bar") == "baz");
  1703. }
  1704. version (Windows)
  1705. {
  1706. assert (relativePath("foo", `c:\bar`) == "foo");
  1707. assert (relativePath(`c:\foo\bar`, `c:\foo\bar`) == ".");
  1708. assert (relativePath(`c:\foo\bar`, `c:\foo\baz`) == `..\bar`);
  1709. assert (relativePath(`c:\foo\bar\baz`, `c:\foo\woo\wee`) == `..\..\bar\baz`);
  1710. assert (relativePath(`c:\foo\bar\baz`, `c:\foo\bar`) == "baz");
  1711. assert (relativePath(`c:\foo\bar`, `d:\foo`) == `c:\foo\bar`);
  1712. }
  1713. ---
  1714. Throws:
  1715. $(D Exception) if the specified _base directory is not absolute.
  1716. */
  1717. string relativePath(CaseSensitive cs = CaseSensitive.osDefault)
  1718. (string path, lazy string base = getcwd())
  1719. //TODO: @safe (object.reserve(T[]) should be @trusted)
  1720. {
  1721. if (!isAbsolute(path)) return path;
  1722. immutable baseVar = base;
  1723. if (!isAbsolute(baseVar)) throw new Exception("Base directory must be absolute");
  1724. // Find common root with current working directory
  1725. string result;
  1726. if (!__ctfe) result.reserve(baseVar.length + path.length);
  1727. auto basePS = pathSplitter(baseVar);
  1728. auto pathPS = pathSplitter(path);
  1729. if (filenameCmp!cs(basePS.front, pathPS.front) != 0) return path;
  1730. basePS.popFront();
  1731. pathPS.popFront();
  1732. while (!basePS.empty && !pathPS.empty
  1733. && filenameCmp!cs(basePS.front, pathPS.front) == 0)
  1734. {
  1735. basePS.popFront();
  1736. pathPS.popFront();
  1737. }
  1738. // Append as many "../" as necessary to reach common base from path
  1739. while (!basePS.empty)
  1740. {
  1741. result ~= "..";
  1742. result ~= dirSeparator;
  1743. basePS.popFront();
  1744. }
  1745. // Append the remainder of path
  1746. while (!pathPS.empty)
  1747. {
  1748. result ~= pathPS.front;
  1749. result ~= dirSeparator;
  1750. pathPS.popFront();
  1751. }
  1752. // base == path
  1753. if (result.empty) return ".";
  1754. // Strip off last path separator
  1755. return result[0 .. $-1];
  1756. }
  1757. unittest
  1758. {
  1759. import std.exception;
  1760. assert (relativePath("foo") == "foo");
  1761. version (Posix)
  1762. {
  1763. assert (relativePath("foo", "/bar") == "foo");
  1764. assert (relativePath("/foo/bar", "/foo/bar") == ".");
  1765. assert (relativePath("/foo/bar", "/foo/baz") == "../bar");
  1766. assert (relativePath("/foo/bar/baz", "/foo/woo/wee") == "../../bar/baz");
  1767. assert (relativePath("/foo/bar/baz", "/foo/bar") == "baz");
  1768. assertThrown(relativePath("/foo", "bar"));
  1769. assertCTFEable!(
  1770. {
  1771. assert (relativePath("/foo/bar", "/foo/baz") == "../bar");
  1772. });
  1773. }
  1774. else version (Windows)
  1775. {
  1776. assert (relativePath("foo", `c:\bar`) == "foo");
  1777. assert (relativePath(`c:\foo\bar`, `c:\foo\bar`) == ".");
  1778. assert (relativePath(`c:\foo\bar`, `c:\foo\baz`) == `..\bar`);
  1779. assert (relativePath(`c:\foo\bar\baz`, `c:\foo\woo\wee`) == `..\..\bar\baz`);
  1780. assert (relativePath(`c:/foo/bar/baz`, `c:\foo\woo\wee`) == `..\..\bar\baz`);
  1781. assert (relativePath(`c:\foo\bar\baz`, `c:\foo\bar`) == "baz");
  1782. assert (relativePath(`c:\foo\bar`, `d:\foo`) == `c:\foo\bar`);
  1783. assert (relativePath(`\\foo\bar`, `c:\foo`) == `\\foo\bar`);
  1784. assertThrown(relativePath(`c:\foo`, "bar"));
  1785. assertCTFEable!(
  1786. {
  1787. assert (relativePath(`c:\foo\bar`, `c:\foo\baz`) == `..\bar`);
  1788. });
  1789. }
  1790. else static assert (0);
  1791. }
  1792. /** Compares filename characters and return $(D < 0) if $(D a < b), $(D 0) if
  1793. $(D a == b) and $(D > 0) if $(D a > b).
  1794. This function can perform a case-sensitive or a case-insensitive
  1795. comparison. This is controlled through the $(D cs) template parameter
  1796. which, if not specified, is given by
  1797. $(LREF CaseSensitive)$(D .osDefault).
  1798. On Windows, the backslash and slash characters ($(D `\`) and $(D `/`))
  1799. are considered equal.
  1800. Examples:
  1801. ---
  1802. assert (filenameCharCmp('a', 'a') == 0);
  1803. assert (filenameCharCmp('a', 'b') < 0);
  1804. assert (filenameCharCmp('b', 'a') > 0);
  1805. version (linux)
  1806. {
  1807. // Same as calling filenameCharCmp!(CaseSensitive.yes)(a, b)
  1808. assert (filenameCharCmp('A', 'a') < 0);
  1809. assert (filenameCharCmp('a', 'A') > 0);
  1810. }
  1811. version (Windows)
  1812. {
  1813. // Same as calling filenameCharCmp!(CaseSensitive.no)(a, b)
  1814. assert (filenameCharCmp('a', 'A') == 0);
  1815. assert (filenameCharCmp('a', 'B') < 0);
  1816. assert (filenameCharCmp('A', 'b') < 0);
  1817. }
  1818. ---
  1819. */
  1820. int filenameCharCmp(CaseSensitive cs = CaseSensitive.osDefault)(dchar a, dchar b)
  1821. @safe pure nothrow
  1822. {
  1823. if (isDirSeparator(a) && isDirSeparator(b)) return 0;
  1824. static if (!cs)
  1825. {
  1826. import std.uni;
  1827. a = toLower(a);
  1828. b = toLower(b);
  1829. }
  1830. return cast(int)(a - b);
  1831. }
  1832. unittest
  1833. {
  1834. assert (filenameCharCmp!(CaseSensitive.yes)('a', 'a') == 0);
  1835. assert (filenameCharCmp!(CaseSensitive.yes)('a', 'b') < 0);
  1836. assert (filenameCharCmp!(CaseSensitive.yes)('b', 'a') > 0);
  1837. assert (filenameCharCmp!(CaseSensitive.yes)('A', 'a') < 0);
  1838. assert (filenameCharCmp!(CaseSensitive.yes)('a', 'A') > 0);
  1839. assert (filenameCharCmp!(CaseSensitive.no)('a', 'a') == 0);
  1840. assert (filenameCharCmp!(CaseSensitive.no)('a', 'b') < 0);
  1841. assert (filenameCharCmp!(CaseSensitive.no)('b', 'a') > 0);
  1842. assert (filenameCharCmp!(CaseSensitive.no)('A', 'a') == 0);
  1843. assert (filenameCharCmp!(CaseSensitive.no)('a', 'A') == 0);
  1844. assert (filenameCharCmp!(CaseSensitive.no)('a', 'B') < 0);
  1845. assert (filenameCharCmp!(CaseSensitive.no)('B', 'a') > 0);
  1846. assert (filenameCharCmp!(CaseSensitive.no)('A', 'b') < 0);
  1847. assert (filenameCharCmp!(CaseSensitive.no)('b', 'A') > 0);
  1848. version (Posix) assert (filenameCharCmp('\\', '/') != 0);
  1849. version (Windows) assert (filenameCharCmp('\\', '/') == 0);
  1850. }
  1851. /** Compares file names and returns
  1852. $(D < 0) if $(D filename1 < filename2),
  1853. $(D 0) if $(D filename1 == filename2) and
  1854. $(D > 0) if $(D filename1 > filename2).
  1855. Individual characters are compared using $(D filenameCharCmp!cs),
  1856. where $(D cs) is an optional template parameter determining whether
  1857. the comparison is case sensitive or not. See the
  1858. $(LREF filenameCharCmp) documentation for details.
  1859. Examples:
  1860. ---
  1861. assert (filenameCmp("abc", "abc") == 0);
  1862. assert (filenameCmp("abc", "abd") < 0);
  1863. assert (filenameCmp("abc", "abb") > 0);
  1864. assert (filenameCmp("abc", "abcd") < 0);
  1865. assert (filenameCmp("abcd", "abc") > 0);
  1866. version (linux)
  1867. {
  1868. // Same as calling filenameCmp!(CaseSensitive.yes)(filename1, filename2)
  1869. assert (filenameCmp("Abc", "abc") < 0);
  1870. assert (filenameCmp("abc", "Abc") > 0);
  1871. }
  1872. version (Windows)
  1873. {
  1874. // Same as calling filenameCmp!(CaseSensitive.no)(filename1, filename2)
  1875. assert (filenameCmp("Abc", "abc") == 0);
  1876. assert (filenameCmp("abc", "Abc") == 0);
  1877. assert (filenameCmp("Abc", "abD") < 0);
  1878. assert (filenameCmp("abc", "AbB") > 0);
  1879. }
  1880. ---
  1881. */
  1882. int filenameCmp(CaseSensitive cs = CaseSensitive.osDefault, C1, C2)
  1883. (const(C1)[] filename1, const(C2)[] filename2)
  1884. @safe pure //TODO: nothrow (because of std.array.front())
  1885. if (isSomeChar!C1 && isSomeChar!C2)
  1886. {
  1887. for (;;)
  1888. {
  1889. if (filename1.empty) return -(cast(int) !filename2.empty);
  1890. if (filename2.empty) return (cast(int) !filename1.empty);
  1891. auto c = filenameCharCmp!cs(filename1.front, filename2.front);
  1892. if (c != 0) return c;
  1893. filename1.popFront();
  1894. filename2.popFront();
  1895. }
  1896. assert (0);
  1897. }
  1898. unittest
  1899. {
  1900. assert (filenameCmp!(CaseSensitive.yes)("abc", "abc") == 0);
  1901. assert (filenameCmp!(CaseSensitive.yes)("abc", "abd") < 0);
  1902. assert (filenameCmp!(CaseSensitive.yes)("abc", "abb") > 0);
  1903. assert (filenameCmp!(CaseSensitive.yes)("abc", "abcd") < 0);
  1904. assert (filenameCmp!(CaseSensitive.yes)("abcd", "abc") > 0);
  1905. assert (filenameCmp!(CaseSensitive.yes)("Abc", "abc") < 0);
  1906. assert (filenameCmp!(CaseSensitive.yes)("abc", "Abc") > 0);
  1907. assert (filenameCmp!(CaseSensitive.no)("abc", "abc") == 0);
  1908. assert (filenameCmp!(CaseSensitive.no)("abc", "abd") < 0);
  1909. assert (filenameCmp!(CaseSensitive.no)("abc", "abb") > 0);
  1910. assert (filenameCmp!(CaseSensitive.no)("abc", "abcd") < 0);
  1911. assert (filenameCmp!(CaseSensitive.no)("abcd", "abc") > 0);
  1912. assert (filenameCmp!(CaseSensitive.no)("Abc", "abc") == 0);
  1913. assert (filenameCmp!(CaseSensitive.no)("abc", "Abc") == 0);
  1914. assert (filenameCmp!(CaseSensitive.no)("Abc", "abD") < 0);
  1915. assert (filenameCmp!(CaseSensitive.no)("abc", "AbB") > 0);
  1916. version (Posix) assert (filenameCmp(`abc\def`, `abc/def`) != 0);
  1917. version (Windows) assert (filenameCmp(`abc\def`, `abc/def`) == 0);
  1918. }
  1919. /** Matches a pattern against a path.
  1920. Some characters of pattern have a special meaning (they are
  1921. $(I meta-characters)) and can't be escaped. These are:
  1922. $(BOOKTABLE,
  1923. $(TR $(TD $(D *))
  1924. $(TD Matches 0 or more instances of any character.))
  1925. $(TR $(TD $(D ?))
  1926. $(TD Matches exactly one instance of any character.))
  1927. $(TR $(TD $(D [)$(I chars)$(D ]))
  1928. $(TD Matches one instance of any character that appears
  1929. between the brackets.))
  1930. $(TR $(TD $(D [!)$(I chars)$(D ]))
  1931. $(TD Matches one instance of any character that does not
  1932. appear between the brackets after the exclamation mark.))
  1933. $(TR $(TD $(D {)$(I string1)$(D ,)$(I string2)$(D ,)&hellip;$(D }))
  1934. $(TD Matches either of the specified strings.))
  1935. )
  1936. Individual characters are compared using $(D filenameCharCmp!cs),
  1937. where $(D cs) is an optional template parameter determining whether
  1938. the comparison is case sensitive or not. See the
  1939. $(LREF filenameCharCmp) documentation for details.
  1940. Note that directory
  1941. separators and dots don't stop a meta-character from matching
  1942. further portions of the path.
  1943. Returns:
  1944. $(D true) if pattern matches path, $(D false) otherwise.
  1945. See_also:
  1946. $(LINK2 http://en.wikipedia.org/wiki/Glob_%28programming%29,Wikipedia: _glob (programming))
  1947. Examples:
  1948. -----
  1949. assert (globMatch("foo.bar", "*"));
  1950. assert (globMatch("foo.bar", "*.*"));
  1951. assert (globMatch(`foo/foo\bar`, "f*b*r"));
  1952. assert (globMatch("foo.bar", "f???bar"));
  1953. assert (globMatch("foo.bar", "[fg]???bar"));
  1954. assert (globMatch("foo.bar", "[!gh]*bar"));
  1955. assert (globMatch("bar.fooz", "bar.{foo,bif}z"));
  1956. assert (globMatch("bar.bifz", "bar.{foo,bif}z"));
  1957. version (Windows)
  1958. {
  1959. // Same as calling globMatch!(CaseSensitive.no)(path, pattern)
  1960. assert (globMatch("foo", "Foo"));
  1961. assert (globMatch("Goo.bar", "[fg]???bar"));
  1962. }
  1963. version (linux)
  1964. {
  1965. // Same as calling globMatch!(CaseSensitive.yes)(path, pattern)
  1966. assert (!globMatch("foo", "Foo"));
  1967. assert (!globMatch("Goo.bar", "[fg]???bar"));
  1968. }
  1969. -----
  1970. */
  1971. bool globMatch(CaseSensitive cs = CaseSensitive.osDefault, C)
  1972. (const(C)[] path, const(C)[] pattern)
  1973. @safe pure nothrow
  1974. if (isSomeChar!C)
  1975. in
  1976. {
  1977. // Verify that pattern[] is valid
  1978. assert(balancedParens(pattern, '[', ']', 0));
  1979. assert(balancedParens(pattern, '{', '}', 0));
  1980. }
  1981. body
  1982. {
  1983. size_t ni; // current character in path
  1984. foreach (ref pi; 0 .. pattern.length)
  1985. {
  1986. C pc = pattern[pi];
  1987. switch (pc)
  1988. {
  1989. case '*':
  1990. if (pi + 1 == pattern.length)
  1991. return true;
  1992. foreach (j; ni .. path.length)
  1993. {
  1994. if (globMatch!(cs, C)(path[j .. path.length],
  1995. pattern[pi + 1 .. pattern.length]))
  1996. return true;
  1997. }
  1998. return false;
  1999. case '?':
  2000. if (ni == path.length)
  2001. return false;
  2002. ni++;
  2003. break;
  2004. case '[':
  2005. if (ni == path.length)
  2006. return false;
  2007. auto nc = path[ni];
  2008. ni++;
  2009. auto not = false;
  2010. pi++;
  2011. if (pattern[pi] == '!')
  2012. {
  2013. not = true;
  2014. pi++;
  2015. }
  2016. auto anymatch = false;
  2017. while (1)
  2018. {
  2019. pc = pattern[pi];
  2020. if (pc == ']')
  2021. break;
  2022. if (!anymatch && (filenameCharCmp!cs(nc, pc) == 0))
  2023. anymatch = true;
  2024. pi++;
  2025. }
  2026. if (anymatch == not)
  2027. return false;
  2028. break;
  2029. case '{':
  2030. // find end of {} section
  2031. auto piRemain = pi;
  2032. for (; piRemain < pattern.length
  2033. && pattern[piRemain] != '}'; piRemain++)
  2034. {}
  2035. if (piRemain < pattern.length) piRemain++;
  2036. pi++;
  2037. while (pi < pattern.length)
  2038. {
  2039. auto pi0 = pi;
  2040. pc = pattern[pi];
  2041. // find end of current alternative
  2042. for (; pi<pattern.length && pc!='}' && pc!=','; pi++)
  2043. {
  2044. pc = pattern[pi];
  2045. }
  2046. if (pi0 == pi)
  2047. {
  2048. if (globMatch!(cs, C)(path[ni..$], pattern[piRemain..$]))
  2049. {
  2050. return true;
  2051. }
  2052. pi++;
  2053. }
  2054. else
  2055. {
  2056. if (globMatch!(cs, C)(path[ni..$],
  2057. pattern[pi0..pi-1]
  2058. ~ pattern[piRemain..$]))
  2059. {
  2060. return true;
  2061. }
  2062. }
  2063. if (pc == '}')
  2064. {
  2065. break;
  2066. }
  2067. }
  2068. return false;
  2069. default:
  2070. if (ni == path.length)
  2071. return false;
  2072. if (filenameCharCmp!cs(pc, path[ni]) != 0)
  2073. return false;
  2074. ni++;
  2075. break;
  2076. }
  2077. }
  2078. assert(ni <= path.length);
  2079. return ni == path.length;
  2080. }
  2081. unittest
  2082. {
  2083. assert (globMatch!(CaseSensitive.no)("foo", "Foo"));
  2084. assert (!globMatch!(CaseSensitive.yes)("foo", "Foo"));
  2085. assert(globMatch("foo", "*"));
  2086. assert(globMatch("foo.bar"w, "*"w));
  2087. assert(globMatch("foo.bar"d, "*.*"d));
  2088. assert(globMatch("foo.bar", "foo*"));
  2089. assert(globMatch("foo.bar"w, "f*bar"w));
  2090. assert(globMatch("foo.bar"d, "f*b*r"d));
  2091. assert(globMatch("foo.bar", "f???bar"));
  2092. assert(globMatch("foo.bar"w, "[fg]???bar"w));
  2093. assert(globMatch("foo.bar"d, "[!gh]*bar"d));
  2094. assert(!globMatch("foo", "bar"));
  2095. assert(!globMatch("foo"w, "*.*"w));
  2096. assert(!globMatch("foo.bar"d, "f*baz"d));
  2097. assert(!globMatch("foo.bar", "f*b*x"));
  2098. assert(!globMatch("foo.bar", "[gh]???bar"));
  2099. assert(!globMatch("foo.bar"w, "[!fg]*bar"w));
  2100. assert(!globMatch("foo.bar"d, "[fg]???baz"d));
  2101. assert(!globMatch("foo.di", "*.d")); // test issue 6634: triggered bad assertion
  2102. assert(globMatch("foo.bar", "{foo,bif}.bar"));
  2103. assert(globMatch("bif.bar"w, "{foo,bif}.bar"w));
  2104. assert(globMatch("bar.foo"d, "bar.{foo,bif}"d));
  2105. assert(globMatch("bar.bif", "bar.{foo,bif}"));
  2106. assert(globMatch("bar.fooz"w, "bar.{foo,bif}z"w));
  2107. assert(globMatch("bar.bifz"d, "bar.{foo,bif}z"d));
  2108. assert(globMatch("bar.foo", "bar.{biz,,baz}foo"));
  2109. assert(globMatch("bar.foo"w, "bar.{biz,}foo"w));
  2110. assert(globMatch("bar.foo"d, "bar.{,biz}foo"d));
  2111. assert(globMatch("bar.foo", "bar.{}foo"));
  2112. assert(globMatch("bar.foo"w, "bar.{ar,,fo}o"w));
  2113. assert(globMatch("bar.foo"d, "bar.{,ar,fo}o"d));
  2114. assert(globMatch("bar.o", "bar.{,ar,fo}o"));
  2115. static assert(globMatch("foo.bar", "[!gh]*bar"));
  2116. }
  2117. /** Checks that the given file or directory name is valid.
  2118. This function returns $(D true) if and only if $(D filename) is not
  2119. empty, not too long, and does not contain invalid characters.
  2120. The maximum length of $(D filename) is given by the constant
  2121. $(D core.stdc.stdio.FILENAME_MAX). (On Windows, this number is
  2122. defined as the maximum number of UTF-16 code points, and the
  2123. test will therefore only yield strictly correct results when
  2124. $(D filename) is a string of $(D wchar)s.)
  2125. On Windows, the following criteria must be satisfied
  2126. ($(LINK2 http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx,source)):
  2127. $(UL
  2128. $(LI $(D filename) must not contain any characters whose integer
  2129. representation is in the range 0-31.)
  2130. $(LI $(D filename) must not contain any of the following $(I reserved
  2131. characters): <>:"/\|?*)
  2132. $(LI $(D filename) may not end with a space ($(D ' ')) or a period
  2133. ($(D '.')).)
  2134. )
  2135. On POSIX, $(D filename) may not contain a forward slash ($(D '/')) or
  2136. the null character ($(D '\0')).
  2137. */
  2138. bool isValidFilename(C)(in C[] filename) @safe pure nothrow if (isSomeChar!C)
  2139. {
  2140. import core.stdc.stdio;
  2141. if (filename.length == 0 || filename.length >= FILENAME_MAX) return false;
  2142. foreach (c; filename)
  2143. {
  2144. version (Windows)
  2145. {
  2146. switch (c)
  2147. {
  2148. case 0:
  2149. ..
  2150. case 31:
  2151. case '<':
  2152. case '>':
  2153. case ':':
  2154. case '"':
  2155. case '/':
  2156. case '\\':
  2157. case '|':
  2158. case '?':
  2159. case '*':
  2160. return false;
  2161. default:
  2162. }
  2163. }
  2164. else version (Posix)
  2165. {
  2166. if (c == 0 || c == '/') return false;
  2167. }
  2168. else static assert (0);
  2169. }
  2170. version (Windows)
  2171. {
  2172. if (filename[$-1] == '.' || filename[$-1] == ' ') return false;
  2173. }
  2174. // All criteria passed
  2175. return true;
  2176. }
  2177. unittest
  2178. {
  2179. auto valid = ["foo"];
  2180. auto invalid = ["", "foo\0bar", "foo/bar"];
  2181. auto pfdep = [`foo\bar`, "*.txt"];
  2182. version (Windows) invalid ~= pfdep;
  2183. else version (Posix) valid ~= pfdep;
  2184. else static assert (0);
  2185. import std.typetuple;
  2186. foreach (T; TypeTuple!(char[], const(char)[], string, wchar[],
  2187. const(wchar)[], wstring, dchar[], const(dchar)[], dstring))
  2188. {
  2189. foreach (fn; valid)
  2190. assert (isValidFilename(to!T(fn)));
  2191. foreach (fn; invalid)
  2192. assert (!isValidFilename(to!T(fn)));
  2193. }
  2194. }
  2195. /** Checks whether $(D path) is a valid _path.
  2196. Generally, this function checks that $(D path) is not empty, and that
  2197. each component of the path either satisfies $(LREF isValidFilename)
  2198. or is equal to $(D ".") or $(D "..").
  2199. It does $(I not) check whether the _path points to an existing file
  2200. or directory; use $(XREF file,exists) for this purpose.
  2201. On Windows, some special rules apply:
  2202. $(UL
  2203. $(LI If the second character of $(D path) is a colon ($(D ':')),
  2204. the first character is interpreted as a drive letter, and
  2205. must be in the range A-Z (case insensitive).)
  2206. $(LI If $(D path) is on the form $(D `\\$(I server)\$(I share)\...`)
  2207. (UNC path), $(LREF isValidFilename) is applied to $(I server)
  2208. and $(I share) as well.)
  2209. $(LI If $(D path) starts with $(D `\\?\`) (long UNC path), the
  2210. only requirement for the rest of the string is that it does
  2211. not contain the null character.)
  2212. $(LI If $(D path) starts with $(D `\\.\`) (Win32 device namespace)
  2213. this function returns $(D false); such paths are beyond the scope
  2214. of this module.)
  2215. )
  2216. */
  2217. bool isValidPath(C)(in C[] path) @safe pure nothrow if (isSomeChar!C)
  2218. {
  2219. if (path.empty) return false;
  2220. // Check whether component is "." or "..", or whether it satisfies
  2221. // isValidFilename.
  2222. bool isValidComponent(in C[] component) @safe pure nothrow
  2223. {
  2224. assert (component.length > 0);
  2225. if (component[0] == '.')
  2226. {
  2227. if (component.length == 1) return true;
  2228. else if (component.length == 2 && component[1] == '.') return true;
  2229. }
  2230. return isValidFilename(component);
  2231. }
  2232. if (path.length == 1)
  2233. return isDirSeparator(path[0]) || isValidComponent(path);
  2234. const(C)[] remainder;
  2235. version (Windows)
  2236. {
  2237. if (isDirSeparator(path[0]) && isDirSeparator(path[1]))
  2238. {
  2239. // Some kind of UNC path
  2240. if (path.length < 5)
  2241. {
  2242. // All valid UNC paths must have at least 5 characters
  2243. return false;
  2244. }
  2245. else if (path[2] == '?')
  2246. {
  2247. // Long UNC path
  2248. if (!isDirSeparator(path[3])) return false;
  2249. foreach (c; path[4 .. $])
  2250. {
  2251. if (c == '\0') return false;
  2252. }
  2253. return true;
  2254. }
  2255. else if (path[2] == '.')
  2256. {
  2257. // Win32 device namespace not supported
  2258. return false;
  2259. }
  2260. else
  2261. {
  2262. // Normal UNC path, i.e. \\server\share\...
  2263. size_t i = 2;
  2264. while (i < path.length && !isDirSeparator(path[i])) ++i;
  2265. if (i == path.length || !isValidFilename(path[2 .. i]))
  2266. return false;
  2267. ++i; // Skip a single dir separator
  2268. size_t j = i;
  2269. while (j < path.length && !isDirSeparator(path[j])) ++j;
  2270. if (!isValidFilename(path[i .. j])) return false;
  2271. remainder = path[j .. $];
  2272. }
  2273. }
  2274. else if (isDriveSeparator(path[1]))
  2275. {
  2276. import std.ascii;
  2277. if (!isAlpha(path[0])) return false;
  2278. remainder = path[2 .. $];
  2279. }
  2280. else
  2281. {
  2282. remainder = path;
  2283. }
  2284. }
  2285. else version (Posix)
  2286. {
  2287. remainder = path;
  2288. }
  2289. else static assert (0);
  2290. assert (remainder !is null);
  2291. remainder = ltrimDirSeparators(remainder);
  2292. // Check that each component satisfies isValidComponent.
  2293. while (!remainder.empty)
  2294. {
  2295. size_t i = 0;
  2296. while (i < remainder.length && !isDirSeparator(remainder[i])) ++i;
  2297. assert (i > 0);
  2298. if (!isValidComponent(remainder[0 .. i])) return false;
  2299. remainder = ltrimDirSeparators(remainder[i .. $]);
  2300. }
  2301. // All criteria passed
  2302. return true;
  2303. }
  2304. unittest
  2305. {
  2306. assert (isValidPath("/foo/bar"));
  2307. assert (!isValidPath("/foo\0/bar"));
  2308. version (Windows)
  2309. {
  2310. assert (isValidPath(`c:\`));
  2311. assert (isValidPath(`c:\foo`));
  2312. assert (isValidPath(`c:\foo\.\bar\\\..\`));
  2313. assert (!isValidPath(`!:\foo`));
  2314. assert (!isValidPath(`c::\foo`));
  2315. assert (!isValidPath(`c:\foo?`));
  2316. assert (!isValidPath(`c:\foo.`));
  2317. assert (isValidPath(`\\server\share`));
  2318. assert (isValidPath(`\\server\share\foo`));
  2319. assert (isValidPath(`\\server\share\\foo`));
  2320. assert (!isValidPath(`\\\server\share\foo`));
  2321. assert (!isValidPath(`\\server\\share\foo`));
  2322. assert (!isValidPath(`\\ser*er\share\foo`));
  2323. assert (!isValidPath(`\\server\sha?e\foo`));
  2324. assert (!isValidPath(`\\server\share\|oo`));
  2325. assert (isValidPath(`\\?\<>:"?*|/\..\.`));
  2326. assert (!isValidPath("\\\\?\\foo\0bar"));
  2327. assert (!isValidPath(`\\.\PhysicalDisk1`));
  2328. }
  2329. }
  2330. /** Performs tilde expansion in paths on POSIX systems.
  2331. On Windows, this function does nothing.
  2332. There are two ways of using tilde expansion in a path. One
  2333. involves using the tilde alone or followed by a path separator. In
  2334. this case, the tilde will be expanded with the value of the
  2335. environment variable $(D HOME). The second way is putting
  2336. a username after the tilde (i.e. $(D ~john/Mail)). Here,
  2337. the username will be searched for in the user database
  2338. (i.e. $(D /etc/passwd) on Unix systems) and will expand to
  2339. whatever path is stored there. The username is considered the
  2340. string after the tilde ending at the first instance of a path
  2341. separator.
  2342. Note that using the $(D ~user) syntax may give different
  2343. values from just $(D ~) if the environment variable doesn't
  2344. match the value stored in the user database.
  2345. When the environment variable version is used, the path won't
  2346. be modified if the environment variable doesn't exist or it
  2347. is empty. When the database version is used, the path won't be
  2348. modified if the user doesn't exist in the database or there is
  2349. not enough memory to perform the query.
  2350. This function performs several memory allocations.
  2351. Returns:
  2352. $(D inputPath) with the tilde expanded, or just $(D inputPath)
  2353. if it could not be expanded.
  2354. For Windows, $(D expandTilde) merely returns its argument $(D inputPath).
  2355. Examples:
  2356. -----
  2357. void processFile(string path)
  2358. {
  2359. // Allow calling this function with paths such as ~/foo
  2360. auto fullPath = expandTilde(path);
  2361. ...
  2362. }
  2363. -----
  2364. */
  2365. string expandTilde(string inputPath)
  2366. {
  2367. version(Posix)
  2368. {
  2369. import core.stdc.string : strlen;
  2370. import core.stdc.stdlib : getenv, malloc, free;
  2371. /* Joins a path from a C string to the remainder of path.
  2372. The last path separator from c_path is discarded. The result
  2373. is joined to path[char_pos .. length] if char_pos is smaller
  2374. than length, otherwise path is not appended to c_path.
  2375. */
  2376. static string combineCPathWithDPath(char* c_path, string path, size_t char_pos)
  2377. {
  2378. assert(c_path != null);
  2379. assert(path.length > 0);
  2380. assert(char_pos >= 0);
  2381. // Search end of C string
  2382. size_t end = core.stdc.string.strlen(c_path);
  2383. // Remove trailing path separator, if any
  2384. if (end && isDirSeparator(c_path[end - 1]))
  2385. end--;
  2386. // Create our own copy, as lifetime of c_path is undocumented
  2387. string cp = c_path[0 .. end].idup;
  2388. // Do we append something from path?
  2389. if (char_pos < path.length)
  2390. cp ~= path[char_pos .. $];
  2391. return cp;
  2392. }
  2393. // Replaces the tilde from path with the environment variable HOME.
  2394. static string expandFromEnvironment(string path)
  2395. {
  2396. assert(path.length >= 1);
  2397. assert(path[0] == '~');
  2398. // Get HOME and use that to replace the tilde.
  2399. auto home = core.stdc.stdlib.getenv("HOME");
  2400. if (home == null)
  2401. return path;
  2402. return combineCPathWithDPath(home, path, 1);
  2403. }
  2404. // Replaces the tilde from path with the path from the user database.
  2405. static string expandFromDatabase(string path)
  2406. {
  2407. assert(path.length > 2 || (path.length == 2 && !isDirSeparator(path[1])));
  2408. assert(path[0] == '~');
  2409. // Extract username, searching for path separator.
  2410. string username;
  2411. auto last_char = std.string.indexOf(path, dirSeparator[0]);
  2412. if (last_char == -1)
  2413. {
  2414. username = path[1 .. $] ~ '\0';
  2415. last_char = username.length + 1;
  2416. }
  2417. else
  2418. {
  2419. username = path[1 .. last_char] ~ '\0';
  2420. }
  2421. assert(last_char > 1);
  2422. // Reserve C memory for the getpwnam_r() function.
  2423. passwd result;
  2424. int extra_memory_size = 5 * 1024;
  2425. void* extra_memory;
  2426. while (1)
  2427. {
  2428. extra_memory = core.stdc.stdlib.malloc(extra_memory_size);
  2429. if (extra_memory == null)
  2430. goto Lerror;
  2431. // Obtain info from database.
  2432. passwd *verify;
  2433. errno = 0;
  2434. if (getpwnam_r(cast(char*) username.ptr, &result, cast(char*) extra_memory, extra_memory_size,
  2435. &verify) == 0)
  2436. {
  2437. // Failure if verify doesn't point at result.
  2438. if (verify != &result)
  2439. // username is not found, so return path[]
  2440. goto Lnotfound;
  2441. break;
  2442. }
  2443. if (errno != ERANGE)
  2444. goto Lerror;
  2445. // extra_memory isn't large enough
  2446. core.stdc.stdlib.free(extra_memory);
  2447. extra_memory_size *= 2;
  2448. }
  2449. path = combineCPathWithDPath(result.pw_dir, path, last_char);
  2450. Lnotfound:
  2451. core.stdc.stdlib.free(extra_memory);
  2452. return path;
  2453. Lerror:
  2454. // Errors are going to be caused by running out of memory
  2455. if (extra_memory)
  2456. core.stdc.stdlib.free(extra_memory);
  2457. onOutOfMemoryError();
  2458. return null;
  2459. }
  2460. // Return early if there is no tilde in path.
  2461. if (inputPath.length < 1 || inputPath[0] != '~')
  2462. return inputPath;
  2463. if (inputPath.length == 1 || isDirSeparator(inputPath[1]))
  2464. return expandFromEnvironment(inputPath);
  2465. else
  2466. return expandFromDatabase(inputPath);
  2467. }
  2468. else version(Windows)
  2469. {
  2470. // Put here real windows implementation.
  2471. return inputPath;
  2472. }
  2473. else
  2474. {
  2475. static assert(0); // Guard. Implement on other platforms.
  2476. }
  2477. }
  2478. version(unittest) import std.process: environment;
  2479. unittest
  2480. {
  2481. version (Posix)
  2482. {
  2483. // Retrieve the current home variable.
  2484. auto oldHome = environment.get("HOME");
  2485. // Testing when there is no environment variable.
  2486. environment.remove("HOME");
  2487. assert(expandTilde("~/") == "~/");
  2488. assert(expandTilde("~") == "~");
  2489. // Testing when an environment variable is set.
  2490. environment["HOME"] = "dmd/test";
  2491. assert(expandTilde("~/") == "dmd/test/");
  2492. assert(expandTilde("~") == "dmd/test");
  2493. // The same, but with a variable ending in a slash.
  2494. environment["HOME"] = "dmd/test/";
  2495. assert(expandTilde("~/") == "dmd/test/");
  2496. assert(expandTilde("~") == "dmd/test");
  2497. // Recover original HOME variable before continuing.
  2498. if (oldHome !is null) environment["HOME"] = oldHome;
  2499. else environment.remove("HOME");
  2500. // Test user expansion for root. Are there unices without /root?
  2501. version (OSX)
  2502. assert(expandTilde("~root") == "/var/root", expandTilde("~root"));
  2503. else
  2504. assert(expandTilde("~root") == "/root", expandTilde("~root"));
  2505. version (OSX)
  2506. assert(expandTilde("~root/") == "/var/root/", expandTilde("~root/"));
  2507. else
  2508. assert(expandTilde("~root/") == "/root/", expandTilde("~root/"));
  2509. assert(expandTilde("~Idontexist/hey") == "~Idontexist/hey");
  2510. }
  2511. }