1 // Copyright (c) 2016 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 // Efficient integer->string conversion and string concatenation. The
22 // hexadecimal conversion may optionally have leading zeros. Other ways to
23 // convert integers to strings in C++ suffer these drawbacks:
25 // * std::stringstream: Inefficient, even more so than stdio.
27 // * std::to_string: No hexadecimal output, tends to use heap allocation, not
28 // supported on Cygwin.
30 // * stdio routines: Requires parsing a format string (inefficient). The
31 // caller *must* know how large the content is for correctness. The
32 // string-printf functions are extremely inconsistent on Windows. In
33 // particular, 64-bit integers, wide strings, and return values are
36 // StringBuilderTest.cc is a standalone program that tests this header.
38 #ifndef WINPTY_STRING_BUILDER_H
39 #define WINPTY_STRING_BUILDER_H
43 #include <type_traits>
45 #ifdef STRING_BUILDER_TESTING
47 #define STRING_BUILDER_CHECK(cond) assert(cond)
49 #define STRING_BUILDER_CHECK(cond)
50 #endif // STRING_BUILDER_TESTING
52 #include "WinptyAssert.h"
54 template <typename C, size_t sz>
56 std::array<C, sz> m_array;
60 const C *c_str() const { return m_array.data() + m_offset; }
61 const C *data() const { return m_array.data() + m_offset; }
62 size_t size() const { return m_size; }
63 std::basic_string<C> str() const {
64 return std::basic_string<C>(data(), m_size);
69 // Disable an MSVC /SDL error that forbids unsigned negation. Signed negation
70 // invokes undefined behavior for INTxx_MIN, so unsigned negation is simpler to
71 // reason about. (We assume twos-complement in any case.)
72 #define STRING_BUILDER_ALLOW_UNSIGNED_NEGATE(x) \
74 __pragma(warning(push)) \
75 __pragma(warning(disable:4146)) \
77 __pragma(warning(pop)) \
80 #define STRING_BUILDER_ALLOW_UNSIGNED_NEGATE(x) (x)
83 // Formats an integer as decimal without leading zeros.
84 template <typename C, typename I>
85 ValueString<C, sizeof(I) * 3 + 1 + 1> gdecOfInt(const I value) {
86 typedef typename std::make_unsigned<I>::type U;
87 auto unsValue = static_cast<U>(value);
88 const bool isNegative = (value < 0);
90 unsValue = STRING_BUILDER_ALLOW_UNSIGNED_NEGATE(-unsValue);
92 decltype(gdecOfInt<C, I>(value)) out;
93 auto &arr = out.m_array;
94 C *const endp = arr.data() + arr.size();
97 STRING_BUILDER_CHECK(outp >= arr.data());
99 const int digit = unsValue % 10;
101 *(--outp) = '0' + digit;
102 STRING_BUILDER_CHECK(outp >= arr.data());
103 } while (unsValue != 0);
106 STRING_BUILDER_CHECK(outp >= arr.data());
108 out.m_offset = outp - arr.data();
109 out.m_size = endp - outp - 1;
113 template <typename I> decltype(gdecOfInt<char, I>(0)) decOfInt(I i) {
114 return gdecOfInt<char>(i);
117 template <typename I> decltype(gdecOfInt<wchar_t, I>(0)) wdecOfInt(I i) {
118 return gdecOfInt<wchar_t>(i);
121 // Formats an integer as hexadecimal, with or without leading zeros.
122 template <typename C, bool leadingZeros=false, typename I>
123 ValueString<C, sizeof(I) * 2 + 1> ghexOfInt(const I value) {
124 typedef typename std::make_unsigned<I>::type U;
125 const auto unsValue = static_cast<U>(value);
126 static const C hex[16] = {'0','1','2','3','4','5','6','7',
127 '8','9','a','b','c','d','e','f'};
128 decltype(ghexOfInt<C, leadingZeros, I>(value)) out;
129 auto &arr = out.m_array;
130 C *outp = arr.data();
132 int shift = sizeof(I) * 8 - 4;
133 const int len = sizeof(I) * 2;
135 for (; inIndex < len - 1; ++inIndex, shift -= 4) {
136 STRING_BUILDER_CHECK(shift >= 0 && shift < sizeof(unsValue) * 8);
137 const int digit = (unsValue >> shift) & 0xF;
143 for (; inIndex < len; ++inIndex, shift -= 4) {
144 const int digit = (unsValue >> shift) & 0xF;
145 *(outp++) = hex[digit];
146 STRING_BUILDER_CHECK(outp <= arr.data() + arr.size());
149 STRING_BUILDER_CHECK(outp <= arr.data() + arr.size());
151 out.m_size = outp - arr.data() - 1;
155 template <bool leadingZeros=false, typename I>
156 decltype(ghexOfInt<char, leadingZeros, I>(0)) hexOfInt(I i) {
157 return ghexOfInt<char, leadingZeros, I>(i);
160 template <bool leadingZeros=false, typename I>
161 decltype(ghexOfInt<wchar_t, leadingZeros, I>(0)) whexOfInt(I i) {
162 return ghexOfInt<wchar_t, leadingZeros, I>(i);
165 template <typename C>
166 class GStringBuilder {
168 typedef std::basic_string<C> StringType;
171 GStringBuilder(size_t capacity) {
172 m_out.reserve(capacity);
175 GStringBuilder &operator<<(C ch) { m_out.push_back(ch); return *this; }
176 GStringBuilder &operator<<(const C *str) { m_out.append(str); return *this; }
177 GStringBuilder &operator<<(const StringType &str) { m_out.append(str); return *this; }
180 GStringBuilder &operator<<(const ValueString<C, sz> &str) {
181 m_out.append(str.data(), str.size());
186 // Forbid output of char/wchar_t for GStringBuilder if the type doesn't
187 // exactly match the builder element type. The code still allows
188 // signed char and unsigned char, but I'm a little worried about what
189 // happens if a user tries to output int8_t or uint8_t.
190 template <typename P>
191 typename std::enable_if<
192 (std::is_same<P, char>::value || std::is_same<P, wchar_t>::value) &&
193 !std::is_same<C, P>::value, GStringBuilder&>::type
195 ASSERT(false && "Method was not supposed to be reachable.");
200 GStringBuilder &operator<<(short i) { return *this << gdecOfInt<C>(i); }
201 GStringBuilder &operator<<(unsigned short i) { return *this << gdecOfInt<C>(i); }
202 GStringBuilder &operator<<(int i) { return *this << gdecOfInt<C>(i); }
203 GStringBuilder &operator<<(unsigned int i) { return *this << gdecOfInt<C>(i); }
204 GStringBuilder &operator<<(long i) { return *this << gdecOfInt<C>(i); }
205 GStringBuilder &operator<<(unsigned long i) { return *this << gdecOfInt<C>(i); }
206 GStringBuilder &operator<<(long long i) { return *this << gdecOfInt<C>(i); }
207 GStringBuilder &operator<<(unsigned long long i) { return *this << gdecOfInt<C>(i); }
209 GStringBuilder &operator<<(const void *p) {
210 m_out.push_back(static_cast<C>('0'));
211 m_out.push_back(static_cast<C>('x'));
212 *this << ghexOfInt<C>(reinterpret_cast<uintptr_t>(p));
216 StringType str() { return m_out; }
217 StringType str_moved() { return std::move(m_out); }
218 const C *c_str() const { return m_out.c_str(); }
224 typedef GStringBuilder<char> StringBuilder;
225 typedef GStringBuilder<wchar_t> WStringBuilder;
227 #endif // WINPTY_STRING_BUILDER_H