* A single string, i.e. an element of a character vector. * This represents CHARSXP SEXP */
| 47 | * This represents CHARSXP SEXP |
| 48 | */ |
| 49 | class String { |
| 50 | public: |
| 51 | typedef internal::string_proxy<STRSXP> StringProxy; |
| 52 | typedef internal::const_string_proxy<STRSXP> const_StringProxy; |
| 53 | |
| 54 | /** default constructor */ |
| 55 | String(): data(Rf_mkCharCE("", CE_UTF8)), token(R_NilValue), buffer(), valid(true), buffer_ready(true), enc(CE_UTF8) { |
| 56 | token = Rcpp_PreciousPreserve(data); |
| 57 | RCPP_STRING_DEBUG("String()"); |
| 58 | } |
| 59 | |
| 60 | /** copy constructor */ |
| 61 | String(const String& s) : data(R_NilValue), token(R_NilValue), buffer(s.buffer), valid(s.valid), buffer_ready(s.buffer_ready), enc(s.enc) { |
| 62 | if (!buffer_ready) { |
| 63 | data = s.get_sexp(); |
| 64 | token = Rcpp_PreciousPreserve(data); |
| 65 | } |
| 66 | RCPP_STRING_DEBUG("String(const String&)"); |
| 67 | } |
| 68 | |
| 69 | /** construct a string from a single CHARSXP SEXP */ |
| 70 | String(SEXP charsxp) : data(R_NilValue), token(R_NilValue) { |
| 71 | if (TYPEOF(charsxp) == STRSXP) { |
| 72 | data = STRING_ELT(charsxp, 0); |
| 73 | } else if (TYPEOF(charsxp) == CHARSXP) { |
| 74 | data = charsxp; |
| 75 | } |
| 76 | |
| 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)), |
| 82 | ::Rf_length(data)); |
| 83 | } |
| 84 | |
| 85 | valid = true; |
| 86 | buffer_ready = false; |
| 87 | enc = Rf_getCharCE(data); |
| 88 | token = Rcpp_PreciousPreserve(data); |
| 89 | RCPP_STRING_DEBUG("String(SEXP)"); |
| 90 | } |
| 91 | |
| 92 | /** from string proxy */ |
| 93 | String(const StringProxy& proxy): data(proxy.get()), token(R_NilValue), valid(true), buffer_ready(false), enc(Rf_getCharCE(proxy.get())) { |
| 94 | token = Rcpp_PreciousPreserve(data); |
| 95 | RCPP_STRING_DEBUG("String(const StringProxy&)"); |
| 96 | } |
| 97 | |
| 98 | String(const StringProxy& proxy, cetype_t enc): data(proxy.get()), token(R_NilValue), valid(true), buffer_ready(false) { |
| 99 | token = Rcpp_PreciousPreserve(data); |
| 100 | set_encoding(enc); |
| 101 | RCPP_STRING_DEBUG("String(const StringProxy&, cetype_t)"); |
| 102 | } |
| 103 | |
| 104 | /** from string proxy */ |
| 105 | String(const const_StringProxy& proxy): data(proxy.get()), token(R_NilValue), valid(true), buffer_ready(false), enc(Rf_getCharCE(proxy.get())) { |
| 106 | token = Rcpp_PreciousPreserve(data); |
no test coverage detected