mirror of
https://github.com/irssi/irssi.git
synced 2026-08-21 01:22:26 +02:00
Introduce string_chars_for_width().
This commit is contained in:
parent
35b3ccc6a4
commit
719efc44a3
2 changed files with 50 additions and 0 deletions
|
|
@ -89,3 +89,47 @@ int string_width(const char *str, int policy)
|
||||||
}
|
}
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int string_chars_for_width(const char *str, int policy, unsigned int n, unsigned int *bytes)
|
||||||
|
{
|
||||||
|
const char *c, *previous_c;
|
||||||
|
int str_width, char_width, char_count;
|
||||||
|
|
||||||
|
g_return_val_if_fail(str != NULL, -1);
|
||||||
|
|
||||||
|
/* Handle the dummy case where n is 0: */
|
||||||
|
if (!n) {
|
||||||
|
if (bytes) {
|
||||||
|
*bytes = 0;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (policy == -1) {
|
||||||
|
policy = string_policy(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Iterate over characters until we reach n: */
|
||||||
|
char_count = 0;
|
||||||
|
str_width = 0;
|
||||||
|
c = str;
|
||||||
|
while (*c != '\0') {
|
||||||
|
previous_c = c;
|
||||||
|
char_width = string_advance(&c, policy);
|
||||||
|
if (str_width + char_width > n) {
|
||||||
|
/* We stepped beyond n, get one step back and stop there: */
|
||||||
|
c = previous_c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++ char_count;
|
||||||
|
str_width += char_width;
|
||||||
|
}
|
||||||
|
/* At this point, we know that char_count characters reach str_width
|
||||||
|
* columns, which is less than or equal to n. */
|
||||||
|
|
||||||
|
/* Optionally provide the equivalent amount of bytes: */
|
||||||
|
if (bytes) {
|
||||||
|
*bytes = c - str;
|
||||||
|
}
|
||||||
|
return char_count;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,12 @@ int string_length(const char *str, int policy);
|
||||||
*/
|
*/
|
||||||
int string_width(const char *str, int policy);
|
int string_width(const char *str, int policy);
|
||||||
|
|
||||||
|
/* Return the amount of characters from str it takes to reach n columns, or -1 if
|
||||||
|
* str is NULL. Optionally return the equivalent amount of bytes.
|
||||||
|
* If policy is -1, this function will call string_policy().
|
||||||
|
*/
|
||||||
|
int string_chars_for_width(const char *str, int policy, unsigned int n, unsigned int *bytes);
|
||||||
|
|
||||||
#define unichar_isprint(c) (((c) & ~0x80) >= 32)
|
#define unichar_isprint(c) (((c) & ~0x80) >= 32)
|
||||||
#define is_utf8_leading(c) (((c) & 0xc0) != 0x80)
|
#define is_utf8_leading(c) (((c) & 0xc0) != 0x80)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue