Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29

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

  1. #21
    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) ?

    So, I think we are at the point to agree, there is no such thing like:

    Qt Code:
    1. QString("%(one) %(two) %(three) %(four) ...").arg(qhash_dictionary);
    To copy to clipboard, switch view to plain text mode 

    Creating only a single new heap object for the new string content.

    1:0 for python

  2. #22
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    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) ?

    So, I think we are at the point to agree, there is no such thing like:
    I didn't know that it was the debate ;-)

    The question is, what is python doing behind the scenes.

    Creating only a single new heap object for the new string content.
    Well, not quite, since the dictionary has to have strings in it...
    In the solution I offered you, you create a QHash, and a QRegExp, and that's it, so its not that much of difference in terms of memory usage.
    From here on it s mostly splitting hairs.
    ==========================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.

  3. #23
    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'm also not familiar with the python internals, the "1:0" is for the API.

    Unfortunately your solution using regex does not fit the requirements: replacing bunch of different named-place-holders in a single string, avoiding temporary objects. (The place-holder strings (the keys) should not be counted, they exist in each case.)

    Thanks anyway.
    Last edited by lexar; 19th February 2010 at 12:50.

  4. #24
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    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) ?

    Unfortunately your solution using regex does not fit the requirements:
    Why not?
    Where do temporary objects are being created?
    ==========================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.

  5. #25
    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 temporary objects are being created?
    each replace() call calls replace_helper(), which calls resize() with allocates a NEW, bigger heap block, for the growing string content.
    Of course if you are lucky, and growing successes, you are done. It depends on memory fragmentation and system activity... See "void QString::resize(int size)".
    Last edited by lexar; 19th February 2010 at 16:01.

  6. #26
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    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) ?

    each replace() call calls replace_helper(), which calls resize() with allocates a NEW, bigger heap block, for the growing string content.
    That is simply not true.
    That is only true (the resizing), if the resulting string is larger then the original string.
    This can happen if your place holders are smaller then the string that replaces them. (line 18,41)
    In such a case, more memory will be allocated for the resulting sting in any case.
    Also note, that afterBuffer gets deleted at the end of the function, which means for each call not more then alen bytes will be allocated, and released (lines 61,65), and that too, only happens in the cases specified in the comment.

    Here is the code:
    Qt Code:
    1. void QString::replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen)
    2. {
    3. // copy *after in case it lies inside our own d->data area
    4. // (which we could possibly invalidate via a realloc or corrupt via memcpy operations.)
    5. QChar *afterBuffer = const_cast<QChar *>(after);
    6. if (after >= reinterpret_cast<QChar *>(d->data) && after < reinterpret_cast<QChar *>(d->data) + d->size) {
    7. afterBuffer = static_cast<QChar *>(qMalloc(alen*sizeof(QChar)));
    8. Q_CHECK_PTR(afterBuffer);
    9. ::memcpy(afterBuffer, after, alen*sizeof(QChar));
    10. }
    11.  
    12. QT_TRY {
    13. detach();
    14. if (blen == alen) {
    15. // replace in place
    16. for (int i = 0; i < nIndices; ++i)
    17. memcpy(d->data + indices[i], afterBuffer, alen * sizeof(QChar));
    18. } else if (alen < blen) {
    19. // replace from front
    20. uint to = indices[0];
    21. if (alen)
    22. memcpy(d->data+to, after, alen*sizeof(QChar));
    23. to += alen;
    24. uint movestart = indices[0] + blen;
    25. for (int i = 1; i < nIndices; ++i) {
    26. int msize = indices[i] - movestart;
    27. if (msize > 0) {
    28. memmove(d->data + to, d->data + movestart, msize * sizeof(QChar));
    29. to += msize;
    30. }
    31. if (alen) {
    32. memcpy(d->data + to, afterBuffer, alen*sizeof(QChar));
    33. to += alen;
    34. }
    35. movestart = indices[i] + blen;
    36. }
    37. int msize = d->size - movestart;
    38. if (msize > 0)
    39. memmove(d->data + to, d->data + movestart, msize * sizeof(QChar));
    40. resize(d->size - nIndices*(blen-alen));
    41. } else {
    42. // replace from back
    43. int adjust = nIndices*(alen-blen);
    44. int newLen = d->size + adjust;
    45. int moveend = d->size;
    46. resize(newLen);
    47.  
    48. while (nIndices) {
    49. --nIndices;
    50. int movestart = indices[nIndices] + blen;
    51. int insertstart = indices[nIndices] + nIndices*(alen-blen);
    52. int moveto = insertstart + alen;
    53. memmove(d->data + moveto, d->data + movestart,
    54. (moveend - movestart)*sizeof(QChar));
    55. memcpy(d->data + insertstart, afterBuffer, alen*sizeof(QChar));
    56. moveend = movestart-blen;
    57. }
    58. }
    59. } QT_CATCH(const std::bad_alloc &) {
    60. if (afterBuffer != after)
    61. qFree(afterBuffer);
    62. QT_RETHROW;
    63. }
    64. if (afterBuffer != after)
    65. qFree(afterBuffer);
    66. }
    To copy to clipboard, switch view to plain text mode 
    ==========================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.

  7. #27
    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
    That is simply not true.
    That is only true (the resizing), if the resulting string is larger then the original string.
    And in my world that is the most probably case: when the replacement is bigger then the place-marker (simplified):

    1: QString("%(placeholder_for_streetname) %(placeholder_for_cityname)").arg({...:"sun street", ...:"suncity"})
    2: QString("%(street) %(city)").arg({...:"simpsons road", ...:"springfeeld"})

    // note: arg() is a fictive construct receiving a dictionary (just to demonstrate the dict content)

    Thus this is _the_ true, at least for me...

  8. #28
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    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) ?

    Thus this is _the_ true, at least for me...
    Please note exactly what I wrote in response to your:
    regex does not fit the requirements:...avoiding temporary objects.
    I showed you that no temp objects are being created.

    What you wrote:
    when the replacement is bigger then the place-marker (simplified):
    Is about the resizing of the string, and this happens in any case/language/api, and has nothing to do with temp objects, in the case you are using too.
    ==========================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.

  9. #29
    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) ?

    Forget it. Seems our understanding of efficiency is just too different.

    Thanks anyway...

Similar Threads

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