summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/str.cc15
-rw-r--r--src/str.hh6
2 files changed, 21 insertions, 0 deletions
diff --git a/src/str.cc b/src/str.cc
index e5b70ed..129e0f3 100644
--- a/src/str.cc
+++ b/src/str.cc
@@ -76,4 +76,19 @@ std::string_view trim(std::string_view str) {
return str.substr(s, e - s);
}
+std::string_view ltrim(std::string_view str) {
+ size_t s = 0;
+ size_t const e = str.size();
+ while (s < e && is_space(str[s]))
+ ++s;
+ return str.substr(s, e - s);
+}
+
+std::string_view rtrim(std::string_view str) {
+ size_t e = str.size();
+ while (e > 0 && is_space(str[e - 1]))
+ --e;
+ return str.substr(0, e);
+}
+
} // namespace str
diff --git a/src/str.hh b/src/str.hh
index 1c618ee..c736f4b 100644
--- a/src/str.hh
+++ b/src/str.hh
@@ -23,6 +23,12 @@ void split(std::string_view str, std::vector<std::string_view>& out,
[[nodiscard]]
std::string_view trim(std::string_view str);
+[[nodiscard]]
+std::string_view ltrim(std::string_view str);
+
+[[nodiscard]]
+std::string_view rtrim(std::string_view str);
+
} // namespace str
#endif // STR_HH