Skip to content

Commit 3ef05ef

Browse files
committed
Strings: Use StringSpan instead of StringView for StringFormat specifiers
1 parent 4451c6f commit 3ef05ef

File tree

3 files changed

+53
-46
lines changed

3 files changed

+53
-46
lines changed

Libraries/Strings/Internal/String.inl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
// SPDX-License-Identifier: MIT
33

44
#include "../../Strings/String.h"
5-
#include "../../Strings/StringFormat.h"
5+
6+
namespace SC
7+
{
8+
template <typename T>
9+
struct StringFormatterFor;
10+
}
611

712
struct SC::String::Internal
813
{

Libraries/Strings/Internal/StringFormat.inl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,22 @@ static bool formatSprintf(StringFormatOutput& data, const char (&formatSpecifier
3838
}
3939
#if SC_COMPILER_MSVC || SC_COMPILER_CLANG_CL
4040
#if SC_PLATFORM_64_BIT == 0
41-
bool StringFormatterFor<ssize_t>::format(StringFormatOutput& data, const StringView specifier, const long value)
41+
bool StringFormatterFor<ssize_t>::format(StringFormatOutput& data, const StringSpan specifier, const long value)
4242
{
4343
constexpr char formatSpecifier[] = "d";
4444
return formatSprintf(data, formatSpecifier, specifier, value);
4545
}
4646
#endif
4747
#else
4848
#if !SC_PLATFORM_LINUX
49-
bool StringFormatterFor<SC::size_t>::format(StringFormatOutput& data, const StringView specifier,
49+
bool StringFormatterFor<SC::size_t>::format(StringFormatOutput& data, const StringSpan specifier,
5050
const SC::size_t value)
5151
{
5252
constexpr char formatSpecifier[] = "zu";
5353
return formatSprintf(data, formatSpecifier, specifier, value);
5454
}
5555

56-
bool StringFormatterFor<SC::ssize_t>::format(StringFormatOutput& data, const StringView specifier,
56+
bool StringFormatterFor<SC::ssize_t>::format(StringFormatOutput& data, const StringSpan specifier,
5757
const SC::ssize_t value)
5858
{
5959
constexpr char formatSpecifier[] = "zd";
@@ -62,122 +62,122 @@ bool StringFormatterFor<SC::ssize_t>::format(StringFormatOutput& data, const Str
6262
#endif
6363
#endif
6464

65-
bool StringFormatterFor<SC::int64_t>::format(StringFormatOutput& data, const StringView specifier,
65+
bool StringFormatterFor<SC::int64_t>::format(StringFormatOutput& data, const StringSpan specifier,
6666
const SC::int64_t value)
6767
{
6868
constexpr char formatSpecifier[] = PRIi64;
6969
return formatSprintf(data, formatSpecifier, specifier, value);
7070
}
7171

72-
bool StringFormatterFor<SC::uint64_t>::format(StringFormatOutput& data, const StringView specifier,
72+
bool StringFormatterFor<SC::uint64_t>::format(StringFormatOutput& data, const StringSpan specifier,
7373
const SC::uint64_t value)
7474
{
7575
constexpr char formatSpecifier[] = PRIu64;
7676
return formatSprintf(data, formatSpecifier, specifier, value);
7777
}
7878

79-
bool StringFormatterFor<SC::int32_t>::format(StringFormatOutput& data, const StringView specifier,
79+
bool StringFormatterFor<SC::int32_t>::format(StringFormatOutput& data, const StringSpan specifier,
8080
const SC::int32_t value)
8181
{
8282
constexpr char formatSpecifier[] = "d";
8383
return formatSprintf(data, formatSpecifier, specifier, value);
8484
}
8585

86-
bool StringFormatterFor<SC::uint32_t>::format(StringFormatOutput& data, const StringView specifier,
86+
bool StringFormatterFor<SC::uint32_t>::format(StringFormatOutput& data, const StringSpan specifier,
8787
const SC::uint32_t value)
8888
{
8989
constexpr char formatSpecifier[] = "d";
9090
return formatSprintf(data, formatSpecifier, specifier, value);
9191
}
9292

93-
bool StringFormatterFor<SC::int16_t>::format(StringFormatOutput& data, const StringView specifier,
93+
bool StringFormatterFor<SC::int16_t>::format(StringFormatOutput& data, const StringSpan specifier,
9494
const SC::int16_t value)
9595
{
9696
return StringFormatterFor<SC::int32_t>::format(data, specifier, value);
9797
}
9898

99-
bool StringFormatterFor<SC::uint16_t>::format(StringFormatOutput& data, const StringView specifier,
99+
bool StringFormatterFor<SC::uint16_t>::format(StringFormatOutput& data, const StringSpan specifier,
100100
const SC::uint16_t value)
101101
{
102102
return StringFormatterFor<SC::uint32_t>::format(data, specifier, value);
103103
}
104-
bool StringFormatterFor<SC::int8_t>::format(StringFormatOutput& data, const StringView specifier,
104+
bool StringFormatterFor<SC::int8_t>::format(StringFormatOutput& data, const StringSpan specifier,
105105
const SC::int8_t value)
106106
{
107107
return StringFormatterFor<SC::int32_t>::format(data, specifier, value);
108108
}
109109

110-
bool StringFormatterFor<SC::uint8_t>::format(StringFormatOutput& data, const StringView specifier,
110+
bool StringFormatterFor<SC::uint8_t>::format(StringFormatOutput& data, const StringSpan specifier,
111111
const SC::uint8_t value)
112112
{
113113
return StringFormatterFor<SC::uint32_t>::format(data, specifier, value);
114114
}
115115

116-
bool StringFormatterFor<bool>::format(StringFormatOutput& data, const StringView specifier, const bool value)
116+
bool StringFormatterFor<bool>::format(StringFormatOutput& data, const StringSpan specifier, const bool value)
117117
{
118118
SC_COMPILER_UNUSED(specifier);
119119
return data.append(value ? "true"_a8 : "false"_a8);
120120
}
121121

122-
bool StringFormatterFor<float>::format(StringFormatOutput& data, StringView specifier, const float value)
122+
bool StringFormatterFor<float>::format(StringFormatOutput& data, const StringSpan specifier, const float value)
123123
{
124124
constexpr char formatSpecifier[] = "f";
125125
return formatSprintf(data, formatSpecifier, specifier, value);
126126
}
127127

128-
bool StringFormatterFor<double>::format(StringFormatOutput& data, const StringView specifier, const double value)
128+
bool StringFormatterFor<double>::format(StringFormatOutput& data, const StringSpan specifier, const double value)
129129
{
130130
constexpr char formatSpecifier[] = "f";
131131
return formatSprintf(data, formatSpecifier, specifier, value);
132132
}
133133

134-
bool StringFormatterFor<char>::format(StringFormatOutput& data, const StringView specifier, const char value)
134+
bool StringFormatterFor<char>::format(StringFormatOutput& data, const StringSpan specifier, const char value)
135135
{
136136
SC_COMPILER_UNUSED(specifier);
137137
return data.append(StringView({&value, sizeof(value)}, false, StringEncoding::Ascii));
138138
}
139139

140-
bool StringFormatterFor<const char*>::format(StringFormatOutput& data, const StringView specifier, const char* value)
140+
bool StringFormatterFor<const char*>::format(StringFormatOutput& data, const StringSpan specifier, const char* value)
141141
{
142142
SC_COMPILER_UNUSED(specifier);
143143
return data.append(StringView::fromNullTerminated(value, StringEncoding::Ascii));
144144
}
145145

146-
bool StringFormatterFor<const void*>::format(StringFormatOutput& data, const StringView specifier, const void* value)
146+
bool StringFormatterFor<const void*>::format(StringFormatOutput& data, const StringSpan specifier, const void* value)
147147
{
148148
constexpr char formatSpecifier[] = "p";
149149
return formatSprintf(data, formatSpecifier, specifier, value);
150150
}
151151

152152
#if SC_PLATFORM_WINDOWS
153-
bool StringFormatterFor<wchar_t>::format(StringFormatOutput& data, const StringView specifier, const wchar_t value)
153+
bool StringFormatterFor<wchar_t>::format(StringFormatOutput& data, const StringSpan specifier, const wchar_t value)
154154
{
155155
SC_COMPILER_UNUSED(specifier);
156156
return data.append(StringView({&value, 1}, false));
157157
}
158158

159-
bool StringFormatterFor<const wchar_t*>::format(StringFormatOutput& data, const StringView specifier,
159+
bool StringFormatterFor<const wchar_t*>::format(StringFormatOutput& data, const StringSpan specifier,
160160
const wchar_t* value)
161161
{
162162
SC_COMPILER_UNUSED(specifier);
163163
return data.append(StringView({value, wcslen(value)}, true));
164164
}
165165
#endif
166166

167-
bool StringFormatterFor<StringView>::format(StringFormatOutput& data, const StringView specifier,
167+
bool StringFormatterFor<StringView>::format(StringFormatOutput& data, const StringSpan specifier,
168168
const StringView value)
169169
{
170170
SC_COMPILER_UNUSED(specifier);
171171
return data.append(value);
172172
}
173173

174-
bool StringFormatterFor<StringSpan>::format(StringFormatOutput& data, const StringView specifier, StringSpan value)
174+
bool StringFormatterFor<StringSpan>::format(StringFormatOutput& data, const StringSpan specifier, StringSpan value)
175175
{
176176
SC_COMPILER_UNUSED(specifier);
177177
return data.append(value);
178178
}
179179

180-
bool StringFormatterFor<StringPath>::format(StringFormatOutput& data, const StringView specifier, const StringPath& str)
180+
bool StringFormatterFor<StringPath>::format(StringFormatOutput& data, const StringSpan specifier, const StringPath& str)
181181
{
182182
return StringFormatterFor<StringSpan>::format(data, specifier, str.view());
183183
}

Libraries/Strings/StringFormat.h

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -229,39 +229,41 @@ bool SC::StringFormat<RangeIterator>::format(StringFormatOutput& data, StringVie
229229
namespace SC
230230
{
231231
// clang-format off
232-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<float> {static bool format(StringFormatOutput&, const StringView, const float);};
233-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<double> {static bool format(StringFormatOutput&, const StringView, const double);};
232+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<float> {static bool format(StringFormatOutput&, const StringSpan, const float);};
233+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<double> {static bool format(StringFormatOutput&, const StringSpan, const double);};
234234
#if SC_COMPILER_MSVC || SC_COMPILER_CLANG_CL
235235
#if SC_PLATFORM_64_BIT == 0
236-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::ssize_t> {static bool format(StringFormatOutput&, const StringView, const SC::ssize_t);};
236+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::ssize_t> {static bool format(StringFormatOutput&, const StringSpan, const SC::ssize_t);};
237237
#endif
238238
#else
239239
#if !SC_PLATFORM_LINUX
240-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::size_t> {static bool format(StringFormatOutput&, const StringView, const SC::size_t);};
241-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::ssize_t> {static bool format(StringFormatOutput&, const StringView, const SC::ssize_t);};
240+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::size_t> {static bool format(StringFormatOutput&, const StringSpan, const SC::size_t);};
241+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::ssize_t> {static bool format(StringFormatOutput&, const StringSpan, const SC::ssize_t);};
242242
#endif
243243
#endif
244-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::int64_t> {static bool format(StringFormatOutput&, const StringView, const SC::int64_t);};
245-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::uint64_t> {static bool format(StringFormatOutput&, const StringView, const SC::uint64_t);};
246-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::int32_t> {static bool format(StringFormatOutput&, const StringView, const SC::int32_t);};
247-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::uint32_t> {static bool format(StringFormatOutput&, const StringView, const SC::uint32_t);};
248-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::int16_t> {static bool format(StringFormatOutput&, const StringView, const SC::int16_t);};
249-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::uint16_t> {static bool format(StringFormatOutput&, const StringView, const SC::uint16_t);};
250-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::int8_t> {static bool format(StringFormatOutput&, const StringView, const SC::int8_t);};
251-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::uint8_t> {static bool format(StringFormatOutput&, const StringView, const SC::uint8_t);};
252-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<char> {static bool format(StringFormatOutput&, const StringView, const char);};
253-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<bool> {static bool format(StringFormatOutput&, const StringView, const bool);};
254-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<StringView> {static bool format(StringFormatOutput&, const StringView, const StringView);};
255-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<const char*> {static bool format(StringFormatOutput&, const StringView, const char*);};
256-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<const void*> {static bool format(StringFormatOutput&, const StringView, const void*);};
244+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::int64_t> {static bool format(StringFormatOutput&, const StringSpan, const SC::int64_t);};
245+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::uint64_t> {static bool format(StringFormatOutput&, const StringSpan, const SC::uint64_t);};
246+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::int32_t> {static bool format(StringFormatOutput&, const StringSpan, const SC::int32_t);};
247+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::uint32_t> {static bool format(StringFormatOutput&, const StringSpan, const SC::uint32_t);};
248+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::int16_t> {static bool format(StringFormatOutput&, const StringSpan, const SC::int16_t);};
249+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::uint16_t> {static bool format(StringFormatOutput&, const StringSpan, const SC::uint16_t);};
250+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::int8_t> {static bool format(StringFormatOutput&, const StringSpan, const SC::int8_t);};
251+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<SC::uint8_t> {static bool format(StringFormatOutput&, const StringSpan, const SC::uint8_t);};
252+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<char> {static bool format(StringFormatOutput&, const StringSpan, const char);};
253+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<bool> {static bool format(StringFormatOutput&, const StringSpan, const bool);};
254+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<StringView> {static bool format(StringFormatOutput&, const StringSpan, const StringView);};
255+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<const char*> {static bool format(StringFormatOutput&, const StringSpan, const char*);};
256+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<const void*> {static bool format(StringFormatOutput&, const StringSpan, const void*);};
257257
#if SC_PLATFORM_WINDOWS
258-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<wchar_t> {static bool format(StringFormatOutput&, const StringView, const wchar_t);};
259-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<const wchar_t*> {static bool format(StringFormatOutput&, const StringView, const wchar_t*);};
258+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<wchar_t> {static bool format(StringFormatOutput&, const StringSpan, const wchar_t);};
259+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<const wchar_t*> {static bool format(StringFormatOutput&, const StringSpan, const wchar_t*);};
260+
#endif
261+
#if !defined(SC_STRING_SPAN_FORMATTER_DEFINED)
262+
#define SC_STRING_SPAN_FORMATTER_DEFINED 1
263+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<StringSpan> {static bool format(StringFormatOutput&, const StringSpan, const StringSpan);};
260264
#endif
261-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<StringSpan> {static bool format(StringFormatOutput&, const StringView, const StringSpan);};
262-
263265
struct StringPath;
264-
template <> struct SC_COMPILER_EXPORT StringFormatterFor<StringPath> {static bool format(StringFormatOutput&, const StringView, const StringPath&);};
266+
template <> struct SC_COMPILER_EXPORT StringFormatterFor<StringPath> {static bool format(StringFormatOutput&, const StringSpan, const StringPath&);};
265267

266268
// clang-format on
267269

0 commit comments

Comments
 (0)