<div dir="ltr">Hello, all<br><br>This is my first post to this mailing list.<br><br>I might have found bugs with String::replace_first() and  String::replace_last().<br>The bugs can be reproduced by following code.<br>(R version 3.2.3  <span class="" id=":11d.52" tabindex="-1">Rcpp</span> 0.12.4)<br><br>-------------------<br>// [[<span class="" id=":11d.53" tabindex="-1">Rcpp</span>::export]]<br>void <span class="" id=":11d.54" tabindex="-1">rcpp</span>_string(){<br>    <br>    String s("<span class="" id=":11d.55" tabindex="-1">AABCDABCDA</span>");<br>    <span class="" id=":11d.56" tabindex="-1">Rcout</span> << "replace_all()\n";<br>    <span class="" id=":11d.57" tabindex="-1">Rcout</span> << s.replace_all("AB", "ab").get_<span class="" id=":11d.58" tabindex="-1">cstring</span>()  << "\n";<br>    <span class="" id=":11d.59" tabindex="-1">Rcout</span> << "\n";<br>    <br>    s="<span class="" id=":11d.60" tabindex="-1">AABCDABCDA</span>";<br>    <span class="" id=":11d.61" tabindex="-1">Rcout</span> << "replace_first()\n";<br>    <span class="" id=":11d.62" tabindex="-1">Rcout</span> << s.replace_first("AB", "ab").get_<span class="" id=":11d.63" tabindex="-1">cstring</span>() << "\n";<br>    <span class="" id=":11d.64" tabindex="-1">Rcout</span> << "\n";<br>    <br>    s="<span class="" id=":11d.65" tabindex="-1">AABCDABCDA</span>";<br>    <span class="" id=":11d.66" tabindex="-1">Rcout</span> << "replace_last()\n";<br>    <span class="" id=":11d.67" tabindex="-1">Rcout</span> << s.replace_last("AB", "ab").get_<span class="" id=":11d.68" tabindex="-1">cstring</span>()  << "\n";<br>    <span class="" id=":11d.69" tabindex="-1">Rcout</span> << "\n";<br>    <br>}<br>-------------------<br>> <span class="" id=":11d.70" tabindex="-1">rcpp</span>_string()<br>replace_all()<br><span class="" id=":11d.71" tabindex="-1">AabCDabCDA</span><br><br>replace_first()<br><span class="" id=":11d.72" tabindex="-1">abBCDABCDA</span><br><br>replace_last()<br><span class="" id=":11d.73" tabindex="-1">AABCDABCDab</span><br>-------------------<br><br>The problem is caused by find_first_of() and find_last_of() and it can be resolved by replacing these functions by find() and <span class="" id=":11d.74" tabindex="-1">rfind</span>().<br><br>-------------------<br><span class="" id=":11d.75" tabindex="-1">inline</span> String& replace_first(<span class="" id=":11d.76" tabindex="-1">const</span> char* s, <span class="" id=":11d.77" tabindex="-1">const</span> char* news) {<br>    <span class="" id=":11d.78" tabindex="-1">RCPP</span>_STRING_DEBUG_2("String::replace_first(<span class="" id=":11d.79" tabindex="-1">const</span> char* = '%s' , <span class="" id=":11d.80" tabindex="-1">const</span> char* = '%s')", s, news);<br>    if (is_<span class="" id=":11d.81" tabindex="-1">na</span>()) return *this;<br>    <span class="" id=":11d.82" tabindex="-1">setBuffer</span>();<br>    size_t index = buffer.find(s);<br>    //size_t index = buffer.find_first_of(s);<br>    if (index != std::string::<span class="" id=":11d.83" tabindex="-1">npos</span>) buffer.replace(index, <span class="" id=":11d.84" tabindex="-1">strlen</span>(s), news);<br>    valid = false;<br>    return *this;<br>}<br><br><span class="" id=":11d.85" tabindex="-1">inline</span> String& replace_last(<span class="" id=":11d.86" tabindex="-1">const</span> char* s, <span class="" id=":11d.87" tabindex="-1">const</span> char* news) {<br>    <span class="" id=":11d.88" tabindex="-1">RCPP</span>_STRING_DEBUG_2("String::replace_last(<span class="" id=":11d.89" tabindex="-1">const</span> char* = '%s' , <span class="" id=":11d.90" tabindex="-1">const</span> char* = '%s')", s, news);<br>    if (is_<span class="" id=":11d.91" tabindex="-1">na</span>()) return *this;<br>    <span class="" id=":11d.92" tabindex="-1">setBuffer</span>();<br>    size_t index = buffer.<span class="" id=":11d.93" tabindex="-1">rfind</span>(s);<br>    //size_t index = buffer.find_last_of(s);<br>    if (index != std::string::<span class="" id=":11d.94" tabindex="-1">npos</span>) buffer.replace(index, <span class="" id=":11d.95" tabindex="-1">strlen</span>(s), news);<br>    valid = false;<br>    return *this;<br>}<br>-------------------<br>> <span class="" id=":11d.96" tabindex="-1">rcpp</span>_string()<br>replace_all()<br><span class="" id=":11d.97" tabindex="-1">AabCDabCDA</span><br><br>replace_first()<br><span class="" id=":11d.98" tabindex="-1">AabCDABCDA</span><br><br>replace_last()<br><span class="" id=":11d.99" tabindex="-1">AABCDabCDA</span><br>-------------------<br><br>Regards<br><br>-------------------------------------------------------------------<br>   <br>     <span class="" id=":11d.100" tabindex="-1">Masaki</span> <span class="" id=":11d.101" tabindex="-1">Tsuda</span><br>     E-mail : <span class="" id=":11d.102" tabindex="-1">teuder</span>@<a href="http://gmail.com">gmail.com</a></div>