Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Infinite loop with regex (non reproducible in command line application)



  • Let me know if you can reproduce this on your qtcreator. I have a button connected to a QlineEdit. A new window appears and is supposed to have a chart appear. But instead it gets stuck in the regex loop. The loop works in a command line app. I think the pointer std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString()); causes an error potentially. In my command line attempt passing around pointers causes a segmentation fault.

    readInMBTI is the QlineEdit name btw;

    Why is it infinitely looping???? it works elsewhere :(

    
    QWidget *wdg = new QWidget;
    wdg->show();
    
    std::ifstream file("file.txt");
       std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString());
       std::string line;
       std::string returnLine;
    
       if(MBTIstring.length() < 17) {
    
           while(getline(file, line)) {
            if(line.find(MBTIstring, 0) != std::string::npos && line.find(MBTIstring, 0) < 2) {
              returnLine = line;
              break;
            }
          }
       }
    
      std::regex r(" *(\\w+ \\w+)");
      std::smatch m;
      auto flag = std::regex_constants::match_continuous;
      while(std::regex_search(returnLine, m, r, flag)) {
           MBTIbroken.push_back(m[1]);
           MBTIstring = m.suffix().str();
       }
    


  • @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

    Why is it infinitely looping???? it works elsewhere :(

      while(std::regex_search(returnLine, m, r, flag)) {
           MBTIbroken.push_back(m[1]);
           MBTIstring = m.suffix().str();
       }
    

    Could you explain how that loop exits, assuming it enters (i.e. there is a match)? Thanks. It's pasted from your first post. And it's what I answered "No, it does not work" to.

    You seem to have changed the last line to

    returnLine = m.suffix().str();
    

    later on. Why? Is one your non-working Qt code and the other your working command line one?

    it works without qt involved man.

    In the code you show the only bit that is "Qt" is

    QWidget *wdg = new QWidget;
    wdg->show();
    

    Other than that there isn't any Qt code.


  • Lifetime Qt Champion

    @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

    std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString());

    wtf?

    while(std::regex_search(returnLine, m, r, flag))

    where do you modify returnLine in your loop?



  • @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

    The loop works in a command line app.

    it works elsewhere :(

    No, it does not.



  • No, it does not.

    it works without qt involved man. You want proof?



  • m.suffix().str(); should be changing it. It works in command line app as such.

    #include <iostream>
    #include <string>
    #include <regex>
    #include <vector>
    #include <fstream>
    
    int main() {
    std::string returnLine;
    std::vector<std::string> MBTIbroken;
    std::string MBTIstring;
    std::regex r(" *(\\w+ \\w+)"); // entire match will be 2 numbers
    std::smatch m;
    std::string line;
    std::cout << "input MBTI type" << std::endl;
    getline(std::cin, MBTIstring);
    
      std::ifstream file("jcalg hardcode.txt");
    
        if(MBTIstring.length() < 17) {
    
            while(getline(file, line)) {
             if(line.find(MBTIstring, 0) != std::string::npos && line.find(MBTIstring, 0) < 2) {
               returnLine = line;
               break;
             }
           }
        }
    
      auto flag = std::regex_constants::match_continuous;
      while(std::regex_search(returnLine, m, r, flag)) {
        MBTIbroken.push_back(m[1]);
        returnLine = m.suffix().str();
      }
    
    
    
      int i = 0;
      std::cout << "Vector Contents:" << std::endl;
      for(auto s : MBTIbroken) {
        std::cout << (++i) << ") " << s << std::endl;
      }
    }
    

    @Christian-Ehrlicher it gets the string input into the QLineEdit which is what i need? wym "wtf"



  • @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

    Why is it infinitely looping???? it works elsewhere :(

      while(std::regex_search(returnLine, m, r, flag)) {
           MBTIbroken.push_back(m[1]);
           MBTIstring = m.suffix().str();
       }
    

    Could you explain how that loop exits, assuming it enters (i.e. there is a match)? Thanks. It's pasted from your first post. And it's what I answered "No, it does not work" to.

    You seem to have changed the last line to

    returnLine = m.suffix().str();
    

    later on. Why? Is one your non-working Qt code and the other your working command line one?

    it works without qt involved man.

    In the code you show the only bit that is "Qt" is

    QWidget *wdg = new QWidget;
    wdg->show();
    

    Other than that there isn't any Qt code.



  • @JonB oh shit. you right. and that worked. programs crashing now, but i'll spend some time on that first. it's not the same issue.



  • @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

    No, it does not.

    it works without qt involved man. You want proof?

    A more productive initial reaction than this would be "I wonder if these guys actually know what they're talking about, I'll go triple-check my code in case..." :) <- Smiley


Log in to reply