From 99c8e034f83ad7e34fddbff6018bf0639533f2d9 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Sun, 27 Sep 2026 01:18:25 +0100 Subject: 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. --- .guix/guile-xapian-package.scm | 7 +++-- Makefile.am | 4 +-- configure.ac | 2 +- std_string_view.i | 68 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 std_string_view.i diff --git a/.guix/guile-xapian-package.scm b/.guix/guile-xapian-package.scm index cc88be7..e8394b5 100644 --- a/.guix/guile-xapian-package.scm +++ b/.guix/guile-xapian-package.scm @@ -21,6 +21,7 @@ #:use-module ((gnu packages autotools) #:select (autoconf autoconf-archive automake libtool)) #:use-module ((gnu packages guile) #:select (guile-2.2)) #:use-module ((gnu packages guile-xyz) #:prefix guix:) + #:use-module ((gnu packages swig) #:select (swig)) #:use-module (guix gexp) #:use-module (guix git-download) #:use-module (guix packages) @@ -36,7 +37,8 @@ (const #t)))) (native-inputs (modify-inputs (package-native-inputs guix:guile-xapian) - (prepend autoconf autoconf-archive automake libtool))))) + (prepend autoconf autoconf-archive automake libtool) + (replace "swig" swig))))) (define-public guile2.2-xapian (package @@ -48,6 +50,7 @@ (const #t)))) (native-inputs (modify-inputs (package-native-inputs guix:guile2.2-xapian) - (prepend autoconf autoconf-archive automake libtool))))) + (prepend autoconf autoconf-archive automake libtool) + (replace "swig" swig))))) guile-xapian diff --git a/Makefile.am b/Makefile.am index 800cef5..9ba38b7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -47,7 +47,7 @@ CLEANFILES = xapian.i xapian_wrap.cc xapian/wrap.scm xapian.i: xapian.i.in Makefile sed -e 's|@libdir[@]|$(libdir)|g' -e 's|@GUILE_EFFECTIVE_VERSION[@]|$(GUILE_EFFECTIVE_VERSION)|g' $< > $@ -xapian_wrap.cc xapian/wrap.scm &: xapian.i xapian-head.i xapian-headers.i except.i +xapian_wrap.cc xapian/wrap.scm &: xapian.i xapian-head.i xapian-headers.i except.i std_string_view.i $(MKDIR_P) xapian $(SWIG_GEN)$(SWIG) $(SWIG_FLAGS) -I$(srcdir) -scmstub -o xapian_wrap.cc -guile -package xapian -c++ $< @@ -83,4 +83,4 @@ check-local: EXTRA_DIST = $(test_files) $(SOURCES) \ examples/100-objects.sexp examples/index.scm examples/search.scm \ - except.i xapian.i.in xapian-head.i xapian-headers.i + except.i std_string_view.i xapian.i.in xapian-head.i xapian-headers.i diff --git a/configure.ac b/configure.ac index 93930da..7573248 100644 --- a/configure.ac +++ b/configure.ac @@ -23,7 +23,7 @@ AM_SILENT_RULES([yes]) LT_INIT AC_PROG_CXX -AX_PKG_SWIG +AX_PKG_SWIG([4.2], [], [ AC_MSG_ERROR([swig >= 4.2 is required]) ]) GUILE_PKG([3.0 2.2]) PKG_CHECK_MODULES([GUILE], [guile-3.0],,[ PKG_CHECK_MODULES([GUILE], [guile-2.2]) 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 +#include +%} + +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; + +} -- cgit 1.4.1