2 * utils.c - multibyte-string helpers
3 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * mbs_at_width - compute screen position in a string
30 * For displaying strings on the screen, we have to know how many character
31 * cells are occupied. This function calculates the position in a multibyte
32 * string that is at a desired position.
36 * width: on input, the desired number of character cells; on output, the actual
37 * position, in character cells, of the return value
38 * dir: -1 or 1; in which direction to round if a multi-column character goes
39 * over the desired width
42 * Pointer to the place in the string that is as near the desired width as
43 * possible. If the string is too short, the return value points to the
44 * terminating zero. If the last character is a multi-column character that
45 * goes over the desired width, the return value may be one character cell
46 * earlier or later than desired, depending on the dir parameter.
47 * In any case, the return value points after any zero-width characters that
48 * follow the last character.
50 const char *mbs_at_width(const char *s, int *width, int dir)
59 mbtowc(NULL, NULL, 0); /* reset shift state */
62 while (len && (bytes = mbtowc(&wc, s, len)) > 0) {
64 if (width_so_far + w > *width && dir < 0)
70 if (width_so_far >= *width) {
71 while (len && (bytes = mbtowc(&wc, s, len)) > 0) {
81 *width = width_so_far;
86 * get_mbs_width - compute screen width of a string
88 unsigned int get_mbs_width(const char *s)
93 mbs_at_width(s, &width, 1);
98 * get_max_mbs_width - get width of longest string in an array
100 unsigned int get_max_mbs_width(const char *const *s, unsigned int count)
102 unsigned int max_width, i, len;
105 for (i = 0; i < count; ++i) {
106 len = get_mbs_width(s[i]);