diff options
Diffstat (limited to 'source/core/slang-string.cpp')
| -rw-r--r-- | source/core/slang-string.cpp | 60 |
1 files changed, 60 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() |
