[413088]: / src / string.cpp

Download this file

26 lines (21 with data), 832 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
#include <Rcpp.h>
using namespace Rcpp;
// Function to replace a pattern in a string
void replacePattern(std::string &str, const std::string &pattern, const std::string &replacement) {
size_t pos = 0;
while ((pos = str.find(pattern, pos)) != std::string::npos) {
str.replace(pos, pattern.length(), replacement);
pos += replacement.length();
}
}
// [[Rcpp::export]]
Rcpp::CharacterVector replacePatternInRcppVectorWrapper(Rcpp::CharacterVector textVector, const std::string &pattern, const std::string &replacement) {
// replacePatternInRcppVector(textVector, pattern, replacement);
R_xlen_t n = textVector.size();
for (R_xlen_t i = 0; i < n; ++i) {
std::string str = Rcpp::as<std::string>(textVector[i]);
replacePattern(str, pattern, replacement);
textVector[i] = str;
}
return textVector;
}