File tree Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -4,17 +4,28 @@ import errno
4
4
import os
5
5
6
6
7
+ from cpython.mem cimport PyMem_Free
8
+ from libc.stddef cimport wchar_t
9
+
7
10
cdef extern from " wchar.h" :
8
- cdef int wcswidth(const Py_UNICODE * str , size_t n)
11
+ # https://www.man7.org/linux/man-pages/man3/wcswidth.3.html
12
+ cdef int wcswidth(const wchar_t * s, size_t n)
13
+
14
+
15
+ cdef extern from " Python.h" :
16
+ # https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_AsWideCharString
17
+ wchar_t* PyUnicode_AsWideCharString(object , Py_ssize_t* ) except NULL
9
18
10
19
11
20
def swidth (s ):
12
- str_len = len (s)
13
- terminal_width = wcswidth(s, str_len)
21
+ cdef Py_ssize_t size
22
+ cdef wchar_t * as_wchar = PyUnicode_AsWideCharString(s, & size)
23
+ terminal_width = wcswidth(as_wchar, < size_t> size)
24
+ PyMem_Free(as_wchar)
14
25
if terminal_width >= 0 :
15
26
return terminal_width
16
27
else :
17
- return str_len
28
+ return len (s)
18
29
19
30
20
31
def process_alive (host , pid , thread ):
You can’t perform that action at this time.
0 commit comments