Skip to content

Commit ea61ada

Browse files
committed
Change BasicLexer to be templated on character types
And rename it from BasicStringLexer to BasicLexer.
1 parent 981fb66 commit ea61ada

File tree

11 files changed

+67
-66
lines changed

11 files changed

+67
-66
lines changed

build/win/libfly/libfly.vcxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@
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" />
240+
<ClInclude Include="..\..\..\fly\types\string\lexer.hpp" />
240241
<ClInclude Include="..\..\..\fly\types\string\string.hpp" />
241242
<ClInclude Include="..\..\..\fly\types\string\string_formatters.hpp" />
242-
<ClInclude Include="..\..\..\fly\types\string\string_lexer.hpp" />
243243
<ClInclude Include="..\..\..\fly\types\string\string_literal.hpp" />
244244
</ItemGroup>
245245
<ItemGroup>

build/win/libfly/libfly.vcxproj.filters

+3-3
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,13 @@
277277
<ClInclude Include="..\..\..\fly\types\numeric\detail\literal_parser.hpp">
278278
<Filter>types\numeric\detail</Filter>
279279
</ClInclude>
280-
<ClInclude Include="..\..\..\fly\types\string\string.hpp">
280+
<ClInclude Include="..\..\..\fly\types\string\lexer.hpp">
281281
<Filter>types\string</Filter>
282282
</ClInclude>
283-
<ClInclude Include="..\..\..\fly\types\string\string_formatters.hpp">
283+
<ClInclude Include="..\..\..\fly\types\string\string.hpp">
284284
<Filter>types\string</Filter>
285285
</ClInclude>
286-
<ClInclude Include="..\..\..\fly\types\string\string_lexer.hpp">
286+
<ClInclude Include="..\..\..\fly\types\string\string_formatters.hpp">
287287
<Filter>types\string</Filter>
288288
</ClInclude>
289289
<ClInclude Include="..\..\..\fly\types\string\string_literal.hpp">

build/win/libfly_unit_tests/libfly_unit_tests.vcxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@
257257
<ClCompile Include="..\..\..\test\types\numeric\literals.cpp" />
258258
<ClCompile Include="..\..\..\test\types\string\format_parameters.cpp" />
259259
<ClCompile Include="..\..\..\test\types\string\format_string.cpp" />
260+
<ClCompile Include="..\..\..\test\types\string\lexer.cpp" />
260261
<ClCompile Include="..\..\..\test\types\string\string.cpp" />
261262
<ClCompile Include="..\..\..\test\types\string\string_classifier.cpp" />
262263
<ClCompile Include="..\..\..\test\types\string\string_converter.cpp" />
263264
<ClCompile Include="..\..\..\test\types\string\string_format.cpp" />
264-
<ClCompile Include="..\..\..\test\types\string\string_lexer.cpp" />
265265
<ClCompile Include="..\..\..\test\types\string\string_traits.cpp" />
266266
<ClCompile Include="..\..\..\test\types\string\string_unicode.cpp" />
267267
</ItemGroup>

build/win/libfly_unit_tests/libfly_unit_tests.vcxproj.filters

+3-3
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@
164164
<ClCompile Include="..\..\..\test\types\string\format_string.cpp">
165165
<Filter>types\string</Filter>
166166
</ClCompile>
167+
<ClCompile Include="..\..\..\test\types\string\lexer.cpp">
168+
<Filter>types\string</Filter>
169+
</ClCompile>
167170
<ClCompile Include="..\..\..\test\types\string\string.cpp">
168171
<Filter>types\string</Filter>
169172
</ClCompile>
@@ -176,9 +179,6 @@
176179
<ClCompile Include="..\..\..\test\types\string\string_format.cpp">
177180
<Filter>types\string</Filter>
178181
</ClCompile>
179-
<ClCompile Include="..\..\..\test\types\string\string_lexer.cpp">
180-
<Filter>types\string</Filter>
181-
</ClCompile>
182182
<ClCompile Include="..\..\..\test\types\string\string_traits.cpp">
183183
<Filter>types\string</Filter>
184184
</ClCompile>

fly/net/endpoint.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "fly/fly.hpp"
44
#include "fly/net/socket/socket_types.hpp"
5-
#include "fly/types/string/string_lexer.hpp"
5+
#include "fly/types/string/lexer.hpp"
66

77
#include <compare>
88
#include <limits>
@@ -216,7 +216,7 @@ Endpoint<IPAddressType>::from_string(std::string_view endpoint)
216216
}
217217
}
218218

219-
fly::BasicStringLexer<std::string> lexer(std::move(port_view));
219+
fly::Lexer lexer(std::move(port_view));
220220

221221
auto address = IPAddressType::from_string(std::move(address_view));
222222
auto port = lexer.consume_number();

fly/net/ipv4_address.hpp

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

33
#include "fly/types/numeric/endian.hpp"
4-
#include "fly/types/string/string_lexer.hpp"
4+
#include "fly/types/string/lexer.hpp"
55

66
#include <array>
77
#include <compare>
@@ -156,7 +156,7 @@ constexpr std::optional<IPv4Address> IPv4Address::from_string(std::string_view a
156156
constexpr const auto s_max32 = static_cast<std::uint64_t>(std::numeric_limits<int_type>::max());
157157
constexpr const auto s_decimal = '.';
158158

159-
fly::BasicStringLexer<std::string> lexer(std::move(address));
159+
fly::Lexer lexer(std::move(address));
160160

161161
std::array<int_type, 4> parts {};
162162
std::size_t index = 0;

fly/net/ipv6_address.hpp

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

33
#include "fly/fly.hpp"
4-
#include "fly/types/string/string_lexer.hpp"
4+
#include "fly/types/string/lexer.hpp"
55

66
#include <algorithm>
77
#include <array>
@@ -172,7 +172,7 @@ constexpr std::optional<IPv6Address> IPv6Address::from_string(std::string_view a
172172
static_cast<std::uint64_t>(std::numeric_limits<std::uint16_t>::max());
173173
constexpr const auto s_colon = ':';
174174

175-
fly::BasicStringLexer<std::string> lexer(std::move(address));
175+
fly::Lexer lexer(std::move(address));
176176
address_type parts {};
177177

178178
std::optional<std::size_t> index_after_short_form;

fly/types/string/detail/format_specifier.hpp

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

33
#include "fly/types/string/detail/string_classifier.hpp"
4-
#include "fly/types/string/string_lexer.hpp"
4+
#include "fly/types/string/lexer.hpp"
55
#include "fly/types/string/string_literal.hpp"
66

77
#include <array>

fly/types/string/detail/format_string.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "fly/fly.hpp"
44
#include "fly/types/string/detail/format_specifier.hpp"
55
#include "fly/types/string/detail/string_traits.hpp"
6-
#include "fly/types/string/string_lexer.hpp"
6+
#include "fly/types/string/lexer.hpp"
77
#include "fly/types/string/string_literal.hpp"
88

99
#include <array>
@@ -31,8 +31,8 @@ template <typename StringType, typename... ParameterTypes>
3131
class BasicFormatString
3232
{
3333
using traits = BasicStringTraits<StringType>;
34-
using lexer = fly::BasicStringLexer<StringType>;
3534
using char_type = typename traits::char_type;
35+
using lexer = fly::BasicLexer<char_type>;
3636
using view_type = typename traits::view_type;
3737

3838
using FormatSpecifier = BasicFormatSpecifier<char_type>;

fly/types/string/string_lexer.hpp fly/types/string/lexer.hpp

+46-37
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,29 @@
99

1010
namespace fly {
1111

12+
template <typename CharType>
13+
class BasicLexer;
14+
15+
using Lexer = BasicLexer<char>;
16+
using WLexer = BasicLexer<wchar_t>;
17+
using Lexer8 = BasicLexer<char8_t>;
18+
using Lexer16 = BasicLexer<char16_t>;
19+
using Lexer32 = BasicLexer<char32_t>;
20+
1221
/**
1322
* Helper class to perform lexical analysis of a C-string literal. All methods are constant
1423
* expressions, allowing for string analysis at compile time.
1524
*
1625
* @author Timothy Flynn ([email protected])
1726
* @version January 3, 2021
1827
*/
19-
template <typename StringType>
20-
class BasicStringLexer
28+
template <typename CharType>
29+
class BasicLexer
2130
{
22-
using traits = detail::BasicStringTraits<StringType>;
23-
using classifier = detail::BasicStringClassifier<StringType>;
24-
using char_type = typename traits::char_type;
31+
using string_type = std::basic_string<CharType>;
32+
33+
using traits = detail::BasicStringTraits<string_type>;
34+
using classifier = detail::BasicStringClassifier<string_type>;
2535
using view_type = typename traits::view_type;
2636

2737
public:
@@ -33,14 +43,14 @@ class BasicStringLexer
3343
* @param literals Reference to a C-string literal of size N.
3444
*/
3545
template <std::size_t N>
36-
constexpr explicit BasicStringLexer(const char_type (&literals)[N]) noexcept;
46+
constexpr explicit BasicLexer(const CharType (&literals)[N]) noexcept;
3747

3848
/**
3949
* Constructor. Stores an existing view into a string.
4050
*
4151
* @param view The existing view into the string.
4252
*/
43-
constexpr explicit BasicStringLexer(view_type view) noexcept;
53+
constexpr explicit BasicLexer(view_type view) noexcept;
4454

4555
/**
4656
* @return A string view into the C-string literal.
@@ -61,15 +71,15 @@ class BasicStringLexer
6171
* @return If available, the character at the provided offset. Otherwise, an uninitialized
6272
* value.
6373
*/
64-
constexpr std::optional<char_type> peek(std::size_t offset = 0);
74+
constexpr std::optional<CharType> peek(std::size_t offset = 0);
6575

6676
/**
6777
* If a character is available at the current position in the C-string literal, return that
6878
* character and advance the current position to the next character.
6979
*
7080
* @return If available, the current character. Otherwise, an uninitialized value.
7181
*/
72-
constexpr std::optional<char_type> consume();
82+
constexpr std::optional<CharType> consume();
7383

7484
/**
7585
* If a character is available at the current position in the C-string literal, and if that
@@ -80,7 +90,7 @@ class BasicStringLexer
8090
*
8191
* @return Whether the current character was available and matched the provided character.
8292
*/
83-
constexpr bool consume_if(char_type ch);
93+
constexpr bool consume_if(CharType ch);
8494

8595
/**
8696
* Beginning with the current position, retrieve characters from the C-string literal and
@@ -113,13 +123,13 @@ class BasicStringLexer
113123
* @return Whether the current character was available and satisfied the provided condition.
114124
*/
115125
template <typename Condition>
116-
constexpr std::optional<char_type> consume_if(Condition condition);
126+
constexpr std::optional<CharType> consume_if(Condition condition);
117127

118-
static constexpr const auto s_zero = FLY_CHR(char_type, '0');
119-
static constexpr const auto s_upper_a = FLY_CHR(char_type, 'A');
120-
static constexpr const auto s_upper_f = FLY_CHR(char_type, 'F');
121-
static constexpr const auto s_lower_a = FLY_CHR(char_type, 'a');
122-
static constexpr const auto s_lower_f = FLY_CHR(char_type, 'f');
128+
static constexpr const auto s_zero = FLY_CHR(CharType, '0');
129+
static constexpr const auto s_upper_a = FLY_CHR(CharType, 'A');
130+
static constexpr const auto s_upper_f = FLY_CHR(CharType, 'F');
131+
static constexpr const auto s_lower_a = FLY_CHR(CharType, 'a');
132+
static constexpr const auto s_lower_f = FLY_CHR(CharType, 'f');
123133

124134
const std::size_t m_size;
125135
const view_type m_view;
@@ -128,39 +138,39 @@ class BasicStringLexer
128138
};
129139

130140
//==================================================================================================
131-
template <typename StringType>
141+
template <typename CharType>
132142
template <std::size_t N>
133-
constexpr BasicStringLexer<StringType>::BasicStringLexer(const char_type (&literals)[N]) noexcept :
143+
constexpr BasicLexer<CharType>::BasicLexer(const CharType (&literals)[N]) noexcept :
134144
m_size(classifier::size(literals)),
135145
m_view(literals, m_size)
136146
{
137147
}
138148

139149
//==================================================================================================
140-
template <typename StringType>
141-
constexpr BasicStringLexer<StringType>::BasicStringLexer(view_type view) noexcept :
150+
template <typename CharType>
151+
constexpr BasicLexer<CharType>::BasicLexer(view_type view) noexcept :
142152
m_size(view.size()),
143153
m_view(std::move(view))
144154
{
145155
}
146156

147157
//==================================================================================================
148-
template <typename StringType>
149-
constexpr auto BasicStringLexer<StringType>::view() const -> view_type
158+
template <typename CharType>
159+
constexpr auto BasicLexer<CharType>::view() const -> view_type
150160
{
151161
return m_view;
152162
}
153163

154164
//==================================================================================================
155-
template <typename StringType>
156-
constexpr std::size_t BasicStringLexer<StringType>::position() const
165+
template <typename CharType>
166+
constexpr std::size_t BasicLexer<CharType>::position() const
157167
{
158168
return m_index;
159169
}
160170

161171
//==================================================================================================
162-
template <typename StringType>
163-
constexpr auto BasicStringLexer<StringType>::peek(std::size_t offset) -> std::optional<char_type>
172+
template <typename CharType>
173+
constexpr std::optional<CharType> BasicLexer<CharType>::peek(std::size_t offset)
164174
{
165175
if ((m_index + offset) >= m_size)
166176
{
@@ -171,8 +181,8 @@ constexpr auto BasicStringLexer<StringType>::peek(std::size_t offset) -> std::op
171181
}
172182

173183
//==================================================================================================
174-
template <typename StringType>
175-
constexpr auto BasicStringLexer<StringType>::consume() -> std::optional<char_type>
184+
template <typename CharType>
185+
constexpr std::optional<CharType> BasicLexer<CharType>::consume()
176186
{
177187
if (m_index >= m_size)
178188
{
@@ -183,8 +193,8 @@ constexpr auto BasicStringLexer<StringType>::consume() -> std::optional<char_typ
183193
}
184194

185195
//==================================================================================================
186-
template <typename StringType>
187-
constexpr bool BasicStringLexer<StringType>::consume_if(char_type ch)
196+
template <typename CharType>
197+
constexpr bool BasicLexer<CharType>::consume_if(CharType ch)
188198
{
189199
if (auto next = peek(); next && (next.value() == ch))
190200
{
@@ -196,8 +206,8 @@ constexpr bool BasicStringLexer<StringType>::consume_if(char_type ch)
196206
}
197207

198208
//==================================================================================================
199-
template <typename StringType>
200-
constexpr std::optional<std::uintmax_t> BasicStringLexer<StringType>::consume_number()
209+
template <typename CharType>
210+
constexpr std::optional<std::uintmax_t> BasicLexer<CharType>::consume_number()
201211
{
202212
bool parsed_number = false;
203213
std::uintmax_t number = 0;
@@ -214,8 +224,8 @@ constexpr std::optional<std::uintmax_t> BasicStringLexer<StringType>::consume_nu
214224
}
215225

216226
//==================================================================================================
217-
template <typename StringType>
218-
constexpr std::optional<std::uintmax_t> BasicStringLexer<StringType>::consume_hex_number()
227+
template <typename CharType>
228+
constexpr std::optional<std::uintmax_t> BasicLexer<CharType>::consume_hex_number()
219229
{
220230
bool parsed_number = false;
221231
std::uintmax_t number = 0;
@@ -243,10 +253,9 @@ constexpr std::optional<std::uintmax_t> BasicStringLexer<StringType>::consume_he
243253
}
244254

245255
//==================================================================================================
246-
template <typename StringType>
256+
template <typename CharType>
247257
template <typename Condition>
248-
constexpr auto BasicStringLexer<StringType>::consume_if(Condition condition)
249-
-> std::optional<char_type>
258+
constexpr std::optional<CharType> BasicLexer<CharType>::consume_if(Condition condition)
250259
{
251260
if (auto next = peek(); next && condition(next.value()))
252261
{

test/types/string/string_lexer.cpp test/types/string/lexer.cpp

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "fly/types/string/string_lexer.hpp"
1+
#include "fly/types/string/lexer.hpp"
22

33
#include "fly/types/string/string_literal.hpp"
44

@@ -7,18 +7,10 @@
77

88
#include <string>
99

10-
CATCH_TEMPLATE_TEST_CASE(
11-
"BasicStringLexer",
12-
"[string]",
13-
std::string,
14-
std::wstring,
15-
std::u8string,
16-
std::u16string,
17-
std::u32string)
10+
CATCH_TEMPLATE_TEST_CASE("BasicLexer", "[string]", char, wchar_t, char8_t, char16_t, char32_t)
1811
{
19-
using StringType = TestType;
20-
using char_type = typename StringType::value_type;
21-
using Lexer = fly::BasicStringLexer<StringType>;
12+
using char_type = TestType;
13+
using Lexer = fly::BasicLexer<char_type>;
2214

2315
CATCH_SECTION("Cannot consume from empty lexer")
2416
{

0 commit comments

Comments
 (0)