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)),
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();
273 buffer = std::move(other.buffer);
278 other.data = R_NilValue;
279 other.token = R_NilValue;
280 other.buffer = std::string();
282 other.buffer_ready =
true;
300 template <
typename T>
302 data = internal::make_charsexp(s);
316 if (
is_na())
return *
this;
323 if (
is_na())
return *
this;
328 template <
typename T>
332 if (
is_na())
return *
this;
333 const char* buf = CHAR(
data);
334 std::wstring tmp(buf, buf + strlen(buf));
336 data = internal::make_charsexp(tmp);
351 if (
is_na())
return *
this;
365 if (
is_na())
return *
this;
366 SEXP proxy_sexp = proxy;
367 if (proxy_sexp == NA_STRING) {
380 if (
is_na())
return *
this;
381 SEXP proxy_sexp = proxy;
382 if (proxy_sexp == NA_STRING) {
395 if (
is_na())
return *
this;
396 if (x == NA_STRING) {
415 RCPP_STRING_DEBUG_2(
"String::replace_first(const char* = '%s' , const char* = '%s')", s, news);
416 if (
is_na())
return *
this;
418 std::string s2 = std::string(s);
419 size_t index = std::distance(
buffer.begin(), std::search(
buffer.begin(),
buffer.end(), s2.begin(), s2.end()));
420 if (index != std::string::npos)
buffer.replace(index, strlen(s), news);
426 if (s.
is_na())
return *
this;
431 if (news.
is_na())
return *
this;
441 RCPP_STRING_DEBUG_2(
"String::replace_last(const char* = '%s' , const char* = '%s')", s, news);
442 if (
is_na())
return *
this;
444 std::string s2 = std::string(s);
445 size_t index = std::distance(
buffer.begin(), std::find_end(
buffer.begin(),
buffer.end(), s2.begin(), s2.end()));
446 if (index != std::string::npos)
buffer.replace(index, strlen(s), news);
452 if (s.
is_na())
return *
this;
457 if (news.
is_na())
return *
this;
468 RCPP_STRING_DEBUG_2(
"String::replace_all(const char* = '%s' , const char* = '%s')", s, news);
469 if (
is_na())
return *
this;
471 std::string s2 = std::string(s);
472 std::string::iterator iter =
buffer.begin();
474 iter = std::search(iter,
buffer.end(), s2.begin(), s2.end());
475 if (iter ==
buffer.end())
break;
476 size_t index = std::distance(
buffer.begin(), iter);
477 if (index != std::string::npos)
buffer.replace(index, strlen(s), news);
483 template <
typename LHS,
typename RHS>
490 if (s.
is_na())
return *
this;
495 if (news.
is_na())
return *
this;
505 if (
is_na())
return *
this;
513 if (
is_na())
return *
this;
519 if (
is_na())
return *
this;
527 if (
is_na())
return *
this;
549 if (
buffer.find(
'\0') != std::string::npos)
550 throw embedded_nul_in_string();
551 return Rf_mkCharLenCE(
buffer.c_str(),
static_cast<int>(
buffer.size()),
enc);
565 inline operator std::string()
const {
569 inline operator std::wstring()
const {
571 return std::wstring(s, s + strlen(s));
587 const char* translated = Rf_translateCharUTF8(
data);
588 data = Rf_mkCharCE(translated, encoding);
679 template <
int RTYPE,
template <
class>
class StoragePolicy>
687 RCPP_DEBUG(
"string_element_converter::get< Rcpp::String >()")
696 template <
int RTYPE,
template <
class>
class StoragePolicy>
697 template <
typename T>
712 SEXP data =
object.get_sexp();
713 SET_STRING_ELT(res, 0, data);
#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 & push_front(const std::string &s)
String & operator=(std::string &&s)
String & operator+=(const String &other)
String & operator=(const StringProxy &proxy)
String & replace_all(const Rcpp::String &s, const char *news)
String & replace_last(const char *s, const Rcpp::String &news)
bool operator==(const StringProxy &other) const
String & operator=(double x)
String & operator=(Rbyte x)
String & push_front(const char *s)
String(const char *s, cetype_t enc=CE_UTF8)
String & assign_wide_string(const T &s)
String & replace_last(const char *s, const char *news)
String & operator+=(const std::wstring &s)
String & replace_last(const Rcpp::String &s, const Rcpp::String &news)
bool operator>(const Rcpp::String &other) const
String & operator=(const char *s)
bool operator!=(const Rcpp::String &other) const
String & operator=(Rcomplex x)
String & replace_all(const Rcpp::String &s, const Rcpp::String &news)
bool operator!=(const const_StringProxy &other) const
String & replace_all(const LHS &s, const RHS &news)
cetype_t get_encoding() const
void set_encoding(cetype_t encoding)
String(const StringProxy &proxy)
String & operator+=(const std::string &s)
String(std::string &&s, cetype_t enc=CE_UTF8)
String & operator=(const std::string &s)
String & operator=(String &&other)
String & operator=(const std::wstring &s)
String & replace_first(const Rcpp::String &s, const Rcpp::String &news)
internal::string_proxy< STRSXP > StringProxy
String & operator+=(const wchar_t *s)
String(const std::string &s, cetype_t enc=CE_UTF8)
String & operator+=(const const_StringProxy &proxy)
String & operator+=(const char *s)
bool operator==(const const_StringProxy &other) const
String & replace_first(const Rcpp::String &s, const char *news)
internal::const_string_proxy< STRSXP > const_StringProxy
String(const const_StringProxy &proxy, cetype_t enc)
String & push_back(const Rcpp::String &s)
String(const std::wstring &s, cetype_t enc=CE_UTF8)
String & operator=(bool x)
String & replace_first(const char *s, const Rcpp::String &news)
const char * get_cstring() const
String & operator+=(const StringProxy &proxy)
bool operator!=(const StringProxy &other) const
String & operator=(SEXP x)
SEXP get_sexp_impl() const
String & operator=(const wchar_t *s)
String(const wchar_t *s, cetype_t enc=CE_UTF8)
bool operator==(const Rcpp::String &other) const
String & operator=(int x)
bool operator<(const Rcpp::String &other) const
String & operator=(const String &other)
String & push_back(const char *s)
String(const StringProxy &proxy, cetype_t enc)
String & push_front(const Rcpp::String &s)
bool operator!=(SEXP other) const
String & operator+=(SEXP x)
bool operator==(SEXP other) const
String & replace_last(const Rcpp::String &s, const char *news)
String & push_back(const std::string &s)
String & replace_all(const char *s, const Rcpp::String &news)
String & append_wide_string(const T &s)
String & replace_first(const char *s, const char *news)
String & replace_all(const char *s, const char *news)
static SEXP get(const T &input)
string_proxy & operator+=(const T &rhs)
string_proxy & operator=(const string_proxy< RTYPE, StoragePolicy > &other)
#define DEMANGLE(__TYPE__)
internal implementation details
SEXP make_charsexp< Rcpp::String >(const Rcpp::String &s)
SEXP wrap< Rcpp::String >(const Rcpp::String &object)
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
size_t operator()(const Rcpp::String &s) const