aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/libshiboken/sbkcppstring.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sources/shiboken6/libshiboken/sbkcppstring.cpp')
-rw-r--r--sources/shiboken6/libshiboken/sbkcppstring.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/sources/shiboken6/libshiboken/sbkcppstring.cpp b/sources/shiboken6/libshiboken/sbkcppstring.cpp
new file mode 100644
index 000000000..8e8324f5e
--- /dev/null
+++ b/sources/shiboken6/libshiboken/sbkcppstring.cpp
@@ -0,0 +1,54 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+#include "sbkcppstring.h"
+#include "autodecref.h"
+
+namespace Shiboken::String
+{
+
+PyObject *fromCppString(const std::string &value)
+{
+ return PyUnicode_FromStringAndSize(value.data(), value.size());
+}
+
+PyObject *fromCppStringView(std::string_view value)
+{
+ return PyUnicode_FromStringAndSize(value.data(), value.size());
+}
+
+PyObject *fromCppWString(const std::wstring &value)
+{
+ return PyUnicode_FromWideChar(value.data(), value.size());
+}
+
+void toCppString(PyObject *str, std::string *value)
+{
+ value->clear();
+
+ if (str == Py_None)
+ return;
+
+ if (PyUnicode_Check(str)) {
+ if (PyUnicode_GetLength(str) > 0)
+ value->assign(_PepUnicode_AsString(str));
+ return;
+ }
+
+ if (PyBytes_Check(str))
+ value->assign(PyBytes_AS_STRING(str));
+}
+
+void toCppWString(PyObject *str, std::wstring *value)
+{
+ value->clear();
+
+ if (str == Py_None || PyUnicode_Check(str) == 0 || PyUnicode_GetLength(str) == 0)
+ return;
+
+ wchar_t *w = PyUnicode_AsWideCharString(str, nullptr);
+ value->assign(w);
+ PyMem_Free(w);
+}
+
+} // namespace Shiboken::String