[Rcpp-commits] r4238 - in pkg/Rcpp: . inst/include/Rcpp
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Feb 4 16:16:02 CET 2013
Author: romain
Date: 2013-02-04 16:16:01 +0100 (Mon, 04 Feb 2013)
New Revision: 4238
Modified:
pkg/Rcpp/ChangeLog
pkg/Rcpp/inst/include/Rcpp/String.h
Log:
added support for wstring in Rcpp::String
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2013-02-04 14:43:57 UTC (rev 4237)
+++ pkg/Rcpp/ChangeLog 2013-02-04 15:16:01 UTC (rev 4238)
@@ -8,7 +8,8 @@
* include/Rcpp/internal/wrap.h : rework the support of wstring
* include/Rcpp/internal/export.h : added as_string_elt to rework the support
of wstring
- * include/Rcpp/traits/char_type.h : new trait to help the wstring support
+ * include/Rcpp/traits/char_type.h : new trait to help the wstring support
+ * include/Rcpp/String.h : added some support for wstring
2013-02-03 Romain Francois <romain at r-enthusiasts.com>
Modified: pkg/Rcpp/inst/include/Rcpp/String.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/String.h 2013-02-04 14:43:57 UTC (rev 4237)
+++ pkg/Rcpp/inst/include/Rcpp/String.h 2013-02-04 15:16:01 UTC (rev 4238)
@@ -2,8 +2,8 @@
//
// String.h: Rcpp R/C++ interface class library -- single string
//
-// Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
-// Copyright (C) 2012 Rice University
+// Copyright (C) 2012 - 2013 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2012 - 2013 Rice University
//
// This file is part of Rcpp.
//
@@ -77,11 +77,20 @@
RCPP_STRING_DEBUG( "String(const std::string& )" ) ;
}
+ String( const std::wstring& s) : data(internal::make_charsexp(s)), valid(true), buffer_ready(false) {
+ RCPP_STRING_DEBUG( "String(const std::wstring& )" ) ;
+ }
+
/** from a const char* */
String( const char* s) : buffer(s), valid(false), buffer_ready(true){
RCPP_STRING_DEBUG( "String(const char*)" ) ;
}
+ String( const wchar_t* s) : data(internal::make_charsexp(s)), valid(true), buffer_ready(false) {
+ RCPP_STRING_DEBUG( "String(const wchar_t* s)" ) ;
+ }
+
+
/** constructors from R primitives */
String( int x ) : data( internal::r_coerce<INTSXP,STRSXP>(x) ), valid(true), buffer_ready(false) {}
String( double x ) : data( internal::r_coerce<REALSXP,STRSXP>(x) ), valid(true), buffer_ready(false){}
@@ -96,11 +105,25 @@
inline String& operator=( bool x ){ data = internal::r_coerce<LGLSXP ,STRSXP>( x ) ; valid = true ; buffer_ready = false ; return *this ; }
inline String& operator=( Rcomplex x){ data = internal::r_coerce<CPLXSXP,STRSXP>( x ) ; valid = true ; buffer_ready = false ; return *this ; }
inline String& operator=( SEXP x){ data = x ; valid = true ; buffer_ready = false ; return *this ; }
+ inline String& operator=( const StringProxy& proxy){ data = proxy.get() ; valid = true ; buffer_ready=false ; return *this ; }
+ inline String& operator=( const String& other ){ data = other.get_sexp() ; valid = true ; buffer_ready = false ; return *this ; }
inline String& operator=( const std::string& s){ buffer = s ; valid = false ; buffer_ready = true ; return *this ; }
inline String& operator=( const char* s){ buffer = s ; valid = false ; buffer_ready = true ; return *this ; }
- inline String& operator=( const StringProxy& proxy){ data = proxy.get() ; valid = true ; buffer_ready=false ; return *this ; }
- inline String& operator=( const String& other ){ data = other.get_sexp() ; valid = true ; buffer_ready = false ; return *this ; }
+ private:
+ template <typename T>
+ inline String& assign_wide_string( const T& s){
+ data = internal::make_charsexp( s ) ;
+ valid = true ;
+ buffer_ready = false ;
+ return *this ;
+ }
+
+ public:
+ inline String& operator=( const std::wstring& s){ return assign_wide_string(s) ; }
+ inline String& operator=( const wchar_t* s){ return assign_wide_string(s) ; }
+
+
inline String& operator+=( const std::string& s){
RCPP_STRING_DEBUG( "String::operator+=( std::string )" ) ;
if( is_na() ) return *this ;
@@ -112,7 +135,27 @@
if( is_na() ) return *this ;
setBuffer() ; buffer += s ; valid = false ;
return *this ;
- }
+ }
+ private:
+ template <typename T>
+ inline String& append_wide_string( const T& s){
+ RCPP_STRING_DEBUG_1( "String::operator+=( %s )", DEMANGLE(T) ) ;
+ setData() ;
+ if( is_na() ) return *this ;
+ const char* buf = CHAR( data );
+ std::wstring tmp( buf, buf + strlen(buf ) ) ;
+ tmp += s ;
+ data = internal::make_charsexp( tmp ) ;
+ valid = true ;
+ buffer_ready = false ;
+ return *this ;
+ }
+
+ public:
+
+ inline String& operator+=( const std::wstring& s){ return append_wide_string( s ); }
+ inline String& operator+=( const wchar_t* s){ return append_wide_string( s ); }
+
inline String& operator+=( const String& other ){
RCPP_STRING_DEBUG( "String::operator+=( const char*)" ) ;
if( is_na() ) return *this ;
@@ -271,6 +314,12 @@
return get_cstring() ;
}
+ inline operator std::wstring() const {
+ const char* s = get_cstring() ;
+ return std::wstring( s, s + strlen(s) );
+ }
+
+
inline const char* get_cstring() const {
return buffer_ready ? buffer.c_str() : CHAR(data) ;
}
More information about the Rcpp-commits
mailing list