mirror of
https://github.com/dlang/phobos.git
synced 2025-04-27 21:51:40 +03:00
buildNormalizedPath() and join() have same issue.
This commit is contained in:
parent
c07fe7c8ba
commit
6b9da73f83
1 changed files with 31 additions and 14 deletions
45
std/path.d
45
std/path.d
|
@ -993,13 +993,17 @@ unittest
|
|||
}
|
||||
---
|
||||
*/
|
||||
immutable(C)[] buildNormalizedPath(C)(const(C)[][] paths...)
|
||||
immutable(C)[] buildNormalizedPath(C)(const(C[])[] paths...)
|
||||
@trusted pure nothrow
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
import std.c.stdlib;
|
||||
auto paths2 = new const(C)[][](paths.length);
|
||||
//(cast(const(C)[]*)alloca((const(C)[]).sizeof * paths.length))[0 .. paths.length];
|
||||
|
||||
// Check whether the resulting path will be absolute or rooted,
|
||||
// calculate its maximum length, and discard segments we won't use.
|
||||
typeof(paths[0]) rootElement;
|
||||
typeof(paths[0][0])[] rootElement;
|
||||
int numPaths = 0;
|
||||
bool seenAbsolute;
|
||||
size_t segmentLengthSum = 0;
|
||||
|
@ -1014,31 +1018,31 @@ immutable(C)[] buildNormalizedPath(C)(const(C)[][] paths...)
|
|||
{
|
||||
if (thisIsAbsolute) seenAbsolute = true;
|
||||
rootElement = rootName(p);
|
||||
paths[0] = p[rootElement.length .. $];
|
||||
paths2[0] = p[rootElement.length .. $];
|
||||
numPaths = 1;
|
||||
segmentLengthSum = paths[0].length;
|
||||
segmentLengthSum = paths2[0].length;
|
||||
}
|
||||
else
|
||||
{
|
||||
paths[0] = p;
|
||||
paths2[0] = p;
|
||||
numPaths = 1;
|
||||
segmentLengthSum = p.length;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
paths[numPaths++] = p;
|
||||
paths2[numPaths++] = p;
|
||||
segmentLengthSum += p.length;
|
||||
}
|
||||
}
|
||||
if (rootElement.length + segmentLengthSum == 0) return null;
|
||||
paths = paths[0 .. numPaths];
|
||||
paths2 = paths2[0 .. numPaths];
|
||||
immutable rooted = !rootElement.empty;
|
||||
assert (rooted || !seenAbsolute); // absolute => rooted
|
||||
|
||||
// Allocate memory for the resulting path, including room for
|
||||
// extra dir separators
|
||||
auto fullPath = new C[rootElement.length + segmentLengthSum + paths.length];
|
||||
auto fullPath = new C[rootElement.length + segmentLengthSum + paths2.length];
|
||||
|
||||
// Copy the root element into fullPath, and let relPart be
|
||||
// the remaining slice.
|
||||
|
@ -1084,7 +1088,7 @@ immutable(C)[] buildNormalizedPath(C)(const(C)[][] paths...)
|
|||
// root we found earlier.
|
||||
bool hasParents = rooted;
|
||||
sizediff_t i;
|
||||
foreach (path; paths)
|
||||
foreach (path; paths2)
|
||||
{
|
||||
path = trimDirSeparators(path);
|
||||
|
||||
|
@ -1368,6 +1372,13 @@ unittest
|
|||
else static assert (false);
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
// 7397
|
||||
string[] ary = ["a", "b"];
|
||||
buildNormalizedPath(ary);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -2141,7 +2152,7 @@ in
|
|||
}
|
||||
body
|
||||
{
|
||||
size_t ni; // current character in path
|
||||
size_t ni; // current character in path
|
||||
|
||||
foreach (ref pi; 0 .. pattern.length)
|
||||
{
|
||||
|
@ -2242,10 +2253,10 @@ body
|
|||
return false;
|
||||
ni++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert(ni <= path.length);
|
||||
return ni == path.length;
|
||||
return ni == path.length;
|
||||
}
|
||||
|
||||
unittest
|
||||
|
@ -3626,7 +3637,7 @@ version (OldStdPathUnittest) unittest
|
|||
* -----
|
||||
*/
|
||||
|
||||
string join()(const(char)[] p1, const(char)[] p2, const(char)[][] more...)
|
||||
string join()(const(char)[] p1, const(char)[] p2, const(char[])[] more...)
|
||||
{
|
||||
if (more.length)
|
||||
{
|
||||
|
@ -3776,6 +3787,12 @@ version (OldStdPathUnittest) unittest
|
|||
assert (join("foo", "") == "foo");
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
// 7397
|
||||
string[] ary = ["a", "b"];
|
||||
join("x", "y", ary);
|
||||
}
|
||||
|
||||
/**********************************
|
||||
* $(RED Scheduled for deprecation in February 2012. Please use
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue