Skip to content

Commit 3823c4d

Browse files
committed
Change BasicClassifier to be templated on character types
And rename it from BasicStringClassifier to BasicClassifier.
1 parent 2e72c81 commit 3823c4d

11 files changed

+90
-91
lines changed

build/win/libfly/libfly.vcxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,12 @@
228228
<ClInclude Include="..\..\..\fly\types\numeric\detail\literal_parser.hpp" />
229229
<ClInclude Include="..\..\..\fly\types\numeric\endian.hpp" />
230230
<ClInclude Include="..\..\..\fly\types\numeric\literals.hpp" />
231+
<ClInclude Include="..\..\..\fly\types\string\detail\classifier.hpp" />
231232
<ClInclude Include="..\..\..\fly\types\string\detail\format_context.hpp" />
232233
<ClInclude Include="..\..\..\fly\types\string\detail\format_parameters.hpp" />
233234
<ClInclude Include="..\..\..\fly\types\string\detail\format_specifier.hpp" />
234235
<ClInclude Include="..\..\..\fly\types\string\detail\format_string.hpp" />
235236
<ClInclude Include="..\..\..\fly\types\string\detail\stream_util.hpp" />
236-
<ClInclude Include="..\..\..\fly\types\string\detail\string_classifier.hpp" />
237237
<ClInclude Include="..\..\..\fly\types\string\detail\string_converter.hpp" />
238238
<ClInclude Include="..\..\..\fly\types\string\detail\string_traits.hpp" />
239239
<ClInclude Include="..\..\..\fly\types\string\detail\string_unicode.hpp" />

build/win/libfly/libfly.vcxproj.filters

+3-3
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@
289289
<ClInclude Include="..\..\..\fly\types\string\string_formatters.hpp">
290290
<Filter>types\string</Filter>
291291
</ClInclude>
292+
<ClInclude Include="..\..\..\fly\types\string\detail\classifier.hpp">
293+
<Filter>types\string\detail</Filter>
294+
</ClInclude>
292295
<ClInclude Include="..\..\..\fly\types\string\detail\format_context.hpp">
293296
<Filter>types\string\detail</Filter>
294297
</ClInclude>
@@ -304,9 +307,6 @@
304307
<ClInclude Include="..\..\..\fly\types\string\detail\stream_util.hpp">
305308
<Filter>types\string\detail</Filter>
306309
</ClInclude>
307-
<ClInclude Include="..\..\..\fly\types\string\detail\string_classifier.hpp">
308-
<Filter>types\string\detail</Filter>
309-
</ClInclude>
310310
<ClInclude Include="..\..\..\fly\types\string\detail\string_converter.hpp">
311311
<Filter>types\string\detail</Filter>
312312
</ClInclude>

build/win/libfly_unit_tests/libfly_unit_tests.vcxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@
255255
<ClCompile Include="..\..\..\test\types\json\json_traits.cpp" />
256256
<ClCompile Include="..\..\..\test\types\numeric\endian.cpp" />
257257
<ClCompile Include="..\..\..\test\types\numeric\literals.cpp" />
258+
<ClCompile Include="..\..\..\test\types\string\classifier.cpp" />
258259
<ClCompile Include="..\..\..\test\types\string\format_parameters.cpp" />
259260
<ClCompile Include="..\..\..\test\types\string\format_string.cpp" />
260261
<ClCompile Include="..\..\..\test\types\string\lexer.cpp" />
261262
<ClCompile Include="..\..\..\test\types\string\string.cpp" />
262-
<ClCompile Include="..\..\..\test\types\string\string_classifier.cpp" />
263263
<ClCompile Include="..\..\..\test\types\string\string_converter.cpp" />
264264
<ClCompile Include="..\..\..\test\types\string\string_format.cpp" />
265265
<ClCompile Include="..\..\..\test\types\string\string_traits.cpp" />

build/win/libfly_unit_tests/libfly_unit_tests.vcxproj.filters

+3-3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@
158158
<ClCompile Include="..\..\..\test\types\numeric\literals.cpp">
159159
<Filter>types\numeric</Filter>
160160
</ClCompile>
161+
<ClCompile Include="..\..\..\test\types\string\classifier.cpp">
162+
<Filter>types\string</Filter>
163+
</ClCompile>
161164
<ClCompile Include="..\..\..\test\types\string\format_parameters.cpp">
162165
<Filter>types\string</Filter>
163166
</ClCompile>
@@ -170,9 +173,6 @@
170173
<ClCompile Include="..\..\..\test\types\string\string.cpp">
171174
<Filter>types\string</Filter>
172175
</ClCompile>
173-
<ClCompile Include="..\..\..\test\types\string\string_classifier.cpp">
174-
<Filter>types\string</Filter>
175-
</ClCompile>
176176
<ClCompile Include="..\..\..\test\types\string\string_converter.cpp">
177177
<Filter>types\string</Filter>
178178
</ClCompile>

fly/types/string/detail/string_classifier.hpp fly/types/string/detail/classifier.hpp

+59-58
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ namespace fly::detail {
1414
* @author Timothy Flynn ([email protected])
1515
* @version January 3, 2021
1616
*/
17-
template <typename StringType>
18-
class BasicStringClassifier
17+
template <typename CharType>
18+
class BasicClassifier
1919
{
20-
using traits = detail::BasicStringTraits<StringType>;
20+
using string_type = std::basic_string<CharType>;
21+
22+
using traits = detail::BasicStringTraits<string_type>;
2123
using size_type = typename traits::size_type;
22-
using char_type = typename traits::char_type;
2324
using view_type = typename traits::view_type;
2425
using int_type = typename traits::int_type;
2526

@@ -45,7 +46,7 @@ class BasicStringClassifier
4546
* @return The length of the character array.
4647
*/
4748
template <std::size_t N>
48-
static constexpr size_type size(const char_type (&value)[N]);
49+
static constexpr size_type size(const CharType (&value)[N]);
4950

5051
/**
5152
* Checks if the given character is an alphabetic character as classified by the default C
@@ -59,7 +60,7 @@ class BasicStringClassifier
5960
*
6061
* @return True if the character is an alphabetic character.
6162
*/
62-
static constexpr bool is_alpha(char_type ch);
63+
static constexpr bool is_alpha(CharType ch);
6364

6465
/**
6566
* Checks if the given character is an upper-case alphabetic character as classified by the
@@ -73,7 +74,7 @@ class BasicStringClassifier
7374
*
7475
* @return True if the character is an alphabetic character.
7576
*/
76-
static constexpr bool is_upper(char_type ch);
77+
static constexpr bool is_upper(CharType ch);
7778

7879
/**
7980
* Checks if the given character is a lower-case alphabetic character as classified by the
@@ -87,7 +88,7 @@ class BasicStringClassifier
8788
*
8889
* @return True if the character is an alphabetic character.
8990
*/
90-
static constexpr bool is_lower(char_type ch);
91+
static constexpr bool is_lower(CharType ch);
9192

9293
/**
9394
* Converts the given character to an upper-case alphabetic character as classified by the
@@ -101,7 +102,7 @@ class BasicStringClassifier
101102
*
102103
* @return The converted character.
103104
*/
104-
static constexpr char_type to_upper(char_type ch);
105+
static constexpr CharType to_upper(CharType ch);
105106

106107
/**
107108
* Converts the given character to a lower-case alphabetic character as classified by the
@@ -115,7 +116,7 @@ class BasicStringClassifier
115116
*
116117
* @return The converted character.
117118
*/
118-
static constexpr char_type to_lower(char_type ch);
119+
static constexpr CharType to_lower(CharType ch);
119120

120121
/**
121122
* Checks if the given character is a decimal digit character.
@@ -128,7 +129,7 @@ class BasicStringClassifier
128129
*
129130
* @return True if the character is a decimal digit character.
130131
*/
131-
static constexpr bool is_digit(char_type ch);
132+
static constexpr bool is_digit(CharType ch);
132133

133134
/**
134135
* Checks if the given character is a hexadecimal digit character.
@@ -141,7 +142,7 @@ class BasicStringClassifier
141142
*
142143
* @return True if the character is a hexadecimal digit character.
143144
*/
144-
static constexpr bool is_x_digit(char_type ch);
145+
static constexpr bool is_x_digit(CharType ch);
145146

146147
/**
147148
* Checks if the given character is a whitespace character as classified by the default C
@@ -155,7 +156,7 @@ class BasicStringClassifier
155156
*
156157
* @return True if the character is a whitespace character.
157158
*/
158-
static constexpr bool is_space(char_type ch);
159+
static constexpr bool is_space(CharType ch);
159160

160161
private:
161162
/**
@@ -166,125 +167,125 @@ class BasicStringClassifier
166167
*
167168
* @return The modified character.
168169
*/
169-
static constexpr char_type unify_az_characters(char_type ch);
170-
171-
static constexpr const auto s_null_terminator = FLY_CHR(char_type, '\0');
172-
static constexpr const auto s_zero = FLY_CHR(char_type, '0');
173-
static constexpr const auto s_upper_a = FLY_CHR(char_type, 'A');
174-
static constexpr const auto s_upper_z = FLY_CHR(char_type, 'Z');
175-
static constexpr const auto s_upper_f = FLY_CHR(char_type, 'F');
176-
static constexpr const auto s_lower_a = FLY_CHR(char_type, 'a');
177-
static constexpr const auto s_lower_z = FLY_CHR(char_type, 'z');
178-
static constexpr const auto s_space = FLY_CHR(char_type, ' ');
179-
static constexpr const auto s_form_feed = FLY_CHR(char_type, '\f');
180-
static constexpr const auto s_line_feed = FLY_CHR(char_type, '\n');
181-
static constexpr const auto s_carriage_return = FLY_CHR(char_type, '\r');
182-
static constexpr const auto s_horizontal_tab = FLY_CHR(char_type, '\t');
183-
static constexpr const auto s_vertical_tab = FLY_CHR(char_type, '\v');
170+
static constexpr CharType unify_az_characters(CharType ch);
171+
172+
static constexpr const auto s_null_terminator = FLY_CHR(CharType, '\0');
173+
static constexpr const auto s_zero = FLY_CHR(CharType, '0');
174+
static constexpr const auto s_upper_a = FLY_CHR(CharType, 'A');
175+
static constexpr const auto s_upper_z = FLY_CHR(CharType, 'Z');
176+
static constexpr const auto s_upper_f = FLY_CHR(CharType, 'F');
177+
static constexpr const auto s_lower_a = FLY_CHR(CharType, 'a');
178+
static constexpr const auto s_lower_z = FLY_CHR(CharType, 'z');
179+
static constexpr const auto s_space = FLY_CHR(CharType, ' ');
180+
static constexpr const auto s_form_feed = FLY_CHR(CharType, '\f');
181+
static constexpr const auto s_line_feed = FLY_CHR(CharType, '\n');
182+
static constexpr const auto s_carriage_return = FLY_CHR(CharType, '\r');
183+
static constexpr const auto s_horizontal_tab = FLY_CHR(CharType, '\t');
184+
static constexpr const auto s_vertical_tab = FLY_CHR(CharType, '\v');
184185

185186
static constexpr const auto s_case_bit = static_cast<int_type>(0x20);
186187
static constexpr const auto s_case_mask = static_cast<int_type>(~s_case_bit);
187188
};
188189

189190
//==================================================================================================
190-
template <typename StringType>
191+
template <typename CharType>
191192
template <typename T, enable_if<detail::is_like_supported_string<T>>>
192-
constexpr inline auto BasicStringClassifier<StringType>::size(T &&value) -> size_type
193+
constexpr inline auto BasicClassifier<CharType>::size(T &&value) -> size_type
193194
{
194-
if constexpr (any_same_v<T, StringType, view_type>)
195+
using U = std::remove_cvref_t<T>;
196+
197+
if constexpr (std::is_array_v<U> || std::is_pointer_v<U>)
195198
{
196-
return value.size();
199+
return std::char_traits<CharType>::length(std::forward<T>(value));
197200
}
198201
else
199202
{
200-
return std::char_traits<char_type>::length(std::forward<T>(value));
203+
return value.size();
201204
}
202205
}
203206

204207
//==================================================================================================
205-
template <typename StringType>
208+
template <typename CharType>
206209
template <std::size_t N>
207-
constexpr inline auto BasicStringClassifier<StringType>::size(const char_type (&value)[N])
208-
-> size_type
210+
constexpr inline auto BasicClassifier<CharType>::size(const CharType (&value)[N]) -> size_type
209211
{
210212
static_assert(N > 0, "Character arrays must have non-zero size");
211213
return N - ((value[N - 1] == s_null_terminator) ? 1 : 0);
212214
}
213215

214216
//==================================================================================================
215-
template <typename StringType>
216-
constexpr inline bool BasicStringClassifier<StringType>::is_alpha(char_type ch)
217+
template <typename CharType>
218+
constexpr inline bool BasicClassifier<CharType>::is_alpha(CharType ch)
217219
{
218220
return is_upper(unify_az_characters(ch));
219221
}
220222

221223
//==================================================================================================
222-
template <typename StringType>
223-
constexpr inline bool BasicStringClassifier<StringType>::is_upper(char_type ch)
224+
template <typename CharType>
225+
constexpr inline bool BasicClassifier<CharType>::is_upper(CharType ch)
224226
{
225227
return (ch >= s_upper_a) && (ch <= s_upper_z);
226228
}
227229

228230
//==================================================================================================
229-
template <typename StringType>
230-
constexpr inline bool BasicStringClassifier<StringType>::is_lower(char_type ch)
231+
template <typename CharType>
232+
constexpr inline bool BasicClassifier<CharType>::is_lower(CharType ch)
231233
{
232234
return (ch >= s_lower_a) && (ch <= s_lower_z);
233235
}
234236

235237
//==================================================================================================
236-
template <typename StringType>
237-
constexpr inline auto BasicStringClassifier<StringType>::to_upper(char_type ch) -> char_type
238+
template <typename CharType>
239+
constexpr inline CharType BasicClassifier<CharType>::to_upper(CharType ch)
238240
{
239241
if (is_lower(ch))
240242
{
241-
ch = static_cast<char_type>(static_cast<int_type>(ch) & s_case_mask);
243+
ch = static_cast<CharType>(static_cast<int_type>(ch) & s_case_mask);
242244
}
243245

244246
return ch;
245247
}
246248

247249
//==================================================================================================
248-
template <typename StringType>
249-
constexpr inline auto BasicStringClassifier<StringType>::to_lower(char_type ch) -> char_type
250+
template <typename CharType>
251+
constexpr inline CharType BasicClassifier<CharType>::to_lower(CharType ch)
250252
{
251253
if (is_upper(ch))
252254
{
253-
ch = static_cast<char_type>(static_cast<int_type>(ch) | s_case_bit);
255+
ch = static_cast<CharType>(static_cast<int_type>(ch) | s_case_bit);
254256
}
255257

256258
return ch;
257259
}
258260

259261
//==================================================================================================
260-
template <typename StringType>
261-
constexpr inline bool BasicStringClassifier<StringType>::is_digit(char_type ch)
262+
template <typename CharType>
263+
constexpr inline bool BasicClassifier<CharType>::is_digit(CharType ch)
262264
{
263265
return (ch ^ s_zero) < 10;
264266
}
265267

266268
//==================================================================================================
267-
template <typename StringType>
268-
constexpr inline bool BasicStringClassifier<StringType>::is_x_digit(char_type ch)
269+
template <typename CharType>
270+
constexpr inline bool BasicClassifier<CharType>::is_x_digit(CharType ch)
269271
{
270272
const auto alpha = unify_az_characters(ch);
271273
return is_digit(ch) || ((alpha >= s_upper_a) && (alpha <= s_upper_f));
272274
}
273275

274276
//==================================================================================================
275-
template <typename StringType>
276-
constexpr inline bool BasicStringClassifier<StringType>::is_space(char_type ch)
277+
template <typename CharType>
278+
constexpr inline bool BasicClassifier<CharType>::is_space(CharType ch)
277279
{
278280
return (ch == s_space) || (ch == s_form_feed) || (ch == s_line_feed) ||
279281
(ch == s_carriage_return) || (ch == s_horizontal_tab) || (ch == s_vertical_tab);
280282
}
281283

282284
//==================================================================================================
283-
template <typename StringType>
284-
constexpr inline auto BasicStringClassifier<StringType>::unify_az_characters(char_type ch)
285-
-> char_type
285+
template <typename CharType>
286+
constexpr inline CharType BasicClassifier<CharType>::unify_az_characters(CharType ch)
286287
{
287-
return static_cast<char_type>(static_cast<int_type>(ch) & s_case_mask);
288+
return static_cast<CharType>(static_cast<int_type>(ch) & s_case_mask);
288289
}
289290

290291
} // namespace fly::detail

fly/types/string/detail/format_parameters.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "fly/types/string/detail/string_classifier.hpp"
3+
#include "fly/types/string/detail/classifier.hpp"
44
#include "fly/types/string/detail/string_traits.hpp"
55
#include "fly/types/string/string_formatters.hpp"
66

@@ -367,7 +367,7 @@ constexpr inline BasicFormatParameter<FormatContext>::BasicFormatParameter(const
367367

368368
if constexpr (std::is_array_v<U> || std::is_pointer_v<U>)
369369
{
370-
view = view_type(value, fly::detail::BasicStringClassifier<string_type>::size(value));
370+
view = view_type(value, BasicClassifier<char_type>::size(value));
371371
}
372372
else
373373
{

fly/types/string/detail/format_specifier.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "fly/types/string/detail/string_classifier.hpp"
3+
#include "fly/types/string/detail/classifier.hpp"
44
#include "fly/types/string/lexer.hpp"
55
#include "fly/types/string/literals.hpp"
66

@@ -587,16 +587,14 @@ constexpr void BasicFormatSpecifier<CharType>::parse_type(
587587
FormatString &format,
588588
typename FormatString::ParameterType parameter_type)
589589
{
590-
using classifier = BasicStringClassifier<std::basic_string<CharType>>;
591-
592590
if (auto ch = format.m_lexer.peek(); ch)
593591
{
594592
if (auto type = type_of(ch.value()); type)
595593
{
596594
m_type = type.value();
597595
format.m_lexer.consume();
598596

599-
if (classifier::is_upper(ch.value()))
597+
if (BasicClassifier<CharType>::is_upper(ch.value()))
600598
{
601599
m_case = Case::Upper;
602600
}

fly/types/string/lexer.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "fly/types/string/detail/string_classifier.hpp"
3+
#include "fly/types/string/detail/classifier.hpp"
44
#include "fly/types/string/detail/string_traits.hpp"
55
#include "fly/types/string/literals.hpp"
66

@@ -31,7 +31,7 @@ class BasicLexer
3131
using string_type = std::basic_string<CharType>;
3232

3333
using traits = detail::BasicStringTraits<string_type>;
34-
using classifier = detail::BasicStringClassifier<string_type>;
34+
using classifier = detail::BasicClassifier<CharType>;
3535
using view_type = typename traits::view_type;
3636

3737
public:

0 commit comments

Comments
 (0)