changeset 31997:7e92d6468a35

Replace deprecated QRegExp by QRegularExpression in libgui/src/m-editor. * libgui/src/m-editor/file-editor-tab.cc (file_editor_tab::handle_context_menu_edit, file_editor_tab::update_lexer_settings, file_editor_tab::do_comment_selected_text, file_editor_tab::get_function_name), libgui/src/m-editor/octave-qscintilla.cc (octave_qscintilla::smart_indent, octave_qscintilla::smart_indent_line_or_selected_text, octave_qscintilla::contextmenu_run, octave_qscintilla::auto_close): Replace deprecated QRegExp by QRegularExpression.
author Markus Mützel <markus.muetzel@gmx.de>
date Wed, 12 Apr 2023 13:22:50 +0200
parents 2f451ad8b410
children 4d01a2860f3e
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/octave-qscintilla.cc
diffstat 2 files changed, 149 insertions(+), 121 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Wed Apr 12 12:02:25 2023 +0200
+++ b/libgui/src/m-editor/file-editor-tab.cc	Wed Apr 12 13:22:50 2023 +0200
@@ -44,8 +44,10 @@
 #include <QMessageBox>
 #include <QPrintDialog>
 #include <QPushButton>
+#include <QRegularExpression>
 #include <QScrollBar>
 #include <QSaveFile>
+#include <QStandardPaths>
 #include <QStyle>
 #include <QTextBlock>
 #include <QTextCodec>
@@ -326,38 +328,42 @@
 
 void file_editor_tab::handle_context_menu_edit (const QString& word_at_cursor)
 {
-  // Search for a subfunction in actual file (this is done first because
-  // Octave finds this function before others with the same name in the
-  // search path.
-  QRegExp rxfun1 ("^[\t ]*function[^=]+=[\t ]*"
-                  + word_at_cursor + "[\t ]*\\([^\\)]*\\)[\t ]*$");
-  QRegExp rxfun2 ("^[\t ]*function[\t ]+"
-                  + word_at_cursor + "[\t ]*\\([^\\)]*\\)[\t ]*$");
-  QRegExp rxfun3 ("^[\t ]*function[\t ]+"
-                  + word_at_cursor + "[\t ]*$");
-  QRegExp rxfun4 ("^[\t ]*function[^=]+=[\t ]*"
-                  + word_at_cursor + "[\t ]*$");
-
-  int pos_fct = -1;
+  // Search for a function with that name in the current file
+  // This is done first because local functions and subfunctions have priority
+  // over other functions with the same name in the load path.
+  QRegularExpression rxfun1 {"^[\t ]*function[^=]+=[\t ]*"
+                             + word_at_cursor + "[\t ]*\\([^\\)]*\\)[\t ]*$"};
+  QRegularExpression rxfun2 {"^[\t ]*function[\t ]+"
+                             + word_at_cursor + "[\t ]*\\([^\\)]*\\)[\t ]*$"};
+  QRegularExpression rxfun3 {"^[\t ]*function[\t ]+"
+                             + word_at_cursor + "[\t ]*$"};
+  QRegularExpression rxfun4 {"^[\t ]*function[^=]+=[\t ]*"
+                             + word_at_cursor + "[\t ]*$"};
+
+  QRegularExpressionMatch match;
   QStringList lines = m_edit_area->text ().split ("\n");
 
   int line;
   for (line = 0; line < lines.count (); line++)
     {
-      if ((pos_fct = rxfun1.indexIn (lines.at (line))) != -1)
+      match = rxfun1.match (lines.at (line));
+      if (match.hasMatch ())
         break;
-      if ((pos_fct = rxfun2.indexIn (lines.at (line))) != -1)
+      match = rxfun2.match (lines.at (line));
+      if (match.hasMatch ())
         break;
-      if ((pos_fct = rxfun3.indexIn (lines.at (line))) != -1)
+      match = rxfun3.match (lines.at (line));
+      if (match.hasMatch ())
         break;
-      if ((pos_fct = rxfun4.indexIn (lines.at (line))) != -1)
+      match = rxfun4.match (lines.at (line));
+      if (match.hasMatch ())
         break;
     }
 
-  if (pos_fct > -1)
+  if (match.hasMatch ())
     {
       // reg expr. found: it is an internal function
-      m_edit_area->setCursorPosition (line, pos_fct);
+      m_edit_area->setCursorPosition (line, match.capturedStart ());
       m_edit_area->SendScintilla (2232, line);     // SCI_ENSUREVISIBLE
       // SCI_VISIBLEFROMDOCLINE
       int vis_line = m_edit_area->SendScintilla (2220, line);
@@ -890,7 +896,7 @@
                   QString keyword = QString (lexer->keywords (i));
 
                   QStringList keyword_list
-                    = keyword.split (QRegExp (R"(\s+)"));
+                    = keyword.split (QRegularExpression {R"(\s+)"});
 
                   for (int j = 0; j < keyword_list.size (); j++)
                     m_lexer_apis->add (keyword_list.at (j));
@@ -1558,7 +1564,7 @@
 
 void file_editor_tab::do_comment_selected_text (bool comment, bool input_str)
 {
-  QRegExp rxc;
+  QRegularExpression rxc;
   QString ws = "^(?:[ \\t]*)";
   QStringList comment_str = m_edit_area->comment_string (comment);
   QString used_comment_str = comment_str.at (0);
@@ -1615,7 +1621,7 @@
             regexp = regexp + QString ("|");
           regexp = regexp + comment_str_sorted.at (i);
         }
-      rxc = QRegExp (ws + "(" + regexp + ")");
+      rxc = QRegularExpression {ws + "(" + regexp + ")"};
     }
 
   // Do the commenting/uncommenting
@@ -1627,7 +1633,7 @@
       int lineFrom, lineTo, colFrom, colTo;
       int change_col_from = 1;
       int change_col_to = 1;
-      bool removed;
+      bool removed = false;
 
       m_edit_area->getSelection (&lineFrom, &colFrom, &lineTo, &colTo);
 
@@ -1643,11 +1649,13 @@
           else
             {
               QString line (m_edit_area->text (i));
-              if ((removed = line.contains (rxc)))
+              QRegularExpressionMatch match = rxc.match (line);
+              
+              if (match.hasMatch ())
                 {
-                  len = rxc.matchedLength ();   // complete length
-                  QString matched_text = rxc.capturedTexts ().at (0);
-                  lenc = matched_text.remove (QRegExp (ws)).length ();  // only comment string
+                  len = match.capturedLength ();   // complete length
+                  QString matched_text = match.captured (0);
+                  lenc = matched_text.remove (QRegularExpression {ws}).length ();  // only comment string
                   m_edit_area->setSelection (i, len-lenc, i, len);
                   m_edit_area->removeSelectedText ();
                 }
@@ -1689,11 +1697,12 @@
       else
         {
           QString line (m_edit_area->text (cpline));
-          if (line.contains (rxc))
+          QRegularExpressionMatch match = rxc.match (line);
+          if (match.hasMatch ())
             {
-              len = rxc.matchedLength ();   // complete length
-              QString matched_text = rxc.capturedTexts ().at (0);
-              lenc = matched_text.remove (QRegExp (ws)).length ();  // only comment string
+              len = match.capturedLength ();  // complete length
+              QString matched_text = match.captured (0);
+              lenc = matched_text.remove (QRegularExpression {ws}).length ();  // only comment string
               m_edit_area->setSelection (cpline, len-lenc, cpline, len);
               m_edit_area->removeSelectedText ();
             }
@@ -2504,13 +2513,14 @@
 
   QFileDialog *file_dialog = qobject_cast<QFileDialog *> (sender ());
 
-  QRegExp rx ("\\*\\.([^ ^\\)]*)[ \\)]");   // regexp for suffix in filter
-  int index = rx.indexIn (filter, 0);       // get first suffix in filter
-
-  if (index > -1)
-    file_dialog->setDefaultSuffix (rx.cap (1)); // found a suffix, set default
+  // regexp for suffix in filter
+  QRegularExpression rx {"\\*\\.([^ ^\\)]*)[ \\)]"};
+  QRegularExpressionMatch match = rx.match (filter);
+
+  if (match.hasMatch ())
+    file_dialog->setDefaultSuffix (match.captured (1)); // found a suffix, set default
   else
-    file_dialog->setDefaultSuffix ("");         // not found, clear default
+    file_dialog->setDefaultSuffix ("");  // not found, clear default
 }
 
 bool file_editor_tab::check_valid_identifier (QString file_name)
@@ -3343,26 +3353,31 @@
 
 QString file_editor_tab::get_function_name ()
 {
-  QRegExp rxfun1 ("^[\t ]*function[^=]+=([^\\(]+)\\([^\\)]*\\)[\t ]*$");
-  QRegExp rxfun2 ("^[\t ]*function[\t ]+([^\\(]+)\\([^\\)]*\\)[\t ]*$");
-  QRegExp rxfun3 ("^[\t ]*function[^=]+=[\t ]*([^\\s]+)[\t ]*$");
-  QRegExp rxfun4 ("^[\t ]*function[\t ]+([^\\s]+)[\t ]*$");
-  QRegExp rxfun5 ("^[\t ]*classdef[\t ]+([^\\s]+)[\t ]*$");
+  QRegularExpression rxfun1 {"^[\t ]*function[^=]+=([^\\(]+)\\([^\\)]*\\)[\t ]*$"};
+  QRegularExpression rxfun2 {"^[\t ]*function[\t ]+([^\\(]+)\\([^\\)]*\\)[\t ]*$"};
+  QRegularExpression rxfun3 {"^[\t ]*function[^=]+=[\t ]*([^\\s]+)[\t ]*$"};
+  QRegularExpression rxfun4 {"^[\t ]*function[\t ]+([^\\s]+)[\t ]*$"};
+  QRegularExpression rxfun5 {"^[\t ]*classdef[\t ]+([^\\s]+)[\t ]*$"};
 
   QStringList lines = m_edit_area->text ().split ("\n");
 
   for (int i = 0; i < lines.count (); i++)
     {
-      if (rxfun1.indexIn (lines.at (i)) != -1)
-        return rxfun1.cap (1).remove (QRegExp ("[ \t]*"));
-      else if (rxfun2.indexIn (lines.at (i)) != -1)
-        return rxfun2.cap (1).remove (QRegExp ("[ \t]*"));
-      else if (rxfun3.indexIn (lines.at (i)) != -1)
-        return rxfun3.cap (1).remove (QRegExp ("[ \t]*"));
-      else if (rxfun4.indexIn (lines.at (i)) != -1)
-        return rxfun4.cap (1).remove (QRegExp ("[ \t]*"));
-      else if (rxfun5.indexIn (lines.at (i)) != -1)
-        return rxfun5.cap (1).remove (QRegExp ("[ \t]*"));
+      QRegularExpressionMatch match = rxfun1.match (lines.at (i));
+      if (match.hasMatch ())
+        return match.captured (1).remove (QRegularExpression {"[ \t]*"});
+      match = rxfun2.match (lines.at (i));
+      if (match.hasMatch ())
+        return match.captured (1).remove (QRegularExpression {"[ \t]*"});
+      match = rxfun3.match (lines.at (i));
+      if (match.hasMatch ())
+        return match.captured (1).remove (QRegularExpression {"[ \t]*"});
+      match = rxfun4.match (lines.at (i));
+      if (match.hasMatch ())
+        return match.captured (1).remove (QRegularExpression {"[ \t]*"});
+      match = rxfun5.match (lines.at (i));
+      if (match.hasMatch ())
+        return match.captured (1).remove (QRegularExpression {"[ \t]*"});
     }
 
   return QString ();
--- a/libgui/src/m-editor/octave-qscintilla.cc	Wed Apr 12 12:02:25 2023 +0200
+++ b/libgui/src/m-editor/octave-qscintilla.cc	Wed Apr 12 13:22:50 2023 +0200
@@ -36,6 +36,7 @@
 #include <QMessageBox>
 #include <QMimeData>
 #include <QPointer>
+#include <QRegularExpression>
 #include <QTemporaryFile>
 #include <QToolTip>
 #include <QVBoxLayout>
@@ -553,31 +554,32 @@
 {
   QString prevline = text (line);
 
-  QRegExp bkey = QRegExp ("^[\t ]*(if|for|while|switch"
-                          "|do|function|properties|events|classdef"
-                          "|unwind_protect|try"
-                          "|parfor|methods)"
-                          "[\r]?[\n\t #%]");
+  QRegularExpression bkey {"^[\t ]*(if|for|while|switch"
+                           "|do|function|properties|events|classdef"
+                           "|unwind_protect|try"
+                           "|parfor|methods)"
+                           "[\r]?[\n\t #%]"};
   // last word except for comments, assuming no ' or " in comment.
   // rx_end = QRegExp ("(\\w+)[ \t;\r\n]*([%#][^\"']*)?$");
 
   // last word except for comments,
   // allowing % and # in single or double quoted strings
   // FIXME: This will get confused by transpose.
-  QRegExp ekey = QRegExp ("(?:(?:['\"][^'\"]*['\"])?[^%#]*)*"
-                          "(\\w+)[ \t;\r\n]*(?:[%#].*)?$");
+  QRegularExpression ekey {"(?:(?:['\"][^'\"]*['\"])?[^%#]*)*"
+                           "(\\w+)[ \t;\r\n]*(?:[%#].*)?$"};
 
-  int bpos = bkey.indexIn (prevline, 0);
-  int epos;
+  QRegularExpressionMatch bmatch = bkey.match (prevline);
 
-  if (bpos > -1)
+  if (bmatch.hasMatch ())
     {
       // Found keyword after that indentation should be added
 
       // Check for existing end statement in the same line
-      epos = ekey.indexIn (prevline, bpos);
-      QString first_word = bkey.cap(1);
-      bool inline_end = (epos > -1) && is_end (ekey.cap(1), first_word);
+      QRegularExpressionMatch ematch = ekey.match (prevline,
+                                                   bmatch.capturedStart ());
+      QString first_word = bmatch.captured (1);
+      bool inline_end = ematch.hasMatch ()
+                        && is_end (ematch.captured (1), first_word);
 
       if (do_smart_indent && ! inline_end)
         {
@@ -588,7 +590,9 @@
 
       if (do_auto_close
           && ! inline_end
-          && ! first_word.contains (QRegExp ("(?:case|otherwise|unwind_protect_cleanup)")))
+          && ! first_word.contains
+                            (QRegularExpression
+                             {"(?:case|otherwise|unwind_protect_cleanup)"}))
         {
           // Do auto close
           auto_close (do_auto_close, line, prevline, first_word);
@@ -597,8 +601,8 @@
       return;
     }
 
-  QRegExp mkey = QRegExp ("^[\t ]*(?:else|elseif|catch|unwind_protect_cleanup)"
-                          "[\r]?[\t #%\n]");
+  QRegularExpression mkey {"^[\t ]*(?:else|elseif|catch|unwind_protect_cleanup)"
+                           "[\r]?[\t #%\n]"};
   if (prevline.contains (mkey))
     {
       int prev_ind = indentation (line-1);
@@ -614,14 +618,14 @@
       return;
     }
 
-  QRegExp case_key = QRegExp ("^[\t ]*(?:case|otherwise)[\r]?[\t #%\n]");
+  QRegularExpression case_key {"^[\t ]*(?:case|otherwise)[\r]?[\t #%\n]"};
   if (prevline.contains (case_key) && do_smart_indent)
     {
       QString last_line = text (line-1);
       int prev_ind = indentation (line-1);
       int act_ind = indentation (line);
 
-      if (last_line.contains (QRegExp ("^[\t ]*switch")))
+      if (last_line.contains (QRegularExpression {"^[\t ]*switch"}))
         {
           indent (line+1);
           act_ind = indentation (line+1);
@@ -638,8 +642,9 @@
       setCursorPosition (line+1, act_ind);
     }
 
-  ekey = QRegExp ("^[\t ]*(?:end|endif|endfor|endwhile|until|endfunction"
-                  "|endswitch|end_try_catch|end_unwind_protect)[\r]?[\t #%\n(;]");
+  ekey = QRegularExpression
+           {"^[\t ]*(?:end|endif|endfor|endwhile|until|endfunction"
+            "|endswitch|end_try_catch|end_unwind_protect)[\r]?[\t #%\n(;]"};
   if (prevline.contains (ekey))
     {
       if (indentation (line-1) <= indentation (line))
@@ -663,38 +668,40 @@
 void octave_qscintilla::smart_indent_line_or_selected_text (int lineFrom,
                                                             int lineTo)
 {
-  QRegExp blank_line_regexp = QRegExp ("^[\t ]*$");
+  QRegularExpression blank_line_regexp {"^[\t ]*$"};
 
   // end[xxxxx] [# comment] at end of a line
-  QRegExp end_word_regexp
-    = QRegExp ("(?:(?:['\"][^'\"]*['\"])?[^%#]*)*"
-               "(?:end\\w*)[\r\n\t ;]*(?:[%#].*)?$");
+  QRegularExpression
+  end_word_regexp {"(?:(?:['\"][^'\"]*['\"])?[^%#]*)*"
+                   "(?:end\\w*)[\r\n\t ;]*(?:[%#].*)?$"};
 
-  QRegExp begin_block_regexp
-    = QRegExp ("^[\t ]*(?:if|elseif|else"
-               "|for|while|do|parfor"
-               "|switch|case|otherwise"
-               "|function"
-               "|classdef|properties|events|enumeration|methods"
-               "|unwind_protect|unwind_protect_cleanup|try|catch)"
-               "[\r\n\t #%]");
+  QRegularExpression
+  begin_block_regexp {"^[\t ]*(?:if|elseif|else"
+                      "|for|while|do|parfor"
+                      "|switch|case|otherwise"
+                      "|function"
+                      "|classdef|properties|events|enumeration|methods"
+                      "|unwind_protect|unwind_protect_cleanup|try|catch)"
+                      "[\r\n\t #%]"};
 
-  QRegExp mid_block_regexp
-    = QRegExp ("^[\t ]*(?:elseif|else"
-               "|unwind_protect_cleanup|catch)"
-               "[\r\n\t #%]");
+  QRegularExpression
+  mid_block_regexp {"^[\t ]*(?:elseif|else"
+                    "|unwind_protect_cleanup|catch)"
+                    "[\r\n\t #%]"};
 
-  QRegExp end_block_regexp
-    = QRegExp ("^[\t ]*(?:end"
-               "|end(for|function|if|parfor|switch|while"
-               "|classdef|enumeration|events|methods|properties)"
-               "|end_(try_catch|unwind_protect)"
-               "|until)"
-               "[\r\n\t #%]");
+  QRegularExpression
+  end_block_regexp {"^[\t ]*(?:end"
+                    "|end(for|function|if|parfor|switch|while"
+                    "|classdef|enumeration|events|methods|properties)"
+                    "|end_(try_catch|unwind_protect)"
+                    "|until)"
+                    "[\r\n\t #%]"};
 
-  QRegExp case_block_regexp
-    = QRegExp ("^[\t ]*(?:case|otherwise)"
-               "[\r\n\t #%]");
+  QRegularExpression
+  case_block_regexp {"^[\t ]*(?:case|otherwise)"
+                     "[\r\n\t #%]"};
+
+  QRegularExpressionMatch match;
 
   int indent_column = -1;
   int indent_increment = indentationWidth ();
@@ -704,7 +711,8 @@
     {
       QString line_text = text (line);
 
-      if (blank_line_regexp.indexIn (line_text) < 0)
+      match = blank_line_regexp.match (line_text);
+      if (! match.hasMatch ())
         {
           // Found first non-blank line above beginning of region or
           // current line.  Base indentation from this line, increasing
@@ -713,7 +721,8 @@
 
           indent_column = indentation (line);
 
-          if (begin_block_regexp.indexIn (line_text) > -1)
+          match = begin_block_regexp.match (line_text);
+          if (match.hasMatch ())
             {
               indent_column += indent_increment;
               if (line_text.contains ("switch"))
@@ -732,7 +741,8 @@
     {
       QString line_text = text (line);
 
-      if (end_block_regexp.indexIn (line_text) > -1)
+      match = end_block_regexp.match (line_text);
+      if (match.hasMatch ())
         {
           indent_column -= indent_increment;
           if (line_text.contains ("endswitch"))
@@ -744,31 +754,35 @@
             }
         }
 
-      if (mid_block_regexp.indexIn (line_text) > -1)
+      match = mid_block_regexp.match (line_text);
+      if (match.hasMatch ())
         indent_column -= indent_increment;
 
-      if (case_block_regexp.indexIn (line_text) > -1)
+      match = case_block_regexp.match (line_text);
+      if (match.hasMatch ())
         {
-          if (case_block_regexp.indexIn (prev_line) < 0
-              && !prev_line.contains("switch"))
+          match = case_block_regexp.match (prev_line);
+          if (! match.hasMatch ()
+              && ! prev_line.contains ("switch"))
             indent_column -= indent_increment;
           in_switch = true;
         }
 
       setIndentation (line, indent_column);
 
-      int bpos = begin_block_regexp.indexIn (line_text);
-      if (bpos > -1)
+      match = begin_block_regexp.match (line_text);
+      if (match.hasMatch ())
         {
           // Check for existing end statement in the same line
-          int epos = end_word_regexp.indexIn (line_text, bpos);
-          if (epos == -1)
+          match = end_word_regexp.match (line_text, match.capturedStart ());
+          if (! match.hasMatch ())
             indent_column += indent_increment;
           if (line_text.contains ("switch"))
             in_switch = true;
         }
 
-      if (blank_line_regexp.indexIn (line_text) < 0)
+      match = blank_line_regexp.match (line_text);
+      if (! match.hasMatch ())
         prev_line = line_text;
     }
 }
@@ -841,7 +855,7 @@
   QString hist = QString ();
 
   // Split contents into single lines and complete commands
-  QStringList lines = selectedText ().split (QRegExp ("[\r\n]"),
+  QStringList lines = selectedText ().split (QRegularExpression {"[\r\n]"},
 #if defined (HAVE_QT_SPLITBEHAVIOR_ENUM)
                                              Qt::SkipEmptyParts);
 #else
@@ -916,7 +930,7 @@
   // Disable opening a file at a breakpoint in case keyboard () is used
   gui_settings settings;
 
-bool show_dbg_file = settings.bool_value (ed_show_dbg_file);
+  bool show_dbg_file = settings.bool_value (ed_show_dbg_file);
   settings.setValue (ed_show_dbg_file.settings_key (), false);
 
   // The interpreter_event callback function below emits a signal.
@@ -974,7 +988,7 @@
              max_stack_size = 2;
            if (stack.size () <= max_stack_size)
              {
-               QRegExp rx ("source: error sourcing file [^\n]*$");
+               QRegularExpression rx {"source: error sourcing file [^\n]*$"};
                if (new_msg.contains (rx))
                  {
                    // Selected code has syntax errors
@@ -995,12 +1009,11 @@
 
                    for (int i = 0; i < rx_list.length (); i++)
                      {
-                       int pos = 0;
-                       rx = QRegExp (rx_list.at (i));
-                       pos = rx.indexIn (new_msg, pos);
-                       if (pos != -1)
+                       rx = QRegularExpression {rx_list.at (i)};
+                       QRegularExpressionMatch match = rx.match(new_msg);
+                       if (match.hasMatch ())
                          {
-                           err_line = rx.cap (1).toInt ();
+                           err_line = match.captured (1).toInt ();
                            new_msg = new_msg.replace (rx, replace_list.at (i));
                          }
                      }
@@ -1308,9 +1321,9 @@
         return;
       if (next_start == start)      // same => check if already is "end"
         {
-          QRegExp rx_start = QRegExp (R"((\w+))");
-          int tmp = rx_start.indexIn (next_line, start);
-          if (tmp != -1 && is_end (rx_start.cap(1), first_word))
+          QRegularExpression rx_start {R"((\w+))"};
+          QRegularExpressionMatch match = rx_start.match (next_line, start);
+          if (match.hasMatch () && is_end (match.captured (1), first_word))
             return;
         }
     }