tier1/strtools.cpp
bool V_StrSubst( 
	const char *pIn, 
	const char *pMatch,
	const char *pReplaceWith,
	char *pOut,
	int outLen,
	bool bCaseSensitive
	)
{
	int replaceFromLen = V_strlen( pMatch );
	int replaceToLen = V_strlen( pReplaceWith );

	const char *pInStart = pIn;
	char *pOutPos = pOut;
	pOutPos[0] = 0;

	while ( 1 )
	{
		int nRemainingOut = outLen - (pOutPos - pOut);

		const char *pTestPos = ( bCaseSensitive ? V_strstr( pInStart, pMatch ) : V_stristr( pInStart, pMatch ) );
		if ( pTestPos )
		{
			// Found an occurence of pMatch. First, copy whatever leads up to the string.
			int copyLen = pTestPos - pInStart;
			if ( !CopyToMaxChars( pOutPos, nRemainingOut, pInStart, copyLen ) )
				return false;
			
			// Did we hit the end of the output string?
			if ( copyLen > nRemainingOut-1 )
				return false;

			pOutPos += V_strlen( pOutPos );
			nRemainingOut = outLen - (pOutPos - pOut);

			// Now add the replacement string.
			if ( !CopyToMaxChars( pOutPos, nRemainingOut, pReplaceWith, replaceToLen ) )
				return false;

			pInStart += copyLen + replaceFromLen;
			pOutPos += replaceToLen;			
		}
		else
		{
			// We're at the end of pIn. Copy whatever remains and get out.
			int copyLen = V_strlen( pInStart );
			V_strncpy( pOutPos, pInStart, nRemainingOut );
			return ( copyLen <= nRemainingOut-1 );
		}
	}
}
// Returns true if it completed successfully.
// If it would overflow pOut, it fills as much as it can and returns false.
Returns true if it completed successfully.
If it would overflow pOut, it fills as much as it can and returns false.
Returns true if it completed successfully . If it would overflow pOut it fills as much as it can and returns false .
bool V_StrSubst( 
	const char *pIn, 
	const char *pMatch,
	const char *pReplaceWith,
	char *pOut,
	int outLen,
	bool bCaseSensitive
	)
{
	int replaceFromLen = V_strlen( pMatch );
	int replaceToLen = V_strlen( pReplaceWith );
	const char *pInStart = pIn;
	char *pOutPos = pOut;
	pOutPos[0] = 0;
	while ( 1 )
	{
		int nRemainingOut = outLen - (pOutPos - pOut);
		const char *pTestPos = ( bCaseSensitive ? V_strstr( pInStart, pMatch ) : V_stristr( pInStart, pMatch ) );
		if ( pTestPos )
		{
			int copyLen = pTestPos - pInStart;
			if ( !CopyToMaxChars( pOutPos, nRemainingOut, pInStart, copyLen ) )
				return false;
			if ( copyLen > nRemainingOut-1 )
				return false;
			pOutPos += V_strlen( pOutPos );
			nRemainingOut = outLen - (pOutPos - pOut);
			if ( !CopyToMaxChars( pOutPos, nRemainingOut, pReplaceWith, replaceToLen ) )
				return false;
			pInStart += copyLen + replaceFromLen;
			pOutPos += replaceToLen;			
		}
		else
		{
			int copyLen = V_strlen( pInStart );
			V_strncpy( pOutPos, pInStart, nRemainingOut );
			return ( copyLen <= nRemainingOut-1 );
		}
	}
}
bool V_StrSubst ( const char * pIn , const char * pMatch , const char * pReplaceWith , char * pOut , int outLen , bool bCaseSensitive ) { int replaceFromLen = V_strlen ( pMatch ) ; int replaceToLen = V_strlen ( pReplaceWith ) ; const char * pInStart = pIn ; char * pOutPos = pOut ; pOutPos [ 0 ] = 0 ; while ( 1 ) { int nRemainingOut = outLen - ( pOutPos - pOut ) ; const char * pTestPos = ( bCaseSensitive ? V_strstr ( pInStart , pMatch ) : V_stristr ( pInStart , pMatch ) ) ; if ( pTestPos ) { int copyLen = pTestPos - pInStart ; if ( ! CopyToMaxChars ( pOutPos , nRemainingOut , pInStart , copyLen ) ) return false ; if ( copyLen > nRemainingOut - 1 ) return false ; pOutPos += V_strlen ( pOutPos ) ; nRemainingOut = outLen - ( pOutPos - pOut ) ; if ( ! CopyToMaxChars ( pOutPos , nRemainingOut , pReplaceWith , replaceToLen ) ) return false ; pInStart += copyLen + replaceFromLen ; pOutPos += replaceToLen ; } else { int copyLen = V_strlen ( pInStart ) ; V_strncpy ( pOutPos , pInStart , nRemainingOut ) ; return ( copyLen <= nRemainingOut - 1 ) ; } } }
Returns true if it completed successfully.
Returns true if it completed successfully .
// Found an occurence of pMatch. First, copy whatever leads up to the string. // Did we hit the end of the output string? // Now add the replacement string. // We're at the end of pIn. Copy whatever remains and get out.