File: String.cpp

package info (click to toggle)
rcpp 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,344 kB
  • sloc: ansic: 43,817; cpp: 39,947; sh: 51; makefile: 2
file content (122 lines) | stat: -rw-r--r-- 2,691 bytes parent folder | download | duplicates (2)
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
// #define RCPP_STRING_DEBUG_LEVEL 0
// #define RCPP_DEBUG_LEVEL 0
#include <Rcpp.h>
using namespace Rcpp ;


// [[Rcpp::export]]
String String_replace_all( String z, String x, String y){
    z.replace_all( x, y ) ;
    return z ;
}

// [[Rcpp::export]]
String String_replace_first( String z, String x, String y){
    z.replace_first( x, y ) ;
    return z ;
}
// [[Rcpp::export]]
String String_replace_last( String z, String x, String y){
    z.replace_last( x, y ) ;
    return z ;
}

class StringConv{
public:
    typedef String result_type ;
    StringConv( CharacterVector old_, CharacterVector new__):
        nr(old_.size()), old(old_), new_(new__){}

    String operator()(String text) const {
        for( int i=0; i<nr; i++){
            text.replace_all( old[i], new_[i] ) ;
        }
        return text ;
    }

private:
    int nr ;
    CharacterVector old ;
    CharacterVector new_ ;
} ;

// [[Rcpp::export]]
CharacterVector test_sapply_string( CharacterVector text, CharacterVector old , CharacterVector new_){
   CharacterVector res = sapply( text, StringConv( old, new_ ) ) ;
   return res ;
}

// [[Rcpp::export]]
String test_ctor(String x) {
    String test = "test";
    test = x;
    return test;
}

// [[Rcpp::export]]
List test_compare_Strings( String aa, String bb ){
    return List::create(
        _["a  < b" ] = aa < bb,
        _["a  > b" ] = aa > bb,
        _["a == b"]  = aa == bb,
        _["a == a"]  = aa == aa
        ) ;
}

// [[Rcpp::export]]
List test_compare_String_string_proxy( String aa, CharacterVector bb ){
    return List::create(
        _["a == b"]  = aa == bb[0],
        _["a != b"]  = aa != bb[0],
        _["b == a"]  = bb[0] == aa,
        _["b != a"]  = bb[0] != aa
        ) ;
}

// [[Rcpp::export]]
List test_compare_String_const_string_proxy( String aa, const CharacterVector bb ){
    return List::create(
        _["a == b"]  = aa == bb[0],
        _["a != b"]  = aa != bb[0],
        _["b == a"]  = bb[0] == aa,
        _["b != a"]  = bb[0] != aa
        ) ;
}

// [[Rcpp::export]]
String test_push_front(String x) {
    x.push_front("abc");
    return x;
}

// [[Rcpp::export]]
int test_String_encoding(String x) {
    return x.get_encoding();
}

// [[Rcpp::export]]
String test_String_set_encoding(String x) {
    x.set_encoding(CE_UTF8);
    return x;
}

// [[Rcpp::export]]
String test_String_ctor_encoding(String x) {
    String y(x);
    y.set_encoding(CE_UTF8);
    return y;
}


// [[Rcpp::export]]
String test_String_ctor_encoding2() {
    String y("å");
    y.set_encoding(CE_UTF8);
    return y;
}

// [[Rcpp::export]]
String test_String_embeddedNul() {
    std::string bad("abc\0abc", 7);
    return String(bad);
}