summaryrefslogtreecommitdiff
path: root/src/core/xbstring.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/xbstring.cpp')
-rwxr-xr-xsrc/core/xbstring.cpp71
1 files changed, 60 insertions, 11 deletions
diff --git a/src/core/xbstring.cpp b/src/core/xbstring.cpp
index 81e67b8..701e50e 100755
--- a/src/core/xbstring.cpp
+++ b/src/core/xbstring.cpp
@@ -1267,6 +1267,45 @@ xbString &xbString::PadRight( char c, xbUInt32 ulNewLen ){
//! @brief Determine position of a given character
/*!
\param c Seek character
+ \param ulStartPos starting position for search, first position is 1
+ \returns Position within string. Returns 0 if not found.
+*/
+xbUInt32 xbString::Pos(char c, xbUInt32 ulStartPos ) const {
+
+ if (data == NULL)
+ return 0;
+ char *p = data;
+
+ if( ulStartPos >= size )
+ return 0;
+
+ xbUInt32 iPos = 0;
+ while( (iPos+1) < ulStartPos ){
+ p++;
+ iPos++;
+ }
+ xbBool bFound = 0;
+ while( *p && !bFound && iPos < ( size - 1 )){
+ if( *p == c )
+ bFound = 1;
+ else {
+ iPos++;
+ p++;
+ }
+ }
+
+ if( bFound )
+ return iPos + 1;
+ else
+ return 0;
+}
+
+
+
+/************************************************************************/
+//! @brief Determine position of a given character
+/*!
+ \param c Seek character
\returns Position within string. Returns 0 if not found.
*/
xbUInt32 xbString::Pos(char c) const {
@@ -1375,41 +1414,51 @@ xbString &xbString::Replace( const char *sReplace, const char *sReplaceWith, xbI
xbBool bDone = xbFalse;
xbUInt32 ulPos;
- xbUInt32 ulNewSize;
+ xbUInt32 ulNewLen;
+ xbUInt32 ulReplaceWithLen;
+ xbUInt32 ulRsLen; // size of right side of string after replaced data
xbUInt32 ulSp2;
char *sBuf2;
+ const char *s; // source ptr
+ char *t; // target ptr
+
while( !bDone ){
ulPos = Pos( sReplace );
if( ulPos == 0 ){
bDone = xbTrue;
} else {
- ulNewSize = this->size + sizeof( sReplaceWith ) - sizeof( sReplace );
- sBuf2 = (char *) calloc( 1, ulNewSize );
+ ulReplaceWithLen = (xbUInt32) strlen( sReplaceWith );
+ ulNewLen = this->size + ulReplaceWithLen - (xbUInt32) strlen( sReplace );
+ sBuf2 = (char *) calloc( 1, ulNewLen );
// copy part1
+ t = sBuf2;
+ s = data;
for( xbUInt32 ul = 0; ul < ulPos-1; ul++ )
- sBuf2[ul] = data[ul];
+ *t++ = *s++;
// copy part2
- strcat( sBuf2, sReplaceWith );
+ s = sReplaceWith;
+ for( xbUInt32 ul = 0; ul < ulReplaceWithLen; ul++ )
+ *t++ = *s++;
// copy part3
- ulSp2 = ulPos + strlen( sReplace );
- char *p = data;
- p+= (ulSp2 - 1);
- strcat( sBuf2, p );
+ ulSp2 = ulPos + (xbUInt32) strlen( sReplace );
+ s = data;
+ s+= (ulSp2 - 1);
+ ulRsLen = (xbUInt32) strlen( s );
+ for( xbUInt32 ul = 0; ul < ulRsLen; ul++ )
+ *t++ = *s++;
if( iOption )
bDone = xbTrue;
free(data);
data = sBuf2;
-
}
}
-
return *this;
}