Returns the number of bytes from the start of `text` whose rendered width does not exceed max_width, without ever splitting a multi-byte character (or ANSI escape sequence) in half. Always advances past at least one character when text is non-empty, even if that character alone is wider than max_width, so callers that force-split long words always make progress. Mirrors get_sequence_length()'s wid
| 190 | // progress. Mirrors get_sequence_length()'s width accounting exactly, so a |
| 191 | // prefix of this many bytes is guaranteed to measure as <= max_width. |
| 192 | inline size_t byte_offset_for_width(const std::string &text, const std::string &locale, |
| 193 | bool is_multi_byte_character_support_enabled, |
| 194 | size_t max_width) { |
| 195 | if (!is_multi_byte_character_support_enabled) |
| 196 | return (std::min)(max_width, text.size()); |
| 197 | |
| 198 | #if defined(_WIN32) || defined(_WIN64) |
| 199 | (void)locale; |
| 200 | size_t consumed_width = 0; |
| 201 | bool first_character = true; |
| 202 | const char *p = text.c_str(); |
| 203 | while (*p != '\0') { |
| 204 | if (static_cast<unsigned char>(*p) == 0x1b) { |
| 205 | p = skip_ansi_escape_sequence(p); |
| 206 | continue; |
| 207 | } |
| 208 | const char *character_start = p; |
| 209 | ++p; |
| 210 | while (*p != '\0' && (static_cast<unsigned char>(*p) & 0xC0) == 0x80) |
| 211 | ++p; // UTF-8 continuation byte: part of the same character |
| 212 | if (!first_character && consumed_width + 1 > max_width) |
| 213 | return static_cast<size_t>(character_start - text.c_str()); |
| 214 | consumed_width += 1; |
| 215 | first_character = false; |
| 216 | } |
| 217 | return text.size(); |
| 218 | #elif defined(__unix__) || defined(__unix) || defined(__APPLE__) |
| 219 | const char *previous_locale = std::setlocale(LC_CTYPE, nullptr); |
| 220 | const std::string saved_locale = previous_locale != nullptr ? previous_locale : ""; |
| 221 | std::setlocale(LC_CTYPE, locale.c_str()); |
| 222 | |
| 223 | const char *p = text.c_str(); |
| 224 | std::mbstate_t state = std::mbstate_t(); |
| 225 | size_t consumed_width = 0; |
| 226 | bool first_character = true; |
| 227 | size_t result = text.size(); |
| 228 | |
| 229 | while (*p != '\0') { |
| 230 | if (static_cast<unsigned char>(*p) == 0x1b) { |
| 231 | p = skip_ansi_escape_sequence(p); |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | const char *character_start = p; |
| 236 | wchar_t wide_character; |
| 237 | size_t length = std::mbrtowc(&wide_character, p, MB_CUR_MAX, &state); |
| 238 | |
| 239 | if (length == static_cast<size_t>(-2)) |
| 240 | break; // truncated multi-byte character at the end of the string |
| 241 | |
| 242 | if (length == static_cast<size_t>(-1)) { |
| 243 | state = std::mbstate_t(); |
| 244 | length = 1; // invalid byte: treat as width 1 and resynchronize |
| 245 | } else if (length == 0) { |
| 246 | length = 1; // embedded null: treat as a 1-byte character |
| 247 | } |
| 248 | |
| 249 | int character_width = wcwidth(wide_character); |
no test coverage detected