|
| 1 | +#ifndef IRIS_ZZ_STRING_ALGO_HPP |
| 2 | +#define IRIS_ZZ_STRING_ALGO_HPP |
| 3 | + |
| 4 | +// SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +#include <iris/config.hpp> |
| 7 | +#include <iris/string.hpp> |
| 8 | + |
| 9 | +#include <string> |
| 10 | +#include <string_view> |
| 11 | +#include <ranges> |
| 12 | +#include <algorithm> |
| 13 | + |
| 14 | +namespace iris { |
| 15 | + |
| 16 | +namespace detail { |
| 17 | + |
| 18 | +template<class CharT> |
| 19 | +struct string_algo_traits; |
| 20 | + |
| 21 | +template<> |
| 22 | +struct string_algo_traits<char> |
| 23 | +{ |
| 24 | + static constexpr char space = ' '; |
| 25 | + static constexpr std::string_view ordinary_spaces{"\u0020\t\r\n"}; |
| 26 | + static constexpr std::string_view space_like_variant_chars{"\t\r\n"}; |
| 27 | +}; |
| 28 | + |
| 29 | +template<> |
| 30 | +struct string_algo_traits<char32_t> |
| 31 | +{ |
| 32 | + static constexpr char32_t space = U' '; |
| 33 | + static constexpr std::u32string_view ordinary_spaces{U"\u0020\u3000\t\r\n"}; // includes Japanese space |
| 34 | + static constexpr std::u32string_view space_like_variant_chars{U"\u3000\t\r\n"}; |
| 35 | +}; |
| 36 | + |
| 37 | +} // detail |
| 38 | + |
| 39 | +template<class CharT, class Traits> |
| 40 | +constexpr void trim_edges( |
| 41 | + std::basic_string<CharT, Traits>& input, |
| 42 | + std::basic_string_view<CharT, Traits> const spaces = detail::string_algo_traits<CharT>::ordinary_spaces |
| 43 | +) |
| 44 | +{ |
| 45 | + auto const first = input.find_first_not_of(spaces); |
| 46 | + if (first == std::basic_string<CharT, Traits>::npos) { |
| 47 | + input.clear(); |
| 48 | + return; |
| 49 | + } |
| 50 | + input.erase(0, first); |
| 51 | + |
| 52 | + auto const last = input.find_last_not_of(spaces); |
| 53 | + input.erase(last + 1); |
| 54 | +} |
| 55 | + |
| 56 | +template<int = 0> |
| 57 | +[[nodiscard]] constexpr std::string trim_edges_copy( |
| 58 | + std::string_view input, |
| 59 | + std::string_view const spaces = detail::string_algo_traits<char>::ordinary_spaces |
| 60 | +) |
| 61 | +{ |
| 62 | + std::string buf(input); |
| 63 | + iris::trim_edges(buf, spaces); |
| 64 | + return buf; |
| 65 | +} |
| 66 | + |
| 67 | +template<int = 0> |
| 68 | +[[nodiscard]] constexpr std::u32string trim_edges_copy( |
| 69 | + std::u32string_view input, |
| 70 | + std::u32string_view const spaces = detail::string_algo_traits<char32_t>::ordinary_spaces |
| 71 | +) |
| 72 | +{ |
| 73 | + std::u32string buf(input); |
| 74 | + iris::trim_edges(buf, spaces); |
| 75 | + return buf; |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +template<class CharT, class Traits> |
| 80 | +constexpr void normalize_spaces( |
| 81 | + std::basic_string<CharT, Traits>& input, |
| 82 | + std::basic_string_view<CharT, Traits> const space_like_variant_chars = detail::string_algo_traits<CharT>::space_like_variant_chars, |
| 83 | + CharT const to_space = detail::string_algo_traits<CharT>::space |
| 84 | +) |
| 85 | +{ |
| 86 | + std::ranges::replace_if(input, [&](CharT const ch) { |
| 87 | + return space_like_variant_chars.contains(ch); |
| 88 | + }, to_space); |
| 89 | +} |
| 90 | + |
| 91 | +template<int = 0> |
| 92 | +[[nodiscard]] constexpr std::string normalize_spaces_copy( |
| 93 | + std::string_view input, |
| 94 | + std::string_view const space_like_variant_chars = detail::string_algo_traits<char>::space_like_variant_chars, |
| 95 | + char const to_space = detail::string_algo_traits<char>::space |
| 96 | +) |
| 97 | +{ |
| 98 | + std::string buf(input); |
| 99 | + iris::normalize_spaces(buf, space_like_variant_chars, to_space); |
| 100 | + return buf; |
| 101 | +} |
| 102 | + |
| 103 | +template<int = 0> |
| 104 | +[[nodiscard]] constexpr std::u32string normalize_spaces_copy( |
| 105 | + std::u32string_view input, |
| 106 | + std::u32string_view const space_like_variant_chars = detail::string_algo_traits<char32_t>::space_like_variant_chars, |
| 107 | + char32_t const to_space = detail::string_algo_traits<char32_t>::space |
| 108 | +) |
| 109 | +{ |
| 110 | + std::u32string buf(input); |
| 111 | + iris::normalize_spaces(buf, space_like_variant_chars, to_space); |
| 112 | + return buf; |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +template<class CharT, class Traits> |
| 117 | +constexpr void compact_spaces( |
| 118 | + std::basic_string<CharT, Traits>& input, |
| 119 | + std::basic_string_view<CharT, Traits> const spaces = detail::string_algo_traits<CharT>::ordinary_spaces, |
| 120 | + CharT const to_space = detail::string_algo_traits<CharT>::space |
| 121 | +) |
| 122 | +{ |
| 123 | + iris::trim_edges(input, spaces); |
| 124 | + if (input.empty()) return; |
| 125 | + |
| 126 | + std::size_t w = 0; |
| 127 | + bool in_space = false; |
| 128 | + |
| 129 | + for (CharT const c : input) { |
| 130 | + if (spaces.contains(c)) { |
| 131 | + if (!in_space) { |
| 132 | + input[w++] = to_space; |
| 133 | + } |
| 134 | + in_space = true; |
| 135 | + |
| 136 | + } else { |
| 137 | + input[w++] = c; |
| 138 | + in_space = false; |
| 139 | + } |
| 140 | + } |
| 141 | + input.resize(w); |
| 142 | +} |
| 143 | + |
| 144 | +template<int = 0> |
| 145 | +[[nodiscard]] constexpr std::string compact_spaces_copy( |
| 146 | + std::string_view input, |
| 147 | + std::string_view const spaces = detail::string_algo_traits<char>::ordinary_spaces, |
| 148 | + char const to_space = detail::string_algo_traits<char>::space |
| 149 | +) |
| 150 | +{ |
| 151 | + std::string buf(input); |
| 152 | + iris::compact_spaces(buf, spaces, to_space); |
| 153 | + return buf; |
| 154 | +} |
| 155 | + |
| 156 | +template<int = 0> |
| 157 | +[[nodiscard]] constexpr std::u32string compact_spaces_copy( |
| 158 | + std::u32string_view input, |
| 159 | + std::u32string_view const spaces = detail::string_algo_traits<char32_t>::ordinary_spaces, |
| 160 | + char32_t const to_space = detail::string_algo_traits<char32_t>::space |
| 161 | +) |
| 162 | +{ |
| 163 | + std::u32string buf(input); |
| 164 | + iris::compact_spaces(buf, spaces, to_space); |
| 165 | + return buf; |
| 166 | +} |
| 167 | + |
| 168 | +} // iris |
| 169 | + |
| 170 | +#endif |
0 commit comments