string: add to_lower and tests

This commit is contained in:
Danny Robson 2019-11-26 08:10:04 +11:00
parent c254846ac2
commit 03786f3fcc
2 changed files with 70 additions and 8 deletions

View File

@ -21,23 +21,26 @@ namespace cruft {
std::string to_utf8 (const std::wstring&);
/// Convert the provided string to all upper case
inline std::string
to_upper (std::string &&val)
/// Apply a functor to each character of a string and return the result.
template <typename FunctionT>
std::string
transform (std::string &&val, FunctionT &&func)
{
std::transform (
std::begin (val),
std::end (val),
std::begin (val),
::toupper
std::forward<FunctionT> (func)
);
return std::move (val);
}
/// Convert the provided string to all upper case
inline std::string
to_upper (std::string const &val)
/// Apply a functor to each character of a string and return the result.
template <typename FunctionT>
std::string
transform (std::string const &val, FunctionT &&func)
{
std::string res;
res.reserve (val.size ());
@ -46,11 +49,43 @@ namespace cruft {
std::begin (val),
std::end (val),
std::back_inserter (res),
::toupper
std::forward<FunctionT> (func)
);
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);
}
}

View File

@ -6,6 +6,32 @@
#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
test_position (cruft::TAP::logger &tap)
@ -203,6 +229,7 @@ main (int, char**)
{
cruft::TAP::logger tap;
test_transforms (tap);
test_tokeniser (tap);
test_position (tap);
test_contains (tap);