File: String.h

package info (click to toggle)
rcpp 1.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,364 kB
  • sloc: ansic: 43,980; cpp: 40,120; sh: 53; makefile: 2
file content (577 lines) | stat: -rw-r--r-- 23,651 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// String.h: Rcpp R/C++ interface class library -- single string
//
// Copyright (C) 2012 - 2018  Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.

#ifndef Rcpp__String_h
#define Rcpp__String_h

#ifndef RCPP_STRING_DEBUG_LEVEL
#define RCPP_STRING_DEBUG_LEVEL 0
#endif


#if RCPP_STRING_DEBUG_LEVEL > 0
    #define RCPP_STRING_DEBUG_FORMAT "%40s:%4d "
    #define RCPP_STRING_DEBUG(MSG) Rprintf(RCPP_STRING_DEBUG_FORMAT "%s\n" , ::Rcpp::internal::debug::short_file_name(__FILE__).c_str(), __LINE__, MSG);
    #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);
    #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);
    #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);
#else
    #define RCPP_STRING_DEBUG(MSG)
    #define RCPP_STRING_DEBUG_1(fmt, MSG)
    #define RCPP_STRING_DEBUG_2(fmt, M1, M2)
    #define RCPP_STRING_DEBUG_3(fmt, M1, M2, M3)
#endif

namespace Rcpp {

    /**
     * A single string, i.e. an element of a character vector.
     * This represents CHARSXP SEXP
     */
    class String {
    public:
        typedef internal::string_proxy<STRSXP> StringProxy;
        typedef internal::const_string_proxy<STRSXP> const_StringProxy;

        /** default constructor */
        String(): data(Rf_mkCharCE("", CE_UTF8)), buffer(), valid(true), buffer_ready(true), enc(CE_UTF8) {
            Rcpp_PreserveObject(data);
            RCPP_STRING_DEBUG("String()");
        }

        /** copy constructor */
        String(const String& other) : data(other.get_sexp()), valid(true), buffer_ready(false), enc(Rf_getCharCE(other.get_sexp())) {
            Rcpp_PreserveObject(data);
            RCPP_STRING_DEBUG("String(const String&)");
        }

        /** construct a string from a single CHARSXP SEXP */
        String(SEXP charsxp) : data(R_NilValue) {
            if (TYPEOF(charsxp) == STRSXP) {
                data = STRING_ELT(charsxp, 0);
            } else if (TYPEOF(charsxp) == CHARSXP) {
                data = charsxp;
            }

            if (::Rf_isString(data) && ::Rf_length(data) != 1) {
                const char* fmt = "Expecting a single string value: "
                                  "[type=%s; extent=%i].";
                throw ::Rcpp::not_compatible(fmt,
                                             Rf_type2char(TYPEOF(data)),
                                             ::Rf_length(data));
            }

            valid = true;
            buffer_ready = false;
            enc = Rf_getCharCE(data);
            Rcpp_PreserveObject(data);
            RCPP_STRING_DEBUG("String(SEXP)");
        }

        /** from string proxy */
        String(const StringProxy& proxy): data(proxy.get()), valid(true), buffer_ready(false), enc(Rf_getCharCE(proxy.get())) {
            Rcpp_PreserveObject(data);
            RCPP_STRING_DEBUG("String(const StringProxy&)");
        }

        String(const StringProxy& proxy, cetype_t enc): data(proxy.get()), valid(true), buffer_ready(false) {
            Rcpp_PreserveObject(data);
            set_encoding(enc);
            RCPP_STRING_DEBUG("String(const StringProxy&, cetype_t)");
        }

        /** from string proxy */
        String(const const_StringProxy& proxy): data(proxy.get()), valid(true), buffer_ready(false), enc(Rf_getCharCE(proxy.get())) {
            Rcpp_PreserveObject(data);
            RCPP_STRING_DEBUG("String(const const_StringProxy&)");
        }

        String(const const_StringProxy& proxy, cetype_t enc): data(proxy.get()), valid(true), buffer_ready(false) {
            Rcpp_PreserveObject(data);
            set_encoding(enc);
            RCPP_STRING_DEBUG("String(const const_StringProxy&, cetype_t)");
        }

        /** from a std::string */
        String(const std::string& s, cetype_t enc = CE_UTF8) : buffer(s), valid(false), buffer_ready(true), enc(enc) {
            data = R_NilValue;
            RCPP_STRING_DEBUG("String(const std::string&, cetype_t)");
        }

        String(const std::wstring& s, cetype_t enc = CE_UTF8) : data(internal::make_charsexp(s)), valid(true), buffer_ready(false), enc(enc) {
            Rcpp_PreserveObject(data);
            RCPP_STRING_DEBUG("String(const std::wstring&, cetype_t)");
        }

        /** from a const char* */
        String(const char* s, cetype_t enc = CE_UTF8) : buffer(s), valid(false), buffer_ready(true), enc(enc) {
            data = R_NilValue;
            RCPP_STRING_DEBUG("String(const char*, cetype_t)");
        }

        String(const wchar_t* s, cetype_t enc = CE_UTF8) : data(internal::make_charsexp(s)), valid(true), buffer_ready(false), enc(enc) {
            Rcpp_PreserveObject(data);
            RCPP_STRING_DEBUG("String(const wchar_t* s, cetype_t)");
        }

        /** constructors from R primitives */
        String(int x) : data(internal::r_coerce<INTSXP,STRSXP>(x)), valid(true), buffer_ready(false), enc(CE_UTF8) {Rcpp_PreserveObject(data);}
        String(double x) : data(internal::r_coerce<REALSXP,STRSXP>(x)), valid(true), buffer_ready(false), enc(CE_UTF8) {Rcpp_PreserveObject(data);}
        String(bool x) : data(internal::r_coerce<LGLSXP,STRSXP>(x)), valid(true) , buffer_ready(false), enc(CE_UTF8) {Rcpp_PreserveObject(data);}
        String(Rcomplex x) : data(internal::r_coerce<CPLXSXP,STRSXP>(x)), valid(true), buffer_ready(false), enc(CE_UTF8) {Rcpp_PreserveObject(data);}
        String(Rbyte x) : data(internal::r_coerce<RAWSXP,STRSXP>(x)), valid(true), buffer_ready(false), enc(CE_UTF8) {Rcpp_PreserveObject(data);}

        ~String() {
            Rcpp_ReleaseObject(data);
            data = R_NilValue;
        }


        inline String& operator=(int x    ) { data = Rcpp_ReplaceObject(data, internal::r_coerce<INTSXP ,STRSXP>(x)); valid = true; buffer_ready = false; return *this; }
        inline String& operator=(double x ) { data = Rcpp_ReplaceObject(data, internal::r_coerce<REALSXP,STRSXP>(x)); valid = true; buffer_ready = false; return *this; }
        inline String& operator=(Rbyte x  ) { data = Rcpp_ReplaceObject(data, internal::r_coerce<RAWSXP ,STRSXP>(x)); valid = true; buffer_ready = false; return *this; }
        inline String& operator=(bool x   ) { data = Rcpp_ReplaceObject(data, internal::r_coerce<LGLSXP ,STRSXP>(x)); valid = true; buffer_ready = false; return *this; }
        inline String& operator=(Rcomplex x) { data = Rcpp_ReplaceObject(data, internal::r_coerce<CPLXSXP,STRSXP>(x)); valid = true; buffer_ready = false; return *this; }
        inline String& operator=(SEXP x) { data = Rcpp_ReplaceObject(data, x); valid = true; buffer_ready = false; return *this; }
        inline String& operator=(const StringProxy& proxy) { data = Rcpp_ReplaceObject(data, proxy.get()); valid = true; buffer_ready=false; return *this; }
        inline String& operator=(const String& other) { data = Rcpp_ReplaceObject(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; }

    private:
        template <typename T>
        inline String& assign_wide_string(const T& s) {
            data = Rcpp_ReplaceObject(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;
            setBuffer(); buffer += s; valid = false;
            return *this;
        }

        inline String& operator+=(const char* s) {
            RCPP_STRING_DEBUG("String::operator+=(const char*)");
            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 = Rcpp_ReplaceObject(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;
            if (other.is_na()) { data = Rcpp_ReplaceObject(data, NA_STRING); valid = true; buffer_ready = false; return *this; }
            setBuffer(); buffer += other; valid = false;
            return *this;
        }
        inline String& operator+=(const StringProxy& proxy) {
            RCPP_STRING_DEBUG("String::operator+=(const StringProxy&)");
            if (is_na()) return *this;
            SEXP proxy_sexp = proxy;
            if (proxy_sexp == NA_STRING) { data = Rcpp_ReplaceObject(data, NA_STRING); valid = true; buffer_ready = false; return *this;}
            setBuffer(); buffer += CHAR(proxy_sexp); valid = false;
            return *this;
        }
        inline String& operator+=(const const_StringProxy& proxy) {
            RCPP_STRING_DEBUG("String::operator+=(const StringProxy&)");
            if (is_na()) return *this;
            SEXP proxy_sexp = proxy;
            if (proxy_sexp == NA_STRING) { data = Rcpp_ReplaceObject(data, NA_STRING); valid = true; buffer_ready = false; return *this;}
            setBuffer(); buffer += CHAR(proxy_sexp); valid = false;
            return *this;
        }
        inline String& operator+=(SEXP x) {
            RCPP_STRING_DEBUG("String::operator+=(SEXP)");
            if (is_na()) return *this;
            if (x == NA_STRING) { data = Rcpp_ReplaceObject(data, NA_STRING); valid = true; buffer_ready = false; return *this;}
            setBuffer(); buffer += CHAR(x); valid = false;
            return *this;
        }
        // inline String& operator+=(int x    ) { data += char_nocheck(internal::r_coerce<INTSXP ,STRSXP>(x)); return *this; }
        // inline String& operator+=(double x ) { data += char_nocheck(internal::r_coerce<REALSXP,STRSXP>(x)); return *this; }
        // inline String& operator+=(Rbyte x  ) { data += char_nocheck(internal::r_coerce<RAWSXP ,STRSXP>(x)); return *this; }
        // inline String& operator+=(bool x   ) { data += char_nocheck(internal::r_coerce<LGLSXP ,STRSXP>(x)); return *this; }
        // inline String& operator+=(Rcomplex x) { data += char_nocheck(internal::r_coerce<CPLXSXP,STRSXP>(x)); return *this; }


        inline String& replace_first(const char* s, const char* news) {
            RCPP_STRING_DEBUG_2("String::replace_first(const char* = '%s' , const char* = '%s')", s, news);
            if (is_na()) return *this;
            setBuffer();
            std::string s2 = std::string(s);
            size_t index = std::distance(buffer.begin(), std::search(buffer.begin(), buffer.end(), s2.begin(), s2.end()));
            if (index != std::string::npos) buffer.replace(index, strlen(s), news);
            valid = false;
            return *this;
        }
        inline String& replace_first(const Rcpp::String& s, const char* news) {
            // replace NA -> do nothing
            if (s.is_na()) return *this;
            return replace_first(s.get_cstring(), news);
        }
        inline String& replace_first(const char* s, const Rcpp::String& news) {
            // replace NA -> do nothing
            if (news.is_na()) return *this;
            return replace_first(s, news.get_cstring());
        }
        inline String& replace_first(const Rcpp::String& s, const Rcpp::String& news) {
            // replace NA -> do nothing
            if (s.is_na() || news.is_na()) return *this;
            return replace_first(s.get_cstring(), news.get_cstring());
        }

        inline String& replace_last(const char* s, const char* news) {
            RCPP_STRING_DEBUG_2("String::replace_last(const char* = '%s' , const char* = '%s')", s, news);
            if (is_na()) return *this;
            setBuffer();
            std::string s2 = std::string(s);
            size_t index = std::distance(buffer.begin(), std::find_end(buffer.begin(), buffer.end(), s2.begin(), s2.end()));
            if (index != std::string::npos) buffer.replace(index, strlen(s), news);
            valid = false;
            return *this;
        }
        inline String& replace_last(const Rcpp::String& s, const char* news) {
            // replace NA -> do nothing
            if (s.is_na()) return *this;
            return replace_last(s.get_cstring(), news);
        }
        inline String& replace_last(const char* s, const Rcpp::String& news) {
            // replace NA -> do nothing
            if (news.is_na()) return *this;
            return replace_last(s, news.get_cstring());
        }
        inline String& replace_last(const Rcpp::String& s, const Rcpp::String& news) {
            // replace NA -> do nothing
            if (s.is_na() || news.is_na()) return *this;
            return replace_last(s.get_cstring(), news.get_cstring());
        }


        inline String& replace_all(const char* s, const char* news) {
            RCPP_STRING_DEBUG_2("String::replace_all(const char* = '%s' , const char* = '%s')", s, news);
            if (is_na()) return *this;
            setBuffer();
            std::string s2 = std::string(s);
            std::string::iterator iter = buffer.begin();
            while(true) {
                iter = std::search(iter, buffer.end(), s2.begin(), s2.end());
                if (iter == buffer.end()) break;
                size_t index = std::distance(buffer.begin(), iter);
                if (index != std::string::npos) buffer.replace(index, strlen(s), news);
            }
            valid = false;
            return *this;
        }

        template <typename LHS, typename RHS>
        inline String& replace_all(const LHS& s, const RHS& news) {
            return replace_all(String(s), String(news));
        }

        inline String& replace_all(const Rcpp::String& s, const char* news) {
            // replace NA -> do nothing
            if (s.is_na()) return *this;
            return replace_all(s.get_cstring(), news);
        }
        inline String& replace_all(const char* s, const Rcpp::String& news) {
            // replace NA -> do nothing
            if (news.is_na()) return *this;
            return replace_all(s, news.get_cstring());
        }
        inline String& replace_all(const Rcpp::String& s, const Rcpp::String& news) {
            // replace NA -> do nothing
            if (s.is_na() || news.is_na()) return *this;
            return replace_all(s.get_cstring(), news.get_cstring());
        }

        inline String& push_back(const char* s) {
            if (is_na()) return *this;
            setBuffer(); valid = false; buffer += s;
            return *this;
        }
        inline String& push_back(const std::string& s) {
            return push_back(s.c_str());
        }
        inline String& push_back(const Rcpp::String& s) {
            if (is_na()) return *this;
            if (s.is_na()) { set_na(); return *this; }
            return push_back(s.get_cstring());
        }

        inline String& push_front(const char* s) {
            if (is_na()) return *this;
            setBuffer(); valid = false; buffer = s + buffer;
            return *this;
        }
        inline String& push_front(const std::string& s) {
            return push_front(s.c_str());
        }
        inline String& push_front(const Rcpp::String& s) {
            if (is_na()) return *this;
            if (s.is_na()) { set_na(); return *this; }
            return push_front(s.get_cstring());
        }


        inline void set_na() {
            data = Rcpp_ReplaceObject(data, NA_STRING);
            valid = true; buffer_ready = false;
        }


        inline SEXP get_sexp_impl() const {

            // workaround for h5 package (currently deprecated so updates
            // to CRAN may not be timely)
#ifdef __H5Cpp_H
            return Rf_mkCharCE(buffer.c_str(), enc);
#else
            if (buffer.find('\0') != std::string::npos)
                throw embedded_nul_in_string();
            return Rf_mkCharLenCE(buffer.c_str(), buffer.size(), enc);
#endif
        }

        inline SEXP get_sexp() const {
            RCPP_STRING_DEBUG_1("String::get_sexp const (valid = %d) ", valid);
            return valid ? data : get_sexp_impl();
        }

        inline SEXP get_sexp() {
            RCPP_STRING_DEBUG_1("String::get_sexp (valid = %d) ", valid);
            setData(); return data;
        }

        inline operator std::string() const {
            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);
        }

        inline cetype_t get_encoding() const {
            return enc;
        }

        inline void set_encoding(cetype_t encoding) {
            enc = encoding;

            if (valid) {
                // TODO: may longjmp on failure to translate?
                const char* translated = Rf_translateCharUTF8(data);
                data = Rcpp_ReplaceObject(data, Rf_mkCharCE(translated, encoding));
            } else {
                data = get_sexp_impl();
                Rcpp_PreserveObject(data);
                valid = true;
            }
        }

        bool operator<(const Rcpp::String& other) const {
            return strcmp(get_cstring(), other.get_cstring()) < 0;
        }

        bool operator==(const Rcpp::String& other) const {
            return get_sexp() == other.get_sexp();
        }
        bool operator!=(const Rcpp::String& other) const {
            return get_sexp() != other.get_sexp();
        }

        bool operator==(const StringProxy& other) const {
            return get_sexp() == other.get();
        }

        bool operator!=(const StringProxy& other) const {
            return get_sexp() != other.get();
        }

        bool operator==(const const_StringProxy& other) const {
            return get_sexp() == other.get();
        }

        bool operator!=(const const_StringProxy& other) const {
            return get_sexp() != other.get();
        }

        bool operator>(const Rcpp::String& other) const {
            return strcmp(get_cstring(), other.get_cstring()) > 0;
        }

        bool operator==(SEXP other) const {
            return get_sexp() == other;
        }

        bool operator!=(SEXP other) const {
            return get_sexp() != other;
        }

    private:

        /** the CHARSXP this String encapsulates */
        SEXP data;

        /** a buffer used to do string operations withough going back to the SEXP */
        std::string buffer;

        /** is data in sync with buffer */
        bool valid;

        /** is the buffer initialized */
        bool buffer_ready;

        /** the encoding of encapsulated CHARSXP */
        cetype_t enc;

        inline bool is_na() const { return data == NA_STRING; }
        inline void setBuffer() {
            if (!buffer_ready) {
                buffer = char_nocheck(data);
                buffer_ready  = true;
            }
        }
        inline void setData() {
            RCPP_STRING_DEBUG("setData");
            if (!valid) {
                data = get_sexp_impl();
                Rcpp_PreserveObject(data);
                valid = true;
            }
        }
        template <typename T> void append(const T& s) { buffer += s;}
    };

    namespace traits{
        template<> struct r_type_traits<Rcpp::String>{ typedef r_type_RcppString_tag r_category; };
        template<> struct r_sexptype_traits<Rcpp::String>{ enum{ rtype = STRSXP }; };
    }

    namespace internal {
        template <int RTYPE, template <class> class StoragePolicy>
        string_proxy<RTYPE, StoragePolicy>& string_proxy<RTYPE, StoragePolicy>::operator=(const String& s) {
            set(s.get_sexp());
            return *this;
        }

        template <int RTYPE>
        SEXP string_element_converter<RTYPE>::get(const Rcpp::String& input) {
            RCPP_DEBUG("string_element_converter::get< Rcpp::String >()")
            return input.get_sexp();
        }

        template <>
        inline SEXP make_charsexp<Rcpp::String>(const Rcpp::String& s) {
            return s.get_sexp();
        }

        template <int RTYPE, template <class> class StoragePolicy>
        template <typename T>
        string_proxy<RTYPE, StoragePolicy>& string_proxy<RTYPE, StoragePolicy>::operator+=(const T& rhs) {
            String tmp = get();
            tmp += rhs;
            set(tmp);
            return *this;
        }

        }


    template <>
    inline SEXP wrap<Rcpp::String>(const Rcpp::String& object) {
        RCPP_STRING_DEBUG("wrap<String>()");
        Shield<SEXP> res(Rf_allocVector(STRSXP, 1));
        SEXP data = object.get_sexp();
        SET_STRING_ELT(res, 0, data);
        return res;
    }

    inline bool operator==(const String::StringProxy& lhs, const String& rhs) {
        return rhs == lhs;
    }

    inline bool operator!=(const String::StringProxy& lhs, const String& rhs) {
        return rhs != lhs;
    }

    inline bool operator==(const String::const_StringProxy& lhs, const String& rhs) {
        return rhs == lhs;
    }

    inline bool operator!=(const String::const_StringProxy& lhs, const String& rhs) {
        return rhs != lhs;
    }

} // Rcpp

/** hash can be in std or std::tr1 */
#if defined(RCPP_USING_CXX11) || defined(HAS_TR1)
namespace std
{
#ifndef RCPP_USING_CXX11
namespace tr1 {
#endif
    template <>
    struct hash<Rcpp::String>
    {
        size_t operator()(const Rcpp::String & s) const{
            return hash<string>()(s.get_cstring());
        }
    };
#ifndef RCPP_USING_CXX11
}
#endif
}
#endif

#endif