<div dir="ltr">Hi, I have confirmed the bug and fix.<div><br></div><div>Please send a PR. We will really appreciate this.</div><div><br></div><div>Best wishes,</div><div><br></div><div>KK</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 8, 2016 at 9:14 AM, 津田真樹 <span dir="ltr"><<a href="mailto:teuder@gmail.com" target="_blank">teuder@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I find a bug in String::replace_last().<br>
<br>
I can reproduce the bug with the following code.<br>
(I also showed output of replace_first and replace_all for comparison.)<br>
<br>
<br>
=============<br>
// [[Rcpp::export]]<br>
void rcpp_string(){<br>
<br>
    String s("abcdabcd");<br>
    Rcout << "replace_first()\n";<br>
    Rcout << s.replace_first("ab", "AB").get_cstring() << "\n";<br>
    Rcout << "\n";<br>
<br>
    s="abcdabcd";<br>
    Rcout << "replace_last()\n";<br>
    Rcout << s.replace_last("ab", "AB").get_cstring()  << "\n";<br>
    Rcout << "\n";<br>
<br>
    s="abcdabcd";<br>
    Rcout << "replace_all()\n";<br>
    Rcout << s.replace_all("ab", "AB").get_cstring()  << "\n";<br>
    Rcout << "\n";<br>
}<br>
=============<br>
<br>
output<br>
=============<br>
replace_first()<br>
ABcdabcd<br>
<br>
replace_last()<br>
abcdaABd<br>
<br>
replace_all()<br>
ABcdABcd<br>
=============<br>
<br>
<br>
I also tried to fix the bug with following code. And the bug seems to be fixed.<br>
<br>
=============<br>
inline String& replace_last(const char* s, const char* news) {<br>
    RCPP_STRING_DEBUG_2("String::replace_last(const char* = '%s' , const char* = '%s')", s, news);<br>
    if (is_na()) return *this;<br>
    setBuffer();<br>
    //size_t index = buffer.find_last_of(s);<br>
    size_t index = buffer.find_last_of(s) - (strlen(s)-1);<br>
    Rcout << "A" << index << "\n";<br>
    if (index != std::string::npos) buffer.replace(index, strlen(s), news);<br>
    valid = false;<br>
    return *this;<br>
}<br>
=============<br>
<br>
output<br>
=============<br>
replace_first()<br>
ABcdabcd<br>
<br>
replace_last()<br>
abcdABcd<br>
<br>
replace_all()<br>
ABcdABcd<br>
=============<br>
<br>
<br>
Mac OS 10.10.5<br>
R version 3.2.3<br>
Rcpp 0.12.4<br>
<br>
<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Qiang Kou<div><a href="mailto:qkou@umail.iu.edu" target="_blank">qkou@umail.iu.edu</a><br><div>School of Informatics and Computing, Indiana University</div><div><br></div></div></div></div>
</div>