diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2022-10-06 10:27:50 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-06 10:27:50 -0400 |
| commit | d1e740d8b25e03a734093bd84b792eaf969649d1 (patch) | |
| tree | bd19d69b54b00c9920dd6b02cc28d90dd8a639d5 /source/core/slang-io.cpp | |
| parent | cf34d2830a3103b2b47a4140d27d054b797705f2 (diff) | |
Improvements around absolute paths and file systems (#2433)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Add handling for root paths.
* Fixes around absolute paths.
* Add SimplifyStyle
* Remove unrequire include.
* Fix some details around RelativeFileSystem canonical paths.
* For MemoryFileSystem make sure "/a" and "a" maps to same canonical path.
* Add test for canonicalPath.
* Improve comment.
* More testing around canonical paths.
Diffstat (limited to 'source/core/slang-io.cpp')
| -rw-r--r-- | source/core/slang-io.cpp | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/source/core/slang-io.cpp b/source/core/slang-io.cpp index b25d5562d..345a2c353 100644 --- a/source/core/slang-io.cpp +++ b/source/core/slang-io.cpp @@ -419,7 +419,7 @@ namespace Slang return false; } - /* static */SlangResult Path::simplifyAbsolute(const UnownedStringSlice& path, StringBuilder& outPath) + /* static */SlangResult Path::simplify(const UnownedStringSlice& path, SimplifyStyle style, StringBuilder& outPath) { if (path.getLength() == 0) { @@ -427,27 +427,38 @@ namespace Slang } List<UnownedStringSlice> splitPath; - Path::split(UnownedStringSlice(path), splitPath); + split(UnownedStringSlice(path), splitPath); - // If the first part of a path is "", it means path of form "/some/path". Turn into "some/path". - if (splitPath.getCount() > 1 && splitPath[0].getLength() == 0) - { - splitPath.removeAt(0); - } + simplify(splitPath); - Path::simplify(splitPath); + const auto simplifyIntegral = SimplifyIntegral(style); // If it has a relative part then it's not absolute - if (splitPath.indexOf(UnownedStringSlice::fromLiteral("..")) >= 0) + if ((simplifyIntegral & SimplifyFlag::AbsoluteOnly) && + splitPath.indexOf(UnownedStringSlice::fromLiteral("..")) >= 0) { return SLANG_E_NOT_FOUND; } // We allow splitPath.getCount() == 0, because // the original path could have been '.' or './.' - // Special handling for this is in join + // + // Special handling this case is in Path::join + + // If we want the path produced such that is *not* output with a root (ie SimplifyFlag::NoRoot) + // we detect if we a rooted path (ie in effect starting with "/") and so splitPath[0] == "" + // and remove that part from when doing the join. + if ((simplifyIntegral & SimplifyFlag::NoRoot) && + (splitPath.getCount() && splitPath[0].getLength() == 0)) + { + // If we allow without a root, we remove from the join + Path::join(splitPath.getBuffer() + 1, splitPath.getCount() - 1, outPath); + } + else + { + Path::join(splitPath.getBuffer(), splitPath.getCount(), outPath); + } - Path::join(splitPath.getBuffer(), splitPath.getCount(), outPath); return SLANG_OK; } @@ -485,10 +496,16 @@ namespace Slang if (count == 0) { out << "."; - return; } - - StringUtil::join(slices, count, kPathDelimiter, out); + else if (count == 1 && slices[0].getLength() == 0) + { + // It's the root + out << kPathDelimiter; + } + else + { + StringUtil::join(slices, count, kPathDelimiter, out); + } } |
