[Rcpp-devel] Bug: replace_last()

Qiang Kou qkou at umail.iu.edu
Mon May 16 20:04:28 CEST 2016


Hi, I have confirmed the bug and fix.

Please send a PR. We will really appreciate this.

Best wishes,

KK

On Sun, May 8, 2016 at 9:14 AM, 津田真樹 <teuder at gmail.com> wrote:

> I find a bug in String::replace_last().
>
> I can reproduce the bug with the following code.
> (I also showed output of replace_first and replace_all for comparison.)
>
>
> =============
> // [[Rcpp::export]]
> void rcpp_string(){
>
>     String s("abcdabcd");
>     Rcout << "replace_first()\n";
>     Rcout << s.replace_first("ab", "AB").get_cstring() << "\n";
>     Rcout << "\n";
>
>     s="abcdabcd";
>     Rcout << "replace_last()\n";
>     Rcout << s.replace_last("ab", "AB").get_cstring()  << "\n";
>     Rcout << "\n";
>
>     s="abcdabcd";
>     Rcout << "replace_all()\n";
>     Rcout << s.replace_all("ab", "AB").get_cstring()  << "\n";
>     Rcout << "\n";
> }
> =============
>
> output
> =============
> replace_first()
> ABcdabcd
>
> replace_last()
> abcdaABd
>
> replace_all()
> ABcdABcd
> =============
>
>
> I also tried to fix the bug with following code. And the bug seems to be
> fixed.
>
> =============
> inline String& replace_last(const char* s, const char* news) {
>     RCPP_STRING_DEBUG_2("String::replace_last(const char* = '%s' , const
> char* = '%s')", s, news);
>     if (is_na()) return *this;
>     setBuffer();
>     //size_t index = buffer.find_last_of(s);
>     size_t index = buffer.find_last_of(s) - (strlen(s)-1);
>     Rcout << "A" << index << "\n";
>     if (index != std::string::npos) buffer.replace(index, strlen(s), news);
>     valid = false;
>     return *this;
> }
> =============
>
> output
> =============
> replace_first()
> ABcdabcd
>
> replace_last()
> abcdABcd
>
> replace_all()
> ABcdABcd
> =============
>
>
> Mac OS 10.10.5
> R version 3.2.3
> Rcpp 0.12.4
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>



-- 
Qiang Kou
qkou at umail.iu.edu
School of Informatics and Computing, Indiana University
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20160516/da663d3a/attachment-0001.html>


More information about the Rcpp-devel mailing list