string_view trim

v1
Brett 2024-01-08 22:08:48 -05:00
parent 903bac9fc1
commit 9147a85dc3
1 changed files with 23 additions and 0 deletions

View File

@ -292,6 +292,29 @@ namespace blt::string
return s; 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) // trim from both ends (in place)
static inline BLT_CPP20_CONSTEXPR std::string& trim(std::string& s) static inline BLT_CPP20_CONSTEXPR std::string& trim(std::string& s)
{ {