diff options
author | Xavier G <xavier.github@kindwolf.org> | 2016-05-13 02:47:26 +0200 |
---|---|---|
committer | Xavier G <xavier.github@kindwolf.org> | 2016-05-13 02:47:26 +0200 |
commit | 35b3ccc6a407f83eba4f0c3787cc5c174bd3385c (patch) | |
tree | 94e9a946b78f3942678e468959a8bebb7b6e714f /src/core/utf8.c | |
parent | 21c07c006066115af4604e26cd89cf60f94a7d53 (diff) | |
download | irssi-35b3ccc6a407f83eba4f0c3787cc5c174bd3385c.zip |
Introduce string_length() and string_width().
Diffstat (limited to 'src/core/utf8.c')
-rw-r--r-- | src/core/utf8.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/core/utf8.c b/src/core/utf8.c index c9303d19..7c75c374 100644 --- a/src/core/utf8.c +++ b/src/core/utf8.c @@ -55,3 +55,37 @@ int string_policy(const char *str) } return TREAT_STRING_AS_BYTES; } + +int string_length(const char *str, int policy) +{ + g_return_val_if_fail(str != NULL, 0); + + if (policy == -1) { + policy = string_policy(str); + } + + if (policy == TREAT_STRING_AS_UTF8) { + return g_utf8_strlen(str, -1); + } + else { + /* Assume TREAT_STRING_AS_BYTES: */ + return strlen(str); + } +} + +int string_width(const char *str, int policy) +{ + int len; + + g_return_val_if_fail(str != NULL, 0); + + if (policy == -1) { + policy = string_policy(str); + } + + len = 0; + while (*str != '\0') { + len += string_advance(&str, policy); + } + return len; +} |