string: add to_lower and tests
This commit is contained in:
parent
c254846ac2
commit
03786f3fcc
51
string.hpp
51
string.hpp
@ -21,23 +21,26 @@ namespace cruft {
|
|||||||
std::string to_utf8 (const std::wstring&);
|
std::string to_utf8 (const std::wstring&);
|
||||||
|
|
||||||
|
|
||||||
/// Convert the provided string to all upper case
|
/// Apply a functor to each character of a string and return the result.
|
||||||
inline std::string
|
template <typename FunctionT>
|
||||||
to_upper (std::string &&val)
|
std::string
|
||||||
|
transform (std::string &&val, FunctionT &&func)
|
||||||
{
|
{
|
||||||
std::transform (
|
std::transform (
|
||||||
std::begin (val),
|
std::begin (val),
|
||||||
std::end (val),
|
std::end (val),
|
||||||
std::begin (val),
|
std::begin (val),
|
||||||
::toupper
|
std::forward<FunctionT> (func)
|
||||||
);
|
);
|
||||||
|
|
||||||
return std::move (val);
|
return std::move (val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Convert the provided string to all upper case
|
/// Apply a functor to each character of a string and return the result.
|
||||||
inline std::string
|
template <typename FunctionT>
|
||||||
to_upper (std::string const &val)
|
std::string
|
||||||
|
transform (std::string const &val, FunctionT &&func)
|
||||||
{
|
{
|
||||||
std::string res;
|
std::string res;
|
||||||
res.reserve (val.size ());
|
res.reserve (val.size ());
|
||||||
@ -46,11 +49,43 @@ namespace cruft {
|
|||||||
std::begin (val),
|
std::begin (val),
|
||||||
std::end (val),
|
std::end (val),
|
||||||
std::back_inserter (res),
|
std::back_inserter (res),
|
||||||
::toupper
|
std::forward<FunctionT> (func)
|
||||||
);
|
);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Convert the provided string to all upper case
|
||||||
|
inline std::string
|
||||||
|
to_upper (std::string &&val)
|
||||||
|
{
|
||||||
|
return transform (std::move (val), ::toupper);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Convert the provided string to all upper case
|
||||||
|
inline std::string
|
||||||
|
to_upper (std::string const &val)
|
||||||
|
{
|
||||||
|
return transform (val, ::toupper);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Convert the provided string to all upper case
|
||||||
|
inline std::string
|
||||||
|
to_lower (std::string &&val)
|
||||||
|
{
|
||||||
|
return transform (std::move (val), ::tolower);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Convert the provided string to all upper case
|
||||||
|
inline std::string
|
||||||
|
to_lower (std::string const &val)
|
||||||
|
{
|
||||||
|
return transform (val, ::tolower);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,32 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
void
|
||||||
|
test_transforms (cruft::TAP::logger &tap)
|
||||||
|
{
|
||||||
|
static struct {
|
||||||
|
std::string initial;
|
||||||
|
std::string upper;
|
||||||
|
std::string lower;
|
||||||
|
|
||||||
|
char const *message;
|
||||||
|
} const TESTS[] = {
|
||||||
|
{ "a", "A", "a", "single lower character" },
|
||||||
|
{ "A", "A", "a", "single upper character" },
|
||||||
|
{ "asdf", "ASDF", "asdf", "four lower characters" },
|
||||||
|
{ "ASDF", "ASDF", "asdf", "four upper characters" },
|
||||||
|
|
||||||
|
{ "Don't.", "DON'T.", "don't.", "upper and lower and symbols" },
|
||||||
|
};
|
||||||
|
|
||||||
|
for (auto const [src, upper, lower, msg]: TESTS) {
|
||||||
|
tap.expect_eq (cruft::to_upper (src), upper, "to_upper, %!", msg);
|
||||||
|
tap.expect_eq (cruft::to_lower (src), lower, "to_lower, %!", msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void
|
void
|
||||||
test_position (cruft::TAP::logger &tap)
|
test_position (cruft::TAP::logger &tap)
|
||||||
@ -203,6 +229,7 @@ main (int, char**)
|
|||||||
{
|
{
|
||||||
cruft::TAP::logger tap;
|
cruft::TAP::logger tap;
|
||||||
|
|
||||||
|
test_transforms (tap);
|
||||||
test_tokeniser (tap);
|
test_tokeniser (tap);
|
||||||
test_position (tap);
|
test_position (tap);
|
||||||
test_contains (tap);
|
test_contains (tap);
|
||||||
|
Loading…
Reference in New Issue
Block a user