FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-lib/src/str.cpp at master · dst/cpp-lib · GitHub
dst
cpp-lib
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp-lib
/
src
/
str.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
136 lines (116 loc) · 3.24 KB
Breadcrumbs
cpp-lib
/
src
/
str.cpp
Copy path
File metadata and controls
136 lines (116 loc) · 3.24 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
/*
* stringlib.cpp
*
* Created on: Feb 18, 2012
* Author: dst
*/
#
include
<
algorithm
>
#
include
<
cassert
>
#
include
<
iterator
>
#
include
<
sstream
>
#
include
<
string
>
#
include
<
vector
>
using
std::string;
inline
bool
replace
(string& str,
const
string& from,
const
string& to) {
size_t
start_pos = str.
find
(from);
if
(start_pos == string::npos) {
return
false
;
}
str.
replace
(start_pos, from.
length
(), to);
return
true
;
}
inline
void
replaceAll
(string& str,
const
string& from,
const
string& to) {
size_t
start_pos =
0
;
while
((start_pos = str.
find
(from, start_pos)) != string::npos) {
str.
replace
(start_pos, from.
length
(), to);
//
Move forward to protect against situation when
//
to' contains 'from', like replacing 'x' with 'yx'
start_pos += to.
length
();
}
}
inline
bool
startsWith
(
const
string& str,
const
string& prefix) {
if
(str.
size
() < prefix.
size
()) {
return
false
;
}
return
str.
compare
(
0
, prefix.
length
(), prefix) ==
0
;
}
inline
bool
endsWith
(
const
string& str,
const
string& sufix) {
if
(str.
size
() < sufix.
size
()) {
return
false
;
}
return
str.
compare
(str.
size
() - sufix.
size
(), sufix.
size
(), sufix) ==
0
;
}
inline
std::vector<string>
splitString
(
const
string& str) {
std::vector<string> tokens;
std::istringstream
iss
(str);
std::copy
(std::istream_iterator<string>(iss),
std::istream_iterator<string>(),
std::back_inserter<std::vector<string> >(tokens));
return
tokens;
}
inline
std::vector<
int
>
string2ints
(
const
string& str) {
std::vector<
int
> numbers;
std::istringstream
is
(str);
int
n;
while
(is >> n) {
numbers.
push_back
(n);
}
return
numbers;
}
inline
bool
contains
(string str,
char
c) {
return
str.
find_first_of
(c) != string::npos;
}
inline
int
countChar
(
const
string& str,
char
c) {
return
std::count
(str.
begin
(), str.
end
(), c);
}
//
c++11
inline
void
removeLastChar
(string& str) {
if
(!str.
empty
()) {
str.
pop_back
();
}
}
inline
void
reverse
(string& str) {
reverse
(str.
begin
(), str.
end
());
}
//
tests
inline
void
str
() {
assert
(
startsWith
(
"
a
"
,
"
a
"
));
assert
(
startsWith
(
"
abcd
"
,
"
abc
"
));
assert
(!
startsWith
(
"
abcd
"
,
"
abe
"
));
assert
(!
startsWith
(
"
abcd
"
,
"
abcde
"
));
assert
(
endsWith
(
"
a
"
,
"
a
"
));
assert
(
endsWith
(
"
abcd
"
,
"
bcd
"
));
assert
(!
endsWith
(
"
abcd
"
,
"
bce
"
));
assert
(!
endsWith
(
"
abcd
"
,
"
abcde
"
));
//
splitString
auto
tokens =
splitString
(
"
aaa bbb
"
);
assert
(tokens[
0
] ==
"
aaa
"
);
assert
(tokens[
1
] ==
"
bbb
"
);
//
string2ints
auto
numbers =
string2ints
(
"
1 2
"
);
assert
(numbers[
0
] ==
1
);
assert
(numbers[
1
] ==
2
);
//
contains
assert
(
contains
(
"
abcde
"
,
'
e
'
));
assert
(!
contains
(
"
abcde
"
,
'
f
'
));
//
countChar
assert
(
countChar
(
"
a_b_c
"
,
'
_
'
) ==
2
);
assert
(
countChar
(
"
_abc
"
,
'
_
'
) ==
1
);
assert
(
countChar
(
"
abc_
"
,
'
_
'
) ==
1
);
assert
(
countChar
(
"
abc
"
,
'
_
'
) ==
0
);
assert
(
countChar
(
"
"
,
'
_
'
) ==
0
);
//
removeLastChar
string s =
"
abc
"
;
removeLastChar
(s);
assert
(s ==
"
ab
"
);
removeLastChar
(s);
assert
(s ==
"
a
"
);
removeLastChar
(s);
assert
(s ==
"
"
);
removeLastChar
(s);
assert
(s ==
"
"
);
//
reverse
s =
"
abc
"
;
reverse
(s);
assert
(s ==
"
cba
"
);
}
Back
|
FazBrowse Home
|
New Git URL