Results 1 to 20 of 29

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

Hybrid View

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

    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.

  2. #2
    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 17:01.

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

    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.

  4. #4
    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...

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

    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.

  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) ?

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

    Thanks anyway...

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
  •  
Qt is a trademark of The Qt Company.