1616
1717#include < cstdarg>
1818#include < cctype>
19- #include < pcre.h>
19+ #define PCRE2_CODE_UNIT_WIDTH 8
20+ #include < pcre2.h>
2021
2122#include < ctype.h>
2223
@@ -58,16 +59,16 @@ struct SoundFile
5859struct SoundInfo
5960{
6061 std::vector<int > sounds;
61- std::unique_ptr<pcre*> re = std::unique_ptr<pcre*>(new (pcre*)());
62- std::unique_ptr<pcre_extra*> extra = std::unique_ptr<pcre_extra*>(new (pcre_extra*)(NULL ));
62+
63+ std::unique_ptr<pcre2_code*> re = std::unique_ptr<pcre2_code*>(new (pcre2_code*)(NULL ));
64+ std::unique_ptr<pcre2_match_data*> match_data = std::unique_ptr<pcre2_match_data*>(new pcre2_match_data*(NULL ));
6365
6466 SoundInfo () = default ;
6567 SoundInfo (SoundInfo&) = delete ;
6668 SoundInfo (SoundInfo&&) = default ;
6769 ~SoundInfo ()
6870 {
69- if (re.get () && *re) free (*re);
70- if (extra.get () && *extra) pcre_free_study (*extra);
71+ if (re.get () && *re) pcre2_code_free (*re);
7172 }
7273};
7374
@@ -91,8 +92,8 @@ festring getstr(FILE *f, truth word)
9192FILE *debf = NULL ;
9293void soundeffects::initSound ()
9394{
94- const char *error ;
95- int erroffset;
95+ int errorCode ;
96+ PCRE2_SIZE erroffset;
9697
9798 if (SoundState == 0 )
9899 {
@@ -174,10 +175,15 @@ void soundeffects::initSound()
174175 }
175176
176177 // configure the regex
177- *si.re = pcre_compile (Pattern.CStr (), 0 , &error, &erroffset, NULL );
178- if (debf && !*si.re ) fprintf (debf, " PCRE compilation failed at expression offset %d: %s\n " , erroffset, error);
179- if (*si.re ) *si.extra = pcre_study (*si.re , 0 , &error);
180- if (error) *si.extra = NULL ;
178+ *si.re = pcre2_compile (reinterpret_cast <const unsigned char *>(Pattern.CStr ()), Pattern.GetSize (), 0 , &errorCode, &erroffset, NULL );
179+ if (debf && !*si.re ) fprintf (debf, " PCRE compilation failed at expression offset %ld: %d\n " , erroffset, errorCode);
180+
181+ if (*si.match_data .get () && *si.match_data )
182+ {
183+ pcre2_match_data_free (*si.match_data );
184+ }
185+
186+ *si.match_data = pcre2_match_data_create_from_pattern (*si.re , NULL );
181187
182188 // configure the assigned files, now they are separated with ',' and the filename now accepts spaces.
183189 festring FileName;
@@ -257,7 +263,7 @@ SoundFile* soundeffects::findMatchingSound(festring Buffer)
257263 DBG1 (Buffer.CStr ());
258264 for (int i = patterns.size () - 1 ; i >= 0 ; i--){
259265 if (*patterns[i].re )
260- if (pcre_exec (*patterns[i].re , *patterns[i]. extra , Buffer.CStr (), Buffer.GetSize (), 0 , 0 , NULL , 0 ) >= 0 ){
266+ if (pcre2_match (*patterns[i].re , reinterpret_cast < const unsigned char *>( Buffer.CStr ()) , Buffer.GetSize (), 0 , 0 , *patterns[i]. match_data , 0 ) >= 0 ){
261267 SoundFile* p = &files[patterns[i].sounds [rand () % patterns[i].sounds .size ()]];
262268 DBG1 (p->filename .CStr ());
263269 return p;
0 commit comments