@@ -262,7 +262,8 @@ namespace Rcpp {
262
262
RCPP_STRING_DEBUG_2 (" String::replace_first(const char* = '%s' , const char* = '%s')" , s, news);
263
263
if (is_na ()) return *this ;
264
264
setBuffer ();
265
- size_t index = buffer.find_first_of (s);
265
+ std::string s2 = std::string (s);
266
+ size_t index = std::distance (buffer.begin (), std::search (buffer.begin (), buffer.end (), s2.begin (), s2.end ()));
266
267
if (index != std::string::npos) buffer.replace (index, strlen (s), news);
267
268
valid = false ;
268
269
return *this ;
@@ -287,7 +288,8 @@ namespace Rcpp {
287
288
RCPP_STRING_DEBUG_2 (" String::replace_last(const char* = '%s' , const char* = '%s')" , s, news);
288
289
if (is_na ()) return *this ;
289
290
setBuffer ();
290
- size_t index = buffer.find_last_of (s);
291
+ std::string s2 = std::string (s);
292
+ size_t index = std::distance (buffer.begin (), std::find_end (buffer.begin (), buffer.end (), s2.begin (), s2.end ()));
291
293
if (index != std::string::npos) buffer.replace (index, strlen (s), news);
292
294
valid = false ;
293
295
return *this ;
@@ -313,10 +315,13 @@ namespace Rcpp {
313
315
RCPP_STRING_DEBUG_2 (" String::replace_all(const char* = '%s' , const char* = '%s')" , s, news);
314
316
if (is_na ()) return *this ;
315
317
setBuffer ();
316
- size_t lens = strlen (s), len_news = strlen (news), index = buffer.find (s);
317
- while (index != std::string::npos) {
318
- buffer.replace (index, lens, news);
319
- index = buffer.find (s, index + len_news);
318
+ std::string s2 = std::string (s);
319
+ std::string::iterator iter = buffer.begin ();
320
+ while (true ) {
321
+ iter = std::search (iter, buffer.end (), s2.begin (), s2.end ());
322
+ if (iter == buffer.end ()) break ;
323
+ size_t index = std::distance (buffer.begin (), iter);
324
+ if (index != std::string::npos) buffer.replace (index, strlen (s), news);
320
325
}
321
326
valid = false ;
322
327
return *this ;
0 commit comments