<div dir="ltr"><div><div><div>Dear Masaki,<br><br></div>There is no need to send the same message 16 times to this list. It won't get it answered faster. If anything, it'll likely lessen the chance of receiving an answer. Please use this list with a bit consideration of those who aren't necessarily able to answer your question but still follow the discussion in order to learn a thing or two.<br><br></div>Thank you<br></div>Joris<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 15, 2016 at 5:19 PM, Masaki Tsuda <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"><div class="HOEnZb"><div class="h5">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  Rcpp 0.12.4)<br>
<br>
-------------------<br>
// [[Rcpp::export]]<br>
void rcpp_string(){<br>
<br>
    String s("AABCDABCDA");<br>
    Rcout << "replace_all()\n";<br>
    Rcout << s.replace_all("AB", "ab").get_cstring()  << "\n";<br>
    Rcout << "\n";<br>
<br>
    s="AABCDABCDA";<br>
    Rcout << "replace_first()\n";<br>
    Rcout << s.replace_first("AB", "ab").get_cstring() << "\n";<br>
    Rcout << "\n";<br>
<br>
    s="AABCDABCDA";<br>
    Rcout << "replace_last()\n";<br>
    Rcout << s.replace_last("AB", "ab").get_cstring()  << "\n";<br>
    Rcout << "\n";<br>
<br>
}<br>
-------------------<br>
> rcpp_string()<br>
replace_all()<br>
AabCDabCDA<br>
<br>
replace_first()<br>
abBCDABCDA<br>
<br>
replace_last()<br>
AABCDABCDab<br>
-------------------<br>
<br>
The problem is caused by find_first_of() and find_last_of() and it can be resolved by replacing these functions to find() and rfind().<br>
<br>
-------------------<br>
inline String& replace_first(const char* s, const char* news) {<br>
    RCPP_STRING_DEBUG_2("String::replace_first(const char* = '%s' , const char* = '%s')", s, news);<br>
    if (is_na()) return *this;<br>
    setBuffer();<br>
    size_t index = buffer.find(s);<br>
    //size_t index = buffer.find_first_of(s);<br>
    if (index != std::string::npos) buffer.replace(index, strlen(s), news);<br>
    valid = false;<br>
    return *this;<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.rfind(s);<br>
    //size_t index = buffer.find_last_of(s);<br>
    if (index != std::string::npos) buffer.replace(index, strlen(s), news);<br>
    valid = false;<br>
    return *this;<br>
}<br>
-------------------<br>
> rcpp_string()<br>
replace_all()<br>
AabCDabCDA<br>
<br>
replace_first()<br>
AabCDABCDA<br>
<br>
replace_last()<br>
AABCDabCDA<br>
-------------------<br>
<br>
Regards<br>
<br>
-------------------------------------------------------------------<br>
<br>
     Masaki Tsuda<br>
     E-mail : <a href="mailto:teuder@gmail.com">teuder@gmail.com</a><br>
<br>
</div></div><div class="HOEnZb"><div class="h5">_______________________________________________<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>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature"><div dir="ltr">Joris Meys<br>Statistical consultant<br><br>Ghent University<br>Faculty of Bioscience Engineering<br>Department of Mathematical Modelling, Statistics and Bio-Informatics<br><br>tel : <span style="color:rgb(49,49,49);font-family:'Trebuchet MS',arial,verdana,tahoma,arial,'Lucida Grande','Lucida Sans Unicode',sans-serif;font-size:12px"> +32 (0)9 264 61 79</span><br>Joris.Meys@Ugent.be<br>-------------------------------<br>Disclaimer : <a href="http://helpdesk.ugent.be/e-maildisclaimer.php" target="_blank">http://helpdesk.ugent.be/e-maildisclaimer.php</a></div></div>
</div>