Ignore:
Timestamp:
Apr 25, 2001, 8:55:35 PM (24 years ago)
Author:
umoeller
Message:

Misc. fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/xstring.c

    r45 r63  
    690690/*
    691691 *@@ xstrrpl:
    692  *      replaces cReplLen characters in pxstr, starting
    693  *      at the position ulFirstReplPos, with the string
    694  *      in pxstrReplaceWith.
     692 *      replaces "cReplLen" characters in pxstr, starting
     693 *      at the position "ulFirstReplPos", with the first
     694 *      "cReplaceWithLen" characters from pcszReplaceWith.
    695695 *
    696696 *      Returns the new length of the string, excluding
     
    727727 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash
    728728 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using xstrReserve
     729 *@@changed V0.9.11 (2001-04-22) [umoeller]: replaced replacement XSTRING with PCSZ
    729730 */
    730731
     
    732733              ULONG ulFirstReplOfs,             // in: ofs of first char to replace
    733734              ULONG cReplLen,                   // in: no. of chars to replace
    734               const XSTRING *pstrReplaceWith)   // in: string to replace chars with
     735              const char *pcszReplaceWith,      // in: string to replace chars with
     736              ULONG cReplaceWithLen)            // in: length of replacement string
     737                                                // (this MUST be specified; if 0, chars are removed only)
    735738{
    736739    ULONG   ulrc = 0;
     
    739742    if (    (pxstr)         // V0.9.9 (2001-02-14) [umoeller]
    740743         && (ulFirstReplOfs + cReplLen <= pxstr->ulLength)
    741          && (pstrReplaceWith)
    742          // && (pstrReplaceWith->ulLength)      no, this can be empty
     744         && (pcszReplaceWith)
    743745       )
    744746    {
    745         ULONG   cReplaceLen = pstrReplaceWith->ulLength;
    746                     // can be 0!
    747 
    748747        // size of new buffer:
    749         ULONG   cbNeeded = pxstr->ulLength
    750                          + cReplaceLen
    751                          - cReplLen
    752                          + 1;                  // null terminator
     748        ULONG   cbNeeded = pxstr->ulLength      // existing
     749                         + cReplaceWithLen      // plus replacement string length
     750                         - cReplLen             // minus replaced characters
     751                         + 1;                   // plus null terminator
    753752        // offset where pszSearch was found
    754753        PSZ     pFound = pxstr->psz + ulFirstReplOfs;
     
    791790                       ulFirstReplOfs);     // up to "found"
    792791
    793             if (cReplaceLen)
     792            if (cReplaceWithLen)
    794793            {
    795794                // we have a replacement:
    796795                // insert it next
     796
     797                /* memcpy(pszNew + ulFirstReplOfs,
     798                       pstrReplaceWith->psz,
     799                       cReplaceWithLen + 1);        // include null terminator
     800                */
     801                // no, we no longer can be sure that pcszReplaceWith is
     802                // null terminated, so terminate explicitly
     803                // V0.9.11 (2001-04-22) [umoeller]
     804
    797805                memcpy(pszNew + ulFirstReplOfs,
    798                        pstrReplaceWith->psz,
    799                        cReplaceLen + 1);        // include null terminator
     806                       pcszReplaceWith,
     807                       cReplaceWithLen);
     808                *(pszNew + ulFirstReplOfs + cReplaceWithLen) = '\0';
    800809            }
    801810
     
    810819            //            ³            ³
    811820            //            pxstr->ulLength = 14
    812             memcpy(pszNew + ulFirstReplOfs + cReplaceLen,
     821            memcpy(pszNew + ulFirstReplOfs + cReplaceWithLen,
    813822                   pFound + cReplLen,
    814823                   // remaining bytes:
     
    833842            // first, we move the end to its new location
    834843            // (memmove handles overlap if needed)
    835             memmove(pFound + cReplaceLen,
     844            memmove(pFound + cReplaceWithLen,
    836845                    pFound + cReplLen,
    837846                    cTailLength + 1); // including null terminator
    838847
    839848            // now overwrite "found" in the middle
    840             if (cReplaceLen)
     849            if (cReplaceWithLen)
    841850            {
    842851                memcpy(pFound,
    843                        pstrReplaceWith->psz,
    844                        cReplaceLen);        // no null terminator
     852                       pcszReplaceWith,
     853                       cReplaceWithLen);        // no null terminator
    845854            }
    846855
     
    10321041                               ulFirstReplOfs,              // where to start
    10331042                               cSearchLen,                  // chars to replace
    1034                                pstrReplace);
     1043                               pstrReplace->psz,
     1044                               pstrReplace->ulLength);      // adjusted V0.9.11 (2001-04-22) [umoeller]
    10351045
    10361046                // return new length
Note: See TracChangeset for help on using the changeset viewer.