Page 1 of 2 12 LastLast
Results 1 to 20 of 29

Thread: QString :: is there some kind of "named place markers" (like in python string) ?

  1. #1
    Join Date
    Jan 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QString :: is there some kind of "named place markers" (like in python string) ?

    Hi Folks,

    I would like to load preformated strings from some persistence and apply current parameters to them.
    But the simple numbered parameter markers can be mixed easily while creating string patterns. Thus i would like to address these parameter markers by name. So:

    1. Is there a way to name the parameter markers like python allows (instead of %1, %2, ...) ?
    2. What is the best way (workaround) to do this ?


    Details:
    So I want something like the python string functionality:
    Qt Code:
    1. >>> "Hello %(first_name)s %(last_name)s!" % {"first_name":"Max", "last_name":"Power"}
    2. 'Hello Max Power!'
    To copy to clipboard, switch view to plain text mode 

  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: QString :: is there some kind of "named place markers" (like in python string) ?


  3. #3
    Join Date
    Jan 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    please read the question before you push the "reply" button...

  4. #4
    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: QString :: is there some kind of "named place markers" (like in python string) ?

    Quote Originally Posted by lexar View Post
    please read the question before you push the "reply" button...
    Can you guess that because of the tonns of dumb newbie questions one only fly over such questions and answer it. Normaly one is 99% right with the answer. In your case I am not. Go and kill me. And sorry for trying to help you.
    And as to your question:
    Qt Code:
    1. QString str = "Go and read %censored manual aka documentation and try to use your %whatsoever";
    2. str.replace("%censored","fucking").replace("%whatsoever","brain");
    3. qWarning() << str;
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    please read the question before you push the "reply" button...
    Where do you people get the nerve to be so obnoctious, and that towards someone who is trying to help you??
    And THAT is what you have to write with your 4th!! post on this forum?
    Have a look at how many posts Lykurg hast on this forum and to how many people he helped already!
    Offending those who are trying to help you will not do you much good!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jan 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    It should be hard to help other people under such pressure, keep cool!

    Of course i have noticed the member "replace(..)", the problem here is that on replacing a whole bunch of parameters we get pretty much temporary strings, what is pretty inefficient. (Sorry, I didn't mention this point in the initial post -- my fault, it's your turn to "Go and kill me" ).

    I was looking for a more elegant solution like python does: a single call with a dictionary...

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    what is pretty inefficient.
    In what way it is inefficient?
    That your line is longer to type?

    I also didn't understand the problem exactly... an example would help.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Jan 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    Quote Originally Posted by high_flyer View Post
    Where do you people get the nerve to be so obnoctious, and that towards someone who is trying to help you??
    And THAT is what you have to write with your 4th!! post on this forum?
    Have a look at how many posts Lykurg hast on this forum and to how many people he helped already!
    Offending those who are trying to help you will not do you much good!
    I'm sorry, this sounds some kind of aggressive to you, was not my intention!

    I was trying to give enough information (repeating stuff, and making examples) so it should be clear, that i'm familiar with the "%N" notation and the basic stuff. But seems that I have to practice a bit more...

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    I was trying to give enough information (repeating stuff, and making examples) so it should be clear,
    no, I respoded to THIS post:
    please read the question before you push the "reply" button...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #10
    Join Date
    Jan 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    Quote Originally Posted by high_flyer View Post
    In what way it is inefficient?
    That your line is longer to type?
    Assume we have a text with, let say 30 placeholders. So calling 30 times the replace() function on a each time newly created temporary string looks pretty inefficient to me.

    Seems that I have to write the required functionality by my self. Receiving a string-pattern and a hash-map, exchanging each next found placeholder by a value from the map in a single buffer.

    But if you have a better Idea, please let me know...

  11. #11
    Join Date
    Jan 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    Quote Originally Posted by high_flyer View Post
    no, I respoded to THIS post:
    I know, I tried to explain why I replied it this way.

  12. #12
    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: QString :: is there some kind of "named place markers" (like in python string) ?

    Quote Originally Posted by lexar View Post
    Of course i have noticed the member "replace(..)", the problem here is that on replacing a whole bunch of parameters we get pretty much temporary strings, what is pretty inefficient.
    Not sure what you exactly mean by "temporary strings", but replace gives you an const reference back, so it is working on its own data directly without creating a working copy of it content:
    Qt Code:
    1. QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs)
    2. {
    3. return replace(before.constData(), before.size(), after.constData(), after.size(), cs);
    4. }
    5.  
    6. QString &QString::replace(const QChar *before, int blen,
    7. const QChar *after, int alen,
    8. Qt::CaseSensitivity cs)
    9. {
    10. if (d->size == 0) {
    11. if (blen)
    12. return *this;
    13. } else {
    14. if (cs == Qt::CaseSensitive && before == after && blen == alen)
    15. return *this;
    16. }
    17. if (alen == 0 && blen == 0)
    18. return *this;
    19.  
    20. QStringMatcher matcher(before, blen, cs);
    21.  
    22. int index = 0;
    23. while (1) {
    24. uint indices[1024];
    25. uint pos = 0;
    26. while (pos < 1023) {
    27. index = matcher.indexIn(*this, index);
    28. if (index == -1)
    29. break;
    30. indices[pos++] = index;
    31. index += blen;
    32. // avoid infinite loop
    33. if (!blen)
    34. index++;
    35. }
    36. if (!pos)
    37. break;
    38.  
    39. replace_helper(indices, pos, blen, after, alen);
    40.  
    41. if (index == -1)
    42. break;
    43. // index has to be adjusted in case we get back into the loop above.
    44. index += pos*(alen-blen);
    45. }
    46.  
    47. return *this;
    48. }
    To copy to clipboard, switch view to plain text mode 
    The only thing is that every time a QStringMatcher is created. But that's normally not such a big thing. (if you don't work on an app werer every 0,0001 ms counts.)

    EDIT: ...and what I forgot to mention/paste: replace_helper() also don't create a temporary copy.
    Last edited by Lykurg; 19th February 2010 at 10:58.

  13. #13
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    Assume we have a text with, let say 30 placeholders. So calling 30 times the replace() function on a each time newly created temporary string looks pretty inefficient to me.
    I would tend to say that working like that with a strings is inefficient on its self, and if you have to replace 30 place holders, then the syntax doesn't change much, you still have to resolve 30 place holder, is some form, be it calling a function 30 times, or some other operations that will do the same job - but the job has to be done.
    I would be interested to see your implementation, and see if it really will be more efficient.
    But enough philosophy.

    One way, which may be more efficient (if its not the way Qt is doing it in replace()) is to use QRegExp to look for the place holders and use QMap<QString,QString> as a dictionary.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #14
    Join Date
    Jan 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    Lykurg, thanks not giving me up

    Nice solution, undoubted. But the replace_helper helps Qt to avoid temporary strings only for a single replace call.
    So it helps in this case:
    Qt Code:
    1. QString("one abd one lakgje one lakgjes one").replace("one", "atomic");
    To copy to clipboard, switch view to plain text mode 

    but it does not help for this case:
    Qt Code:
    1. QString("one two three four five, ...").replace("one","1").replace("two","2").replace("three","3").rep...
    To copy to clipboard, switch view to plain text mode 
    Last edited by lexar; 19th February 2010 at 11:41.

  15. #15
    Join Date
    Jan 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    Quote Originally Posted by high_flyer View Post
    I would tend to say that working like that with a strings is inefficient on its self, and if you have to replace 30 place holders, then the syntax doesn't change much, you still have to resolve 30 place holder, is some form, be it calling a function 30 times, or some other operations that will do the same job - but the job has to be done.
    I would be interested to see your implementation, and see if it really will be more efficient.
    But enough philosophy.

    One way, which may be more efficient (if its not the way Qt is doing it in replace()) is to use QRegExp to look for the place holders and use QMap<QString,QString> as a dictionary.
    Yea, unfortunately strings are inefficient, but until humans cannot read bitstreams...

    Seems like the QHash class is the right dictionary object, but also it seems, that I have to write the string parsing and buffer filling by my own, cause I want to avoid expensive heap allocation calls for temporary strings with partial replacements...

    Thanks All!

  16. #16
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    Yes probably QHash is better then QMap.
    However I still think that QRegExp in conjunction with QHash will be the best way to go.

    Here we've passed the QRegExp to QString's replace() function to replace the matched text with new text.

    Qt Code:
    1. QRegExp rx("&(?!amp;)"); // match ampersands but not &amp;
    2. QString line1 = "This & that";
    3. line1.replace(rx, "&amp;");
    4. // line1 == "This &amp; that"
    5. QString line2 = "His &amp; hers & theirs";
    6. line2.replace(rx, "&amp;");
    7. // line2 == "His &amp; hers &amp; theirs"
    To copy to clipboard, switch view to plain text mode 
    So you could do a similar think in a loop that iterates through your QHash.
    Last edited by high_flyer; 19th February 2010 at 11:46.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  17. #17
    Join Date
    Jan 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    Quote Originally Posted by high_flyer View Post
    Yes probably QHash is better then QMap.
    However I still think that QRegExp in conjunction with QHash will be the best way to go.
    Then I probably do not get the point?!?
    As far as I understand QRegExp is able to find a placeholder pattern, but it cannot help to replace them (especially a bunch of them) -- or do I miss something?


    Ah, in your just inserted example code, we still have the mentioned 30 calls to the replace(), resulting in 30 temporary strings. That was the "inefficient" point...

  18. #18
    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: QString :: is there some kind of "named place markers" (like in python string) ?

    Quote Originally Posted by lexar View Post
    Ah, in your just inserted example code, we still have the mentioned 30 calls to the replace(), resulting in 30 temporary strings. That was the "inefficient" point...
    I am still strugelling on understanding why and where Qt produces temporary strings if you use replace multiple on a string. But if you have a loop you only call it once per each "replaceword". So for that there is no temporary string created.
    But that leads to: You have to make it by yourown and Qt does not provide a function for that.
    Qt Code:
    1. QString yourString;
    2. QHash<QString, QString> searchReplace;
    3. //fill it
    4. QHash<QString, QString>::const_iterator i = searchReplace.constBegin();
    5. while (i != hash.constEnd()) {
    6. yourString.replace(i.key(), i.value());
    7. ++i;
    8. }
    To copy to clipboard, switch view to plain text mode 

  19. #19
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    As far as I understand QRegExp is able to find a placeholder pattern, but it cannot help to replace them
    Read the RegExp docs:
    Search and Replace A regexp can replace all occurrences of a substring with a different substring, e.g., replace all occurrences of & with &amp; except where the & is already followed by an amp;.
    Ah, in your just inserted example code, we still have the mentioned 30 calls to the replace(), resulting in 30 temporary strings. That was the "inefficient" point...
    But if you call replace once for each iteration you wrote:
    Nice solution, undoubted. But the replace_helper helps Qt to avoid temporary strings only for a single replace call.
    So by calling it in a loop you are not getting any temps.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  20. #20
    Join Date
    Jan 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString :: is there some kind of "named place markers" (like in python string) ?

    I am still strugelling on understanding why and where Qt produces temporary strings if you use replace multiple on a string.
    Each call to replace() (in replace_helper) creates a new heap buffer for the new string instance (assumed the replacements are bigger, then the markers) see "resize(newLen);" call in replace_helper().
    Okay, the hull of the string remains the same, but we have a heap allocation, and that is the expensive part!


    So by calling it in a loop you are not getting any temps.
    Can U make an example? Still no idea how it should work all together meeting my point: no temporary strings with partial replacements.

Similar Threads

  1. Replies: 3
    Last Post: 15th February 2010, 18:27
  2. Need definedInHeader("QString") == "q<somewhere>.h"
    By muenalan in forum Qt Programming
    Replies: 6
    Last Post: 29th September 2009, 12:04
  3. Replies: 3
    Last Post: 8th July 2008, 20:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 16:58

Tags for this Thread

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.