summaryrefslogtreecommitdiff
path: root/src/strutil.hh
blob: 28bbf558ce58809a5ea02f2b5a0bcce59f0e7adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef STRUTIL_HH
#define STRUTIL_HH

#include <optional>
#include <stdint.h>
#include <string>
#include <string_view>
#include <vector>

namespace str {

std::optional<uint16_t> parse_uint16(std::string const& str);
std::optional<uint32_t> parse_uint32(std::string const& str);
std::optional<uint64_t> parse_uint64(std::string const& str);

// Empty substrings are ignored but out will always be at least one entry
std::vector<std::string_view> split(std::string_view str, char delim = ' ');

std::vector<std::string> split(std::string const& str, char delim = ' ');

std::string join(std::vector<std::string> const& in, char delim);
std::string join(std::vector<std::string> const& in, std::string_view delim);

void join(std::vector<std::string> const& in, char delim, std::string& out);
void join(std::vector<std::string> const& in, std::string_view delim,
          std::string& out);

std::string join(std::vector<std::string_view> const& in, char delim);
std::string join(std::vector<std::string_view> const& in,
                 std::string_view delim);

void join(std::vector<std::string_view> const& in, char delim,
          std::string& out);
void join(std::vector<std::string_view> const& in, std::string_view delim,
          std::string& out);

[[nodiscard]] std::string_view trim(std::string_view str);
[[nodiscard]] std::string trim(std::string const& str);

[[nodiscard]] std::string_view ltrim(std::string_view str);
[[nodiscard]] std::string ltrim(std::string const& str);

[[nodiscard]] std::string_view rtrim(std::string_view str);
[[nodiscard]] std::string rtrim(std::string const& str);

[[nodiscard]] bool starts_with(std::string_view str, std::string_view prefix);
[[nodiscard]] bool ends_with(std::string_view str, std::string_view suffix);

}  // namespace str

#endif  // STRUTIL_HH