Results 1 to 4 of 4

Thread: Problems with QString::startsWith("--[[")

  1. #1
    Join Date
    Oct 2012
    Posts
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Problems with QString::startsWith("--[[")

    Hello everybody.

    I'm facing a very strange behavior when using the the following method in Qt (Windows 7 64bit, Qt v.4.8.1, VisualStudio 2010 compiler):

    bool QString::startsWith ( const QString & s, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const.
    I'm reading a text file (lua script file) and looking for lines where a comment block exist. Comments blocks are defined by "--[[" at the beginning of the line. Individual lines are then stored in a QStringList .
    When executing this code I was hoping that the first comparison would return true, instead it returns a false.

    Qt Code:
    1. {...}
    2. for( int i=0; i<ioSourceCodeLines.size(); i++)
    3. {
    4. const QString currentLine = ioSourceCodeLines.at(0);
    5. if(currentLine.startsWith("--[["))
    6. insideCommentBlock = true; // dont't stop here.
    7. else if(currentLine.startsWith("--"))
    8. insideComment = true; //stops here.
    9. ...
    10. }
    11. {...}
    To copy to clipboard, switch view to plain text mode 

    I've inspected the memory values and I've noticed that for some reason the content that is saved in memory for currentLine is slighty different:
    2d 00 2d 00 5b 00 20 00 5b 00 20 ... and so on. This translates to the following string. "--[ [ " notice how spaces were added.

    If the comparison is made this way:

    Qt Code:
    1. {...}
    2. for( int i=0; i<ioSourceCodeLines.size(); i++)
    3. {
    4. const QString currentLine = ioSourceCodeLines.at(0);
    5. if(currentLine.startsWith("--[ [ "))
    6. insideCommentBlock = true; // stop here.
    7. else if(currentLine.startsWith("--"))
    8. insideComment = true; //stops here.
    9. ...
    10. }
    11. {...}
    To copy to clipboard, switch view to plain text mode 

    the result of the first comparison is now true. But that is not what its contained in the file. Does someone has an idea of what is going on, or is it a bug?
    I'm just looking for an explanation of this behavior or what is my mistake?
    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QString::startsWith("--[[")

    How do you populate ioSourceCodeLines? Are the encodings right when reading the file?
    Does
    Qt Code:
    1. #include <QtCore>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. if (!f.open())
    6. qDebug() << "Could not open file";
    7. qDebug() << "File name" << f.fileName();
    8.  
    9. QFile file(f.fileName());
    10. file.open(QIODevice::WriteOnly | QIODevice::Text);
    11. QTextStream out(&file);
    12. out << "--[[ foo bar";
    13. file.close();
    14.  
    15. file.open(QIODevice::ReadOnly | QIODevice::Text);
    16. QString str = file.readLine();
    17. qDebug() << str << (str == "--[[ foo bar");
    18.  
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 
    work for you?
    Last edited by Lykurg; 22nd January 2013 at 13:57. Reason: spelling corrections

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QString::startsWith("--[[")

    BTW Is the line number 4 should look like this :
    Qt Code:
    1. const QString currentLine = ioSourceCodeLines.at(i)
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2012
    Posts
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Problems with QString::startsWith("--[[")

    Hello thanks for the answer. It seems now everything is beeing done correctly.
    I've added the "QIODevice::Text" when opening the file.
    Qt Code:
    1. ...
    2. QFile sourceCodeFile(iFilePath);
    3. if(!sourceCodeFile.open(QIODevice::ReadOnly | QIODevice::Text))
    4. return false;
    5. ...
    To copy to clipboard, switch view to plain text mode 
    and now there are no extra spaces and also the comparisons are behaving the way they should.
    Thanks

Similar Threads

  1. Replies: 1
    Last Post: 14th May 2011, 08:02
  2. Replies: 28
    Last Post: 22nd February 2010, 10:27
  3. Need definedInHeader("QString") == "q<somewhere>.h"
    By muenalan in forum Qt Programming
    Replies: 6
    Last Post: 29th September 2009, 11:04
  4. Error "QString::arg: Argument missing"
    By Lawand in forum Qt Programming
    Replies: 3
    Last Post: 18th February 2009, 20:26
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.