srctree

Robin Linden parent 9e2a0aec f9db1cf1
util/string: Add `std::string to_lower(std::string)`

inlinesplit
util/string.h added: 14, removed: 3, total 11
@@ -11,6 +11,7 @@
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <string_view>
#include <vector>
 
@@ -56,6 +57,11 @@ constexpr char to_lower(char c) {
return c + ('a' - 'A');
}
 
[[nodiscard]] inline std::string to_lower(std::string s) {
std::transform(begin(s), end(s), begin(s), [](char c) { return to_lower(c); });
return s;
}
 
constexpr bool no_case_compare(std::string_view a, std::string_view b) {
return ranges::equal(a, b, [](auto c1, auto c2) { return to_lower(c1) == to_lower(c2); });
}
 
util/string_test.cpp added: 14, removed: 3, total 11
@@ -55,11 +55,16 @@ int main() {
expect(!is_hex_digit('!'));
});
 
etest::test("to_lower", [] {
etest::test("to_lower(char)", [] {
expect_eq(to_lower('A'), 'a');
expect_eq(to_lower('a'), 'a');
});
 
etest::test("to_lower(std::string)", [] {
expect_eq(to_lower("Hello There!!1"), "hello there!!1");
expect_eq(to_lower("woop woop"), "woop woop");
});
 
etest::test("no case compare", [] {
expect(no_case_compare("word"sv, "word"sv));
expect(no_case_compare("WORD"sv, "WORD"sv));