diff options
| author | Stefan Boberg <[email protected]> | 2026-04-16 14:22:09 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2026-04-16 14:22:09 +0200 |
| commit | db616a2ede14670423b7b2b28aa36c33cabb030e (patch) | |
| tree | ced9c440544f12961207a0465590b6df3d0609e9 /src/zencore/string.cpp | |
| parent | 5.8.5-pre1 (diff) | |
| download | archived-zen-db616a2ede14670423b7b2b28aa36c33cabb030e.tar.xz archived-zen-db616a2ede14670423b7b2b28aa36c33cabb030e.zip | |
Add reduce-allocs skill and string builder infrastructure (#937)
Adds infrastructure for reducing short-lived heap allocations, to be applied across the codebase in follow-up PRs.
- **`reduce-allocs` Claude Code skill** — reviews code for unnecessary heap allocations and suggests fixes using stack-friendly patterns (`ExtendableStringBuilder`, `eastl::fixed_vector`, `TRefCounted`, etc.)
- **`TransparentStringHash`** (`zencore/hashutils.h`) — enables `std::string_view` lookups on `std::string`-keyed `unordered_map` without allocating a temporary string (C++20 heterogeneous lookup via `is_transparent`)
- **`AppendPaddedInt()`** and **`AppendFill()`** on `StringBuilderBase` (`zencore/string.h`) — zero-padded integer formatting and repeated-character fills without going through `fmt::format`
- **`StringBuilderAppender`** output iterator adapter — allows `fmt::format_to` to write directly into a `StringBuilderBase`
Diffstat (limited to 'src/zencore/string.cpp')
| -rw-r--r-- | src/zencore/string.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/zencore/string.cpp b/src/zencore/string.cpp index df7d71250..44f78aa75 100644 --- a/src/zencore/string.cpp +++ b/src/zencore/string.cpp @@ -543,6 +543,40 @@ template class StringBuilderImpl<wchar_t>; ////////////////////////////////////////////////////////////////////////// void +StringBuilderBase::AppendPaddedInt(int64_t Value, int MinWidth) +{ + char Buf[24]; + char* End = Buf + sizeof(Buf); + char* Ptr = End; + bool Negative = Value < 0; + uint64_t Abs = Negative ? uint64_t(-Value) : uint64_t(Value); + do + { + *--Ptr = '0' + char(Abs % 10); + Abs /= 10; + } while (Abs > 0); + while ((End - Ptr) < MinWidth) + { + *--Ptr = '0'; + } + if (Negative) + { + *--Ptr = '-'; + } + AppendRange(Ptr, End); +} + +void +StringBuilderBase::AppendFill(char C, size_t Count) +{ + EnsureCapacity(Count); + std::memset(m_CurPos, C, Count); + m_CurPos += Count; +} + +////////////////////////////////////////////////////////////////////////// + +void UrlDecode(std::string_view InUrl, StringBuilderBase& OutUrl) { std::string_view::size_type i = 0; |