diff options
Diffstat (limited to '')
| -rw-r--r-- | src/string.hpp | 209 |
1 files changed, 95 insertions, 114 deletions
diff --git a/src/string.hpp b/src/string.hpp index 24d0425..dee1966 100644 --- a/src/string.hpp +++ b/src/string.hpp @@ -5,155 +5,136 @@ #pragma once +#include <algorithm> #include <cctype> #include <cstddef> - -#include <algorithm> #include <locale> #include <sstream> #include <string> #include <type_traits> #include <vector> -namespace string -{ - template <typename Char> - void ltrim(std::basic_string<Char>& s) - { - s.erase(s.begin(), std::find_if(s.begin(), s.end(), [] (const Char& c) - { - return !std::isspace(c); - })); - } - - template <typename Char> - void rtrim(std::basic_string<Char>& s) - { - s.erase(std::find_if(s.rbegin(), s.rend(), [] (const Char& c) - { - return !std::isspace(c); - }).base(), s.end()); - } +namespace string { - template <typename Char> - void trim(std::basic_string<Char>& s) - { - ltrim(s); - rtrim(s); - } +template <typename Char> +void ltrim(std::basic_string<Char>& s) { + s.erase(s.begin(), + std::find_if(s.begin(), s.end(), [](const Char& c) { return !std::isspace(c); })); +} - template <typename Char, typename Sep, typename InputIterator> - std::basic_string<Char> join( - const Sep& sep, - InputIterator beg, - InputIterator end) - { - std::basic_ostringstream<Char> oss; +template <typename Char> +void rtrim(std::basic_string<Char>& s) { + s.erase( + std::find_if(s.rbegin(), s.rend(), [](const Char& c) { return !std::isspace(c); }).base(), + s.end()); +} - if (beg != end) - { - oss << *beg; - ++beg; - } +template <typename Char> +void trim(std::basic_string<Char>& s) { + ltrim(s); + rtrim(s); +} - for (; beg != end; ++beg) - oss << sep << *beg; +template <typename Char, typename Sep, typename InputIterator> +std::basic_string<Char> join(const Sep& sep, InputIterator beg, InputIterator end) { + std::basic_ostringstream<Char> oss; - return oss.str(); + if (beg != end) { + oss << *beg; + ++beg; } - template <typename Char, typename Sep> - std::basic_string<Char> join( - const Sep& sep, - const std::vector<std::basic_string<Char>>& args) - { - return join<Char>(sep, args.cbegin(), args.cend()); - } + for (; beg != end; ++beg) + oss << sep << *beg; - template <typename Char, typename String, typename = void> - struct StringHelper - { }; + return oss.str(); +} - template <typename Char, typename String> - struct StringHelper<Char, String, typename std::enable_if<std::is_same<typename std::decay<typename std::remove_pointer<String>::type>::type, Char>::value>::type> - { - StringHelper(const Char& c) - : buf{&c} - , len{1} - { } +template <typename Char, typename Sep> +std::basic_string<Char> join(const Sep& sep, const std::vector<std::basic_string<Char>>& args) { + return join<Char>(sep, args.cbegin(), args.cend()); +} - StringHelper(const Char* s) - : buf{s} - , len{std::char_traits<Char>::length(s)} - { } +template <typename Char, typename String, typename = void> +struct StringHelper {}; - const Char* buffer() const { return buf; } +template <typename Char, typename String> +struct StringHelper<Char, + String, + typename std::enable_if<std::is_same< + typename std::decay<typename std::remove_pointer<String>::type>::type, + Char>::value>::type> { + StringHelper(const Char& c) : buf{&c}, len{1} {} - std::size_t length() const { return len; } + StringHelper(const Char* s) : buf{s}, len{std::char_traits<Char>::length(s)} {} - private: - const Char* const buf; - const std::size_t len; - }; + const Char* buffer() const { + return buf; + } - template <typename Char, typename String> - struct StringHelper<Char, String, typename std::enable_if<std::is_same<String, std::basic_string<Char>>::value>::type> - { - StringHelper(const std::basic_string<Char>& s) - : s{s} - { } + std::size_t length() const { + return len; + } - const Char* buffer() const { return s.c_str(); } +private: + const Char* const buf; + const std::size_t len; +}; - std::size_t length() const { return s.length(); } +template <typename Char, typename String> +struct StringHelper< + Char, + String, + typename std::enable_if<std::is_same<String, std::basic_string<Char>>::value>::type> { + StringHelper(const std::basic_string<Char>& s) : s{s} {} - private: - const std::basic_string<Char>& s; - }; + const Char* buffer() const { + return s.c_str(); + } - template <typename Char, typename What, typename By> - void replace( - std::basic_string<Char>& s, - const What& what, - const By& by) - { - std::size_t pos = 0; + std::size_t length() const { + return s.length(); + } - const StringHelper<Char, typename std::decay<What>::type> what_helper{what}; - const StringHelper<Char, typename std::decay<By>::type> by_helper{by}; +private: + const std::basic_string<Char>& s; +}; - const auto what_buf = what_helper.buffer(); - const auto what_len = what_helper.length(); +template <typename Char, typename What, typename By> +void replace(std::basic_string<Char>& s, const What& what, const By& by) { + std::size_t pos = 0; - const auto by_buf = by_helper.buffer(); - const auto by_len = by_helper.length(); + const StringHelper<Char, typename std::decay<What>::type> what_helper{what}; + const StringHelper<Char, typename std::decay<By>::type> by_helper{by}; - while ((pos = s.find(what_buf, pos, what_len)) != std::basic_string<Char>::npos) - { - s.replace(pos, what_len, by_buf, by_len); - pos += by_len; - } + const auto what_buf = what_helper.buffer(); + const auto what_len = what_helper.length(); + + const auto by_buf = by_helper.buffer(); + const auto by_len = by_helper.length(); + + while ((pos = s.find(what_buf, pos, what_len)) != std::basic_string<Char>::npos) { + s.replace(pos, what_len, by_buf, by_len); + pos += by_len; } +} - template <typename Char, typename What> - void prefix_with( - std::basic_string<Char>& s, - const What& what, - const Char& by) - { - const StringHelper<Char, typename std::decay<What>::type> what_helper{what}; +template <typename Char, typename What> +void prefix_with(std::basic_string<Char>& s, const What& what, const Char& by) { + const StringHelper<Char, typename std::decay<What>::type> what_helper{what}; - const auto what_buf = what_helper.buffer(); - const auto what_len = what_helper.length(); + const auto what_buf = what_helper.buffer(); + const auto what_len = what_helper.length(); - std::size_t numof_by = 0; + std::size_t numof_by = 0; - for (std::size_t i = 0; i < what_len; ++i) - numof_by += std::count(s.cbegin(), s.cend(), what_buf[i]); + for (std::size_t i = 0; i < what_len; ++i) + numof_by += std::count(s.cbegin(), s.cend(), what_buf[i]); - s.reserve(s.capacity() + numof_by); + s.reserve(s.capacity() + numof_by); - for (std::size_t i = 0; i < what_len; ++i) - replace(s, what_buf[i], by); - } + for (std::size_t i = 0; i < what_len; ++i) + replace(s, what_buf[i], by); } + +} // namespace string |
