File: String.cpp

package info (click to toggle)
rcpp 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,480 kB
  • sloc: cpp: 27,436; ansic: 7,778; sh: 53; makefile: 2
file content (151 lines) | stat: -rw-r--r-- 3,352 bytes parent folder | download | duplicates (3)
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
// #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]]
CharacterVector test_move_ctor() {
  std::vector<String> v = { "test" };
  String t = std::move( v[0] );
  v.push_back( std::move( t ) );
  return CharacterVector( v.begin(), v.end() );
}

// [[Rcpp::export]]
String test_move_std_string_ctor() {
  std::string s = "test";
  return String( std::move( s ) );
}

// [[Rcpp::export]]
CharacterVector test_move_assignment() {
  std::vector<String> v = { "test", "abc" };
  v[1] = std::move( v[0] );
  return CharacterVector( v.begin(), v.end() );
}

// [[Rcpp::export]]
String test_move_std_string_assignment() {
  String x = "abc";
  std::string s = "test";
  x = std::move( s );
  return x;
}

// [[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);
}