22 #ifndef Rcpp__String_h
23 #define Rcpp__String_h
25 #ifndef RCPP_STRING_DEBUG_LEVEL
26 #define RCPP_STRING_DEBUG_LEVEL 0
30 #if RCPP_STRING_DEBUG_LEVEL > 0
31 #define RCPP_STRING_DEBUG_FORMAT "%40s:%4d "
32 #define RCPP_STRING_DEBUG(MSG) Rprintf(RCPP_STRING_DEBUG_FORMAT "%s\n" , ::Rcpp::internal::debug::short_file_name(__FILE__).c_str(), __LINE__, MSG);
33 #define RCPP_STRING_DEBUG_1(fmt, MSG) Rprintf(RCPP_STRING_DEBUG_FORMAT fmt "\n" , ::Rcpp::internal::debug::short_file_name(__FILE__).c_str(), __LINE__, MSG);
34 #define RCPP_STRING_DEBUG_2(fmt, M1, M2) Rprintf(RCPP_STRING_DEBUG_FORMAT fmt "\n" , ::Rcpp::internal::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2);
35 #define RCPP_STRING_DEBUG_3(fmt, M1, M2, M3) Rprintf(RCPP_STRING_DEBUG_FORMAT fmt "\n" , ::Rcpp::internal::debug::short_file_name(__FILE__).c_str(), __LINE__, M1, M2, M3);
37 #define RCPP_STRING_DEBUG(MSG)
38 #define RCPP_STRING_DEBUG_1(fmt, MSG)
39 #define RCPP_STRING_DEBUG_2(fmt, M1, M2)
40 #define RCPP_STRING_DEBUG_3(fmt, M1, M2, M3)
71 if (TYPEOF(charsxp) == STRSXP) {
72 data = STRING_ELT(charsxp, 0);
73 }
else if (TYPEOF(charsxp) == CHARSXP) {
77 if (::Rf_isString(
data) && ::Rf_length(
data) != 1) {
78 const char* fmt =
"Expecting a single string value: "
79 "[type=%s; extent=%i].";
80 throw ::Rcpp::not_compatible(fmt,
81 Rf_type2char(TYPEOF(
data)),
120 #ifdef RCPP_USING_CXX11
125 s.token = R_NilValue;
126 s.buffer = std::string();
128 s.buffer_ready =
true;
180 data = internal::r_coerce<INTSXP, STRSXP>(x);
188 data = internal::r_coerce<REALSXP, STRSXP>(x);
196 data = internal::r_coerce<RAWSXP, STRSXP>(x);
204 data = internal::r_coerce<LGLSXP, STRSXP>(x);
212 data = internal::r_coerce<CPLXSXP, STRSXP>(x);
230 SEXP x = proxy.
get();
270 #ifdef RCPP_USING_CXX11
274 buffer = std::move(other.buffer);
279 other.data = R_NilValue;
280 other.token = R_NilValue;
281 other.buffer = std::string();
283 other.buffer_ready =
true;
302 template <
typename T>
304 data = internal::make_charsexp(s);
318 if (
is_na())
return *
this;
325 if (
is_na())
return *
this;
330 template <
typename T>
334 if (
is_na())
return *
this;
335 const char* buf = CHAR(
data);
336 std::wstring tmp(buf, buf + strlen(buf));
338 data = internal::make_charsexp(tmp);
353 if (
is_na())
return *
this;
367 if (
is_na())
return *
this;
368 SEXP proxy_sexp = proxy;
369 if (proxy_sexp == NA_STRING) {
382 if (
is_na())
return *
this;
383 SEXP proxy_sexp = proxy;
384 if (proxy_sexp == NA_STRING) {
397 if (
is_na())
return *
this;
398 if (x == NA_STRING) {
417 RCPP_STRING_DEBUG_2(
"String::replace_first(const char* = '%s' , const char* = '%s')", s, news);
418 if (
is_na())
return *
this;
420 std::string s2 = std::string(s);
421 size_t index = std::distance(
buffer.begin(), std::search(
buffer.begin(),
buffer.end(), s2.begin(), s2.end()));
422 if (index != std::string::npos)
buffer.replace(index, strlen(s), news);
428 if (s.
is_na())
return *
this;
433 if (news.
is_na())
return *
this;
443 RCPP_STRING_DEBUG_2(
"String::replace_last(const char* = '%s' , const char* = '%s')", s, news);
444 if (
is_na())
return *
this;
446 std::string s2 = std::string(s);
447 size_t index = std::distance(
buffer.begin(), std::find_end(
buffer.begin(),
buffer.end(), s2.begin(), s2.end()));
448 if (index != std::string::npos)
buffer.replace(index, strlen(s), news);
454 if (s.
is_na())
return *
this;
459 if (news.
is_na())
return *
this;
470 RCPP_STRING_DEBUG_2(
"String::replace_all(const char* = '%s' , const char* = '%s')", s, news);
471 if (
is_na())
return *
this;
473 std::string s2 = std::string(s);
474 std::string::iterator iter =
buffer.begin();
476 iter = std::search(iter,
buffer.end(), s2.begin(), s2.end());
477 if (iter ==
buffer.end())
break;
478 size_t index = std::distance(
buffer.begin(), iter);
479 if (index != std::string::npos)
buffer.replace(index, strlen(s), news);
485 template <
typename LHS,
typename RHS>
492 if (s.
is_na())
return *
this;
497 if (news.
is_na())
return *
this;
507 if (
is_na())
return *
this;
515 if (
is_na())
return *
this;
521 if (
is_na())
return *
this;
529 if (
is_na())
return *
this;
551 if (
buffer.find(
'\0') != std::string::npos)
552 throw embedded_nul_in_string();
567 inline operator std::string()
const {
571 inline operator std::wstring()
const {
573 return std::wstring(s, s + strlen(s));
589 const char* translated = Rf_translateCharUTF8(
data);
590 data = Rf_mkCharCE(translated, encoding);
681 template <
int RTYPE,
template <
class>
class StoragePolicy>
689 RCPP_DEBUG(
"string_element_converter::get< Rcpp::String >()")
698 template <
int RTYPE,
template <
class>
class StoragePolicy>
699 template <
typename T>
714 SEXP data =
object.get_sexp();
715 SET_STRING_ELT(res, 0, data);
738 #if defined(RCPP_USING_CXX11) || defined(HAS_TR1)
741 #ifndef RCPP_USING_CXX11
745 struct hash<
Rcpp::String>
751 #ifndef RCPP_USING_CXX11
#define RCPP_STRING_DEBUG_2(fmt, M1, M2)
#define RCPP_STRING_DEBUG_1(fmt, MSG)
#define RCPP_STRING_DEBUG(MSG)
const char * char_nocheck(SEXP)
String(const const_StringProxy &proxy)
String & replace_first(const Rcpp::String &s, const char *news)
String & operator+=(const const_StringProxy &proxy)
String & operator+=(const String &other)
bool operator==(const StringProxy &other) const
String & replace_all(const Rcpp::String &s, const char *news)
String & replace_last(const char *s, const char *news)
String & operator=(const wchar_t *s)
String(const char *s, cetype_t enc=CE_UTF8)
String & push_back(const Rcpp::String &s)
String & push_front(const Rcpp::String &s)
String & replace_first(const char *s, const Rcpp::String &news)
String & operator=(Rcomplex x)
bool operator>(const Rcpp::String &other) const
String & push_back(const char *s)
String & replace_all(const Rcpp::String &s, const Rcpp::String &news)
bool operator!=(const Rcpp::String &other) const
String & replace_all(const LHS &s, const RHS &news)
String & operator=(double x)
bool operator!=(const const_StringProxy &other) const
cetype_t get_encoding() const
void set_encoding(cetype_t encoding)
const char * get_cstring() const
String(const StringProxy &proxy)
String & push_front(const std::string &s)
String & replace_last(const Rcpp::String &s, const Rcpp::String &news)
internal::string_proxy< STRSXP > StringProxy
String & operator=(const StringProxy &proxy)
String(const std::string &s, cetype_t enc=CE_UTF8)
bool operator==(const const_StringProxy &other) const
String & operator=(const std::wstring &s)
internal::const_string_proxy< STRSXP > const_StringProxy
String & operator=(bool x)
String(const const_StringProxy &proxy, cetype_t enc)
String & operator=(int x)
String(const std::wstring &s, cetype_t enc=CE_UTF8)
String & replace_last(const char *s, const Rcpp::String &news)
String & operator+=(const std::string &s)
bool operator!=(const StringProxy &other) const
SEXP get_sexp_impl() const
String(const wchar_t *s, cetype_t enc=CE_UTF8)
String & operator+=(const wchar_t *s)
bool operator==(const Rcpp::String &other) const
String & operator+=(SEXP x)
String & replace_first(const char *s, const char *news)
String & operator+=(const std::wstring &s)
String & push_front(const char *s)
String & replace_all(const char *s, const char *news)
bool operator<(const Rcpp::String &other) const
String & operator=(const String &other)
String & operator=(SEXP x)
String & replace_last(const Rcpp::String &s, const char *news)
String & replace_all(const char *s, const Rcpp::String &news)
String(const StringProxy &proxy, cetype_t enc)
String & push_back(const std::string &s)
String & assign_wide_string(const T &s)
bool operator!=(SEXP other) const
String & operator+=(const char *s)
String & operator=(const char *s)
String & operator=(Rbyte x)
bool operator==(SEXP other) const
String & operator=(const std::string &s)
String & operator+=(const StringProxy &proxy)
String & append_wide_string(const T &s)
String & replace_first(const Rcpp::String &s, const Rcpp::String &news)
static SEXP get(const T &input)
string_proxy & operator=(const string_proxy< RTYPE, StoragePolicy > &other)
string_proxy & operator+=(const T &rhs)
#define DEMANGLE(__TYPE__)
bool operator!=(const Date &d1, const Date &d2)
SEXP get(const std::string &name) const
SEXP Rcpp_PreciousPreserve(SEXP object)
void Rcpp_PreciousRelease(SEXP token)
bool operator==(const Date &d1, const Date &d2)
r_type_RcppString_tag r_category