|
a |
|
b/src/string.cpp |
|
|
1 |
#include <Rcpp.h> |
|
|
2 |
using namespace Rcpp; |
|
|
3 |
|
|
|
4 |
// Function to replace a pattern in a string |
|
|
5 |
void replacePattern(std::string &str, const std::string &pattern, const std::string &replacement) { |
|
|
6 |
size_t pos = 0; |
|
|
7 |
while ((pos = str.find(pattern, pos)) != std::string::npos) { |
|
|
8 |
str.replace(pos, pattern.length(), replacement); |
|
|
9 |
pos += replacement.length(); |
|
|
10 |
} |
|
|
11 |
} |
|
|
12 |
|
|
|
13 |
// [[Rcpp::export]] |
|
|
14 |
Rcpp::CharacterVector replacePatternInRcppVectorWrapper(Rcpp::CharacterVector textVector, const std::string &pattern, const std::string &replacement) { |
|
|
15 |
// replacePatternInRcppVector(textVector, pattern, replacement); |
|
|
16 |
|
|
|
17 |
R_xlen_t n = textVector.size(); |
|
|
18 |
for (R_xlen_t i = 0; i < n; ++i) { |
|
|
19 |
std::string str = Rcpp::as<std::string>(textVector[i]); |
|
|
20 |
replacePattern(str, pattern, replacement); |
|
|
21 |
textVector[i] = str; |
|
|
22 |
} |
|
|
23 |
|
|
|
24 |
return textVector; |
|
|
25 |
} |