about summary refs log tree commit diff
path: root/std_string_view.i
diff options
context:
space:
mode:
authorArun Isaac2026-09-27 01:18:25 +0100
committerArun Isaac2026-09-27 01:38:36 +0100
commit99c8e034f83ad7e34fddbff6018bf0639533f2d9 (patch)
tree61e250c65f6edf08d07951e54800f68b386c14a3 /std_string_view.i
parentce500331047b4f949e50c905c83ed121aa317f72 (diff)
downloadguile-xapian-99c8e034f83ad7e34fddbff6018bf0639533f2d9.tar.gz
guile-xapian-99c8e034f83ad7e34fddbff6018bf0639533f2d9.tar.lz
guile-xapian-99c8e034f83ad7e34fddbff6018bf0639533f2d9.zip
Support std::string_view in swig.
swig does not yet support std::string_view. We add support here for
our immediate use. We will contribute this upstream to the swig
project.
Diffstat (limited to 'std_string_view.i')
-rw-r--r--std_string_view.i68
1 files changed, 68 insertions, 0 deletions
diff --git a/std_string_view.i b/std_string_view.i
new file mode 100644
index 0000000..585281f
--- /dev/null
+++ b/std_string_view.i
@@ -0,0 +1,68 @@
+/* -----------------------------------------------------------------------------
+ * std_string_view.i
+ *
+ * std::string_view typemaps for Guile
+ * ----------------------------------------------------------------------------- */
+
+%{
+#include <string>
+#include <string_view>
+%}
+
+namespace std {
+
+  %naturalvar string_view;
+
+  %typemap(in) string_view (std::string temp) {
+    char *ptr = scm_to_stringn($input, NULL, "utf8",
+                               SCM_FAILED_CONVERSION_ERROR);
+    temp.assign(ptr);
+    SWIG_free(ptr);
+    $1 = std::string_view(temp);
+  }
+
+  %typemap(out) string_view %{
+    $result = scm_from_stringn($1.data(), $1.size(), "utf8",
+                               SCM_FAILED_CONVERSION_ERROR);
+    %}
+
+  %typemap(in) const string_view& (std::string temp, $*1_ltype view) {
+    char *ptr = scm_to_stringn($input, NULL, "utf8",
+                               SCM_FAILED_CONVERSION_ERROR);
+    temp.assign(ptr);
+    SWIG_free(ptr);
+    view = std::string_view(temp);
+    $1 = &view;
+  }
+
+  %typemap(out) const string_view& %{
+    $result = scm_from_stringn($1->data(), $1->size(), "utf8",
+                               SCM_FAILED_CONVERSION_ERROR);
+    %}
+
+  %typemap(varout) string_view %{
+    $result = scm_from_stringn($1.data(), $1.size(), "utf8",
+                               SCM_FAILED_CONVERSION_ERROR);
+    %}
+
+  // for throwing of any kind of string_view, string_view ref's and
+  // string_view pointers we convert all to Guile strings
+  %typemap(throws) string_view, string_view&, const string_view& %{
+    scm_throw(scm_from_locale_symbol("swig-exception"),
+              scm_list_1(scm_from_stringn($1.data(), $1.size(), "utf8",
+                                          SCM_FAILED_CONVERSION_ERROR)));
+    %}
+
+  %typemap(throws) string_view*, const string_view* %{
+    scm_throw(scm_from_locale_symbol("swig-exception"),
+              scm_list_1(scm_from_stringn($1->data(), $1->size(), "utf8",
+                                          SCM_FAILED_CONVERSION_ERROR)));
+    %}
+
+  %typemap(typecheck,precedence=SWIG_TYPECHECK_STRINGVIEW) string_view, const string_view& {
+    $1 = scm_is_string($input);
+  }
+
+  class string_view;
+
+}