RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
Trim.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <string_view>
5
6namespace RST::String {
7
13
15 inline constexpr std::string_view Whitespace = " \t\n\r\f\v";
16
17 [[nodiscard]] constexpr std::string_view ltrimView(std::string_view str, std::string_view chars = Whitespace) noexcept
18 {
19 const std::size_t first = str.find_first_not_of(chars);
20 str.remove_prefix(first == std::string_view::npos ? str.size() : first);
21 return str;
22 }
23
24 [[nodiscard]] constexpr std::string_view rtrimView(std::string_view str, std::string_view chars = Whitespace) noexcept
25 {
26 const std::size_t last = str.find_last_not_of(chars);
27 str.remove_suffix(last == std::string_view::npos ? str.size() : str.size() - last - 1);
28 return str;
29 }
30
31 [[nodiscard]] constexpr std::string_view trimView(std::string_view str, std::string_view chars = Whitespace) noexcept
32 {
34 }
35
36 // ── In place ─────────────────────────────────────────────────────────────
37 void trim(std::string& str, std::string_view chars = Whitespace);
38 void ltrim(std::string& str, std::string_view chars = Whitespace);
39 void rtrim(std::string& str, std::string_view chars = Whitespace);
40
41 // ── Copies ─────────────────────
42 [[nodiscard]] std::string trimCopy(std::string_view str, std::string_view chars = Whitespace);
43 [[nodiscard]] std::string ltrimCopy(std::string_view str, std::string_view chars = Whitespace);
44 [[nodiscard]] std::string rtrimCopy(std::string_view str, std::string_view chars = Whitespace);
46}
String helpers: case, trimming, splitting, joining, replacing, parsing and hashing.
Definition Char.hpp:3
std::string rtrimCopy(std::string_view str, std::string_view chars=Whitespace)
std::string ltrimCopy(std::string_view str, std::string_view chars=Whitespace)
constexpr std::string_view trimView(std::string_view str, std::string_view chars=Whitespace) noexcept
Definition Trim.hpp:31
constexpr std::string_view rtrimView(std::string_view str, std::string_view chars=Whitespace) noexcept
Definition Trim.hpp:24
constexpr std::string_view ltrimView(std::string_view str, std::string_view chars=Whitespace) noexcept
Definition Trim.hpp:17
void trim(std::string &str, std::string_view chars=Whitespace)
void rtrim(std::string &str, std::string_view chars=Whitespace)
constexpr std::string_view Whitespace
Characters trimmed by default: space, tab, line feed, carriage return, form feed and vertical tab.
Definition Trim.hpp:15
std::string trimCopy(std::string_view str, std::string_view chars=Whitespace)
void ltrim(std::string &str, std::string_view chars=Whitespace)
std::optional< T > parseNumber(std::string_view text, int base=10) noexcept
Parses text as a number, or returns std::nullopt if it is not entirely one.
Definition Convert.hpp:23