Path: blob/main_old/src/compiler/translator/ImmutableStringBuilder.cpp
1693 views
//1// Copyright 2018 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// ImmutableStringBuilder.cpp: Stringstream-like utility for building pool allocated strings where6// the maximum length is known in advance.7//89#include "compiler/translator/ImmutableStringBuilder.h"1011#include <stdio.h>1213namespace sh14{1516ImmutableStringBuilder &ImmutableStringBuilder::operator<<(const ImmutableString &str)17{18ASSERT(mData != nullptr);19ASSERT(mPos + str.length() <= mMaxLength);20memcpy(mData + mPos, str.data(), str.length());21mPos += str.length();22return *this;23}2425ImmutableStringBuilder &ImmutableStringBuilder::operator<<(const char *str)26{27ASSERT(mData != nullptr);28size_t len = strlen(str);29ASSERT(mPos + len <= mMaxLength);30memcpy(mData + mPos, str, len);31mPos += len;32return *this;33}3435ImmutableStringBuilder &ImmutableStringBuilder::operator<<(const char &c)36{37ASSERT(mData != nullptr);38ASSERT(mPos + 1 <= mMaxLength);39mData[mPos++] = c;40return *this;41}4243void ImmutableStringBuilder::appendDecimal(const uint32_t &u)44{45int numChars = snprintf(mData + mPos, mMaxLength - mPos, "%d", u);46ASSERT(numChars >= 0);47ASSERT(mPos + numChars <= mMaxLength);48mPos += numChars;49}5051ImmutableStringBuilder::operator ImmutableString()52{53mData[mPos] = '\0';54ImmutableString str(static_cast<const char *>(mData), mPos);55#if defined(ANGLE_ENABLE_ASSERTS)56// Make sure that nothing is added to the string after it is finalized.57mData = nullptr;58#endif59return str;60}6162} // namespace sh636465