Skip to content

Commit 521c4e2

Browse files
committed
refactor(string): make StrType::asUCS4 a static method
1 parent 69d0e48 commit 521c4e2

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

include/StrType.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public:
5050
* @return PyObject* - the UCS4-encoding of the pyObject string
5151
*
5252
*/
53-
PyObject *asUCS4();
53+
static PyObject *asUCS4(PyObject *pyObject);
5454
};
5555

5656
#endif

src/StrType.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ StrType::StrType(JSContext *cx, JSString *str) {
103103

104104
if (containsSurrogatePair(chars, length)) {
105105
// We must convert to UCS4 here because Python does not support decoding string containing surrogate pairs to bytes
106-
PyObject *ucs4Obj = this->asUCS4(); // convert `pyObject` to a new PyUnicodeObject with UCS4 data
106+
PyObject *ucs4Obj = asUCS4(pyObject); // convert to a new PyUnicodeObject with UCS4 data
107107
if (!ucs4Obj) {
108108
// conversion fails, keep the original `pyObject`
109109
return;
@@ -118,7 +118,8 @@ const char *StrType::getValue() const {
118118
return PyUnicode_AsUTF8(pyObject);
119119
}
120120

121-
PyObject *StrType::asUCS4() {
121+
/* static */
122+
PyObject *StrType::asUCS4(PyObject *pyObject) {
122123
if (PyUnicode_KIND(pyObject) != PyUnicode_2BYTE_KIND) {
123124
return Py_NewRef(pyObject);
124125
}

src/modules/pythonmonkey/pythonmonkey.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ static PyObject *collect(PyObject *self, PyObject *args) {
143143
}
144144

145145
static PyObject *asUCS4(PyObject *self, PyObject *args) {
146-
StrType *str = new StrType(PyTuple_GetItem(args, 0));
147-
if (!PyUnicode_Check(str->getPyObject())) {
146+
PyObject *str = PyTuple_GetItem(args, 0);
147+
if (!PyUnicode_Check(str)) {
148148
PyErr_SetString(PyExc_TypeError, "pythonmonkey.asUCS4 expects a string as its first argument");
149149
return NULL;
150150
}
151151

152-
PyObject *ucs4Str = str->asUCS4();
152+
PyObject *ucs4Str = StrType::asUCS4(str);
153153
if (!ucs4Str) {
154154
PyErr_SetString(PyExc_UnicodeTranslateError, "string contains an unpaired surrogates");
155155
return NULL;

0 commit comments

Comments
 (0)