| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #ifndef DEMANGLE_UTILITY_H |
| | #define DEMANGLE_UTILITY_H |
| |
|
| | #include "DemangleConfig.h" |
| |
|
| | #include <array> |
| | #include <cassert> |
| | #include <cstdint> |
| | #include <cstdlib> |
| | #include <cstring> |
| | #include <exception> |
| | #include <limits> |
| | #include <string_view> |
| |
|
| | DEMANGLE_NAMESPACE_BEGIN |
| |
|
| | |
| | |
| | class OutputBuffer { |
| | char *Buffer = nullptr; |
| | size_t CurrentPosition = 0; |
| | size_t BufferCapacity = 0; |
| |
|
| | |
| | void grow(size_t N) { |
| | size_t Need = N + CurrentPosition; |
| | if (Need > BufferCapacity) { |
| | |
| | |
| | |
| | Need += 1024 - 32; |
| | BufferCapacity *= 2; |
| | if (BufferCapacity < Need) |
| | BufferCapacity = Need; |
| | Buffer = static_cast<char *>(std::realloc(Buffer, BufferCapacity)); |
| | if (Buffer == nullptr) |
| | std::terminate(); |
| | } |
| | } |
| |
|
| | OutputBuffer &writeUnsigned(uint64_t N, bool isNeg = false) { |
| | std::array<char, 21> Temp; |
| | char *TempPtr = Temp.data() + Temp.size(); |
| |
|
| | |
| | do { |
| | *--TempPtr = char('0' + N % 10); |
| | N /= 10; |
| | } while (N); |
| |
|
| | |
| | if (isNeg) |
| | *--TempPtr = '-'; |
| |
|
| | return operator+=( |
| | std::string_view(TempPtr, Temp.data() + Temp.size() - TempPtr)); |
| | } |
| |
|
| | public: |
| | OutputBuffer(char *StartBuf, size_t Size) |
| | : Buffer(StartBuf), BufferCapacity(Size) {} |
| | OutputBuffer(char *StartBuf, size_t *SizePtr) |
| | : OutputBuffer(StartBuf, StartBuf ? *SizePtr : 0) {} |
| | OutputBuffer() = default; |
| | |
| | OutputBuffer(const OutputBuffer &) = delete; |
| | OutputBuffer &operator=(const OutputBuffer &) = delete; |
| |
|
| | operator std::string_view() const { |
| | return std::string_view(Buffer, CurrentPosition); |
| | } |
| |
|
| | |
| | |
| | unsigned CurrentPackIndex = std::numeric_limits<unsigned>::max(); |
| | unsigned CurrentPackMax = std::numeric_limits<unsigned>::max(); |
| |
|
| | |
| | |
| | unsigned GtIsGt = 1; |
| |
|
| | bool isGtInsideTemplateArgs() const { return GtIsGt == 0; } |
| |
|
| | void printOpen(char Open = '(') { |
| | GtIsGt++; |
| | *this += Open; |
| | } |
| | void printClose(char Close = ')') { |
| | GtIsGt--; |
| | *this += Close; |
| | } |
| |
|
| | OutputBuffer &operator+=(std::string_view R) { |
| | if (size_t Size = R.size()) { |
| | grow(Size); |
| | std::memcpy(Buffer + CurrentPosition, &*R.begin(), Size); |
| | CurrentPosition += Size; |
| | } |
| | return *this; |
| | } |
| |
|
| | OutputBuffer &operator+=(char C) { |
| | grow(1); |
| | Buffer[CurrentPosition++] = C; |
| | return *this; |
| | } |
| |
|
| | OutputBuffer &prepend(std::string_view R) { |
| | size_t Size = R.size(); |
| |
|
| | grow(Size); |
| | std::memmove(Buffer + Size, Buffer, CurrentPosition); |
| | std::memcpy(Buffer, &*R.begin(), Size); |
| | CurrentPosition += Size; |
| |
|
| | return *this; |
| | } |
| |
|
| | OutputBuffer &operator<<(std::string_view R) { return (*this += R); } |
| |
|
| | OutputBuffer &operator<<(char C) { return (*this += C); } |
| |
|
| | OutputBuffer &operator<<(long long N) { |
| | return writeUnsigned(static_cast<unsigned long long>(std::abs(N)), N < 0); |
| | } |
| |
|
| | OutputBuffer &operator<<(unsigned long long N) { |
| | return writeUnsigned(N, false); |
| | } |
| |
|
| | OutputBuffer &operator<<(long N) { |
| | return this->operator<<(static_cast<long long>(N)); |
| | } |
| |
|
| | OutputBuffer &operator<<(unsigned long N) { |
| | return this->operator<<(static_cast<unsigned long long>(N)); |
| | } |
| |
|
| | OutputBuffer &operator<<(int N) { |
| | return this->operator<<(static_cast<long long>(N)); |
| | } |
| |
|
| | OutputBuffer &operator<<(unsigned int N) { |
| | return this->operator<<(static_cast<unsigned long long>(N)); |
| | } |
| |
|
| | void insert(size_t Pos, const char *S, size_t N) { |
| | assert(Pos <= CurrentPosition); |
| | if (N == 0) |
| | return; |
| | grow(N); |
| | std::memmove(Buffer + Pos + N, Buffer + Pos, CurrentPosition - Pos); |
| | std::memcpy(Buffer + Pos, S, N); |
| | CurrentPosition += N; |
| | } |
| |
|
| | size_t getCurrentPosition() const { return CurrentPosition; } |
| | void setCurrentPosition(size_t NewPos) { CurrentPosition = NewPos; } |
| |
|
| | char back() const { |
| | assert(CurrentPosition); |
| | return Buffer[CurrentPosition - 1]; |
| | } |
| |
|
| | bool empty() const { return CurrentPosition == 0; } |
| |
|
| | char *getBuffer() { return Buffer; } |
| | char *getBufferEnd() { return Buffer + CurrentPosition - 1; } |
| | size_t getBufferCapacity() const { return BufferCapacity; } |
| | }; |
| |
|
| | template <class T> class ScopedOverride { |
| | T &Loc; |
| | T Original; |
| |
|
| | public: |
| | ScopedOverride(T &Loc_) : ScopedOverride(Loc_, Loc_) {} |
| |
|
| | ScopedOverride(T &Loc_, T NewVal) : Loc(Loc_), Original(Loc_) { |
| | Loc_ = std::move(NewVal); |
| | } |
| | ~ScopedOverride() { Loc = std::move(Original); } |
| |
|
| | ScopedOverride(const ScopedOverride &) = delete; |
| | ScopedOverride &operator=(const ScopedOverride &) = delete; |
| | }; |
| |
|
| | DEMANGLE_NAMESPACE_END |
| |
|
| | #endif |
| |
|