diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-01-21 15:33:59 -0500 |
|---|---|---|
| committer | Tim Foley <tfoleyNV@users.noreply.github.com> | 2019-01-21 12:33:59 -0800 |
| commit | 0a3ef7b4ae02983ea3f986ba8211e7c6af9d254b (patch) | |
| tree | 86c84c7b20be86827c047cd82918f7f47537a338 /source/core | |
| parent | c55b462bf993dcd65ec17ffeeab97ac138e6d206 (diff) | |
Path simplification/hash mode, plus bug fixes (#788)
* * Fix memory bug around expanding va_args - needed buffer to have space for terminating 0
* Fix problem with FileWriter defaults being globals, as memory they allocate, will only be freed after return from main - work around by making StdWriters RefObject derived, and kept in scope such the writers are destroyed before checks for leaks is found
* Added SimplifyPathAndHash mode for CacheFileSystem - will simplify the path and see if simplified path is in cache before reading file (limiting amout of underlying file requests)
* * Added calcReplaceChar
* Renamed DefaultFileSystem to OSFileSystem
* Made OSFileSystem convert windows \ to / on linux
* Simplified logic for caching in CacheFileSystem.
* Added pragma-once-c to add extra test, but also so there is an 'include' directory in preprocessor tests.
* Small fixes in pragma once test.
* Simplified cache handling path, so that paths/simplified paths area always added.
* Improve naming of methods for different caches.
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/slang-io.cpp | 18 | ||||
| -rw-r--r-- | source/core/slang-io.h | 6 | ||||
| -rw-r--r-- | source/core/slang-std-writers.cpp | 33 | ||||
| -rw-r--r-- | source/core/slang-std-writers.h | 8 | ||||
| -rw-r--r-- | source/core/slang-string-util.cpp | 33 | ||||
| -rw-r--r-- | source/core/slang-string-util.h | 4 | ||||
| -rw-r--r-- | source/core/slang-writer.cpp | 4 |
7 files changed, 81 insertions, 25 deletions
diff --git a/source/core/slang-io.cpp b/source/core/slang-io.cpp index f1948200c..cf099aa04 100644 --- a/source/core/slang-io.cpp +++ b/source/core/slang-io.cpp @@ -174,6 +174,21 @@ namespace Slang } } + /* static */bool Path::IsRelative(const UnownedStringSlice& path) + { + List<UnownedStringSlice> split; + Split(path, split); + + for (const auto& cur : split) + { + if (cur == "." || cur == "..") + { + return true; + } + } + return false; + } + /* static */String Path::Simplify(const UnownedStringSlice& path) { List<UnownedStringSlice> split; @@ -208,7 +223,7 @@ namespace Slang { split.Add(UnownedStringSlice::fromLiteral(".")); } - + // Reconstruct the string StringBuilder builder; for (int i = 0; i < int(split.Count()); i++) @@ -219,6 +234,7 @@ namespace Slang } builder.Append(split[i]); } + return builder; } diff --git a/source/core/slang-io.h b/source/core/slang-io.h index 3703aee08..177671442 100644 --- a/source/core/slang-io.h +++ b/source/core/slang-io.h @@ -43,10 +43,14 @@ namespace Slang static String Simplify(const UnownedStringSlice& path); static String Simplify(const String& path) { return Simplify(path.getUnownedSlice()); } + /// Returns true if a path contains a . or .. + static bool IsRelative(const UnownedStringSlice& path); + static bool IsRelative(const String& path) { return IsRelative(path.getUnownedSlice()); } + static SlangResult GetPathType(const String & path, SlangPathType* pathTypeOut); static SlangResult GetCanonical(const String & path, String& canonicalPathOut); }; } -#endif
\ No newline at end of file +#endif diff --git a/source/core/slang-std-writers.cpp b/source/core/slang-std-writers.cpp index 963af9ef5..8b2f6f604 100644 --- a/source/core/slang-std-writers.cpp +++ b/source/core/slang-std-writers.cpp @@ -6,29 +6,28 @@ namespace Slang /* static */StdWriters* StdWriters::s_singleton = nullptr; -/* static */StdWriters* StdWriters::getDefault() +/* static */RefPtr<StdWriters> StdWriters::createDefault() { - static StdWriters* s_context = nullptr; + RefPtr<StdWriters> stdWriters(new StdWriters); - if (!s_context) - { - static FileWriter s_stdError(stderr, WriterFlag::IsStatic | WriterFlag::IsUnowned | WriterFlag::AutoFlush); - static FileWriter s_stdOut(stdout, WriterFlag::IsStatic | WriterFlag::IsUnowned | WriterFlag::AutoFlush); - - static StdWriters s_contextVar; - s_context = &s_contextVar; + RefPtr<FileWriter> stdError(new FileWriter(stderr, WriterFlag::AutoFlush)); + RefPtr<FileWriter> stdOut(new FileWriter(stdout, WriterFlag::AutoFlush)); - s_context->setWriter(SLANG_WRITER_CHANNEL_STD_ERROR, &s_stdError); - s_context->setWriter(SLANG_WRITER_CHANNEL_STD_OUTPUT, &s_stdOut); - } - return s_context; + stdWriters->setWriter(SLANG_WRITER_CHANNEL_STD_ERROR, stdError); + stdWriters->setWriter(SLANG_WRITER_CHANNEL_STD_OUTPUT, stdOut); + + return stdWriters; } -/* static */StdWriters* StdWriters::initDefault() +/* static */RefPtr<StdWriters> StdWriters::initDefaultSingleton() { - StdWriters* context = getDefault(); - setSingleton(context); - return context; + if (s_singleton) + { + return s_singleton; + } + auto defaults = createDefault(); + setSingleton(defaults); + return defaults; } void StdWriters::setRequestWriters(SlangCompileRequest* request) diff --git a/source/core/slang-std-writers.h b/source/core/slang-std-writers.h index d580842eb..b35d3d037 100644 --- a/source/core/slang-std-writers.h +++ b/source/core/slang-std-writers.h @@ -8,9 +8,10 @@ namespace Slang { /* Holds standard writers for the channels */ -class StdWriters +class StdWriters: public RefObject { public: + ISlangWriter * getWriter(SlangWriterChannel chan) const { return m_writers[chan]; } void setWriter(SlangWriterChannel chan, ISlangWriter* writer) { m_writers[chan] = writer; } @@ -21,9 +22,8 @@ public: StdWriters() {} /// Initialize a default context - static StdWriters* initDefault(); - - static StdWriters* getDefault(); + static RefPtr<StdWriters> createDefault(); + static RefPtr<StdWriters> initDefaultSingleton(); static StdWriters* getSingleton() { return s_singleton; } static void setSingleton(StdWriters* context) { s_singleton = context; } diff --git a/source/core/slang-string-util.cpp b/source/core/slang-string-util.cpp index cb5709759..5d3e2188d 100644 --- a/source/core/slang-string-util.cpp +++ b/source/core/slang-string-util.cpp @@ -59,7 +59,7 @@ static const Guid IID_ISlangBlob = SLANG_UUID_ISlangBlob; /* static */void StringUtil::append(const char* format, va_list args, StringBuilder& buf) { - // Calculate the size + // Calculate the size required (not including terminating 0) size_t numChars; { // Create a copy of args, as will be consumed by calcFormattedSize @@ -69,6 +69,7 @@ static const Guid IID_ISlangBlob = SLANG_UUID_ISlangBlob; va_end(argsCopy); } + // Requires + 1 , because calcFormatted appends a terminating 0 char* dst = buf.prepareForAppend(numChars + 1); calcFormatted(format, args, numChars, dst); buf.appendInPlace(dst, numChars); @@ -118,4 +119,34 @@ ComPtr<ISlangBlob> StringUtil::createStringBlob(const String& string) return ComPtr<ISlangBlob>(new StringBlob(string)); } +/* static */String StringUtil::calcCharReplaced(const UnownedStringSlice& slice, char fromChar, char toChar) +{ + if (fromChar == toChar) + { + return slice; + } + + const UInt numChars = slice.size(); + const char* srcChars = slice.begin(); + + StringBuilder builder; + char* dstChars = builder.prepareForAppend(numChars); + + for (UInt i = 0; i < numChars; ++i) + { + char c = srcChars[i]; + dstChars[i] = (c == fromChar) ? toChar : c; + } + + builder.appendInPlace(dstChars, numChars); + return builder; +} + +/* static */String StringUtil::calcCharReplaced(const String& string, char fromChar, char toChar) +{ + return (fromChar == toChar || string.IndexOf(fromChar) == UInt(-1)) ? string : calcCharReplaced(string.getUnownedSlice(), fromChar, toChar); +} + + + } // namespace Slang diff --git a/source/core/slang-string-util.h b/source/core/slang-string-util.h index 0579dd057..3547fd97c 100644 --- a/source/core/slang-string-util.h +++ b/source/core/slang-string-util.h @@ -63,6 +63,10 @@ struct StringUtil /// Returns an empty string if blob is nullptr static String getString(ISlangBlob* blob); + /// Given a string or slice, replaces all instances of fromChar with toChar + static String calcCharReplaced(const UnownedStringSlice& slice, char fromChar, char toChar); + static String calcCharReplaced(const String& string, char fromChar, char toChar); + /// Create a blob from a string static ComPtr<ISlangBlob> createStringBlob(const String& string); }; diff --git a/source/core/slang-writer.cpp b/source/core/slang-writer.cpp index 433cc6992..a3222b0d2 100644 --- a/source/core/slang-writer.cpp +++ b/source/core/slang-writer.cpp @@ -28,6 +28,7 @@ SlangResult WriterHelper::print(const char* format, ...) SlangResult res = SLANG_OK; + // numChars is the amount of characters needed *not* including terminating 0 size_t numChars; { // Create a copy of args, as will be consumed by calcFormattedSize @@ -39,7 +40,8 @@ SlangResult WriterHelper::print(const char* format, ...) if (numChars > 0) { - char* appendBuffer = m_writer->beginAppendBuffer(numChars); + // We need to add 1 here, because calcFormatted, *requires* space for terminating 0 + char* appendBuffer = m_writer->beginAppendBuffer(numChars + 1); StringUtil::calcFormatted(format, args, numChars, appendBuffer); res = m_writer->endAppendBuffer(appendBuffer, numChars); } |
