40
40
#define RCPP_STRING_DEBUG_3 (fmt, M1, M2, M3 )
41
41
#endif
42
42
43
-
44
43
namespace Rcpp {
45
44
46
45
/* *
@@ -53,7 +52,7 @@ namespace Rcpp {
53
52
typedef internal::const_string_proxy<STRSXP> const_StringProxy;
54
53
55
54
/* * default constructor */
56
- String (): data(Rf_mkChar (" " )), buffer(), valid(true ), buffer_ready(true ), enc(CE_NATIVE ) {
55
+ String (): data(Rf_mkCharCE (" " , CE_UTF8 )), buffer(), valid(true ), buffer_ready(true ), enc(CE_UTF8 ) {
57
56
Rcpp_PreserveObject (data);
58
57
RCPP_STRING_DEBUG (" String()" );
59
58
}
@@ -64,12 +63,6 @@ namespace Rcpp {
64
63
RCPP_STRING_DEBUG (" String(const String&)" );
65
64
}
66
65
67
- String (const String& other, const std::string& enc) : data(other.get_sexp()), valid(true ), buffer_ready(false ) {
68
- Rcpp_PreserveObject (data);
69
- set_encoding (enc);
70
- RCPP_STRING_DEBUG (" String(const String&)" );
71
- }
72
-
73
66
/* * construct a string from a single CHARSXP SEXP */
74
67
String (SEXP charsxp) : data(R_NilValue) {
75
68
if (TYPEOF (charsxp) == STRSXP) {
@@ -88,33 +81,16 @@ namespace Rcpp {
88
81
RCPP_STRING_DEBUG (" String(SEXP)" );
89
82
}
90
83
91
- String (SEXP charsxp, const std::string& enc) : data(R_NilValue) {
92
- if (TYPEOF (charsxp) == STRSXP) {
93
- data = STRING_ELT (charsxp, 0 );
94
- } else if (TYPEOF (charsxp) == CHARSXP) {
95
- data = charsxp;
96
- }
97
-
98
- if (::Rf_isString (data) && ::Rf_length (data) != 1 )
99
- throw ::Rcpp::not_compatible (" expecting a single value" );
100
-
101
- valid = true ;
102
- buffer_ready = false ;
103
- Rcpp_PreserveObject (data);
104
- set_encoding (enc);
105
- RCPP_STRING_DEBUG (" String(SEXP)" );
106
- }
107
-
108
84
/* * from string proxy */
109
85
String (const StringProxy& proxy): data(proxy.get()), valid(true ), buffer_ready(false ), enc(Rf_getCharCE(proxy.get())) {
110
86
Rcpp_PreserveObject (data);
111
87
RCPP_STRING_DEBUG (" String(const StringProxy&)" );
112
88
}
113
89
114
- String (const StringProxy& proxy, const std::string& enc): data(proxy.get()), valid(true ), buffer_ready(false ) {
90
+ String (const StringProxy& proxy, cetype_t enc): data(proxy.get()), valid(true ), buffer_ready(false ) {
115
91
Rcpp_PreserveObject (data);
116
92
set_encoding (enc);
117
- RCPP_STRING_DEBUG (" String(const StringProxy&)" );
93
+ RCPP_STRING_DEBUG (" String(const StringProxy&, cetype_t )" );
118
94
}
119
95
120
96
/* * from string proxy */
@@ -123,40 +99,40 @@ namespace Rcpp {
123
99
RCPP_STRING_DEBUG (" String(const const_StringProxy&)" );
124
100
}
125
101
126
- String (const const_StringProxy& proxy, const std::string& enc): data(proxy.get()), valid(true ), buffer_ready(false ) {
102
+ String (const const_StringProxy& proxy, cetype_t enc): data(proxy.get()), valid(true ), buffer_ready(false ) {
127
103
Rcpp_PreserveObject (data);
128
104
set_encoding (enc);
129
- RCPP_STRING_DEBUG (" String(const const_StringProxy&)" );
105
+ RCPP_STRING_DEBUG (" String(const const_StringProxy&, cetype_t )" );
130
106
}
131
107
132
108
/* * from a std::string */
133
- String (const std::string& s) : buffer(s), valid(false ), buffer_ready(true ), enc(CE_NATIVE ) {
109
+ String (const std::string& s, cetype_t enc = CE_UTF8 ) : buffer(s), valid(false ), buffer_ready(true ), enc(enc ) {
134
110
data = R_NilValue;
135
- RCPP_STRING_DEBUG (" String(const std::string&)" );
111
+ RCPP_STRING_DEBUG (" String(const std::string&, cetype_t )" );
136
112
}
137
113
138
- String (const std::wstring& s) : data(internal::make_charsexp(s)), valid(true ), buffer_ready(false ), enc(CE_NATIVE ) {
114
+ String (const std::wstring& s, cetype_t enc = CE_UTF8 ) : data(internal::make_charsexp(s)), valid(true ), buffer_ready(false ), enc(enc ) {
139
115
Rcpp_PreserveObject (data);
140
- RCPP_STRING_DEBUG (" String(const std::wstring&)" );
116
+ RCPP_STRING_DEBUG (" String(const std::wstring&, cetype_t )" );
141
117
}
142
118
143
119
/* * from a const char* */
144
- String (const char * s) : buffer(s), valid(false ), buffer_ready(true ), enc(CE_NATIVE ) {
120
+ String (const char * s, cetype_t enc = CE_UTF8 ) : buffer(s), valid(false ), buffer_ready(true ), enc(enc ) {
145
121
data = R_NilValue;
146
- RCPP_STRING_DEBUG (" String(const char*)" );
122
+ RCPP_STRING_DEBUG (" String(const char*, cetype_t )" );
147
123
}
148
124
149
- String (const wchar_t * s) : data(internal::make_charsexp(s)), valid(true ), buffer_ready(false ), enc(CE_NATIVE ) {
125
+ String (const wchar_t * s, cetype_t enc = CE_UTF8 ) : data(internal::make_charsexp(s)), valid(true ), buffer_ready(false ), enc(enc ) {
150
126
Rcpp_PreserveObject (data);
151
- RCPP_STRING_DEBUG (" String(const wchar_t* s)" );
127
+ RCPP_STRING_DEBUG (" String(const wchar_t* s, cetype_t )" );
152
128
}
153
129
154
130
/* * constructors from R primitives */
155
- String (int x) : data(internal::r_coerce<INTSXP,STRSXP>(x)), valid(true ), buffer_ready(false ), enc(CE_NATIVE ) {Rcpp_PreserveObject (data);}
156
- String (double x) : data(internal::r_coerce<REALSXP,STRSXP>(x)), valid(true ), buffer_ready(false ), enc(CE_NATIVE ) {Rcpp_PreserveObject (data);}
157
- String (bool x) : data(internal::r_coerce<LGLSXP,STRSXP>(x)), valid(true ) , buffer_ready(false ), enc(CE_NATIVE ) {Rcpp_PreserveObject (data);}
158
- String (Rcomplex x) : data(internal::r_coerce<CPLXSXP,STRSXP>(x)), valid(true ), buffer_ready(false ), enc(CE_NATIVE ) {Rcpp_PreserveObject (data);}
159
- String (Rbyte x) : data(internal::r_coerce<RAWSXP,STRSXP>(x)), valid(true ), buffer_ready(false ), enc(CE_NATIVE ) {Rcpp_PreserveObject (data);}
131
+ String (int x) : data(internal::r_coerce<INTSXP,STRSXP>(x)), valid(true ), buffer_ready(false ), enc(CE_UTF8 ) {Rcpp_PreserveObject (data);}
132
+ String (double x) : data(internal::r_coerce<REALSXP,STRSXP>(x)), valid(true ), buffer_ready(false ), enc(CE_UTF8 ) {Rcpp_PreserveObject (data);}
133
+ String (bool x) : data(internal::r_coerce<LGLSXP,STRSXP>(x)), valid(true ) , buffer_ready(false ), enc(CE_UTF8 ) {Rcpp_PreserveObject (data);}
134
+ String (Rcomplex x) : data(internal::r_coerce<CPLXSXP,STRSXP>(x)), valid(true ), buffer_ready(false ), enc(CE_UTF8 ) {Rcpp_PreserveObject (data);}
135
+ String (Rbyte x) : data(internal::r_coerce<RAWSXP,STRSXP>(x)), valid(true ), buffer_ready(false ), enc(CE_UTF8 ) {Rcpp_PreserveObject (data);}
160
136
161
137
~String () {
162
138
Rcpp_ReleaseObject (data);
@@ -406,17 +382,8 @@ namespace Rcpp {
406
382
return buffer_ready ? buffer.c_str () : CHAR (data);
407
383
}
408
384
409
- inline const std::string get_encoding () const {
410
- switch (enc) {
411
- case CE_BYTES:
412
- return " bytes" ;
413
- case CE_LATIN1:
414
- return " latin1" ;
415
- case CE_UTF8:
416
- return " UTF-8" ;
417
- default :
418
- return " unknown" ;
419
- }
385
+ inline cetype_t get_encoding () const {
386
+ return enc;
420
387
}
421
388
422
389
inline void set_encoding (cetype_t encoding) {
@@ -431,18 +398,6 @@ namespace Rcpp {
431
398
}
432
399
}
433
400
434
- inline void set_encoding (const std::string & encoding) {
435
- if (encoding == " bytes" ) {
436
- set_encoding (CE_BYTES);
437
- } else if (encoding == " latin1" ) {
438
- set_encoding (CE_LATIN1);
439
- } else if (encoding == " UTF-8" ) {
440
- set_encoding (CE_UTF8);
441
- } else {
442
- set_encoding (CE_ANY);
443
- }
444
- }
445
-
446
401
bool operator <(const Rcpp::String& other) const {
447
402
return strcmp (get_cstring (), other.get_cstring ()) < 0 ;
448
403
}
0 commit comments