2222#include " wikisite.hpp"
2323#include " wikiutil.hpp"
2424#include " wikiuser.hpp"
25+ #include < QRegularExpression>
2526#include < QUrl>
2627
2728using namespace Huggle ;
@@ -205,7 +206,7 @@ void Message::processNextStep()
205206 this ->Error = MessageError_ArticleExist;
206207 } else
207208 {
208- this ->Fail (_l (" error-unknown-code" ,ec));
209+ this ->Fail (_l (" error-unknown-code" , ec));
209210 this ->Error = MessageError_Unknown;
210211 }
211212 return ;
@@ -319,17 +320,15 @@ void Message::processSend()
319320 if (this ->SectionKeep )
320321 {
321322 this ->Text = this ->appendText (this ->Text , this ->originalUnmodifiedPageText , this ->Title );
322- } else
323- {
323+ } else {
324324 // original page needs to be included in new value
325325 if (!this ->originalUnmodifiedPageText .isEmpty ())
326326 this ->Text = this ->originalUnmodifiedPageText + " \n\n " + this ->Text ;
327327 }
328328 this ->query ->Parameters = " title=" + QUrl::toPercentEncoding (User->GetTalk ()) + " &summary=" + QUrl::toPercentEncoding (summary)
329329 + " &text=" + QUrl::toPercentEncoding (this ->Text ) + parameters
330330 + " &token=" + QUrl::toPercentEncoding (this ->User ->GetSite ()->GetProjectConfig ()->Token_Csrf );
331- }else
332- {
331+ } else {
333332 this ->query ->Parameters = " title=" + QUrl::toPercentEncoding (User->GetTalk ()) + " §ion=new§iontitle="
334333 + QUrl::toPercentEncoding (this ->Title ) + " &summary=" + QUrl::toPercentEncoding (summary)
335334 + " &text=" + QUrl::toPercentEncoding (this ->Text ) + parameters + " &token="
@@ -357,8 +356,7 @@ void Message::processTalkPageRetrieval()
357356 this ->originalUnmodifiedPageText = page->Value ;
358357 this ->previousTalkPageRetrieved = true ;
359358 return ;
360- } else
361- {
359+ } else {
362360 if (!missing)
363361 {
364362 Huggle::Syslog::HuggleLogs->DebugLog (this ->query ->Result ->Data );
@@ -375,12 +373,10 @@ QString Message::appendText(QString text, QString original_text, QString section
375373 // there is nothing to insert this to
376374 return original_text += " \n\n " + text + " \n\n " ;
377375 }
378- # ifdef QT6_BUILD
376+
379377 QRegularExpression regex (" \\ s*==\\ s*" + QRegularExpression::escape (section_name) + " \\ s*==" );
380- #else
381- QRegExp regex (" \\ s*==\\ s*" + QRegExp::escape (section_name) + " \\ s*==" );
382- #endif
383- if (!original_text.contains (regex))
378+ QRegularExpressionMatch regex_match;
379+ if (!original_text.contains (regex, ®ex_match))
384380 {
385381 // there is no section to append to
386382 if (!original_text.isEmpty ())
@@ -390,32 +386,20 @@ QString Message::appendText(QString text, QString original_text, QString section
390386 original_text += " == " + section_name + " ==\n\n " + text;
391387 return original_text;
392388 }
393- #ifdef QT6_BUILD
394- QRegularExpression header (" \\ s*==.*==\\ s*" );
395- #else
396- QRegExp header (" \\ s*==.*==\\ s*" );
397- #endif
398- int start_of_desired_section = original_text.lastIndexOf (regex);
389+
399390 // we need to check if there is any other section after this one
391+ int start_of_desired_section = original_text.lastIndexOf (regex) + regex_match.capturedView ().length ();
400392 QString section = original_text.mid (start_of_desired_section);
401- if (section.contains (" \n " ))
402- {
403- // cut the header text
404- int Diff = section.indexOf (" \n " ) + 1 ;
405- start_of_desired_section += Diff;
406- section = section.mid (Diff);
407- }
393+ QRegularExpression next_header (" \\ s*==.*==\\ s*" );
408394 int start_point = start_of_desired_section;
409- if (section.contains (header ))
395+ if (section.contains (next_header ))
410396 {
411397 // yes there is some other section, so we need to know where it is, so that we know where current
412398 // section ends (we want to append text to bottom of current section)
413- start_point += section.indexOf (header);
414- } else
415- {
399+ start_point += section.indexOf (next_header);
400+ } else {
416401 start_point += section.length ();
417402 }
418403 // write the text exactly after the start point, but leave some space after it
419- original_text = original_text.insert (start_point, " \n\n " + text + " \n " );
420- return original_text;
404+ return original_text.insert (start_point, " \n\n " + text + " \n " );
421405}
0 commit comments