Ignore:
Timestamp:
Jan 16, 2001, 8:49:10 PM (25 years ago)
Author:
umoeller
Message:

Fixes for V0.9.7.

File:
1 edited

Legend:

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

    r21 r23  
    370370 *      If pxstr contains something, its contents are destroyed.
    371371 *
     372 *      With ulSourceLength, specify the length of pcszSource.
     373 *      If you specify 0, this function will run strlen(pcszSource)
     374 *      itself. If you already know the length of pcszSource (or
     375 *      only want to copy a substring), you can speed this function
     376 *      up a bit this way.
     377 *
    372378 *      Returns the length of the new string (excluding the null
    373379 *      terminator), or null upon errors.
     
    377383 +          XSTRING str;
    378384 +          xstrInit(&str, 0);
    379  +          xstrcpy(&str, "blah");
     385 +          xstrcpy(&str, "blah", 0);
    380386 *
    381387 *      This sequence can be abbreviated using xstrInitCopy.
     
    383389 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxcpy
    384390 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
     391 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength
    385392 */
    386393
    387394ULONG xstrcpy(PXSTRING pxstr,               // in/out: string
    388               const char *pcszSource)       // in: source, can be NULL
    389 {
    390     xstrClear(pxstr);
     395              const char *pcszSource,       // in: source, can be NULL
     396              ULONG ulSourceLength)         // in: length of pcszSource or 0
     397{
     398    // xstrClear(pxstr);        NOOOO! this frees the string, we want to keep the memory
    391399
    392400    if (pxstr)
    393401    {
    394         ULONG   ulSourceLength = 0;
    395402        if (pcszSource)
    396             ulSourceLength = strlen(pcszSource);
    397 
    398         if (ulSourceLength)
    399403        {
    400             // we do have a source string:
    401             ULONG cbNeeded = ulSourceLength + 1;
    402             if (cbNeeded > pxstr->cbAllocated)
     404            if (ulSourceLength == 0)
     405                ulSourceLength = strlen(pcszSource);
     406
     407            if (ulSourceLength)
    403408            {
    404                 // we need more memory than we have previously
    405                 // allocated:
    406                 pxstr->cbAllocated = cbNeeded;
    407                 pxstr->psz = (PSZ)malloc(cbNeeded);
     409                // we do have a source string:
     410                ULONG cbNeeded = ulSourceLength + 1;
     411                if (cbNeeded > pxstr->cbAllocated)
     412                {
     413                    // we need more memory than we have previously
     414                    // allocated:
     415                    pxstr->cbAllocated = cbNeeded;
     416                    pxstr->psz = (PSZ)malloc(cbNeeded);
     417                }
     418                // else: we have enough memory
     419
     420                // strcpy(pxstr->psz, pcszSource);
     421                memcpy(pxstr->psz,
     422                       pcszSource,
     423                       ulSourceLength);
     424                *(pxstr->psz + ulSourceLength) = 0;
    408425            }
    409             // else: we have enough memory
    410 
    411             strcpy(pxstr->psz, pcszSource);
     426            else
     427            {
     428                // no source specified or source is empty:
     429                if (pxstr->cbAllocated)
     430                    // we did have a string: set to empty,
     431                    // but leave allocated memory intact
     432                    *(pxstr->psz) = 0;
     433                // else: pxstr->psz is still NULL
     434            }
     435
     436            // in all cases, set new length
     437            pxstr->ulLength = ulSourceLength;
    412438        }
    413         else
    414         {
    415             // no source specified or source is empty:
    416             if (pxstr->cbAllocated)
    417                 // we did have a string: set to empty,
    418                 // but leave allocated memory intact
    419                 *(pxstr->psz) = 0;
    420             // else: pxstr->psz is still NULL
    421         }
    422 
    423         // in all cases, set new length
    424         pxstr->ulLength = ulSourceLength;
    425439    }
    426440
     
    435449 *      If pxstr is empty, this behaves just like xstrcpy.
    436450 *
     451 *      With ulSourceLength, specify the length of pcszSource.
     452 *      If you specify 0, this function will run strlen(pcszSource)
     453 *      itself. If you already know the length of pcszSource (or
     454 *      only want to copy a substring), you can speed this function
     455 *      up a bit this way.
     456 *
    437457 *      Returns the length of the new string (excluding the null
    438458 *      terminator) if the string was changed, or 0 if nothing
     
    446466 +          XSTRING str;
    447467 +          xstrInit(&str, 0);
    448  +          xstrcpy(&str, "blah");
    449  +          xstrcat(&str, "blup");
     468 +          xstrcpy(&str, "blah", 0);
     469 +          xstrcat(&str, "blup", 0);
    450470 *
    451471 *      After this, str.psz points to a new string containing
     
    458478 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
    459479 *@@changed V0.9.7 (2000-12-10) [umoeller]: return value was wrong
     480 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength
    460481 */
    461482
    462483ULONG xstrcat(PXSTRING pxstr,               // in/out: string
    463               const char *pcszSource)       // in: source, can be NULL
     484              const char *pcszSource,       // in: source, can be NULL
     485              ULONG ulSourceLength)         // in: length of pcszSource or 0
    464486{
    465487    ULONG   ulrc = 0;
     
    467489    if (pxstr)
    468490    {
    469         ULONG   ulSourceLength = 0;
    470491        if (pcszSource)
    471             ulSourceLength = strlen(pcszSource);
    472 
    473         if (ulSourceLength)
    474492        {
    475             // we do have a source string:
    476 
    477             // 1) memory management
    478             ULONG   cbNeeded = pxstr->ulLength + ulSourceLength + 1;
    479             if (cbNeeded > pxstr->cbAllocated)
     493            if (ulSourceLength == 0)
     494                ulSourceLength = strlen(pcszSource);
     495
     496            if (ulSourceLength)
    480497            {
    481                 // we need more memory than we have previously
    482                 // allocated:
    483                 if (pxstr->cbAllocated)
    484                     // appendee already had memory:
    485                     // reallocate
    486                     pxstr->psz = (PSZ)realloc(pxstr->psz,
    487                                               cbNeeded);
    488                 else
    489                     // appendee has no memory:
    490                     pxstr->psz = (PSZ)malloc(cbNeeded);
    491 
    492                 pxstr->cbAllocated = cbNeeded;
    493                         // ulLength is unchanged yet
    494             }
    495             // else: we have enough memory, both if appendee
    496             //       is empty or not empty
    497 
    498             // now we have:
    499             // -- if appendee (pxstr) had enough memory, no problem
    500             // -- if appendee (pxstr) needed more memory
    501             //      -- and was not empty: pxstr->psz now points to a
    502             //         reallocated copy of the old string
    503             //      -- and was empty: pxstr->psz now points to a
    504             //         new (unitialized) buffer
    505 
    506             // 2) append source string:
    507             memcpy(pxstr->psz + pxstr->ulLength,
    508                    pcszSource,
    509                    ulSourceLength + 1);     // null terminator
    510 
    511             // in all cases, set new length
    512             pxstr->ulLength += ulSourceLength;
    513             ulrc = pxstr->ulLength;     // V0.9.7 (2000-12-10) [umoeller]
    514 
    515         } // end if (ulSourceLength)
     498                // we do have a source string:
     499
     500                // 1) memory management
     501                ULONG   cbNeeded = pxstr->ulLength + ulSourceLength + 1;
     502                if (cbNeeded > pxstr->cbAllocated)
     503                {
     504                    // we need more memory than we have previously
     505                    // allocated:
     506                    if (pxstr->cbAllocated)
     507                        // appendee already had memory:
     508                        // reallocate
     509                        pxstr->psz = (PSZ)realloc(pxstr->psz,
     510                                                  cbNeeded);
     511                    else
     512                        // appendee has no memory:
     513                        pxstr->psz = (PSZ)malloc(cbNeeded);
     514
     515                    pxstr->cbAllocated = cbNeeded;
     516                            // ulLength is unchanged yet
     517                }
     518                // else: we have enough memory, both if appendee
     519                //       is empty or not empty
     520
     521                // now we have:
     522                // -- if appendee (pxstr) had enough memory, no problem
     523                // -- if appendee (pxstr) needed more memory
     524                //      -- and was not empty: pxstr->psz now points to a
     525                //         reallocated copy of the old string
     526                //      -- and was empty: pxstr->psz now points to a
     527                //         new (unitialized) buffer
     528
     529                // 2) append source string:
     530                memcpy(pxstr->psz + pxstr->ulLength,
     531                       pcszSource,
     532                       ulSourceLength + 1);     // null terminator
     533
     534                // in all cases, set new length
     535                pxstr->ulLength += ulSourceLength;
     536                ulrc = pxstr->ulLength;     // V0.9.7 (2000-12-10) [umoeller]
     537
     538            } // end if (ulSourceLength)
     539        }
     540
    516541        // else no source specified or source is empty:
    517542        // do nothing
     
    601626
    602627/*
     628 *@@ xstrrpl:
     629 *      replaces cSearchLen characters in pxstr, starting
     630 *      at the position ulStart, with the string
     631 *      in pxstrReplaceWith.
     632 *
     633 *      Returns the new length of the string, excluding
     634 *      the null terminator, or 0 if the replacement failed
     635 *      (e.g. because the offsets were too large).
     636 *
     637 *      This has been extracted from xstrFindReplace because
     638 *      if you already know the position of a substring,
     639 *      you can now call this directly. This properly
     640 *      reallocates the string if more memory is needed.
     641 *
     642 *      Example:
     643 *
     644 +          XSTRING xstr, xstrReplacement;
     645 +          xstrInitCopy(&xstr, "This is a test string.");
     646 +          //  positions:       0123456789012345678901
     647 +          //                             1         2
     648 +
     649 +          xstrInitCopy(&xstrReplacement, "stupid");
     650 +
     651 +          xstrrpl(&xstr,
     652 +                  10,     // position of "test"
     653 +                  4,      // length of "test"
     654 +                  &xstrReplacement);
     655 *
     656 *      This would yield "This is a stupid string."
     657 *
     658 *@@added V0.9.7 (2001-01-15) [umoeller]
     659 */
     660
     661ULONG xstrrpl(PXSTRING pxstr,                   // in/out: string
     662              ULONG ulFirstReplOfs,             // in: ofs of first char to replace
     663              ULONG cReplLen,                   // in: no. of chars to replace
     664              const XSTRING *pstrReplaceWith)   // in: string to replace chars with
     665{
     666    ULONG   ulrc = 0;
     667
     668    // security checks...
     669    if (    (ulFirstReplOfs + cReplLen <= pxstr->ulLength)
     670         && (pstrReplaceWith)
     671         // && (pstrReplaceWith->ulLength)      no, this can be empty
     672       )
     673    {
     674        ULONG   cReplaceLen = pstrReplaceWith->ulLength;
     675                    // can be 0!
     676
     677        // length of new string
     678        ULONG   cbNeeded = pxstr->ulLength
     679                         + cReplaceLen
     680                         - cReplLen
     681                         + 1;                  // null terminator
     682        // offset where pszSearch was found
     683                // ulFirstReplOfs = pFound - pxstr->psz; now ulFirstReplOfs
     684        PSZ     pFound = pxstr->psz + ulFirstReplOfs;
     685
     686        // now check if we have enough memory...
     687        if (pxstr->cbAllocated < cbNeeded)
     688        {
     689            // no, we need more memory:
     690            // allocate new buffer
     691            PSZ pszNew = (PSZ)malloc(cbNeeded);
     692
     693            if (ulFirstReplOfs)
     694                // "found" was not at the beginning:
     695                // copy from beginning up to found-offset
     696                memcpy(pszNew,
     697                       pxstr->psz,
     698                       ulFirstReplOfs);     // up to "found"
     699
     700            if (cReplaceLen)
     701            {
     702                // we have a replacement:
     703                // insert it next
     704                memcpy(pszNew + ulFirstReplOfs,
     705                       pstrReplaceWith->psz,
     706                       cReplaceLen + 1);        // include null terminator
     707            }
     708
     709            // copy rest:
     710            // pxstr      frontFOUNDtail
     711            //            0         1
     712            //            01234567890123
     713            //            ³    ³    ³  ³
     714            //            ³    ³    ÀÄ ulFirstReplOfs + cReplLen = 10
     715            //            ³    ³       ³
     716            //            ³    ÀÄ ulFirstReplOfs = 5
     717            //            ³            ³
     718            //            pxstr->ulLength = 14
     719            memcpy(pszNew + ulFirstReplOfs + cReplaceLen,
     720                   pFound + cReplLen,
     721                   // remaining bytes:
     722                   pxstr->ulLength - ulFirstReplOfs - cReplLen // 9
     723                        + 1); // null terminator
     724
     725            // replace old buffer with new one
     726            free(pxstr->psz);
     727            pxstr->psz = pszNew;
     728            pxstr->ulLength = cbNeeded - 1;
     729            pxstr->cbAllocated = cbNeeded;
     730        } // end if (pxstr->cbAllocated < cbNeeded)
     731        else
     732        {
     733            // we have enough memory left,
     734            // we can just overwrite in the middle...
     735
     736            PSZ     pszAfterFoundBackup = 0;
     737            // calc length of string after "found"
     738            ULONG   cTailLength = pxstr->ulLength - ulFirstReplOfs - cReplLen;
     739
     740            // if "replace" is longer than "found",
     741            // make a backup of the stuff after "found",
     742            // or this would get overwritten
     743            if (cReplaceLen > cReplLen)
     744            {
     745                pszAfterFoundBackup = (PSZ)malloc(cTailLength + 1);
     746                memcpy(pszAfterFoundBackup,
     747                       pFound + cReplLen,
     748                       cTailLength + 1);
     749            }
     750
     751            // now overwrite "found" in the middle
     752            if (cReplaceLen)
     753            {
     754                memcpy(pxstr->psz + ulFirstReplOfs,
     755                       pstrReplaceWith->psz,
     756                       cReplaceLen);        // no null terminator
     757            }
     758
     759            // now append tail (stuff after "found") again...
     760            if (pszAfterFoundBackup)
     761            {
     762                // we made a backup above:
     763                memcpy(pxstr->psz + ulFirstReplOfs + cReplaceLen,
     764                       pszAfterFoundBackup,
     765                       cTailLength + 1);
     766                free(pszAfterFoundBackup);
     767                        // done!
     768            }
     769            else
     770                // no backup:
     771                if (cReplaceLen < cReplLen)
     772                    // "replace" is shorter than "found:
     773                    memcpy(pxstr->psz + ulFirstReplOfs + cReplaceLen,
     774                           pFound + cReplLen,
     775                           cTailLength + 1);
     776                // else (cReplaceLen == cReplLen):
     777                // we can leave the tail as it is
     778
     779            pxstr->ulLength = cbNeeded - 1;
     780        }
     781
     782        ulrc = cbNeeded - 1;
     783    } // end checks
     784
     785    return (ulrc);
     786}
     787
     788/*
    603789 *@@ xstrFindWord:
    604790 *      searches for pstrFind in pxstr, starting at ulOfs.
     
    668854
    669855/*
    670  *@@ xstrrpl:
     856 *@@ xstrFindReplace:
    671857 *      replaces the first occurence of pstrSearch with
    672858 *      pstrReplace in pxstr.
     
    688874 *      (*pulOffset == 0), this starts from the beginning
    689875 *      of pxstr.
     876 *
    690877 *      If the string was found, *pulOffset will be set to the
    691878 *      first character after the new replacement string. This
     
    697884 *      speed advantage):
    698885 *
    699  *      -- strhrpl operates on C strings only;
    700  *
    701  *      -- xstrcrpl uses C strings for the search and replace
     886 *      -- strhFindReplace operates on C strings only;
     887 *
     888 *      -- xstrFindReplaceC uses C strings for the search and replace
    702889 *         parameters.
    703890 *
    704891 *      <B>Example usage:</B>
    705892 *
    706  +          XSTRING str;
    707  +          ULONG ulOffset = 0;
    708  +          xstrInit(&str, 0);
    709  +          xstrcpy(&str, "Test phrase 1. Test phrase 2.");
    710  +          while (xstrrpl(&str,
    711  +                         &ulPos,      // in/out: offset
    712  +                         "Test",      // search
    713  +                         "Dummy")     // replace
     893 +          XSTRING strBuf,
     894 +                  strFind,
     895 +                  strRepl;
     896 +          size_t  ShiftTable[256];
     897 +          BOOL    fRepeat = FALSE;
     898 +          ULONG   ulOffset = 0;
     899 +
     900 +          xstrInitCopy(&strBuf, "Test phrase 1. Test phrase 2.", 0);
     901 +          xstrInitSet(&strFind, "Test");
     902 +          xstrInitSet(&strRepl, "Dummy");
     903 +          while (xstrFindReplace(&str,
     904 +                                 &ulPos,      // in/out: offset
     905 +                                 &strFind,    // search
     906 +                                 &strRepl,    // replace
     907 +                                 ShiftTable,
     908 +                                 &fRepeat))
    714909 +              ;
    715910 *
     
    722917 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
    723918 *@@changed V0.9.6 (2000-11-12) [umoeller]: now using strhmemfind
    724  */
    725 
    726 ULONG xstrrpl(PXSTRING pxstr,               // in/out: string
    727               PULONG pulOfs,                // in: where to begin search (0 = start);
    728                                             // out: ofs of first char after replacement string
    729               const XSTRING *pstrSearch,    // in: search string; cannot be NULL
    730               const XSTRING *pstrReplace,   // in: replacement string; cannot be NULL
    731               size_t *pShiftTable,          // in: shift table (see strhmemfind)
    732               PBOOL pfRepeatFind)           // in: repeat find? (see strhmemfind)
     919 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrrpl; extracted new xstrrpl
     920 */
     921
     922ULONG xstrFindReplace(PXSTRING pxstr,               // in/out: string
     923                      PULONG pulOfs,                // in: where to begin search (0 = start);
     924                                                    // out: ofs of first char after replacement string
     925                      const XSTRING *pstrSearch,    // in: search string; cannot be NULL
     926                      const XSTRING *pstrReplace,   // in: replacement string; cannot be NULL
     927                      size_t *pShiftTable,          // in: shift table (see strhmemfind)
     928                      PBOOL pfRepeatFind)           // in: repeat find? (see strhmemfind)
    733929{
    734930    ULONG    ulrc = 0;      // default: not found
     
    744940        {
    745941            // yes:
    746             /* PSZ     pFound = strstr(pxstr->psz + *pulOfs,
    747                                     pstrSearch->psz);        */
    748             PSZ pFound = (PSZ)strhmemfind(pxstr->psz + *pulOfs, // in: haystack
    749                                           pxstr->ulLength - *pulOfs,
    750                                           pstrSearch->psz,
    751                                           cSearchLen,
    752                                           pShiftTable,
    753                                           pfRepeatFind);
     942            ULONG   ulOfs = *pulOfs;
     943            const char *pFound
     944                = (const char *)strhmemfind(pxstr->psz + ulOfs, // in: haystack
     945                                            pxstr->ulLength - ulOfs,
     946                                            pstrSearch->psz,
     947                                            cSearchLen,
     948                                            pShiftTable,
     949                                            pfRepeatFind);
    754950            if (pFound)
    755951            {
     952                ULONG ulFirstReplOfs = pFound - pxstr->psz;
    756953                // found in buffer from ofs:
    757                 ULONG   cReplaceLen = pstrReplace->ulLength;
    758                             // can be 0!
    759 
    760                 // length of new string
    761                 ULONG   cbNeeded = pxstr->ulLength
    762                                  + cReplaceLen
    763                                  - cSearchLen
    764                                  + 1,                  // null terminator
    765                 // offset where pszSearch was found
    766                         ulFoundOfs = pFound - pxstr->psz;
    767 
    768                 // now check if we have enough memory...
    769                 if (pxstr->cbAllocated < cbNeeded)
    770                 {
    771                     // no, we need more memory:
    772                     // allocate new buffer
    773                     PSZ pszNew = (PSZ)malloc(cbNeeded);
    774 
    775                     if (ulFoundOfs)
    776                         // "found" was not at the beginning:
    777                         // copy from beginning up to found-offset
    778                         memcpy(pszNew,
    779                                pxstr->psz,
    780                                ulFoundOfs);     // up to "found"
    781 
    782                     if (cReplaceLen)
    783                     {
    784                         // we have a replacement:
    785                         // insert it next
    786                         memcpy(pszNew + ulFoundOfs,
    787                                pstrReplace->psz,
    788                                cReplaceLen + 1);        // include null terminator
    789                     }
    790 
    791                     // copy rest:
    792                     // pxstr      frontFOUNDtail
    793                     //            0         1
    794                     //            01234567890123
    795                     //            ³    ³    ³  ³
    796                     //            ³    ³    ÀÄ ulFoundOfs + cSearchLen = 10
    797                     //            ³    ³       ³
    798                     //            ³    ÀÄ ulFoundOfs = 5
    799                     //            ³            ³
    800                     //            pxstr->ulLength = 14
    801                     memcpy(pszNew + ulFoundOfs + cReplaceLen,
    802                            pFound + cSearchLen,
    803                            // remaining bytes:
    804                            pxstr->ulLength - ulFoundOfs - cSearchLen // 9
    805                                 + 1); // null terminator
    806 
    807                     // replace old buffer with new one
    808                     free(pxstr->psz);
    809                     pxstr->psz = pszNew;
    810                     pxstr->ulLength = cbNeeded - 1;
    811                     pxstr->cbAllocated = cbNeeded;
    812                 } // end if (pxstr->cbAllocated < cbNeeded)
    813                 else
    814                 {
    815                     // we have enough memory left,
    816                     // we can just overwrite in the middle...
    817 
    818                     PSZ     pszAfterFoundBackup = 0;
    819                     // calc length of string after "found"
    820                     ULONG   cTailLength = pxstr->ulLength - ulFoundOfs - cSearchLen;
    821 
    822                     // if "replace" is longer than "found",
    823                     // make a backup of the stuff after "found",
    824                     // or this would get overwritten
    825                     if (cReplaceLen > cSearchLen)
    826                     {
    827                         pszAfterFoundBackup = (PSZ)malloc(cTailLength + 1);
    828                         memcpy(pszAfterFoundBackup,
    829                                pFound + cSearchLen,
    830                                cTailLength + 1);
    831                     }
    832 
    833                     // now overwrite "found" in the middle
    834                     if (cReplaceLen)
    835                     {
    836                         memcpy(pxstr->psz + ulFoundOfs,
    837                                pstrReplace->psz,
    838                                cReplaceLen);        // no null terminator
    839                     }
    840 
    841                     // now append tail (stuff after "found") again...
    842                     if (pszAfterFoundBackup)
    843                     {
    844                         // we made a backup above:
    845                         memcpy(pxstr->psz + ulFoundOfs + cReplaceLen,
    846                                pszAfterFoundBackup,
    847                                cTailLength + 1);
    848                         free(pszAfterFoundBackup);
    849                                 // done!
    850                     }
    851                     else
    852                         // no backup:
    853                         if (cReplaceLen < cSearchLen)
    854                             // "replace" is shorter than "found:
    855                             memcpy(pxstr->psz + ulFoundOfs + cReplaceLen,
    856                                    pFound + cSearchLen,
    857                                    cTailLength + 1);
    858                         // else (cReplaceLen == cSearchLen):
    859                         // we can leave the tail as it is
    860 
    861                     pxstr->ulLength = cbNeeded - 1;
    862                 }
     954                // replace pFound with pstrReplace
     955                ulrc = xstrrpl(pxstr,
     956                               ulFirstReplOfs,              // where to start
     957                               cSearchLen,                  // chars to replace
     958                               pstrReplace);
    863959
    864960                // return new length
    865                 ulrc = cbNeeded - 1;
    866                 *pulOfs = ulFoundOfs + cReplaceLen;
     961                *pulOfs = ulFirstReplOfs + pstrReplace->ulLength;
    867962            } // end if (pFound)
    868963        } // end if (    (*pulOfs < pxstr->ulLength) ...
     
    873968
    874969/*
    875  *@@ xstrcrpl:
    876  *      wrapper around xstrrpl() which allows using C strings
    877  *      for the find and replace parameters.
     970 *@@ xstrFindReplaceC:
     971 *      wrapper around xstrFindReplace() which allows using
     972 *      C strings for the find and replace parameters.
    878973 *
    879974 *      This creates two temporary XSTRING's for pcszSearch
    880  *      pcszReplace. As a result, this is slower than xstrrpl.
     975 *      and pcszReplace and thus cannot use the shift table
     976 *      for repetitive searches. As a result, this is slower
     977 *      than xstrFindReplace.
     978 *
    881979 *      If you search with the same strings several times,
    882  *      you'll be better off using xstrrpl() directly.
     980 *      you'll be better off using xstrFindReplace() directly.
    883981 *
    884982 *@@added V0.9.6 (2000-11-01) [umoeller]
    885  */
    886 
    887 ULONG xstrcrpl(PXSTRING pxstr,              // in/out: string
    888                PULONG pulOfs,               // in: where to begin search (0 = start);
    889                                             // out: ofs of first char after replacement string
    890                const char *pcszSearch,      // in: search string; cannot be NULL
    891                const char *pcszReplace)     // in: replacement string; cannot be NULL
    892 {
    893     // ULONG   ulrc = 0;
     983 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrcrpl
     984 */
     985
     986ULONG xstrFindReplaceC(PXSTRING pxstr,              // in/out: string
     987                       PULONG pulOfs,               // in: where to begin search (0 = start);
     988                                                    // out: ofs of first char after replacement string
     989                       const char *pcszSearch,      // in: search string; cannot be NULL
     990                       const char *pcszReplace)     // in: replacement string; cannot be NULL
     991{
    894992    XSTRING xstrFind,
    895993            xstrReplace;
     
    9021000    xstrInitSet(&xstrReplace, (PSZ)pcszReplace);
    9031001
    904     return (xstrrpl(pxstr, pulOfs, &xstrFind, &xstrReplace, ShiftTable, &fRepeat));
     1002    return (xstrFindReplace(pxstr, pulOfs, &xstrFind, &xstrReplace, ShiftTable, &fRepeat));
     1003}
     1004
     1005/*
     1006 *@@ xstrConvertLineFormat:
     1007 *      converts between line formats.
     1008 *
     1009 *      If (fToCFormat == TRUE), all \r\n pairs are replaced
     1010 *      with \n chars (UNIX or C format).
     1011 *
     1012 *      Reversely, if (fToCFormat == FALSE), all \n chars
     1013 *      are converted to \r\n pairs (DOS and OS/2 formats).
     1014 *      No check is made whether this has already been done.
     1015 *
     1016 *@@added V0.9.7 (2001-01-15) [umoeller]
     1017 */
     1018
     1019VOID xstrConvertLineFormat(PXSTRING pxstr,
     1020                           BOOL fToCFormat) // in: if TRUE, to C format; if FALSE, to OS/2 format.
     1021{
     1022    XSTRING     strFind,
     1023                strRepl;
     1024    size_t      ShiftTable[256];
     1025    BOOL        fRepeat = FALSE;
     1026    ULONG       ulOfs = 0;
     1027
     1028    if (fToCFormat)
     1029    {
     1030        // OS/2 to C:
     1031        xstrInitSet(&strFind, "\r\n");
     1032        xstrInitSet(&strRepl, "\n");
     1033    }
     1034    else
     1035    {
     1036        // C to OS/2:
     1037        xstrInitSet(&strFind, "\n");
     1038        xstrInitSet(&strRepl, "\r\n");
     1039    }
     1040
     1041    while (xstrFindReplace(pxstr,
     1042                           &ulOfs,
     1043                           &strFind,
     1044                           &strRepl,
     1045                           ShiftTable,
     1046                           &fRepeat))
     1047            ;
    9051048}
    9061049
     
    9121055            strFind,
    9131056            strReplace;
     1057    size_t  shift[256];
     1058    BOOL    fRepeat = FALSE;
    9141059    ULONG   ulOfs = 0;
    9151060
     
    9181063    xstrInit(&strReplace, 0);
    9191064
    920     xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !");
    921     xstrcpy(&strFind, "Test");
    922     xstrcpy(&strReplace, "Dummy");
     1065    xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !", 0);
     1066    xstrcpy(&strFind, "Test", 0);
     1067    xstrcpy(&strReplace, "Dummy", 0);
    9231068
    9241069    printf("Old string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    9251070
    926     while (xstrrpl(&str,
    927                    ulOfs,
     1071    printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
     1072
     1073    fRepeat = FALSE;
     1074    ulOfs = 0;
     1075    while (xstrFindReplace(&str,
     1076                           &ulOfs,
     1077                           &strFind,
     1078                           &strReplace,
     1079                           shift, &fRepeat));
     1080        ;
     1081
     1082    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
     1083
     1084    xstrcpy(&strFind, strReplace.psz, 0);
     1085    xstrClear(&strReplace);
     1086
     1087    printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
     1088
     1089    fRepeat = FALSE;
     1090    ulOfs = 0;
     1091    while (xstrFindReplace(&str,
     1092                   &ulOfs,
    9281093                   &strFind,
    9291094                   &strReplace,
    930                    &ulOfs))
     1095                   shift, &fRepeat));
    9311096        ;
    9321097
    9331098    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    9341099
    935     xstrcpy(&strFind, strReplace.psz);
    936     xstrClear(&strReplace);
     1100    xstrcpy(&strFind, " ", 0);
     1101    xstrcpy(&strReplace, ".", 0);
     1102
     1103    printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
     1104
     1105    fRepeat = FALSE;
    9371106    ulOfs = 0;
    938     while (xstrrpl(&str,
    939                    ulOfs,
     1107    while (xstrFindReplace(&str,
     1108                   &ulOfs,
    9401109                   &strFind,
    9411110                   &strReplace,
    942                    &ulOfs))
     1111                   shift, &fRepeat));
    9431112        ;
    9441113
    9451114    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    9461115
    947     xstrcpy(&strFind, " ");
    948     xstrcpy(&strReplace, ".");
     1116    xstrcpy(&strFind, ".", 0);
     1117    xstrcpy(&strReplace, "*.........................*", 0);
     1118
     1119    printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
     1120
     1121    fRepeat = FALSE;
    9491122    ulOfs = 0;
    950     while (xstrrpl(&str,
    951                    ulOfs,
     1123    while (xstrFindReplace(&str,
     1124                   &ulOfs,
    9521125                   &strFind,
    9531126                   &strReplace,
    954                    &ulOfs))
     1127                   shift, &fRepeat));
    9551128        ;
    9561129
    9571130    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    9581131
    959     xstrcpy(&strFind, ".");
    960     xstrcpy(&strReplace, "***************************");
     1132    printf("Reserving extra mem.\n");
     1133
     1134    xstrReserve(&str, 6000);
     1135    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
     1136
     1137    xstrcpy(&strFind, "..........", 0);
     1138    xstrcpy(&strReplace, "@", 0);
     1139
     1140    printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
     1141
     1142    fRepeat = FALSE;
    9611143    ulOfs = 0;
    962     while (xstrrpl(&str,
    963                    ulOfs,
     1144    while (xstrFindReplace(&str,
     1145                   &ulOfs,
    9641146                   &strFind,
    9651147                   &strReplace,
    966                    &ulOfs))
     1148                   shift, &fRepeat));
    9671149        ;
    9681150
    9691151    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    9701152
    971     xstrcpy(&strFind, "*");
    972     xstrClear(&strReplace);
    973     ulOfs = 0;
    974     while (xstrrpl(&str,
    975                    ulOfs,
    976                    &strFind,
    977                    &strReplace,
    978                    &ulOfs))
    979         ;
    980 
    981     printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    982 } */
    983 
     1153    return (0);
     1154}
     1155*/
     1156
     1157
Note: See TracChangeset for help on using the changeset viewer.