| 8147 | } |
| 8148 | |
| 8149 | bool utf32_to_punycode(std::u32string_view input, std::string &out) { |
| 8150 | out.reserve(input.size() + out.size()); |
| 8151 | uint32_t n = initial_n; |
| 8152 | int32_t d = 0; |
| 8153 | int32_t bias = initial_bias; |
| 8154 | size_t h = 0; |
| 8155 | // first push the ascii content |
| 8156 | for (uint32_t c : input) { |
| 8157 | if (c < 0x80) { |
| 8158 | ++h; |
| 8159 | out.push_back(char(c)); |
| 8160 | } |
| 8161 | if (c > 0x10ffff || (c >= 0xd800 && c < 0xe000)) { |
| 8162 | return false; |
| 8163 | } |
| 8164 | } |
| 8165 | size_t b = h; |
| 8166 | if (b > 0) { |
| 8167 | out.push_back('-'); |
| 8168 | } |
| 8169 | while (h < input.size()) { |
| 8170 | uint32_t m = 0x10FFFF; |
| 8171 | for (auto code_point : input) { |
| 8172 | if (code_point >= n && code_point < m) m = code_point; |
| 8173 | } |
| 8174 | |
| 8175 | if ((m - n) > (0x7fffffff - d) / (h + 1)) { |
| 8176 | return false; |
| 8177 | } |
| 8178 | d = d + int32_t((m - n) * (h + 1)); |
| 8179 | n = m; |
| 8180 | for (auto c : input) { |
| 8181 | if (c < n) { |
| 8182 | if (d == 0x7fffffff) { |
| 8183 | return false; |
| 8184 | } |
| 8185 | ++d; |
| 8186 | } |
| 8187 | if (c == n) { |
| 8188 | int32_t q = d; |
| 8189 | for (int32_t k = base;; k += base) { |
| 8190 | int32_t t = k <= bias ? tmin : k >= bias + tmax ? tmax : k - bias; |
| 8191 | |
| 8192 | if (q < t) { |
| 8193 | break; |
| 8194 | } |
| 8195 | out.push_back(digit_to_char(t + ((q - t) % (base - t)))); |
| 8196 | q = (q - t) / (base - t); |
| 8197 | } |
| 8198 | out.push_back(digit_to_char(q)); |
| 8199 | bias = adapt(d, int32_t(h + 1), h == b); |
| 8200 | d = 0; |
| 8201 | ++h; |
| 8202 | } |
| 8203 | } |
| 8204 | ++d; |
| 8205 | ++n; |
| 8206 | } |
no test coverage detected