Skip to content

Commit 6633770

Browse files
authored
std::string_view support in Rcpp::wrap() (#1356)
* std::string_view support in Rcpp::wrap() * Unit test for Rcpp::wrap(std::string_view) * Update ChangeLog * Fix test name and formatting
1 parent 6dfb816 commit 6633770

File tree

7 files changed

+34
-0
lines changed

7 files changed

+34
-0
lines changed

ChangeLog

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2025-01-31 Lev Kandel <[email protected]>
2+
3+
* inst/include/Rcpp/internal/wrap.h: Add support for std::string_view
4+
* inst/include/Rcpp/traits/r_type_traits.h: Idem
5+
* inst/include/Rcpp/traits/wrap_type_traits.h: Idem
6+
* inst/include/RcppCommon.h: Include <string_view>
7+
* inst/tinytest/cpp/wrap.cpp: Add unit test for wrap(std::string_view)
8+
* inst/tinytest/test_wrap.R: Idem
9+
110
2025-01-26 Dirk Eddelbuettel <[email protected]>
211

312
* DESCRIPTION (Version, Date): Roll micro version and date

inst/include/Rcpp/internal/wrap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ namespace Rcpp {
6666
return make_charsexp__impl__cstring(st.c_str());
6767
}
6868

69+
#if __cplusplus >= 201703L
70+
inline SEXP make_charsexp__impl__cstring(std::string_view st) {
71+
return Rf_mkCharLen(st.data(), st.size());
72+
}
73+
#endif
74+
6975
template <typename T>
7076
inline SEXP make_charsexp__impl(const T& s, Rcpp::traits::true_type) {
7177
return make_charsexp__impl__wstring(s);

inst/include/Rcpp/traits/r_type_traits.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ template<> struct r_type_traits<Rcomplex>{ typedef r_type_primitive_tag r_catego
141141
template<> struct r_type_traits<bool>{ typedef r_type_primitive_tag r_category ; } ;
142142
template<> struct r_type_traits<std::string>{ typedef r_type_string_tag r_category ; } ;
143143
template<> struct r_type_traits<std::wstring>{ typedef r_type_string_tag r_category ; } ;
144+
#if __cplusplus >= 201703L
145+
template<> struct r_type_traits<std::string_view>{ typedef r_type_string_tag r_category ; } ;
146+
#endif
144147
template<> struct r_type_traits<char>{ typedef r_type_string_tag r_category ; } ;
145148
template<> struct r_type_traits<wchar_t>{ typedef r_type_string_tag r_category ; } ;
146149

inst/include/Rcpp/traits/wrap_type_traits.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ template <> struct wrap_type_traits<unsigned int> { typedef wrap_type_primitive_
8282
template <> struct wrap_type_traits<bool> { typedef wrap_type_primitive_tag wrap_category; } ;
8383
template <> struct wrap_type_traits<std::string> { typedef wrap_type_primitive_tag wrap_category; } ;
8484
template <> struct wrap_type_traits<std::wstring> { typedef wrap_type_primitive_tag wrap_category; } ;
85+
#if __cplusplus >= 201703L
86+
template <> struct wrap_type_traits<std::string_view> { typedef wrap_type_primitive_tag wrap_category; } ;
87+
#endif
8588
template <> struct wrap_type_traits<Rcpp::String> { typedef wrap_type_primitive_tag wrap_category; } ;
8689
template <> struct wrap_type_traits<char> { typedef wrap_type_primitive_tag wrap_category; } ;
8790
template <> struct wrap_type_traits<wchar_t> { typedef wrap_type_primitive_tag wrap_category; } ;

inst/include/RcppCommon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ namespace Rcpp {
5454
#include <iomanip>
5555
#include <sstream>
5656
#include <string>
57+
#if __cplusplus >= 201703L
58+
#include <string_view>
59+
#endif
5760
#include <list>
5861
#include <map>
5962
#include <set>

inst/tinytest/cpp/wrap.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,10 @@ SEXP vector_Foo(){
304304
vec[1] = Foo( 3 ) ;
305305
return wrap(vec) ;
306306
}
307+
308+
// [[Rcpp::plugins(cpp17)]]
309+
// [[Rcpp::export]]
310+
SEXP test_wrap_string_view(){
311+
std::string_view sv = "test string value" ;
312+
return wrap(sv) ;
313+
}

inst/tinytest/test_wrap.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,6 @@ expect_equal(sapply( vector_Foo(), function(.) .$get() ), c(2, 3),
132132

133133
# test.wrap.custom.class <- function() {
134134
expect_equal(test_wrap_custom_class(), 42)
135+
136+
# test.wrap.string_view <- function() {
137+
expect_equal(test_wrap_string_view(), "test string value")

0 commit comments

Comments
 (0)