forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.h
More file actions
50 lines (42 loc) · 1.41 KB
/
string_utils.h
File metadata and controls
50 lines (42 loc) · 1.41 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
#pragma once
#include <string_view>
#include <string>
#include <algorithm>
template<typename CharT>
struct default_trim_arg
{
};
template<>
struct default_trim_arg<char>
{
static inline constexpr std::string_view value = " \t\r\n";
};
template<>
struct default_trim_arg<wchar_t>
{
static inline constexpr std::wstring_view value = L" \t\r\n";
};
template<typename CharT>
inline std::basic_string_view<CharT> left_trim(std::basic_string_view<CharT> s, const std::basic_string_view<CharT> chars_to_trim = default_trim_arg<CharT>::value)
{
s.remove_prefix(std::min<size_t>(s.find_first_not_of(chars_to_trim), size(s)));
return s;
}
template<typename CharT>
inline std::basic_string_view<CharT> right_trim(std::basic_string_view<CharT> s, const std::basic_string_view<CharT> chars_to_trim = default_trim_arg<CharT>::value)
{
s.remove_suffix(std::min<size_t>(size(s) - s.find_last_not_of(chars_to_trim) - 1, size(s)));
return s;
}
template<typename CharT>
inline std::basic_string_view<CharT> trim(std::basic_string_view<CharT> s, const std::basic_string_view<CharT> chars_to_trim = default_trim_arg<CharT>::value)
{
return left_trim(right_trim(s, chars_to_trim), chars_to_trim);
}
inline void replace_chars(std::string& s, const std::string_view chars_to_replace, const char replacement_char)
{
for (const char c : chars_to_replace)
{
std::replace(begin(s), end(s), c, replacement_char);
}
}