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. Comparing QStringView with std::string and char*
Forum Updated to NodeBB v4.3 + New Features

Comparing QStringView with std::string and char*

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.3k Views 1 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.
  • A Offline
    A Offline
    AlQuemist
    wrote on 25 Feb 2022, 21:16 last edited by AlQuemist
    #1

    QStringView is introduced as a light-weight reference to QString, similar to the relation of std::string_view to std::string in C++.
    Yet it apparently lacks some important functionality: Comparison to char* or std::string with the == operator.
    One has always to convert either the lhs or rhs of the comparison to QString which entails a full copy and hence, violates the raison d’être of QStringView.
    See also https://stackoverflow.com/q/18228720.

    Is there a better way for such comparisons (in Qt5 or Qt6)?

    • Minimal example
    #include <string>
    #include <iostream>
    
    #include <QString>
    #include <QStringView>
    
    int main()
    {
        constexpr const char* const str0 {"my string"};
        QStringView qstrv0(QString::fromStdString(str0));
        if(qstrv0 == QString::fromStdString(str0))
            std::cout << "1) equal" << std::endl;
        if(qstrv0.toString() == str0)
            std::cout << "2) equal" << std::endl;
    
        std::string str1 = "my string";
        QStringView qstrv1(QString::fromStdString(str1));
        if(qstrv1 == QString::fromStdString(str1))
            std::cout << "3) equal" << std::endl;
    
        if(qstrv1.toString() == str1)  // does not compile
            std::cout << "4) equal" << std::endl;
    
        if(qstrv0 == str0)  // does not compile
            std::cout << "5) equal" << std::endl;
    
        if(qstrv0 == str1)  // does not compile
            std::cout << "6) equal" << std::endl;
    
        return 0;
    }
    
    • Compile & link:
    $ qmake -project
    $ qmake
    $ make
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 26 Feb 2022, 08:53 last edited by
      #2

      @AlQuemist said in Comparomg QStringView with std::string and char*:

      Comparison to char* or std::string with the == operator.

      How should this work - char* and std::string are just a bunch of bytes without any encoding. QString on the other side is well encoded utf-16.

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

      R 1 Reply Last reply 6 Dec 2024, 09:30
      4
      • C Christian Ehrlicher
        26 Feb 2022, 08:53

        @AlQuemist said in Comparomg QStringView with std::string and char*:

        Comparison to char* or std::string with the == operator.

        How should this work - char* and std::string are just a bunch of bytes without any encoding. QString on the other side is well encoded utf-16.

        R Offline
        R Offline
        Roy Qu
        wrote on 6 Dec 2024, 09:30 last edited by
        #3

        @Christian-Ehrlicher Sometimes you need to compare a QStringView with string literals. In the following example, when parsing a xml file, I need to differentiate tags by their name. Since QXmlStreamReader.name() returns a QStringView, compares it to a const char * literal is very natural. Wrap all comparable literals using QLatin1String is very inconvinient.

            QXmlStreamReader xml;
            xml.setDevice(&file);
            while(!xml.atEnd()) {
                xml.readNext();
                switch (xml.tokenType()) {
                case QXmlStreamReader::TokenType::StartElement:
                    if (xml.name() == "item") {
                        ...
                    } 
                    ...
                }
            }
        
        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 6 Dec 2024, 10:11 last edited by
          #4

          And what the big difference to

          if (xml.name() == "item"_L1) {
          

          Are we really discussing here about three characters?

          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
          3

          • Login

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