LIPH's C++ Codes
string.h
Go to the documentation of this file.
1#ifndef LIPH_STRING_H_
2#define LIPH_STRING_H_
3
4#include <optional>
5#include <sstream>
6#include <string>
7#include <string_view>
8#include <vector>
9
10namespace liph {
11
15std::string_view ltrim(std::string_view s, std::string_view charset = " \n\r\t\f\v");
16
19std::string_view rtrim(std::string_view s, std::string_view charset = " \n\r\t\f\v");
20
23std::string_view trim(std::string_view s, std::string_view charset = " \n\r\t\f\v");
24
26constexpr bool startswith(std::string_view s, std::string_view t) noexcept { return s.starts_with(t); }
27
29constexpr bool endswith(std::string_view s, std::string_view t) noexcept { return s.ends_with(t); }
30
32std::string replace(std::string_view str, std::string_view old, std::string_view New);
33
35void split(std::vector<std::string>& tokens, const std::string& s, const std::string& delimiters = " ");
36
38std::vector<std::string> split(const std::string& s, const std::string& delimiters = " ");
39
41std::string basename(std::string_view path);
42
44std::string dirname(std::string_view path);
45
47template <typename Target = std::string, typename Source = std::string>
48Target to(const Source& s) {
49 std::stringstream buf;
50 Target result;
51 if (!(buf << s) || !(buf >> result) || !(buf >> std::ws).eof()) throw std::runtime_error{"to<>() failed"};
52 return result;
53}
54
56void skip_whitespace(std::string_view& sv);
57
60bool eat_symbol(std::string_view& sv, std::string_view symbol);
61
63std::optional<int> to_int(std::string_view str);
64
66template <std::ranges::input_range Range>
67std::string join(const Range& range, const std::string& sep = " ") {
68 std::string str;
69 bool first = true;
70 for (const auto& value : range) {
71 if (!first) str += sep;
72 first = false;
73 if constexpr (std::is_same<std::ranges::range_value_t<Range>, std::string>::value)
74 str += value;
75 else
76 str += std::to_string(value);
77 }
78 return str;
79}
80
81} // namespace liph
82
83#endif // LIPH_STRING_H_
Definition: algorithm.h:10
std::string_view ltrim(std::string_view s, std::string_view charset)
Definition: string.cpp:7
void split(std::vector< std::string > &tokens, const std::string &s, const std::string &delimiters)
Split @s to tokens by @delimiters and append to @tokens.
Definition: string.cpp:27
void skip_whitespace(std::string_view &sv)
@sv skips leading whitespace characters and be the substr
Definition: string.cpp:74
std::string_view trim(std::string_view s, std::string_view charset)
Definition: string.cpp:17
std::string_view rtrim(std::string_view s, std::string_view charset)
Definition: string.cpp:12
std::string dirname(std::string_view path)
Return dirname of @path.
Definition: string.cpp:61
std::optional< int > to_int(std::string_view s)
Convert @str to int.
Definition: string.cpp:86
constexpr bool endswith(std::string_view s, std::string_view t) noexcept
Definition: string.h:29
bool eat_symbol(std::string_view &sv, std::string_view symbol)
Definition: string.cpp:80
std::string replace(std::string_view str, std::string_view s, std::string_view t)
Replace all @old in @str with @New.
Definition: string.cpp:93
std::string basename(std::string_view path)
Return basename of @path.
Definition: string.cpp:43
std::string join(const Range &range, const std::string &sep=" ")
Join all items in @range into a string, using @sep as separator.
Definition: string.h:67
constexpr bool startswith(std::string_view s, std::string_view t) noexcept
Definition: string.h:26
Target to(const Source &s)
Convert source to target.
Definition: string.h:48