aboutsummaryrefslogtreecommitdiff
path: root/Lib/typemaps/std_strings.swg
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/typemaps/std_strings.swg')
-rw-r--r--Lib/typemaps/std_strings.swg25
1 files changed, 25 insertions, 0 deletions
diff --git a/Lib/typemaps/std_strings.swg b/Lib/typemaps/std_strings.swg
index e9c23ba91..842545826 100644
--- a/Lib/typemaps/std_strings.swg
+++ b/Lib/typemaps/std_strings.swg
@@ -45,6 +45,16 @@ SWIG_From_dec(String)(const String& s)
}
%enddef
+%define %std_string_view_from(String, SWIG_FromCharPtrAndSize, Frag)
+%fragment(SWIG_From_frag(String),"header",fragment=Frag) {
+SWIGINTERNINLINE SWIG_Object
+SWIG_From_dec(String)(const String& s)
+{
+ return SWIG_FromCharPtrAndSize(s.data() ? s.data() : "", s.size());
+}
+}
+%enddef
+
%define %std_string_asval(String)
%fragment(SWIG_AsVal_frag(String),"header", fragment=SWIG_AsPtr_frag(String)) {
SWIGINTERN int
@@ -76,3 +86,18 @@ SWIG_AsVal_dec(String)(SWIG_Object obj, String *val)
%typemaps_asptrfromn(%arg(CheckCode), String);
%enddef
+
+
+/* An empty string_view returns NULL from data() but SWIG_FromCharPtrAndSize()
+ * implementations treat that as invalid and return None/Null/undef or similar
+ * in the target language so we can't just use %typemaps_std_string.
+ */
+%define %typemaps_std_string_view(String, Char, AsPtrMethod, FromMethod, CheckCode)
+
+%std_string_asptr(String, Char, AsPtrMethod, #AsPtrMethod)
+%std_string_asval(String)
+%std_string_view_from(String, FromMethod, #FromMethod)
+
+%typemaps_asptrfromn(%arg(CheckCode), String);
+
+%enddef