From 517ad9d4b6eae320b708d03a9340a22893b0cab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Sun, 29 Jan 2023 15:45:51 +0100 Subject: New upstream version 4.0.3 --- src/core/xbstring.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'src/core/xbstring.cpp') diff --git a/src/core/xbstring.cpp b/src/core/xbstring.cpp index 872062f..81e67b8 100755 --- a/src/core/xbstring.cpp +++ b/src/core/xbstring.cpp @@ -1358,6 +1358,61 @@ xbString &xbString::Remove(xbUInt32 ulStartPos, xbUInt32 ulDelSize ) { } + + + +/************************************************************************/ +//! @brief Replace a value within a string with another value +/*! + \param sReplace - Character string to replace. + \param sReplaceWith - Character string to replace with + \param iOption - 0 = All occurrences, 1 = first occurrence + \returns Reference to this string. +*/ + +//the new size includes the null termination byte +xbString &xbString::Replace( const char *sReplace, const char *sReplaceWith, xbInt16 iOption ){ + + xbBool bDone = xbFalse; + xbUInt32 ulPos; + xbUInt32 ulNewSize; + xbUInt32 ulSp2; + char *sBuf2; + + while( !bDone ){ + ulPos = Pos( sReplace ); + if( ulPos == 0 ){ + bDone = xbTrue; + } else { + + ulNewSize = this->size + sizeof( sReplaceWith ) - sizeof( sReplace ); + sBuf2 = (char *) calloc( 1, ulNewSize ); + + // copy part1 + for( xbUInt32 ul = 0; ul < ulPos-1; ul++ ) + sBuf2[ul] = data[ul]; + + // copy part2 + strcat( sBuf2, sReplaceWith ); + + // copy part3 + ulSp2 = ulPos + strlen( sReplace ); + char *p = data; + p+= (ulSp2 - 1); + strcat( sBuf2, p ); + + if( iOption ) + bDone = xbTrue; + + free(data); + data = sBuf2; + + } + } + + return *this; +} + /************************************************************************/ //! @brief Resize a string /*! -- cgit v1.2.3