Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
jlib
Commits
303f3530
Commit
303f3530
authored
5 years ago
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
a2deb999
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
47 deletions
+31
-47
str_util.h
jlib/util/str_util.h
+19
-35
test_strutil.cpp
test/test_strutil/test_strutil.cpp
+12
-12
No files found.
jlib/util/str_util.h
View file @
303f3530
...
...
@@ -12,43 +12,29 @@ namespace jlib
// Taken from https://stackoverflow.com/a/217605/2963736
// Thanks https://stackoverflow.com/users/13430/evan-teran
// trim from start (in place)
template
<
typename
StringType
>
inline
void
ltrim
(
StringType
&
s
)
{
s
.
erase
(
s
.
begin
(),
std
::
find_if
(
s
.
begin
(),
s
.
end
(),
[](
int
ch
)
{
return
!
std
::
isspace
(
ch
);
}));
}
// trim from end (in place)
template
<
typename
StringType
>
inline
void
rtrim
(
StringType
&
s
)
{
s
.
erase
(
std
::
find_if
(
s
.
rbegin
(),
s
.
rend
(),
[](
int
ch
)
{
return
!
std
::
isspace
(
ch
);
}).
base
(),
s
.
end
());
}
// trim from both ends (in place)
template
<
typename
StringType
>
inline
void
trim
(
StringType
&
s
)
{
ltrim
(
s
);
rtrim
(
s
);
}
inline
void
trim
(
StringType
&
s
)
{
ltrim
(
s
);
rtrim
(
s
);
}
// trim from start (copying)
template
<
typename
StringType
>
inline
StringType
ltrim_copy
(
StringType
s
)
{
ltrim
(
s
);
return
s
;
}
inline
StringType
ltrim_copy
(
StringType
s
)
{
ltrim
(
s
);
return
s
;
}
// trim from end (copying)
template
<
typename
StringType
>
inline
StringType
rtrim_copy
(
StringType
s
)
{
rtrim
(
s
);
return
s
;
}
inline
StringType
rtrim_copy
(
StringType
s
)
{
rtrim
(
s
);
return
s
;
}
// trim from both ends (copying)
template
<
typename
StringType
>
inline
StringType
trim_copy
(
StringType
s
)
{
trim
(
s
);
return
s
;
}
inline
StringType
trim_copy
(
StringType
s
)
{
trim
(
s
);
return
s
;
}
/**************************** join ***************************/
...
...
@@ -58,7 +44,7 @@ inline StringType trim_copy(StringType s) {
* @note StringContainer 必须为上述StringType的iterable容器类,如vector,list
*/
template
<
typename
StringType
,
typename
StringContainer
>
StringType
join
(
const
StringContainer
&
container
,
const
StringType
&
conjunction
=
StringType
())
inline
StringType
join
(
const
StringContainer
&
container
,
const
StringType
&
conjunction
=
StringType
())
{
StringType
result
;
auto
itBegin
=
container
.
cbegin
();
...
...
@@ -75,25 +61,23 @@ StringType join(const StringContainer& container, const StringType& conjunction
}
/****************************
remov
e_all ***************************/
/****************************
eras
e_all ***************************/
// taken from https://stackoverflow.com/a/20326454
/**
* @brief 从字符串str内移除所有c字符
*/
inline
void
remove_all
(
std
::
string
&
str
,
char
c
)
{
inline
void
erase_all
(
std
::
string
&
str
,
char
c
)
{
str
.
erase
(
std
::
remove
(
str
.
begin
(),
str
.
end
(),
c
),
str
.
end
());
}
inline
void
remov
e_all
(
std
::
wstring
&
str
,
wchar_t
c
)
{
inline
void
eras
e_all
(
std
::
wstring
&
str
,
wchar_t
c
)
{
str
.
erase
(
std
::
remove
(
str
.
begin
(),
str
.
end
(),
c
),
str
.
end
());
}
inline
std
::
string
remov
e_all_copy
(
std
::
string
str
,
char
c
)
{
inline
std
::
string
eras
e_all_copy
(
std
::
string
str
,
char
c
)
{
str
.
erase
(
std
::
remove
(
str
.
begin
(),
str
.
end
(),
c
),
str
.
end
());
return
str
;
}
inline
std
::
wstring
remov
e_all_copy
(
std
::
wstring
str
,
wchar_t
c
)
{
inline
std
::
wstring
eras
e_all_copy
(
std
::
wstring
str
,
wchar_t
c
)
{
str
.
erase
(
std
::
remove
(
str
.
begin
(),
str
.
end
(),
c
),
str
.
end
());
return
str
;
}
...
...
@@ -101,38 +85,38 @@ inline std::wstring remove_all_copy(std::wstring str, wchar_t c) {
/**************************** case-conv ***************************/
void
to_upper
(
std
::
string
&
str
)
{
inline
void
to_upper
(
std
::
string
&
str
)
{
std
::
for_each
(
str
.
begin
(),
str
.
end
(),
[](
char
&
c
)
{
c
=
::
toupper
(
c
);
});
}
void
to_upper
(
std
::
wstring
&
str
)
{
inline
void
to_upper
(
std
::
wstring
&
str
)
{
std
::
for_each
(
str
.
begin
(),
str
.
end
(),
[](
wchar_t
&
c
)
{
c
=
::
toupper
(
c
);
});
}
std
::
string
to_upper_copy
(
std
::
string
str
)
{
inline
std
::
string
to_upper_copy
(
std
::
string
str
)
{
std
::
for_each
(
str
.
begin
(),
str
.
end
(),
[](
char
&
c
)
{
c
=
::
toupper
(
c
);
});
return
str
;
}
std
::
wstring
to_upper_copy
(
std
::
wstring
str
)
{
inline
std
::
wstring
to_upper_copy
(
std
::
wstring
str
)
{
std
::
for_each
(
str
.
begin
(),
str
.
end
(),
[](
wchar_t
&
c
)
{
c
=
::
toupper
(
c
);
});
return
str
;
}
void
to_lower
(
std
::
string
&
str
)
{
inline
void
to_lower
(
std
::
string
&
str
)
{
std
::
for_each
(
str
.
begin
(),
str
.
end
(),
[](
char
&
c
)
{
c
=
::
tolower
(
c
);
});
}
void
to_lower
(
std
::
wstring
&
str
)
{
inline
void
to_lower
(
std
::
wstring
&
str
)
{
std
::
for_each
(
str
.
begin
(),
str
.
end
(),
[](
wchar_t
&
c
)
{
c
=
::
tolower
(
c
);
});
}
std
::
string
to_lower_copy
(
std
::
string
str
)
{
inline
std
::
string
to_lower_copy
(
std
::
string
str
)
{
std
::
for_each
(
str
.
begin
(),
str
.
end
(),
[](
char
&
c
)
{
c
=
::
tolower
(
c
);
});
return
str
;
}
std
::
wstring
to_lower_copy
(
std
::
wstring
str
)
{
inline
std
::
wstring
to_lower_copy
(
std
::
wstring
str
)
{
std
::
for_each
(
str
.
begin
(),
str
.
end
(),
[](
wchar_t
&
c
)
{
c
=
::
tolower
(
c
);
});
return
str
;
}
...
...
This diff is collapsed.
Click to expand it.
test/test_strutil/test_strutil.cpp
View file @
303f3530
...
...
@@ -41,22 +41,22 @@ int main()
// remove_all
{
string
str
=
"aabbcc"
;
assert
(
remov
e_all_copy
(
str
,
'a'
)
==
"bbcc"
);
assert
(
remov
e_all_copy
(
str
,
'b'
)
==
"aacc"
);
assert
(
remov
e_all_copy
(
str
,
'c'
)
==
"aabb"
);
assert
(
eras
e_all_copy
(
str
,
'a'
)
==
"bbcc"
);
assert
(
eras
e_all_copy
(
str
,
'b'
)
==
"aacc"
);
assert
(
eras
e_all_copy
(
str
,
'c'
)
==
"aabb"
);
remov
e_all
(
str
,
'a'
);
assert
(
str
==
"bbcc"
);
remov
e_all
(
str
,
'b'
);
assert
(
str
==
"cc"
);
remov
e_all
(
str
,
'c'
);
assert
(
str
==
""
);
eras
e_all
(
str
,
'a'
);
assert
(
str
==
"bbcc"
);
eras
e_all
(
str
,
'b'
);
assert
(
str
==
"cc"
);
eras
e_all
(
str
,
'c'
);
assert
(
str
==
""
);
wstring
wstr
=
L"aabbcc"
;
assert
(
remov
e_all_copy
(
wstr
,
L'a'
)
==
L"bbcc"
);
assert
(
remov
e_all_copy
(
wstr
,
L'b'
)
==
L"aacc"
);
assert
(
remov
e_all_copy
(
wstr
,
L'c'
)
==
L"aabb"
);
assert
(
eras
e_all_copy
(
wstr
,
L'a'
)
==
L"bbcc"
);
assert
(
eras
e_all_copy
(
wstr
,
L'b'
)
==
L"aacc"
);
assert
(
eras
e_all_copy
(
wstr
,
L'c'
)
==
L"aabb"
);
remov
e_all
(
wstr
,
L'a'
);
assert
(
wstr
==
L"bbcc"
);
remov
e_all
(
wstr
,
L'b'
);
assert
(
wstr
==
L"cc"
);
remov
e_all
(
wstr
,
L'c'
);
assert
(
wstr
==
L""
);
eras
e_all
(
wstr
,
L'a'
);
assert
(
wstr
==
L"bbcc"
);
eras
e_all
(
wstr
,
L'b'
);
assert
(
wstr
==
L"cc"
);
eras
e_all
(
wstr
,
L'c'
);
assert
(
wstr
==
L""
);
}
// case-conv
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment