Solved: 15 - STD String
This commit is contained in:
@@ -4,9 +4,9 @@
|
|||||||
|
|
||||||
namespace arrays {
|
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 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;
|
[[nodiscard]] auto starts_with(const std::string& text, const std::string& prefix) -> bool;
|
||||||
|
|
||||||
} // namespace arrays
|
} // namespace arrays
|
||||||
|
|||||||
@@ -1,10 +1,46 @@
|
|||||||
#include "std_string.hpp"
|
#include "std_string.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <locale>
|
||||||
|
#include <ranges>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
namespace arrays {
|
namespace arrays {
|
||||||
|
|
||||||
auto to_uppercase(std::string text) -> std::string { (void)text; return {}; }
|
auto to_uppercase(const std::string &text) -> std::string {
|
||||||
auto trim(const std::string& text) -> std::string { (void)text; return {}; }
|
// Returns copy of the string without mutating the original string
|
||||||
auto replace_all(std::string text, char from, char to) -> std::string { (void)text; (void)from; (void)to; return {}; }
|
auto s = std::string{text};
|
||||||
auto starts_with(const std::string& text, const std::string& prefix) -> bool { (void)text; (void)prefix; return false; }
|
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
|
} // namespace arrays
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ TEST(StdString, ToUppercase) {
|
|||||||
|
|
||||||
TEST(StdString, Trim) {
|
TEST(StdString, Trim) {
|
||||||
EXPECT_EQ(arrays::trim(" hi "), "hi");
|
EXPECT_EQ(arrays::trim(" hi "), "hi");
|
||||||
|
EXPECT_EQ(arrays::trim(" a b c "), "a b c");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StdString, ReplaceAll) {
|
TEST(StdString, ReplaceAll) {
|
||||||
|
|||||||
Reference in New Issue
Block a user