Olli Etuaho | 12c0376 | 2018-01-25 12:22:33 +0200 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (c) 2018 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // ImmutableStringBuilder.cpp: Stringstream-like utility for building pool allocated strings where |
| 7 | // the maximum length is known in advance. |
| 8 | // |
| 9 | |
| 10 | #include "compiler/translator/ImmutableStringBuilder.h" |
| 11 | |
| 12 | namespace sh |
| 13 | { |
| 14 | |
| 15 | ImmutableStringBuilder &ImmutableStringBuilder::operator<<(const ImmutableString &str) |
| 16 | { |
| 17 | ASSERT(mData != nullptr); |
| 18 | ASSERT(mPos + str.length() <= mMaxLength); |
| 19 | memcpy(mData + mPos, str.data(), str.length()); |
| 20 | mPos += str.length(); |
| 21 | return *this; |
| 22 | } |
| 23 | |
| 24 | ImmutableStringBuilder &ImmutableStringBuilder::operator<<(const char *str) |
| 25 | { |
| 26 | ASSERT(mData != nullptr); |
| 27 | size_t len = strlen(str); |
| 28 | ASSERT(mPos + len <= mMaxLength); |
| 29 | memcpy(mData + mPos, str, len); |
| 30 | mPos += len; |
| 31 | return *this; |
| 32 | } |
| 33 | |
| 34 | ImmutableStringBuilder &ImmutableStringBuilder::operator<<(const char &c) |
| 35 | { |
| 36 | ASSERT(mData != nullptr); |
| 37 | ASSERT(mPos + 1 <= mMaxLength); |
| 38 | mData[mPos++] = c; |
| 39 | return *this; |
| 40 | } |
| 41 | |
| 42 | ImmutableStringBuilder::operator ImmutableString() |
| 43 | { |
| 44 | mData[mPos] = '\0'; |
| 45 | ImmutableString str(static_cast<const char *>(mData), mPos); |
| 46 | #if defined(ANGLE_ENABLE_ASSERTS) |
| 47 | // Make sure that nothing is added to the string after it is finalized. |
| 48 | mData = nullptr; |
| 49 | #endif |
| 50 | return str; |
| 51 | } |
| 52 | |
| 53 | } // namespace sh |