Comments (11)
@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.
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
from jpcre2.
@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.
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.
@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.
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.
@neurobin
Okay sounds great, thanks!
from jpcre2.
@neurobin
Hi, just checking on the preplace() methods?
Thanks! :-)
~ Will
from jpcre2.
@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.
@neurobin
I appreciate that you will be making time, I need this update to move forward with RPerl.
Thanks,
~ Will
from jpcre2.
@neurobin
Hi thanks for any updates?
~ Will
from jpcre2.
Related Issues (20)
- multiple definition of c++ header-only-library HOT 5
- Is the library thread safe? HOT 4
- Get character positions for matches (only outermost group) HOT 3
- undefined reference to `std::ios_base::Init::~Init()' when compiling user code HOT 4
- Configure Script Needs To Detect Compiler Support For wstring_convert HOT 3
- std::wstring_convert deprecated in C++17 HOT 6
- Clang on BSD & MacOSX; error: using 'typename' outside of template HOT 1
- Clang on BSD & MacOSX; error: no member named 'setlocale' in namespace 'std' HOT 1
- Where the matching ends? HOT 9
- Replacing by evaluator, where the match starts? HOT 2
- GCC Warnings, "type qualifiers ignored on function return type" HOT 1
- unmatched optional captures at the end don't end up in numbered substring vector HOT 3
- Require C++11? HOT 3
- pcre2_dfa_match()? HOT 1
- Tutorial for embbed on JNI Android App CMAKE
- Use vector instead of map for numbered substrings HOT 1
- Memory leak with getMatchObject() function HOT 1
- jpcre2.hpp: error: undefined reference to `_imp__pcre2_code_free_8' HOT 22
- Match Evaluator HOT 21
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jpcre2.