FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpputils/include/cpputils/string-base.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-base.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
150 lines (137 loc) · 2.96 KB
Breadcrumbs
cpputils
/
include
/
cpputils
/
string-base.h
Copy path
File metadata and controls
150 lines (137 loc) · 2.96 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
148
149
#
pragma
once
#
include
<
utility
>
#
include
<
iterator
>
#
include
<
algorithm
>
/*
* Returns the byte length of a NUL terminated string.
*/
template
<
typename
T>
size_t
stringlength
(
const
T *str)
{
size_t
len=
0
;
while
(*str++)
len++;
return
len;
}
/*
* case sensitive compare of two NUL terminated strings.
*/
template
<
typename
TA
,
typename
TB
>
int
stringcompare
(
const
TA
*a,
const
TB
*b)
{
while
(*a && *b && *a == *b)
{
a++;
b++;
}
if
(*a<*b)
return
-
1
;
if
(*a>*b)
return
1
;
return
0
;
}
/*
* Copy a NUL terminated string.
* Returns a pointer to the last element copied.
*/
template
<
typename
TA
,
typename
TB
>
TA
*
stringcopy
(
TA
*a,
const
TB
*b)
{
while
((*a++ = *b++)!=
0
)
;
return
a-
1
;
}
/*
* case insensitive character compare
*/
template
<
typename
TA
,
typename
TB
>
int
charicompare
(
TA
a,
TB
b)
{
a=(
TA
)
tolower
(a);
b=(
TB
)
tolower
(b);
if
(a<b)
return
-
1
;
if
(a>b)
return
1
;
return
0
;
}
/*
* case sensitive character compare
*/
template
<
typename
TA
,
typename
TB
>
int
charcompare
(
TA
a,
TB
b)
{
if
(a<b)
return
-
1
;
if
(a>b)
return
1
;
return
0
;
}
/*
* case insensitive compare of two NUL terminated strings.
*/
template
<
class
PA
,
class
PB
>
int
stringicompare
(
const
PA
* a,
const
PB
* b)
{
while
(*a && *b &&
charicompare
(*a, *b)==
0
)
{
a++;
b++;
}
return
charicompare
(*a, *b);
}
/*
* case insensitive compare of two stl string types.
*
* arguments can be anything which has begin and end, like:
* vector, array, string, string_view, span, etc.
*/
template
<
class
TA
,
class
TB
>
int
stringicompare
(
const
TA
& a,
const
TB
& b)
{
auto
pa=
std::begin
(a);
auto
pa_end=
std::end
(a);
auto
pb=
std::begin
(b);
auto
pb_end=
std::end
(b);
while
(pa!=pa_end && pb!=pb_end &&
charicompare
(*pa, *pb)==
0
)
{
pa++;
pb++;
}
if
(pa==pa_end && pb==pb_end)
return
0
;
if
(pa==pa_end)
return
-
1
;
if
(pb==pb_end)
return
1
;
return
charicompare
(*pa, *pb);
}
/*
* case sensitive compare of two stl string types.
*
* arguments can be anything which has begin and end, like:
* vector, array, string, string_view, span, etc.
*/
template
<
class
TA
,
class
TB
>
int
stringcompare
(
const
TA
& a,
const
TB
& b)
{
auto
pa=
std::begin
(a);
auto
pa_end=
std::end
(a);
auto
pb=
std::begin
(b);
auto
pb_end=
std::end
(b);
while
(pa!=pa_end && pb!=pb_end && *pa == *pb)
{
pa++;
pb++;
}
if
(pa==pa_end && pb==pb_end)
return
0
;
if
(pa==pa_end)
return
-
1
;
if
(pb==pb_end)
return
1
;
return
charcompare
(*pa, *pb);
}
/*
returns true when sequence 'v' starts with the same elements as 'w'
*/
template
<
typename
T,
typename
U>
bool
beginswith
(
const
T& v,
const
U& w)
{
return
v.
size
()>=w.
size
() &&
std::equal
(w.
begin
(), w.
end
(), v.
begin
());
//
return std::size(v)>=std::size(w) && std::equal(std::begin(w), std::end(w), std::begin(v));
}
Back
|
FazBrowse Home
|
New Git URL