Add an optional size argument to _Py_char2wchar()
_Py_char2wchar() callers usually need the result size in characters. Since it's
trivial to compute it in _Py_char2wchar() (O(1) whereas wcslen() is O(n)), add
an option to get it.
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 98427e3..9fe9c42 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1783,17 +1783,18 @@
/* locale encoding with surrogateescape */
wchar_t *wchar;
PyObject *unicode;
+ size_t len;
if (s[size] != '\0' || size != strlen(s)) {
PyErr_SetString(PyExc_TypeError, "embedded NUL character");
return NULL;
}
- wchar = _Py_char2wchar(s);
+ wchar = _Py_char2wchar(s, &len);
if (wchar == NULL)
return NULL;
- unicode = PyUnicode_FromWideChar(wchar, -1);
+ unicode = PyUnicode_FromWideChar(wchar, len);
PyMem_Free(wchar);
return unicode;
}