Coder Social home page Coder Social logo

Comments (11)

wbraswell avatar wbraswell commented on June 1, 2024 1

@neurobin
Thank you so much for implementing the preplace() support, so far it seems to work as needed and thus I have included this upgraded regex functionality as part of the new RPerl v3.2!
👍

from jpcre2.

neurobin avatar neurobin commented on June 1, 2024

It's implemented,

Example:

typedef jpcre2::select<char>  jp;


jp::RegexReplace rr;
std::cout<<rr.getLastReplaceCount(); //should print 0

jp::Regex re("\\d");
rr.setSubject("123456789")
  .setRegexObject(&re)
  .setReplaceWith("#")
  .addModifier("g")
  .replace();

std::cout<<rr.getLastReplaceCount(); //should print 9

refer to the doc for details

from jpcre2.

wbraswell avatar wbraswell commented on June 1, 2024

@neurobin
I have been testing this new code for Perl compatibility, we are close but not quite there yet...
Imagine we have this input code in Perl, which will change $foo in place and set $bax to the replace count:

my string $foo = "SUBJECT";
my integer $bax = ( $foo =~ s/FIND/REPLACE_WITH/gms );

To implement this concisely in C++ using JPCRE2, we must introduce a new _fooRC "replace count" variable and some weird hacks to convert from string-to-integer type:

string foo = (const string) "SUBJECT";
integer _fooRC;
integer bax = ((((foo = regex("FIND", "ms").replace(foo, "REPLACE_WITH", "g", (regexsize*) &_fooRC)).max_size() * 0) + _fooRC));

You originally said we could implement a preplace() method, which should act like Perl by modifying $foo in place and returning the replace count, which should remove the need for the extra _fooRC variable and hack code:

string foo = (const string) "SUBJECT";
integer bax = ((regex("FIND", "ms").preplace(&foo, "REPLACE_WITH", "g"));

Also, we need preplace() to accept string literals as well as variable references, like this variant of the Perl code which has no $foo variable at all, changes the string literal and then discards the change, then returns the replace count to be stored in $bax as before:

my integer $bax = ( "SUBJECT" =~ s/FIND/REPLACE_WITH/gms );

This should be implemented in C++ using JPCRE2 as follows:

integer bax = ((regex("FIND", "ms").preplace("SUBJECT", "REPLACE_WITH", "g"));

Can you please create these 2 variants of the preplace() method?

Thanks so much! :-)

from jpcre2.

neurobin avatar neurobin commented on June 1, 2024

How about something like this:

size_t bax;
string foo = regex("FIND", "ms").replace("SUBJECT", "REPLACE_WITH", "g", &bax);

By the way, I don't think you can do something like (size_t*)&_fooRC from int _fooRC;

And for the second one:

size_t bax;
regex("FIND", "ms").replace("SUBJECT", "REPLACE_WITH", "g", &bax);

from jpcre2.

wbraswell avatar wbraswell commented on June 1, 2024

@neurobin
I was afraid that I did not give enough information in my previous post, this seems to be the case and for that I apologize.

Unfortunately, your suggestions do not provide the Perl compatibility we need.

In the examples I gave, we are storing the regex's return value into the variable $bax, but in Perl the return value of a regex can be used for any purpose, not only for the purpose of storing into a named variable. The very existence of a variable such as $bax is in no way guaranteed, thus we cannot use $bax itself anywhere within the regex, and we are forced to create the internal-use-only "replace count" (RC) variable such as _fooRC which is automatically named after the subject of the regex. The problem with the _fooRC solution is that it creates an extra variable, _fooRC itself, which does not exist in the original Perl input code and which must be created as an entirely separate code statement prepended to the regex. In Perl, all the regex code is done within one code statement without creating extra variables, this is what we must have.

This all means that I really must have the 2 variations of the preplace() method as I have specified above, they both modify the subject in-place and they both return the replace count. There is no other good solution, so can you please implement the 2 prelace() methods as we originally discussed?

(Also, yes you can cast the SIZE_T to and from int, it seems to work fine in my example code.)

from jpcre2.

neurobin avatar neurobin commented on June 1, 2024

ok, guess I will implement the preplace() method then.

And, I was not talking about size_t to/from int casting. That will work without any problem in this case. What I was trying to say is basically this:

#include <iostream>

void fun(size_t* x){
    std::cout<<"\n"<<(*x);
}

int main(){
    int rc=0;
    fun((size_t*)&rc); //should print 0, but outputs random value
    
    size_t c=0;
    fun(&c); //prints 0
    return 0;
}

But anyway, you won't have to dealt with pointer to counter with new preplace() method, it will return size_t count by value.

from jpcre2.

wbraswell avatar wbraswell commented on June 1, 2024

@neurobin
Okay sounds great, thanks!

from jpcre2.

wbraswell avatar wbraswell commented on June 1, 2024

@neurobin
Hi, just checking on the preplace() methods?
Thanks! :-)
~ Will

from jpcre2.

neurobin avatar neurobin commented on June 1, 2024

@wbraswell
I am kinda busy right now. It may take a week or two to make time for this project.
Thanks for all your support as usual.

~ Jahid

from jpcre2.

wbraswell avatar wbraswell commented on June 1, 2024

@neurobin
I appreciate that you will be making time, I need this update to move forward with RPerl.
Thanks,
~ Will

from jpcre2.

wbraswell avatar wbraswell commented on June 1, 2024

@neurobin
Hi thanks for any updates?
~ Will

from jpcre2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.