blob: 00a8261eb10f3741bb845d6e8c29b7f48b1def62 [file] [log] [blame]
Olli Etuaho12c03762018-01-25 12:22:33 +02001//
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
12namespace sh
13{
14
15ImmutableStringBuilder &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
24ImmutableStringBuilder &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
34ImmutableStringBuilder &ImmutableStringBuilder::operator<<(const char &c)
35{
36 ASSERT(mData != nullptr);
37 ASSERT(mPos + 1 <= mMaxLength);
38 mData[mPos++] = c;
39 return *this;
40}
41
42ImmutableStringBuilder::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