| 265 | } |
| 266 | |
| 267 | bool Truncate(std::string & utf8, size_t const maxTextLengthPlus1) |
| 268 | { |
| 269 | size_t codePoints = 0; |
| 270 | |
| 271 | for (size_t i = 0; i < utf8.length(); ++i) |
| 272 | { |
| 273 | if ((utf8[i] & 0xC0) != 0x80) |
| 274 | { |
| 275 | if (++codePoints == maxTextLengthPlus1) |
| 276 | { |
| 277 | --i; |
| 278 | |
| 279 | auto const & byte = utf8[i]; |
| 280 | uint8_t bytesInCodepoint = 1; |
| 281 | |
| 282 | if ((byte & 0x80) == 0x00) |
| 283 | {} |
| 284 | else if ((byte & 0xE0) == 0xC0) |
| 285 | bytesInCodepoint = 2; |
| 286 | else if ((byte & 0xF0) == 0xE0) |
| 287 | bytesInCodepoint = 3; |
| 288 | else if ((byte & 0xF8) == 0xF0) |
| 289 | bytesInCodepoint = 4; |
| 290 | |
| 291 | utf8.resize(i + bytesInCodepoint); |
| 292 | return true; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | bool ReplaceFirst(std::string & str, std::string const & from, std::string const & to) |
| 300 | { |
no test coverage detected