diff --git a/modules/04_arrays_and_strings/include/std_string.hpp b/modules/04_arrays_and_strings/include/std_string.hpp index 1b0fa9a..8377141 100644 --- a/modules/04_arrays_and_strings/include/std_string.hpp +++ b/modules/04_arrays_and_strings/include/std_string.hpp @@ -4,9 +4,9 @@ namespace arrays { -[[nodiscard]] auto to_uppercase(std::string text) -> std::string; +[[nodiscard]] auto to_uppercase(const std::string &text) -> std::string; [[nodiscard]] auto trim(const std::string& text) -> std::string; -[[nodiscard]] auto replace_all(std::string text, char from, char to) -> std::string; +[[nodiscard]] auto replace_all(const std::string &text, char from, char to) -> std::string; [[nodiscard]] auto starts_with(const std::string& text, const std::string& prefix) -> bool; } // namespace arrays diff --git a/modules/04_arrays_and_strings/src/std_string.cpp b/modules/04_arrays_and_strings/src/std_string.cpp index 9f1f729..e03de4a 100644 --- a/modules/04_arrays_and_strings/src/std_string.cpp +++ b/modules/04_arrays_and_strings/src/std_string.cpp @@ -1,10 +1,46 @@ #include "std_string.hpp" +#include +#include +#include +#include + namespace arrays { -auto to_uppercase(std::string text) -> std::string { (void)text; return {}; } -auto trim(const std::string& text) -> std::string { (void)text; return {}; } -auto replace_all(std::string text, char from, char to) -> std::string { (void)text; (void)from; (void)to; return {}; } -auto starts_with(const std::string& text, const std::string& prefix) -> bool { (void)text; (void)prefix; return false; } +auto to_uppercase(const std::string &text) -> std::string { + // Returns copy of the string without mutating the original string + auto s = std::string{text}; + auto c_to_upper = [](const unsigned char c) { return std::toupper(c); }; + std::ranges::transform(s, s.begin(), c_to_upper); + return s; +} + +auto trim(const std::string& text) -> std::string { + // Returns a trimmed copy of the string + auto s = std::string{text}; + auto is_not_space = [](const unsigned char c) { return !std::isspace(c); }; + const auto first = std::ranges::find_if(s, is_not_space); + const auto last = std::ranges::find_if(s.rbegin(), s.rend(), is_not_space).base(); + s.erase(last, s.end()); + s.erase(s.begin(), first); + return s; +} + +auto replace_all(const std::string &text, const char from, const char to) -> std::string { + // This one also copies the string + auto s = std::string{text}; + std::ranges::replace(s, from, to); + return s; +} + +auto starts_with(const std::string& text, const std::string& prefix) -> bool { + // Since C++20 you can use std::string::starts_with, but anyways. + if (text.size() < prefix.size()) return false; + // uz -> unsigned size_t + for (auto idx = 0uz; idx < prefix.size(); ++idx) { + if (text[idx] != prefix[idx]) return false; + } + return true; +} } // namespace arrays diff --git a/modules/04_arrays_and_strings/test/std_string_test.cpp b/modules/04_arrays_and_strings/test/std_string_test.cpp index 4cd21d1..55e5c2f 100644 --- a/modules/04_arrays_and_strings/test/std_string_test.cpp +++ b/modules/04_arrays_and_strings/test/std_string_test.cpp @@ -7,6 +7,7 @@ TEST(StdString, ToUppercase) { TEST(StdString, Trim) { EXPECT_EQ(arrays::trim(" hi "), "hi"); + EXPECT_EQ(arrays::trim(" a b c "), "a b c"); } TEST(StdString, ReplaceAll) {