* Compute the byte distance to the end of the string or *target_width * display character positions, whichever comes first. Update *target_width * to be the number of display character positions actually filled. */
| 3622 | * to be the number of display character positions actually filled. |
| 3623 | */ |
| 3624 | static int |
| 3625 | strlen_max_width(unsigned char *str, int *target_width, int encoding) |
| 3626 | { |
| 3627 | unsigned char *start = str; |
| 3628 | unsigned char *end = str + strlen((char *) str); |
| 3629 | int curr_width = 0; |
| 3630 | |
| 3631 | while (str < end) |
| 3632 | { |
| 3633 | int char_width = PQdsplen((char *) str, encoding); |
| 3634 | |
| 3635 | /* |
| 3636 | * If the display width of the new character causes the string to |
| 3637 | * exceed its target width, skip it and return. However, if this is |
| 3638 | * the first character of the string (curr_width == 0), we have to |
| 3639 | * accept it. |
| 3640 | */ |
| 3641 | if (*target_width < curr_width + char_width && curr_width != 0) |
| 3642 | break; |
| 3643 | |
| 3644 | curr_width += char_width; |
| 3645 | |
| 3646 | str += PQmblen((char *) str, encoding); |
| 3647 | |
| 3648 | if (str > end) /* Don't overrun invalid string */ |
| 3649 | str = end; |
| 3650 | } |
| 3651 | |
| 3652 | *target_width = curr_width; |
| 3653 | |
| 3654 | return str - start; |
| 3655 | } |
no test coverage detected