From 0af330d2deeb10403d3581ae90383d14d969f6fb Mon Sep 17 00:00:00 2001 From: Jonathan M Davis Date: Sun, 21 May 2017 09:12:44 -0700 Subject: [PATCH 1/3] Fix safety of driveName for implicitly convertible types. --- std/path.d | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/std/path.d b/std/path.d index 325985490..1770eeab1 100644 --- a/std/path.d +++ b/std/path.d @@ -784,9 +784,21 @@ if (isConvertibleToString!R) Always returns an empty range on POSIX. */ auto driveName(R)(R path) -if ((isRandomAccessRange!R && hasSlicing!R && hasLength!R && isSomeChar!(ElementType!R) || - isNarrowString!R) && - !isConvertibleToString!R) +if (isRandomAccessRange!R && hasSlicing!R && hasLength!R && isSomeChar!(ElementType!R) && !isSomeString!R) +{ + return _driveName(path); +} + +/// ditto +auto driveName(C)(C[] path) +if (isSomeChar!C) +{ + return _driveName(path); +} + +private auto _driveName(R)(R path) +if (isRandomAccessRange!R && hasSlicing!R && hasLength!R && isSomeChar!(ElementType!R) || + isNarrowString!R) { version (Windows) { @@ -820,15 +832,20 @@ if ((isRandomAccessRange!R && hasSlicing!R && hasLength!R && isSomeChar!(Element } } -auto driveName(R)(auto ref R path) -if (isConvertibleToString!R) -{ - return driveName!(StringTypeOf!R)(path); -} - @safe unittest { - assert(testAliasedString!driveName(`d:\file`)); + assert(testAliasedString!driveName("d:/file")); + + version(Posix) + immutable result = ""; + else version(Windows) + immutable result = "d:"; + + enum S : string { a = "d:/file" } + assert(S.a.driveName == result); + + char[S.a.length] sa = S.a[]; + assert(sa.driveName == result); } @safe unittest From 13240d78251e43dd756e844580a116db478330f3 Mon Sep 17 00:00:00 2001 From: Jonathan M Davis Date: Sun, 21 May 2017 09:20:13 -0700 Subject: [PATCH 2/3] Fix safety of rootName for implicitly convertible types. --- std/path.d | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/std/path.d b/std/path.d index 1770eeab1..c440d892a 100644 --- a/std/path.d +++ b/std/path.d @@ -688,9 +688,21 @@ if (isRandomAccessRange!R && hasSlicing!R && hasLength!R && isSomeChar!(ElementT A slice of $(D path). */ auto rootName(R)(R path) -if ((isRandomAccessRange!R && hasSlicing!R && hasLength!R && isSomeChar!(ElementType!R) || - isNarrowString!R) && - !isConvertibleToString!R) +if (isRandomAccessRange!R && hasSlicing!R && hasLength!R && isSomeChar!(ElementType!R) && !isSomeString!R) +{ + return _rootName(path); +} + +/// ditto +auto rootName(C)(C[] path) +if (isSomeChar!C) +{ + return _rootName(path); +} + +private auto _rootName(R)(R path) +if (isRandomAccessRange!R && hasSlicing!R && hasLength!R && isSomeChar!(ElementType!R) || + isNarrowString!R) { if (path.empty) goto Lnull; @@ -742,6 +754,12 @@ Lnull: @safe unittest { assert(testAliasedString!rootName("/foo/bar")); + + enum S : string { a = "/foo/bar" } + assert(S.a.rootName == "/"); + + char[S.a.length] sa = S.a[]; + assert(sa.rootName == "/"); } @safe unittest @@ -763,12 +781,6 @@ Lnull: } } -auto rootName(R)(R path) -if (isConvertibleToString!R) -{ - return rootName!(StringTypeOf!R)(path); -} - /** Get the drive portion of a path. From d4a7e693d70ae2d2d533fd079dd1fddaaf582f9e Mon Sep 17 00:00:00 2001 From: Jonathan M Davis Date: Sun, 21 May 2017 09:29:54 -0700 Subject: [PATCH 3/3] Fix safety of stripDrive for implicitly convertible types. --- std/path.d | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/std/path.d b/std/path.d index c440d892a..bb63d74a4 100644 --- a/std/path.d +++ b/std/path.d @@ -890,9 +890,20 @@ if (isRandomAccessRange!R && hasSlicing!R && hasLength!R && isSomeChar!(ElementT Returns: A slice of path without the drive component. */ auto stripDrive(R)(R path) -if ((isRandomAccessRange!R && hasSlicing!R && isSomeChar!(ElementType!R) || - isNarrowString!R) && - !isConvertibleToString!R) +if (isRandomAccessRange!R && hasSlicing!R && isSomeChar!(ElementType!R) && !isSomeString!R) +{ + return _stripDrive(path); +} + +/// ditto +auto stripDrive(C)(C[] path) +{ + return _stripDrive(path); +} + +private auto _stripDrive(R)(R path) +if (isRandomAccessRange!R && hasSlicing!R && isSomeChar!(ElementType!R) || + isNarrowString!R) { version(Windows) { @@ -912,15 +923,20 @@ if ((isRandomAccessRange!R && hasSlicing!R && isSomeChar!(ElementType!R) || } } -auto stripDrive(R)(auto ref R path) -if (isConvertibleToString!R) -{ - return stripDrive!(StringTypeOf!R)(path); -} - @safe unittest { - assert(testAliasedString!stripDrive(`d:\dir\file`)); + assert(testAliasedString!stripDrive("d:/dir/file")); + + version(Posix) + immutable result = "d:/dir/file"; + else version(Windows) + immutable result = "/dir/file"; + + enum S : string { a = "d:/dir/file" } + assert(S.a.stripDrive == result); + + char[S.a.length] sa = S.a[]; + assert(sa.stripDrive == result); } @safe unittest