Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Guilhem Saurel
hpp-util
Commits
23c7ef8e
Commit
23c7ef8e
authored
Dec 17, 2020
by
Joseph Mirabel
Browse files
Add hpp/util/string.hh
parent
fa81206b
Changes
4
Hide whitespace changes
Inline
Side-by-side
CMakeLists.txt
View file @
23c7ef8e
...
...
@@ -49,6 +49,7 @@ SET(${PROJECT_NAME}_HEADERS
include/hpp/util/factories/sequence.hh
include/hpp/util/serialization.hh
include/hpp/util/serialization-fwd.hh
include/hpp/util/string.hh
)
SET
(
${
PROJECT_NAME
}
_SOURCES
...
...
include/hpp/util/string.hh
0 → 100644
View file @
23c7ef8e
// Copyright (C) 2020 by Joseph Mirabel, CNRS.
//
// This file is part of the hpp-util.
//
// This software is provided "as is" without warranty of any kind,
// either expressed or implied, including but not limited to the
// implied warranties of fitness for a particular purpose.
//
// See the COPYING file for more information.
#ifndef HPP_UTIL_STRING_HH
# define HPP_UTIL_STRING_HH
# include <hpp/util/config.hh>
#include
<vector>
#include
<string>
#include
<cstring>
#include
<iterator>
#include
<algorithm>
namespace
hpp
{
namespace
util
{
template
<
typename
InputIt
,
typename
Predicate
>
bool
string_split
(
InputIt
first
,
InputIt
last
,
const
char
&
c
,
Predicate
p
)
{
while
(
true
)
{
InputIt
next
=
std
::
find
(
first
,
last
,
c
);
if
(
p
(
first
,
next
))
return
true
;
if
(
next
==
last
)
return
false
;
first
=
std
::
next
(
next
);
}
}
template
<
typename
InputIt
,
typename
Predicate
>
bool
string_split
(
InputIt
first
,
InputIt
last
,
const
char
*
c
,
Predicate
p
)
{
auto
n
=
std
::
strlen
(
c
);
while
(
true
)
{
InputIt
next
=
std
::
find_if
(
first
,
last
,
[
&
c
,
&
n
](
char
l
)
->
bool
{
return
c
+
n
!=
std
::
find
(
c
,
c
+
n
,
l
);
});
if
(
p
(
first
,
next
))
return
true
;
if
(
next
==
last
)
return
false
;
first
=
std
::
next
(
next
);
}
}
template
<
typename
InputIt
>
std
::
vector
<
std
::
string
>
string_split
(
InputIt
first
,
InputIt
last
,
const
char
&
c
)
{
std
::
vector
<
std
::
string
>
strings
;
string_split
(
first
,
last
,
c
,
[
&
strings
](
InputIt
begin
,
InputIt
end
)
{
strings
.
emplace_back
(
&
(
*
begin
),
std
::
distance
(
begin
,
end
));
return
false
;
});
return
strings
;
}
template
<
typename
InputIt
>
std
::
vector
<
std
::
string
>
string_split
(
InputIt
first
,
InputIt
last
,
const
char
*
c
)
{
std
::
vector
<
std
::
string
>
strings
;
string_split
(
first
,
last
,
c
,
[
&
strings
](
InputIt
begin
,
InputIt
end
)
{
strings
.
emplace_back
(
&
(
*
begin
),
std
::
distance
(
begin
,
end
));
return
false
;
});
return
strings
;
}
inline
bool
iequal
(
const
std
::
string
&
a
,
const
std
::
string
&
b
)
{
return
(
a
.
size
()
==
b
.
size
())
&&
std
::
equal
(
a
.
begin
(),
a
.
end
(),
b
.
begin
(),
[](
char
a
,
char
b
)
->
bool
{
return
std
::
tolower
(
a
)
==
std
::
tolower
(
b
);
});
}
}
// end of namespace util.
}
// end of namespace hpp.
#endif //! HPP_UTIL_STRING_HH
tests/CMakeLists.txt
View file @
23c7ef8e
...
...
@@ -29,6 +29,7 @@ DEFINE_TEST(assertion)
DEFINE_TEST
(
exception
)
DEFINE_TEST
(
exception-factory
)
DEFINE_TEST
(
timer
)
DEFINE_TEST
(
string
)
ADD_UNIT_TEST
(
serialization serialization.cc serialization-test.cc
)
TARGET_LINK_LIBRARIES
(
serialization
${
PROJECT_NAME
}
)
...
...
tests/string.cc
0 → 100644
View file @
23c7ef8e
// Copyright (c) 2020, Joseph Mirabel
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
//
// This file is part of hpp-util.
// hpp-util is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// hpp-util is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Lesser Public License for more details. You should have
// received a copy of the GNU Lesser General Public License along with
// hpp-util. If not, see <http://www.gnu.org/licenses/>.
#include
<iostream>
#include
<hpp/util/string.hh>
#include
"common.hh"
using
namespace
hpp
::
util
;
int
run_test
()
{
std
::
string
str
(
"avq vfb
\t
cfdsqf
\n
dfqs;"
);
string_split
(
str
.
begin
(),
str
.
end
(),
"
\t\n
"
,
[](
decltype
(
str
.
begin
())
begin
,
decltype
(
str
.
begin
())
end
)
{
std
::
cout
<<
std
::
string
(
&
(
*
begin
),
std
::
distance
(
begin
,
end
))
<<
"
\n
"
;
return
false
;
});
if
(
iequal
(
"abd"
,
"AB"
))
return
TEST_FAILED
;
if
(
!
iequal
(
"abd"
,
"ABd"
))
return
TEST_FAILED
;
if
(
iequal
(
"abd"
,
"Acd"
))
return
TEST_FAILED
;
return
TEST_SUCCEED
;
}
GENERATE_TEST
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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