-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstringmagic.cpp
More file actions
54 lines (33 loc) · 969 Bytes
/
stringmagic.cpp
File metadata and controls
54 lines (33 loc) · 969 Bytes
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
#include "stringmagic.h"
std::vector<std::string> trim_ws(std::vector<std::string> x){
int n = x.size();
std::vector<std::string> res(x);
std::string tmp, xi;
for(int i=0 ; i<n ; ++i){
xi = x[i];
if(!xi.empty() && (is_blank(xi[0]) || is_blank(xi[xi.size() - 1]))){
tmp = "";
int n_xi = xi.size();
while(n_xi >= 1 && is_blank(xi[n_xi - 1])) --n_xi;
int j = 0;
while(j < n_xi && is_blank(xi[j])) ++j;
while(j < n_xi) tmp += xi[j++];
res[i] = tmp;
}
}
return res;
}
SEXP std_string_to_r_string(std::string x){
SEXP res = PROTECT(Rf_ScalarString(Rf_mkCharCE(x.c_str(), CE_UTF8)));
UNPROTECT(1);
return res;
}
SEXP std_string_to_r_string(std::vector<std::string> x){
int n = x.size();
SEXP res = PROTECT(Rf_allocVector(STRSXP, n));
for(int i=0 ; i<n ; ++i){
SET_STRING_ELT(res, i, Rf_mkCharCE(x[i].c_str(), CE_UTF8));
}
UNPROTECT(1);
return res;
}