diff options
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/slang-string.cpp | 60 | ||||
| -rw-r--r-- | source/core/slang-string.h | 8 |
2 files changed, 68 insertions, 0 deletions
diff --git a/source/core/slang-string.cpp b/source/core/slang-string.cpp index e21333809..1a6221cc8 100644 --- a/source/core/slang-string.cpp +++ b/source/core/slang-string.cpp @@ -11,6 +11,66 @@ namespace Slang // for anything that uses core static const auto s_charUtilLink = CharUtil::_ensureLink(); + + // StringRepresentation + + void StringRepresentation::setContents(const UnownedStringSlice& slice) + { + const auto sliceLength = slice.getLength(); + SLANG_ASSERT(sliceLength <= capacity); + + char* chars = getData(); + + // Use move (rather than memcpy), because the slice *could* be contained in the StringRepresentation + ::memmove(chars, slice.begin(), sliceLength * sizeof(char)); + // Zero terminate. + chars[sliceLength] = 0; + // Set the length + length = sliceLength; + } + + + /* static */StringRepresentation* StringRepresentation::create(const UnownedStringSlice& slice) + { + const auto sliceLength = slice.getLength(); + + if (sliceLength) + { + StringRepresentation* rep = StringRepresentation::createWithLength(sliceLength); + + char* chars = rep->getData(); + ::memcpy(chars, slice.begin(), sizeof(char) * sliceLength); + chars[sliceLength] = 0; + + return rep; + } + else + { + return nullptr; + } + } + + /* static */StringRepresentation* StringRepresentation::createWithReference(const UnownedStringSlice& slice) + { + const auto sliceLength = slice.getLength(); + + if (sliceLength) + { + StringRepresentation* rep = StringRepresentation::createWithLength(sliceLength); + rep->addReference(); + + char* chars = rep->getData(); + ::memcpy(chars, slice.begin(), sizeof(char) * sliceLength); + chars[sliceLength] = 0; + + return rep; + } + else + { + return nullptr; + } + } + // OSString OSString::OSString() diff --git a/source/core/slang-string.h b/source/core/slang-string.h index 85f7b894f..cb13ec415 100644 --- a/source/core/slang-string.h +++ b/source/core/slang-string.h @@ -210,6 +210,9 @@ namespace Slang return (const char*)(this + 1); } + /// Set the contents to be the slice. Must be enough capacity to hold the slice. + void setContents(const UnownedStringSlice& slice); + static const char* getData(const StringRepresentation* stringRep) { return stringRep ? stringRep->getData() : ""; @@ -246,6 +249,11 @@ namespace Slang return createWithCapacityAndLength(length, length); } + /// Create a representation from the slice. If slice is empty will return nullptr. + static StringRepresentation* create(const UnownedStringSlice& slice); + /// Same as create, but representation will have refcount of 1 (if not nullptr) + static StringRepresentation* createWithReference(const UnownedStringSlice& slice); + StringRepresentation* cloneWithCapacity(Index newCapacity) { StringRepresentation* newObj = createWithCapacityAndLength(newCapacity, length); |
