Add a wcswidth() implementation useable from C code

This commit is contained in:
Kovid Goyal 2021-03-31 15:19:39 +05:30
parent 0372242d12
commit ada3f9e547
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 10 additions and 0 deletions

View File

@ -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;

View File

@ -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);