Skip to content

Commit ed41cff

Browse files
committed
Remove BasicString::starts_with and BasicString::ends_with
These are now members of std::basic_string in C++20.
1 parent 3378453 commit ed41cff

File tree

9 files changed

+31
-245
lines changed

9 files changed

+31
-245
lines changed

fly/parser/ini_parser.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ std::optional<Json> IniParser::parse_internal()
2222
{
2323
String::trim(data);
2424

25-
if (data.empty() || String::starts_with(data, ';'))
25+
if (data.empty() || data.starts_with(';'))
2626
{
2727
// Ignore comments and blank lines.
2828
continue;
@@ -164,8 +164,8 @@ IniParser::TrimResult IniParser::trim_value(std::string &str, char ch) const
164164
//==================================================================================================
165165
IniParser::TrimResult IniParser::trim_value(std::string &str, char start, char end) const
166166
{
167-
bool starts_with_char = String::starts_with(str, start);
168-
bool ends_with_char = String::ends_with(str, end);
167+
bool starts_with_char = str.starts_with(start);
168+
bool ends_with_char = str.ends_with(end);
169169

170170
if (starts_with_char && ends_with_char)
171171
{

fly/system/nix/system_monitor_impl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void SystemMonitorImpl::update_system_cpu_count()
4242
{
4343
contents += line + "\\n";
4444

45-
if (String::starts_with(line, "cpu"))
45+
if (line.starts_with("cpu"))
4646
{
4747
if ((line.size() > 3) && (line[3] != ' '))
4848
{

fly/types/string/string.hpp

+6-110
Original file line numberDiff line numberDiff line change
@@ -135,46 +135,6 @@ class BasicString
135135
*/
136136
static void remove_all(StringType &target, const StringType &search);
137137

138-
/**
139-
* Check if a string begins with a character.
140-
*
141-
* @param source The string to check.
142-
* @param search The beginning to search for.
143-
*
144-
* @return True if the string begins with the search character.
145-
*/
146-
static bool starts_with(const StringType &source, const char_type &search);
147-
148-
/**
149-
* Check if a string begins with another string.
150-
*
151-
* @param source The string to check.
152-
* @param search The beginning to search for.
153-
*
154-
* @return True if the string begins with the search string.
155-
*/
156-
static bool starts_with(const StringType &source, const StringType &search);
157-
158-
/**
159-
* Check if a string ends with a character.
160-
*
161-
* @param source The string to check.
162-
* @param search The ending to search for.
163-
*
164-
* @return True if the string ends with the search character.
165-
*/
166-
static bool ends_with(const StringType &source, const char_type &search);
167-
168-
/**
169-
* Check if a string ends with another string.
170-
*
171-
* @param source The string to check.
172-
* @param search The ending to search for.
173-
*
174-
* @return True if the string ends with the search string.
175-
*/
176-
static bool ends_with(const StringType &source, const StringType &search);
177-
178138
/**
179139
* Check if a string matches another string with wildcard expansion.
180140
*
@@ -566,70 +526,6 @@ void BasicString<StringType>::remove_all(StringType &target, const StringType &s
566526
replace_all(target, search, StringType());
567527
}
568528

569-
//==================================================================================================
570-
template <typename StringType>
571-
bool BasicString<StringType>::starts_with(const StringType &source, const char_type &search)
572-
{
573-
bool result = false;
574-
575-
if (!source.empty())
576-
{
577-
result = source[0] == search;
578-
}
579-
580-
return result;
581-
}
582-
583-
//==================================================================================================
584-
template <typename StringType>
585-
bool BasicString<StringType>::starts_with(const StringType &source, const StringType &search)
586-
{
587-
bool result = false;
588-
589-
const size_type source_sz = source.size();
590-
const size_type search_sz = search.size();
591-
592-
if (source_sz >= search_sz)
593-
{
594-
result = source.compare(0, search_sz, search) == 0;
595-
}
596-
597-
return result;
598-
}
599-
600-
//==================================================================================================
601-
template <typename StringType>
602-
bool BasicString<StringType>::ends_with(const StringType &source, const char_type &search)
603-
{
604-
bool result = false;
605-
606-
const size_type source_sz = source.size();
607-
608-
if (source_sz > 0)
609-
{
610-
result = source[source_sz - 1] == search;
611-
}
612-
613-
return result;
614-
}
615-
616-
//==================================================================================================
617-
template <typename StringType>
618-
bool BasicString<StringType>::ends_with(const StringType &source, const StringType &search)
619-
{
620-
bool result = false;
621-
622-
const size_type source_sz = source.size();
623-
const size_type search_sz = search.size();
624-
625-
if (source_sz >= search_sz)
626-
{
627-
result = source.compare(source_sz - search_sz, search_sz, search) == 0;
628-
}
629-
630-
return result;
631-
}
632-
633529
//==================================================================================================
634530
template <typename StringType>
635531
bool BasicString<StringType>::wildcard_match(const StringType &source, const StringType &search)
@@ -644,11 +540,11 @@ bool BasicString<StringType>::wildcard_match(const StringType &source, const Str
644540
{
645541
if (result && (search.front() != s_wildcard))
646542
{
647-
result = starts_with(source, segments.front());
543+
result = source.starts_with(segments.front());
648544
}
649545
if (result && (search.back() != s_wildcard))
650546
{
651-
result = ends_with(source, segments.back());
547+
result = source.ends_with(segments.back());
652548
}
653549

654550
for (auto it = segments.begin(); result && (it != segments.end()); ++it)
@@ -795,18 +691,18 @@ StringType BasicString<StringType>::generate_random_string(size_type length)
795691
constexpr auto limit = static_cast<short_distribution::result_type>(s_alpha_num_length - 1);
796692
static_assert(limit > 0);
797693

798-
static thread_local const auto now = std::chrono::system_clock::now().time_since_epoch();
799-
static thread_local const auto seed = static_cast<std::mt19937::result_type>(now.count());
694+
static thread_local const auto s_now = std::chrono::system_clock::now().time_since_epoch();
695+
static thread_local const auto s_seed = static_cast<std::mt19937::result_type>(s_now.count());
800696

801-
static thread_local std::mt19937 engine(seed);
697+
static thread_local std::mt19937 s_engine(s_seed);
802698
short_distribution distribution(0, limit);
803699

804700
StringType result;
805701
result.reserve(length);
806702

807703
while (length-- != 0)
808704
{
809-
result.push_back(s_alpha_num[distribution(engine)]);
705+
result.push_back(s_alpha_num[distribution(s_engine)]);
810706
}
811707

812708
return result;

test/logger/console_logger.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ CATCH_TEST_CASE("ConsoleLogger", "[logger]")
7878
CATCH_REQUIRE_FALSE(contents.empty());
7979

8080
const std::string styled = styled_contents(contents, "Debug Log");
81-
CATCH_CHECK(fly::String::starts_with(styled, "\x1b[0m"));
82-
CATCH_CHECK(fly::String::ends_with(styled, "\x1b[0m"));
81+
CATCH_CHECK(styled.starts_with("\x1b[0m"));
82+
CATCH_CHECK(styled.ends_with("\x1b[0m"));
8383
}
8484

8585
CATCH_SECTION("Validate style of informational console logs")
@@ -91,8 +91,8 @@ CATCH_TEST_CASE("ConsoleLogger", "[logger]")
9191
CATCH_REQUIRE_FALSE(contents.empty());
9292

9393
const std::string styled = styled_contents(contents, "Info Log");
94-
CATCH_CHECK(fly::String::starts_with(styled, "\x1b[0;32m"));
95-
CATCH_CHECK(fly::String::ends_with(styled, "\x1b[0m"));
94+
CATCH_CHECK(styled.starts_with("\x1b[0;32m"));
95+
CATCH_CHECK(styled.ends_with("\x1b[0m"));
9696
}
9797

9898
CATCH_SECTION("Validate style of warning console logs")
@@ -104,8 +104,8 @@ CATCH_TEST_CASE("ConsoleLogger", "[logger]")
104104
CATCH_REQUIRE_FALSE(contents.empty());
105105

106106
const std::string styled = styled_contents(contents, "Warning Log");
107-
CATCH_CHECK(fly::String::starts_with(styled, "\x1b[0;33m"));
108-
CATCH_CHECK(fly::String::ends_with(styled, "\x1b[0m"));
107+
CATCH_CHECK(styled.starts_with("\x1b[0;33m"));
108+
CATCH_CHECK(styled.ends_with("\x1b[0m"));
109109
}
110110

111111
CATCH_SECTION("Validate style of error console logs")
@@ -117,8 +117,8 @@ CATCH_TEST_CASE("ConsoleLogger", "[logger]")
117117
CATCH_REQUIRE_FALSE(contents.empty());
118118

119119
const std::string styled = styled_contents(contents, "Error Log");
120-
CATCH_CHECK(fly::String::starts_with(styled, "\x1b[1;31m"));
121-
CATCH_CHECK(fly::String::ends_with(styled, "\x1b[0m"));
120+
CATCH_CHECK(styled.starts_with("\x1b[1;31m"));
121+
CATCH_CHECK(styled.ends_with("\x1b[0m"));
122122
}
123123
}
124124
#endif

test/logger/file_logger.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ CATCH_TEST_CASE("FileLogger", "[logger]")
110110
CATCH_SECTION("Valid logger file paths should be created after creating logger")
111111
{
112112
std::filesystem::path log_file = find_log_file(path);
113-
CATCH_CHECK(fly::String::starts_with(log_file.string(), path().string()));
113+
CATCH_CHECK(log_file.string().starts_with(path().string()));
114114

115115
CATCH_REQUIRE(std::filesystem::exists(log_file));
116116
}

test/logger/logger.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ CATCH_TEST_CASE("Logger", "[logger]")
118118
CATCH_CHECK(log.m_index == i);
119119
CATCH_CHECK(log.m_level == expected_level);
120120
CATCH_CHECK(log.m_time >= last_time);
121-
CATCH_CHECK(fly::String::starts_with(log.m_message, expected_messages[i]));
121+
CATCH_CHECK(log.m_message.starts_with(expected_messages[i]));
122122

123123
if (expected_function != nullptr)
124124
{

test/logger/styler.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ void test_styler(std::string &&expected_escape, Modifiers &&...modifiers)
3030
const std::string contents = capture();
3131
CATCH_REQUIRE_FALSE(contents.empty());
3232

33-
CATCH_CHECK(fly::String::starts_with(contents, expected_escape));
33+
CATCH_CHECK(contents.starts_with(expected_escape));
3434

3535
if constexpr (style_or_color)
3636
{
37-
CATCH_CHECK(fly::String::ends_with(contents, "\x1b[0m"));
37+
CATCH_CHECK(contents.ends_with("\x1b[0m"));
3838
}
3939
}
4040
{
@@ -44,11 +44,11 @@ void test_styler(std::string &&expected_escape, Modifiers &&...modifiers)
4444
const std::string contents = capture();
4545
CATCH_REQUIRE_FALSE(contents.empty());
4646

47-
CATCH_CHECK(fly::String::starts_with(contents, expected_escape));
47+
CATCH_CHECK(contents.starts_with(expected_escape));
4848

4949
if constexpr (style_or_color)
5050
{
51-
CATCH_CHECK(fly::String::ends_with(contents, "\x1b[0m"));
51+
CATCH_CHECK(contents.ends_with("\x1b[0m"));
5252
}
5353
}
5454
}
@@ -62,8 +62,8 @@ CATCH_TEST_CASE("Styler", "[logger]")
6262
stream << fly::Styler(fly::Color::Red) << "non-stylized text";
6363
const std::string contents = stream.str();
6464

65-
CATCH_CHECK_FALSE(fly::String::starts_with(contents, "\x1b[38;5;1m"));
66-
CATCH_CHECK_FALSE(fly::String::ends_with(contents, "\x1b[0m"));
65+
CATCH_CHECK_FALSE(contents.starts_with("\x1b[38;5;1m"));
66+
CATCH_CHECK_FALSE(contents.ends_with("\x1b[0m"));
6767
CATCH_CHECK(contents == "non-stylized text");
6868
}
6969

test/parser/json_parser.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ CATCH_TEST_CASE("JsonParser", "[parser]")
7272
const auto file = it.path().filename();
7373
CATCH_CAPTURE(file);
7474

75-
if (fly::String::starts_with(file.string(), "pass"))
75+
if (file.string().starts_with("pass"))
7676
{
7777
CATCH_CHECK(parser.parse_file(it.path()).has_value());
7878
}
79-
else if (fly::String::starts_with(file.string(), "fail"))
79+
else if (file.string().starts_with("fail"))
8080
{
8181
CATCH_CHECK_FALSE(parser.parse_file(it.path()).has_value());
8282
}
@@ -124,15 +124,15 @@ CATCH_TEST_CASE("JsonParser", "[parser]")
124124
const auto file = it.path().filename();
125125
CATCH_CAPTURE(file);
126126

127-
if (fly::String::starts_with(file.string(), 'y'))
127+
if (file.string().starts_with('y'))
128128
{
129129
CATCH_CHECK(type_parser.parse_file(it.path()).has_value());
130130
}
131-
else if (fly::String::starts_with(file.string(), 'n'))
131+
else if (file.string().starts_with('n'))
132132
{
133133
CATCH_CHECK_FALSE(type_parser.parse_file(it.path()).has_value());
134134
}
135-
else if (fly::String::starts_with(file.string(), 'i'))
135+
else if (file.string().starts_with('i'))
136136
{
137137
if (std::find(i_pass.begin(), i_pass.end(), file) != i_pass.end())
138138
{

0 commit comments

Comments
 (0)