diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2025-10-19 00:06:54 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2025-10-19 00:13:47 +0200 |
| commit | 800bdbb18617dfac36067b8841781a92b19d57c1 (patch) | |
| tree | 2b003e2273eb2f20e31b69ae8b6120bfc2062873 /src/str.cc | |
| parent | dad0aaa9b33a5a217ac115334a94fe299dce9e08 (diff) | |
str: Add ltrim and rtrim
Useful when you only want to trim one side..
Diffstat (limited to 'src/str.cc')
| -rw-r--r-- | src/str.cc | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -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 |
