summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-09-28 22:50:56 +0200
committerJoel Klinghed <the_jk@spawned.biz>2025-09-28 22:52:31 +0200
commit0ca22c7d6d650c80906bd1217fccf32066cc2502 (patch)
tree518c864de533308dd491d72b24338ad6bd596a1c /src
parentfb776823ec3174354f14866495ce11d38cbe5701 (diff)
str: Add trim()
Diffstat (limited to 'src')
-rw-r--r--src/str.cc19
-rw-r--r--src/str.hh3
2 files changed, 22 insertions, 0 deletions
diff --git a/src/str.cc b/src/str.cc
index bd7a654..44db3a6 100644
--- a/src/str.cc
+++ b/src/str.cc
@@ -6,6 +6,15 @@
namespace str {
+namespace {
+
+[[nodiscard]]
+inline bool is_space(char c) {
+ return c == ' ' || c == '\t' || c == '\r' || c == '\n';
+}
+
+} // namespace
+
void split(std::string_view str, std::vector<std::string_view>& out,
char separator, bool keep_empty) {
out.clear();
@@ -31,4 +40,14 @@ std::vector<std::string_view> split(std::string_view str, char separator,
return vec;
}
+std::string_view trim(std::string_view str) {
+ size_t s = 0;
+ size_t e = str.size();
+ while (s < e && is_space(str[s]))
+ ++s;
+ while (e > s && is_space(str[e - 1]))
+ --e;
+ return str.substr(s, e - s);
+}
+
} // namespace str
diff --git a/src/str.hh b/src/str.hh
index 58d5d32..e1ee549 100644
--- a/src/str.hh
+++ b/src/str.hh
@@ -13,6 +13,9 @@ void split(std::string_view str, std::vector<std::string_view>& out,
char separator = ' ',
bool keep_empty = false);
+[[nodiscard]]
+std::string_view trim(std::string_view str);
+
} // namespace str
#endif // STR_HH