summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/strutil.cc10
-rw-r--r--src/strutil.hh2
-rw-r--r--test/test_strutil.cc7
3 files changed, 19 insertions, 0 deletions
diff --git a/src/strutil.cc b/src/strutil.cc
index adee769..a22f4b4 100644
--- a/src/strutil.cc
+++ b/src/strutil.cc
@@ -208,4 +208,14 @@ std::string rtrim(std::string const& str) {
return str.substr(0, end);
}
+std::string to_lower_ascii(std::string_view str) {
+ std::string ret(str);
+ for (auto& c : ret) {
+ if (c >= 'A' && c <= 'Z') {
+ c |= 0x20;
+ }
+ }
+ return ret;
+}
+
} // namespace str
diff --git a/src/strutil.hh b/src/strutil.hh
index 28bbf55..27b5827 100644
--- a/src/strutil.hh
+++ b/src/strutil.hh
@@ -46,6 +46,8 @@ void join(std::vector<std::string_view> const& in, std::string_view delim,
[[nodiscard]] bool starts_with(std::string_view str, std::string_view prefix);
[[nodiscard]] bool ends_with(std::string_view str, std::string_view suffix);
+std::string to_lower_ascii(std::string_view str);
+
} // namespace str
#endif // STRUTIL_HH
diff --git a/test/test_strutil.cc b/test/test_strutil.cc
index 4b65218..038851d 100644
--- a/test/test_strutil.cc
+++ b/test/test_strutil.cc
@@ -217,3 +217,10 @@ TEST(strutil, join_view) {
EXPECT_EQ(",foo,", str::join(std::vector<std::string_view>({",", ","}),
"foo"));
}
+
+TEST(strutil, to_lower_ascii) {
+ EXPECT_EQ("", str::to_lower_ascii(""));
+ EXPECT_EQ("foo", str::to_lower_ascii("foo"));
+ EXPECT_EQ("foo", str::to_lower_ascii("FOO"));
+ EXPECT_EQ("smÖrgÅs", str::to_lower_ascii("SMÖRGÅS"));
+}