FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpputils/include/cpputils/string-strip.h at master · nlitsme/cpputils · GitHub
nlitsme
/
cpputils
Public
Notifications
You must be signed in to change notification settings
Fork
10
Star
24
Code
Issues
1
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpputils
/
include
/
cpputils
/
string-strip.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
150 lines (120 loc) · 3.31 KB
Breadcrumbs
cpputils
/
include
/
cpputils
/
string-strip.h
Copy path
File metadata and controls
150 lines (120 loc) · 3.31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#
pragma
once
#
include
<
cstring
>
#
include
<
algorithm
>
#
include
<
utility
>
#
include
<
iterator
>
#
include
<
string_view
>
#
include
<
type_traits
>
#
include
<
cpputils/templateutils.h
>
template
<
typename
SEQUENCE
,
typename
CHAR
>
bool
is_in
(
const
SEQUENCE
& trims,
CHAR
c)
{
if
constexpr
(is_searchable_v<
SEQUENCE
>)
return
trims.
find
(c) != trims.
end
();
return
std::find
(
std::begin
(trims),
std::end
(trims), c) !=
std::end
(trims);
}
template
<
typename
CHAR
>
bool
is_in
(
const
char
*trims,
CHAR
c)
{
return
std::strchr
(trims, c) !=
NULL
;
}
template
<
typename
CHAR
>
bool
is_in
(
char
trims,
CHAR
c)
{
return
trims == c;
}
//
strip algorithms taking first, last pointer pairs.
//
//
note: i used to have identical names for the predicate variants,
//
and select the function by using:
//
std::enable_if_t<is_callable_v<PRED>, P>
//
std::enable_if_t<!std::is_void_v<typename S::value_type>, P>
//
//
where 'std::is_void_v<typename S::value_type>' means: 'is_container'
template
<
typename
P,
typename
PRED
>
P
rstrip_if
(P first, P last,
PRED
pred)
{
auto
i = last;
while
(i > first)
{
i--;
if
(!
pred
(*i))
return
i+
1
;
}
return
first;
}
template
<
typename
P,
typename
S>
P
rstrip
(P first, P last,
const
S& trims)
{
return
rstrip_if
(first, last, [&](
auto
c) {
return
is_in<S>(trims, c); });
}
template
<
typename
P,
typename
PRED
>
P
lstrip_if
(P first, P last,
PRED
pred)
{
auto
i = first;
while
(i < last)
{
if
(!
pred
(*i))
return
i;
i++;
}
return
last;
}
template
<
typename
P,
typename
S>
P
lstrip
(P first, P last,
const
S& trims)
{
return
lstrip_f
(first, last, [&](
auto
c) {
return
is_in<S>(trims, c); });
}
template
<
typename
P,
typename
PRED
>
std::pair<P,P>
strip_if
(P first, P last,
PRED
pred)
{
auto
l =
lstrip_if
(first, last, pred);
auto
r =
rstrip_if
(first, last, pred);
return
{l, r};
}
template
<
typename
P,
typename
S>
std::pair<P,P>
strip
(P first, P last,
const
S& trims)
{
return
strip_if
(first, last, [&](
auto
c) {
return
is_in<S>(trims, c); });
}
template
<
typename
T,
typename
P>
T
makeresult
(P first, P last)
{
if
constexpr
(std::is_same_v<T, std::string_view> || std::is_same_v<T, std::wstring_view>)
return
{first,
typename
T::size_type
(last-first)};
else
return
{first, last};
}
//
adaptors for stl types
template
<
typename
T,
typename
PRED
>
T
rstrip_if
(
const
T& str,
PRED
pred)
{
return
makeresult<T>(
std::begin
(str),
rstrip_if
(
std::begin
(str),
std::end
(str), pred));
}
template
<
typename
T,
typename
S>
T
rstrip
(
const
T& str,
const
S& trims)
{
return
rstrip_if
(str, [&](
auto
c) {
return
is_in<S>(trims, c); });
}
template
<
typename
T,
typename
PRED
>
T
lstrip_if
(
const
T& str,
PRED
pred)
{
return
makeresult<T>(
lstrip_if
(
std::begin
(str),
std::end
(str), pred),
std::end
(str));
}
template
<
typename
T,
typename
S>
T
lstrip
(
const
T& str,
const
S& trims)
{
return
lstrip_if
(str, [&](
auto
c) {
return
is_in<S>(trims, c); });
}
template
<
typename
T,
typename
PRED
>
T
strip_if
(
const
T& str,
PRED
pred)
{
auto
l =
lstrip_if
(
std::begin
(str),
std::end
(str), pred);
auto
r =
rstrip_if
(
std::begin
(str),
std::end
(str), pred);
return
makeresult<T>(l, r);
}
template
<
typename
T,
typename
S>
T
strip
(
const
T& str,
const
S& trims)
{
return
strip_if
(str, [&](
auto
c) {
return
is_in<S>(trims, c); });
}
Back
|
FazBrowse Home
|
New Git URL