From 9147a85dc32f06be2a4cfe4e422fdbc52679adc5 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 8 Jan 2024 22:08:48 -0500 Subject: [PATCH] string_view trim --- include/blt/std/string.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/include/blt/std/string.h b/include/blt/std/string.h index 4f3e44f..d4b82b2 100755 --- a/include/blt/std/string.h +++ b/include/blt/std/string.h @@ -292,6 +292,29 @@ namespace blt::string return s; } + static inline BLT_CPP20_CONSTEXPR std::string_view ltrim(std::string_view s) + { + size_t start_pos = 0; + for (auto c = s.begin(); c != s.end() && std::isblank(*c); ++c, start_pos++); + return s.substr(start_pos); + } + + static inline BLT_CPP20_CONSTEXPR std::string_view rtrim(std::string_view s) + { + size_t end_pos = 0; + for (auto c = s.rbegin(); c != s.rend() && std::isblank(*c); ++c, end_pos++); + return s.substr(0, s.size() - end_pos); + } + + static inline BLT_CPP20_CONSTEXPR std::string_view trim(std::string_view s) + { + size_t start_pos = 0; + for (auto c = s.begin(); c != s.end() && std::isblank(*c); ++c, start_pos++); + size_t end_pos = s.size(); + for (auto c = s.rbegin(); c != s.rend() && std::isblank(*c); ++c, end_pos--); + return s.substr(start_pos, end_pos - start_pos); + } + // trim from both ends (in place) static inline BLT_CPP20_CONSTEXPR std::string& trim(std::string& s) {