Path: blob/main_old/src/compiler/translator/ImmutableStringBuilder.h
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.h: Stringstream-like utility for building pool allocated strings where the6// maximum length is known in advance.7//89#ifndef COMPILER_TRANSLATOR_IMMUTABLESTRINGBUILDER_H_10#define COMPILER_TRANSLATOR_IMMUTABLESTRINGBUILDER_H_1112#include "compiler/translator/ImmutableString.h"1314namespace sh15{1617class ImmutableStringBuilder18{19public:20ImmutableStringBuilder(size_t maxLength)21: mPos(0u), mMaxLength(maxLength), mData(AllocateEmptyPoolCharArray(maxLength))22{}2324ImmutableStringBuilder &operator<<(const ImmutableString &str);2526ImmutableStringBuilder &operator<<(const char *str);2728ImmutableStringBuilder &operator<<(const char &c);2930// This invalidates the ImmutableStringBuilder, so it should only be called once.31operator ImmutableString();3233void appendDecimal(const uint32_t &i);3435template <typename T>36void appendHex(T number)37{38ASSERT(mData != nullptr);39ASSERT(mPos + sizeof(T) * 2u <= mMaxLength);40int index = static_cast<int>(sizeof(T)) * 2 - 1;41// Loop through leading zeroes.42while (((number >> (index * 4)) & 0xfu) == 0 && index > 0)43{44--index;45}46// Write the rest of the hex digits.47while (index >= 0)48{49char digit = static_cast<char>((number >> (index * 4)) & 0xfu);50char digitChar = (digit < 10) ? (digit + '0') : (digit + ('a' - 10));51mData[mPos++] = digitChar;52--index;53}54}5556template <typename T>57static constexpr size_t GetHexCharCount()58{59return sizeof(T) * 2;60}6162private:63inline static char *AllocateEmptyPoolCharArray(size_t strLength)64{65size_t requiredSize = strLength + 1u;66return static_cast<char *>(GetGlobalPoolAllocator()->allocate(requiredSize));67}6869size_t mPos;70size_t mMaxLength;71char *mData;72};7374// GLSL ES 3.00.6 section 3.9: the maximum length of an identifier is 1024 characters.75constexpr unsigned int kESSLMaxIdentifierLength = 1024u;7677} // namespace sh7879#endif // COMPILER_TRANSLATOR_IMMUTABLESTRINGBUILDER_H_808182