Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString::section() NO bug
Qt 6.11 is out! See what's new in the release blog

QString::section() NO bug

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 728 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gourmet
    wrote on last edited by Gourmet
    #1

    Here I get QString looking like this:

    title = "6 product feature key1:365 days key2:value"
    

    To parse it to elements I use cycle:

    for( int i = 0; i < MAXELEMS; i++ )
    {
        QString tmp = title.section(QRegExp("\\s+"), i, i);
        qWarning()<<i<<tmp;
    //..... further processing
    }
    

    Then suddenly see this:

    0 6
    1 product
    2 feature
    3 key1:35 //...ooops!!! not key1:365
    4 days
    5 key2:value
    

    Ok, let's change to

    title = "6 product feature key1:375 days key2:value"
    

    Result:

    0 6
    1 product
    2 feature
    3 key1:375 //...right
    4 days
    5 key2:value
    

    Then

    title = "7 product feature key1:375 days key2:value"
    

    Oops:

    0 7
    1 product
    2 feature
    3 key1:35 //...right
    4 days
    5 key2:value
    

    And finally:

        QString tmp = title.section(QRegExp("\\s+"), i, i);
        qWarning()<<title;
    

    Prints of:

    product feature key1:35 days key2:value // 8-()
    

    That means QString::section cuts off all occurrences equal to required section from source string! This is a bug!
    1st - There is nothing about source string change in QString::section() description.
    2nd - Why it cuts all occurrences if I need only first one?

    I found this in my Android app build with Qt 5.12. Anybody can try check in other OSes.

    J.HilkJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Gourmet said in QString::section() bug:

      There is nothing about source string change in QString::section() description.

      QString::section() is const - it can't change the source.
      Why not simply use QString::split(QLatin1Char(' '))? Or even QString::splitRef()?

      btw: QRegExp is deprecated.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      G 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @Gourmet said in QString::section() bug:

        There is nothing about source string change in QString::section() description.

        QString::section() is const - it can't change the source.
        Why not simply use QString::split(QLatin1Char(' '))? Or even QString::splitRef()?

        btw: QRegExp is deprecated.

        G Offline
        G Offline
        Gourmet
        wrote on last edited by Gourmet
        #3

        @Christian-Ehrlicher said in QString::section bug:

        QString::section() is const - it can't change the source.

        But it changes.

        @Christian-Ehrlicher said in QString::section bug:

        Why not simply use QString::split

        The processing is more complex than just split. QString::section() is more useful in my case.

        @Christian-Ehrlicher said in QString::section bug:

        QRegExp is deprecated

        There is nothing about this in Qt docs. As I see only some functions are obsolete but not entire class.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Gourmet said in QString::section() bug:

          But it changes.

          Please show this to us by providing a small example - you just show snippets.

          There is nothing about this in Qt docs.

          Hmm: https://doc.qt.io/qt-5/qregexp.html#details

          "Note: In Qt 5, the new QRegularExpression class provides a Perl compatible implementation of regular expressions and is recommended in place of QRegExp."

          @SGaist: why is \obsolete missing here?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          G J.HilkJ SGaistS 3 Replies Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            @Gourmet said in QString::section() bug:

            But it changes.

            Please show this to us by providing a small example - you just show snippets.

            There is nothing about this in Qt docs.

            Hmm: https://doc.qt.io/qt-5/qregexp.html#details

            "Note: In Qt 5, the new QRegularExpression class provides a Perl compatible implementation of regular expressions and is recommended in place of QRegExp."

            @SGaist: why is \obsolete missing here?

            G Offline
            G Offline
            Gourmet
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in QString::section bug:

            Please show this to us by providing a small example - you just show snippets.

            Sorry, I have no time for experiments. I must fix and release product. May be some time later when I will be free.

            Christian EhrlicherC 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @Gourmet said in QString::section() bug:

              But it changes.

              Please show this to us by providing a small example - you just show snippets.

              There is nothing about this in Qt docs.

              Hmm: https://doc.qt.io/qt-5/qregexp.html#details

              "Note: In Qt 5, the new QRegularExpression class provides a Perl compatible implementation of regular expressions and is recommended in place of QRegExp."

              @SGaist: why is \obsolete missing here?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @Christian-Ehrlicher said in QString::section() bug:

              why is \obsolete missing here?

              I swear last month it was there, because I explicitly looked up which one is obsolete!


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • G Gourmet

                Here I get QString looking like this:

                title = "6 product feature key1:365 days key2:value"
                

                To parse it to elements I use cycle:

                for( int i = 0; i < MAXELEMS; i++ )
                {
                    QString tmp = title.section(QRegExp("\\s+"), i, i);
                    qWarning()<<i<<tmp;
                //..... further processing
                }
                

                Then suddenly see this:

                0 6
                1 product
                2 feature
                3 key1:35 //...ooops!!! not key1:365
                4 days
                5 key2:value
                

                Ok, let's change to

                title = "6 product feature key1:375 days key2:value"
                

                Result:

                0 6
                1 product
                2 feature
                3 key1:375 //...right
                4 days
                5 key2:value
                

                Then

                title = "7 product feature key1:375 days key2:value"
                

                Oops:

                0 7
                1 product
                2 feature
                3 key1:35 //...right
                4 days
                5 key2:value
                

                And finally:

                    QString tmp = title.section(QRegExp("\\s+"), i, i);
                    qWarning()<<title;
                

                Prints of:

                product feature key1:35 days key2:value // 8-()
                

                That means QString::section cuts off all occurrences equal to required section from source string! This is a bug!
                1st - There is nothing about source string change in QString::section() description.
                2nd - Why it cuts all occurrences if I need only first one?

                I found this in my Android app build with Qt 5.12. Anybody can try check in other OSes.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @Gourmet said in QString::section() bug:

                Why it cuts all occurrences if I need only first one?

                You have to tell it that
                https://doc.qt.io/qt-5/qregexp.html#setMinimal


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                2
                • G Gourmet

                  @Christian-Ehrlicher said in QString::section bug:

                  Please show this to us by providing a small example - you just show snippets.

                  Sorry, I have no time for experiments. I must fix and release product. May be some time later when I will be free.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Gourmet said in QString::section() bug:

                  Sorry, I have no time for experiments. I must fix and release product.

                  So we can't help you - QString::split() can not change the object - no matter what you tell us.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2
                  • Christian EhrlicherC Christian Ehrlicher

                    @Gourmet said in QString::section() bug:

                    But it changes.

                    Please show this to us by providing a small example - you just show snippets.

                    There is nothing about this in Qt docs.

                    Hmm: https://doc.qt.io/qt-5/qregexp.html#details

                    "Note: In Qt 5, the new QRegularExpression class provides a Perl compatible implementation of regular expressions and is recommended in place of QRegExp."

                    @SGaist: why is \obsolete missing here?

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher said in QString::section() bug:

                    @SGaist: why is \obsolete missing here?

                    Because not all APIs making use of it had replacements. However it's getting nuked in Qt 6.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      Gourmet
                      wrote on last edited by
                      #10

                      Finally I have found error in my code. It was hard because of weird processing. QString::split() works fine.

                      But what about QRegExp - I do not remember why I used it instead of QRegularExpression. May be it looked simpler. This code was redesigned and regular expression was much more complex at beginning.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved