From ada3f9e54768cbb25d8b72df87e61a3418d9e3c4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 31 Mar 2021 15:19:39 +0530 Subject: [PATCH] Add a wcswidth() implementation useable from C code --- kitty/wcswidth.c | 9 +++++++++ kitty/wcswidth.h | 1 + 2 files changed, 10 insertions(+) diff --git a/kitty/wcswidth.c b/kitty/wcswidth.c index 940cc1a97..f75915dfb 100644 --- a/kitty/wcswidth.c +++ b/kitty/wcswidth.c @@ -118,6 +118,15 @@ wcswidth_step(WCSState *state, const char_type ch) { return ans; } +size_t +wcswidth_string(const char_type *s) { + WCSState state; + initialize_wcs_state(&state); + size_t ans = 0; + while (*s) ans += wcswidth_step(&state, *(s++)); + return ans; +} + PyObject * wcswidth_std(PyObject UNUSED *self, PyObject *str) { if (PyUnicode_READY(str) != 0) return NULL; diff --git a/kitty/wcswidth.h b/kitty/wcswidth.h index 05bea98bb..016574daa 100644 --- a/kitty/wcswidth.h +++ b/kitty/wcswidth.h @@ -20,3 +20,4 @@ typedef struct { void initialize_wcs_state(WCSState *state); int wcswidth_step(WCSState *state, const char_type ch); PyObject * wcswidth_std(PyObject UNUSED *self, PyObject *str); +size_t wcswidth_string(const char_type *s);