[Rcpp-devel] Bug report: String::replace_first() and String::replace_last()

Joris Meys jorismeys at gmail.com
Mon May 16 21:07:55 CEST 2016


Dear Masaki,

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.

Thank you
Joris

On Sun, May 15, 2016 at 5:19 PM, Masaki Tsuda <teuder at gmail.com> wrote:

> Hello, all
>
> This is my first post to this mailing list.
>
> I might have found bugs with String::replace_first() and
> String::replace_last().
> The bugs can be reproduced by following code.
> (R version 3.2.3  Rcpp 0.12.4)
>
> -------------------
> // [[Rcpp::export]]
> void rcpp_string(){
>
>     String s("AABCDABCDA");
>     Rcout << "replace_all()\n";
>     Rcout << s.replace_all("AB", "ab").get_cstring()  << "\n";
>     Rcout << "\n";
>
>     s="AABCDABCDA";
>     Rcout << "replace_first()\n";
>     Rcout << s.replace_first("AB", "ab").get_cstring() << "\n";
>     Rcout << "\n";
>
>     s="AABCDABCDA";
>     Rcout << "replace_last()\n";
>     Rcout << s.replace_last("AB", "ab").get_cstring()  << "\n";
>     Rcout << "\n";
>
> }
> -------------------
> > rcpp_string()
> replace_all()
> AabCDabCDA
>
> replace_first()
> abBCDABCDA
>
> replace_last()
> AABCDABCDab
> -------------------
>
> 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().
>
> -------------------
> inline String& replace_first(const char* s, const char* news) {
>     RCPP_STRING_DEBUG_2("String::replace_first(const char* = '%s' , const
> char* = '%s')", s, news);
>     if (is_na()) return *this;
>     setBuffer();
>     size_t index = buffer.find(s);
>     //size_t index = buffer.find_first_of(s);
>     if (index != std::string::npos) buffer.replace(index, strlen(s), news);
>     valid = false;
>     return *this;
> }
>
> 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.rfind(s);
>     //size_t index = buffer.find_last_of(s);
>     if (index != std::string::npos) buffer.replace(index, strlen(s), news);
>     valid = false;
>     return *this;
> }
> -------------------
> > rcpp_string()
> replace_all()
> AabCDabCDA
>
> replace_first()
> AabCDABCDA
>
> replace_last()
> AABCDabCDA
> -------------------
>
> Regards
>
> -------------------------------------------------------------------
>
>      Masaki Tsuda
>      E-mail : teuder at gmail.com
>
> _______________________________________________
> 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
>



-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Mathematical Modelling, Statistics and Bio-Informatics

tel :  +32 (0)9 264 61 79
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20160516/3db6464b/attachment.html>


More information about the Rcpp-devel mailing list