Skip to content

Commit

Permalink
Add Static Class Variable LastReplaceCount
Browse files Browse the repository at this point in the history
  • Loading branch information
wbraswell committed Jul 9, 2017
1 parent f6d7d3c commit 1e473ca
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 20 deletions.
24 changes: 23 additions & 1 deletion src/jpcre2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3697,6 +3697,14 @@ struct select{
PCRE2_SIZE error_offset;

public:
/// static class variable, get & set number of substitutions in most recent substitute regex
static int LastReplaceCount;
static int getLastReplaceCount() {
return LastReplaceCount;
}
static void setLastReplaceCount(int NewReplaceCount) {
LastReplaceCount = NewReplaceCount;
}

/// Default Constructor.
/// Initializes all class variables to defaults.
Expand Down Expand Up @@ -4370,7 +4378,7 @@ struct select{
return initReplace().setSubject(mains).setReplaceWith(repl).setModifier(mod).replace();
}
};

private:
//prevent object instantiation of select class
select();
Expand All @@ -4382,6 +4390,12 @@ struct select{
};//struct select
}//jpcre2 namespace

// static class variable, initialize number of substitutions in most recent substitute regex
template<> int jpcre2::select<char, (unsigned char)8>::Regex::LastReplaceCount = 0;
template<> int jpcre2::select<char16_t, (unsigned char)16>::Regex::LastReplaceCount = 0;
template<> int jpcre2::select<char32_t, (unsigned char)32>::Regex::LastReplaceCount = 0;
template<> int jpcre2::select<wchar_t, (unsigned char)32>::Regex::LastReplaceCount = 0;


inline void jpcre2::ModifierTable::parseModifierTable(std::string& tabjs, VecOpt& tabjv,
std::string& tab_s, VecOpt& tab_v,
Expand Down Expand Up @@ -4545,6 +4559,10 @@ typename jpcre2::select<Char_T, BS>::String jpcre2::select<Char_T, BS>::MatchEva
return RegexMatch::getSubject();
}
}

// static class variable, update number of substitutions in most recent substitute regex
Regex::setLastReplaceCount(ret);

//If everything's ok exit the loop
break;
}
Expand Down Expand Up @@ -4663,6 +4681,10 @@ typename jpcre2::select<Char_T, BS>::String jpcre2::select<Char_T, BS>::RegexRep
return *r_subject_ptr;
}
}

// static class variable, update number of substitutions in most recent substitute regex
Regex::setLastReplaceCount(ret);

//If everything's ok exit the loop
break;
}
Expand Down
40 changes: 26 additions & 14 deletions src/test0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
typedef jpcre2::select<char> jpc;
typedef jpcre2::select<wchar_t> jpw;


int main(){
jpc::Regex rec;
jpw::Regex rew;

rec.setPattern("\\d+").compile();
rew.setPattern(L"\\d+").compile();

jpw::VecNum vec_num32;
jpcre2::VecOff vec_eoff;

jpw::RegexMatch rmw;
size_t count =
rmw.setRegexObject(&rew)
Expand All @@ -29,7 +29,7 @@ int main(){
.setNumberedSubstringVector(&vec_num32)
.setMatchEndOffsetVector(&vec_eoff)
.match();

std::cout<<"\nMatch count: "<<count;
std::wcout<<"\nFirst match: "<<vec_num32[0][0];
std::cout<<"\nMatch ended at offset: "<<vec_eoff[vec_eoff.size()-1];
Expand All @@ -39,39 +39,51 @@ int main(){

jpc::RegexMatch rm;
jpc::RegexReplace rr;

rm.setRegexObject(&rec);
rr.setRegexObject(&rec);


jpc::VecNum vec_num8;
rm.setSubject("I am a subject with digits 3343242 4433243 443244")
.setModifier("g")
.setNumberedSubstringVector(&vec_num8)
.match();

std::cout<<"\nFirst match: " + vec_num8[0][0];

jpc::Regex rec_2("[\\S]+");
rm.setRegexObject(&rec_2)
.setSubject("I am subject")
.setNumberedSubstringVector(&vec_num8)
.match();
std::cout<<"\nFirst match: " + vec_num8[0][0];


int lrc = jpc::Regex::getLastReplaceCount();
if(lrc != 0) { std::cerr << "\n" << "jpc::Regex::LastReplaceCount should be 0 but instead is " << lrc << "\n"; }
else { std::cout << "\n" << "jpc::Regex::LastReplaceCount correctly found to be " << lrc << "\n"; }

std::cout<<"\nReplace: " +
rr.setSubject("I am a subject with digits 3343242 4433243 443244")
.setReplaceWith("@")
.setModifier("g")
.replace();



lrc = jpc::Regex::getLastReplaceCount();
if(lrc != 3) { std::cerr << "\n" << "jpc::Regex::LastReplaceCount should be 3 but instead is " << lrc << "\n"; }
else { std::cout << "\n" << "jpc::Regex::LastReplaceCount correctly found to be " << lrc << "\n"; }


std::cout<<"\nReplace2: " +
rr.setSubject("I am a subject with digits 3343242 4433243 443244")
.setReplaceWith("@")
.setModifier("g")
.setRegexObject(&rec_2)
.replace();

return 0;
}

lrc = jpc::Regex::getLastReplaceCount();
if(lrc != 9) { std::cerr << "\n" << "jpc::Regex::LastReplaceCount should be 9 but instead is " << lrc << "\n"; }
else { std::cout << "\n" << "jpc::Regex::LastReplaceCount correctly found to be " << lrc << "\n"; }

return 0;
}
12 changes: 10 additions & 2 deletions src/test_replace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ typedef jpcre2::select<char> jp;


int main(){
jp::Regex re;
jp::Regex re;

int lrc = jp::Regex::getLastReplaceCount();
if(lrc != 0) { std::cerr << "\n" << "jp::Regex::LastReplaceCount should be 0 but instead is " << lrc << "\n"; }
else { std::cout << "\n" << "jp::Regex::LastReplaceCount correctly found to be " << lrc << "\n"; }

//Compile the pattern
re.setPattern("(?:(?<word>[?.#@:]+)|(?<word>\\w+))\\s*(?<digit>\\d+)") //Set various parameters
Expand All @@ -36,6 +40,10 @@ int main(){

if(rr.getErrorNumber() != 0)
std::cerr<<"\n"<<rr.getErrorMessage();


lrc = jp::Regex::getLastReplaceCount();
if(lrc != 1) { std::cerr << "\n" << "jp::Regex::LastReplaceCount should be 1 but instead is " << lrc << "\n"; }
else { std::cout << "\n" << "jp::Regex::LastReplaceCount correctly found to be " << lrc << "\n"; }

return 0;
}
21 changes: 21 additions & 0 deletions src/test_shorts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,44 @@ int main(){
* A call to RegexReplace::replace() in the method chain will return the resultant string
*/

int lrc = jp::Regex::getLastReplaceCount();
if(lrc != 0) { std::cerr << "\n" << "jp::Regex::LastReplaceCount should be 0 but instead is " << lrc << "\n"; }
else { std::cout << "\n" << "jp::Regex::LastReplaceCount correctly found to be " << lrc << "\n"; }

std::cout<<"\n"<<
//replace first occurrence of a digit with @
jp::Regex("\\d").replace("I am the subject string 44", "@");

lrc = jp::Regex::getLastReplaceCount();
if(lrc != 1) { std::cerr << "\n" << "jp::Regex::LastReplaceCount should be 1 but instead is " << lrc << "\n"; }
else { std::cout << "\n" << "jp::Regex::LastReplaceCount correctly found to be " << lrc << "\n"; }

std::cout<<"\n"<<
//replace all occurrences of a digit with @
jp::Regex("\\d").replace("I am the subject string 44", "@", "g");

lrc = jp::Regex::getLastReplaceCount();
if(lrc != 2) { std::cerr << "\n" << "jp::Regex::LastReplaceCount should be 2 but instead is " << lrc << "\n"; }
else { std::cout << "\n" << "jp::Regex::LastReplaceCount correctly found to be " << lrc << "\n"; }

//swap two parts of a string
std::cout<<"\n"<<
jp::Regex("^([^\t]+)\t([^\t]+)$")
.replace("I am the subject\tTo be swapped according to tab", "$2 $1");

lrc = jp::Regex::getLastReplaceCount();
if(lrc != 1) { std::cerr << "\n" << "jp::Regex::LastReplaceCount should be 1 but instead is " << lrc << "\n"; }
else { std::cout << "\n" << "jp::Regex::LastReplaceCount correctly found to be " << lrc << "\n"; }

//Doing the above with method chain:
re.compile("^([^\t]+)\t([^\t]+)$");
jp::RegexReplace(&re).setSubject("I am the subject\tTo be swapped according to tab")
.setReplaceWith("$2 $1")
.replace();

lrc = jp::Regex::getLastReplaceCount();
if(lrc != 1) { std::cerr << "\n" << "jp::Regex::LastReplaceCount should be 1 but instead is " << lrc << "\n"; }
else { std::cout << "\n" << "jp::Regex::LastReplaceCount correctly found to be " << lrc << "\n"; }

return 0;
}
9 changes: 6 additions & 3 deletions src/testme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,12 @@ int main(){
//populates all vectors. It erases the matched part/s from the subject string.
std::cout<<"\n\n### default callback: \n"<<cme.setCallback(jp::callback::eraseFill).nreplace();
//After populating all vectors, you can use any type of callback without performing the match again.



//The following (uncomment if you wanna test) will give you assertion failure, because the callback1 only populates NumSub vector,
//but callback2 requires pre-exisiting (due to the 'false' argument to nreplace()) MapNas data:
cme.reset().setSubject(&s3).setRegexObject(&re).setFindAll().setCallback(callback1).nreplace();
//~ std::cout<<"\n\n### callback2: \n"<<cme.setCallback(callback2).nreplace(false); //Assertion failure.




Expand All @@ -155,6 +154,10 @@ int main(){
//Short note:
// * replace() funtion is for PCRE2 compatible substitute.
// * nreplace() is JPCRE2 native replace function.

int lrc = jp::Regex::getLastReplaceCount();
if(lrc != 1) { std::cerr << "\n" << "jp::Regex::LastReplaceCount should be 1 but instead is " << lrc << "\n"; }
else { std::cout << "\n" << "jp::Regex::LastReplaceCount correctly found to be " << lrc << "\n"; }

std::cout<<"\ncallback7: \n"<<cme.setCallback(callback7).setFindAll(false).replace();

Expand Down

0 comments on commit 1e473ca

Please sign in to comment.