-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.hpp
More file actions
72 lines (58 loc) · 1.58 KB
/
Copy pathstring_utils.hpp
File metadata and controls
72 lines (58 loc) · 1.58 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
// std
#include <cctype>
#include <cstring>
#include <string>
#include <string_view>
#include <vector>
namespace util {
/// trim whitespace from the end of \a s
constexpr std::string_view rtrim(std::string_view s)
{
size_t i=0;
for ( auto I = s.crbegin(); std::isspace(*I) && I != s.crend(); ++I )
++i;
return s.substr(0, s.size() - i);
}
/// trim whitespace from the start of \a s
constexpr std::string_view ltrim(std::string_view s)
{
size_t i=0;
for ( auto I = s.cbegin(); std::isspace(*I) && I != s.cend(); ++I )
++i;
return s.substr(i, s.size() - i);
}
/// trim whitespace from the start and end of \a s
constexpr std::string_view trim(std::string_view s)
{
return ltrim(rtrim(s));
}
/// split \a s on char \a c; this will continue until the
/// end of s:
///
/// - split("a-bcd-ef-g", '-') -> ["a", "bcd", "ef", "g"]
/// - split("a-bcd", '-') -> ["a", "bcd"]
/// - split("a", '-') -> ["a"]
std::vector<std::string_view> split(std::string_view s, char c)
{
if ( s.empty() )
return {};
std::vector<std::string_view> result;
size_t p = 0, i = 0;
while (true)
{
if (s[i] == c || i == s.size())
{
result.push_back(s.substr(p, i-p));
if ( i == s.size() )
break;
p = ++i;
}
else
{
++i;
}
}
return result;
}
}