core/sm_stringutil.cpp
changeset 3005 2db965f8db3f
parent 3004 e3ac0dc6d9a4
child 3010 8ea042c00c80
     1.1 --- a/core/sm_stringutil.cpp	Fri May 14 19:43:53 2010 -0700
     1.2 +++ b/core/sm_stringutil.cpp	Fri May 14 20:04:30 2010 -0700
     1.3 @@ -1331,194 +1331,6 @@
     1.4  	return ptr;
     1.5  }
     1.6  
     1.7 -unsigned int UTIL_ReplaceAll(char *subject, size_t maxlength, const char *search, const char *replace, bool caseSensitive)
     1.8 -{
     1.9 -	size_t searchLen = strlen(search);
    1.10 -	size_t replaceLen = strlen(replace);
    1.11 -
    1.12 -	char *ptr = subject;
    1.13 -	unsigned int total = 0;
    1.14 -	while ((ptr = UTIL_ReplaceEx(ptr, maxlength, search, searchLen, replace, replaceLen, caseSensitive)) != NULL)
    1.15 -	{
    1.16 -		total++;
    1.17 -		if (*ptr == '\0')
    1.18 -		{
    1.19 -			break;
    1.20 -		}
    1.21 -	}
    1.22 -
    1.23 -	return total;
    1.24 -}
    1.25 -
    1.26 -/**
    1.27 - * NOTE: Do not edit this for the love of god unless you have 
    1.28 - * read the test cases and understand the code behind each one.
    1.29 - * While I don't guarantee there aren't mistakes, I do guarantee
    1.30 - * that plugins will end up relying on tiny idiosyncrasies of this
    1.31 - * function, just like they did with AMX Mod X.
    1.32 - *
    1.33 - * There are explicitly more cases than the AMX Mod X version because
    1.34 - * we're not doing a blind copy.  Each case is specifically optimized
    1.35 - * for what needs to be done.  Even better, we don't have to error on
    1.36 - * bad buffer sizes.  Instead, this function will smartly cut off the 
    1.37 - * string in a way that pushes old data out.
    1.38 - */
    1.39 -char *UTIL_ReplaceEx(char *subject, size_t maxLen, const char *search, size_t searchLen, const char *replace, size_t replaceLen, bool caseSensitive)
    1.40 -{
    1.41 -	char *ptr = subject;
    1.42 -	size_t browsed = 0;
    1.43 -	size_t textLen = strlen(subject);
    1.44 -
    1.45 -	/* It's not possible to search or replace */
    1.46 -	if (searchLen > textLen)
    1.47 -	{
    1.48 -		return NULL;
    1.49 -	}
    1.50 -
    1.51 -	/* Handle the case of one byte replacement.
    1.52 -	 * It's only valid in one case.
    1.53 -	 */
    1.54 -	if (maxLen == 1)
    1.55 -	{
    1.56 -		/* If the search matches and the replace length is 0, 
    1.57 -		 * we can just terminate the string and be done.
    1.58 -		 */
    1.59 -		if ((caseSensitive ? strcmp(subject, search) : strcasecmp(subject, search)) == 0 && replaceLen == 0)
    1.60 -		{
    1.61 -			*subject = '\0';
    1.62 -			return subject;
    1.63 -		}
    1.64 -		else
    1.65 -		{
    1.66 -			return NULL;
    1.67 -		}
    1.68 -	}
    1.69 -
    1.70 -	/* Subtract one off the maxlength so we can include the null terminator */
    1.71 -	maxLen--;
    1.72 -
    1.73 -	while (*ptr != '\0' && (browsed <= textLen - searchLen))
    1.74 -	{
    1.75 -		/* See if we get a comparison */
    1.76 -		if ((caseSensitive ? strncmp(ptr, search, searchLen) : strncasecmp(ptr, search, searchLen)) == 0)
    1.77 -		{
    1.78 -			if (replaceLen > searchLen)
    1.79 -			{
    1.80 -				/* First, see if we have enough space to do this operation */
    1.81 -				if (maxLen - textLen < replaceLen - searchLen)
    1.82 -				{
    1.83 -					/* First, see if the replacement length goes out of bounds. */
    1.84 -					if (browsed + replaceLen >= maxLen)
    1.85 -					{
    1.86 -						/* EXAMPLE CASE:
    1.87 -						 * Subject: AABBBCCC
    1.88 -						 * Buffer : 12 bytes
    1.89 -						 * Search : BBB
    1.90 -						 * Replace: DDDDDDDDDD
    1.91 -						 * OUTPUT : AADDDDDDDDD
    1.92 -						 * POSITION:           ^
    1.93 -						 */
    1.94 -						/* If it does, we'll just bound the length and do a strcpy. */
    1.95 -						replaceLen = maxLen - browsed;
    1.96 -						/* Note, we add one to the final result for the null terminator */
    1.97 -						strncopy(ptr, replace, replaceLen+1);
    1.98 -					}
    1.99 -					else
   1.100 -					{
   1.101 -						/* EXAMPLE CASE:
   1.102 -						 * Subject: AABBBCCC
   1.103 -						 * Buffer : 12 bytes
   1.104 -						 * Search : BBB
   1.105 -						 * Replace: DDDDDDD
   1.106 -						 * OUTPUT : AADDDDDDDCC
   1.107 -						 * POSITION:         ^
   1.108 -						 */
   1.109 -						/* We're going to have some bytes left over... */
   1.110 -						size_t origBytesToCopy = (textLen - (browsed + searchLen)) + 1;
   1.111 -						size_t realBytesToCopy = (maxLen - (browsed + replaceLen)) + 1;
   1.112 -						char *moveFrom = ptr + searchLen + (origBytesToCopy - realBytesToCopy);
   1.113 -						char *moveTo = ptr + replaceLen;
   1.114 -
   1.115 -						/* First, move our old data out of the way. */
   1.116 -						memmove(moveTo, moveFrom, realBytesToCopy);
   1.117 -
   1.118 -						/* Now, do our replacement. */
   1.119 -						memcpy(ptr, replace, replaceLen);
   1.120 -					}
   1.121 -				}
   1.122 -				else
   1.123 -				{
   1.124 -					/* EXAMPLE CASE:
   1.125 -					 * Subject: AABBBCCC
   1.126 -					 * Buffer : 12 bytes
   1.127 -					 * Search : BBB
   1.128 -					 * Replace: DDDD
   1.129 -					 * OUTPUT : AADDDDCCC
   1.130 -					 * POSITION:      ^
   1.131 -					 */
   1.132 -					/* Yes, we have enough space.  Do a normal move operation. */
   1.133 -					char *moveFrom = ptr + searchLen;
   1.134 -					char *moveTo = ptr + replaceLen;
   1.135 -
   1.136 -					/* First move our old data out of the way. */
   1.137 -					size_t bytesToCopy = (textLen - (browsed + searchLen)) + 1;
   1.138 -					memmove(moveTo, moveFrom, bytesToCopy);
   1.139 -
   1.140 -					/* Now do our replacement. */
   1.141 -					memcpy(ptr, replace, replaceLen);
   1.142 -				}
   1.143 -			}
   1.144 -			else if (replaceLen < searchLen)
   1.145 -			{
   1.146 -				/* EXAMPLE CASE:
   1.147 -				 * Subject: AABBBCCC
   1.148 -				 * Buffer : 12 bytes
   1.149 -				 * Search : BBB
   1.150 -				 * Replace: D
   1.151 -				 * OUTPUT : AADCCC
   1.152 -				 * POSITION:   ^
   1.153 -				 */
   1.154 -				/* If the replacement does not grow the string length, we do not
   1.155 -				 * need to do any fancy checking at all.  Yay!
   1.156 -				 */
   1.157 -				char *moveFrom = ptr + searchLen;		/* Start after the search pointer */
   1.158 -				char *moveTo = ptr + replaceLen;		/* Copy to where the replacement ends */
   1.159 -
   1.160 -				/* Copy our replacement in, if any */
   1.161 -				if (replaceLen)
   1.162 -				{
   1.163 -					memcpy(ptr, replace, replaceLen);
   1.164 -				}
   1.165 -
   1.166 -				/* Figure out how many bytes to move down, including null terminator */
   1.167 -				size_t bytesToCopy = (textLen - (browsed + searchLen)) + 1;
   1.168 -
   1.169 -				/* Move the rest of the string down */
   1.170 -				memmove(moveTo, moveFrom, bytesToCopy);
   1.171 -			}
   1.172 -			else
   1.173 -			{
   1.174 -				/* EXAMPLE CASE:
   1.175 -				 * Subject: AABBBCCC
   1.176 -				 * Buffer : 12 bytes
   1.177 -				 * Search : BBB
   1.178 -				 * Replace: DDD
   1.179 -				 * OUTPUT : AADDDCCC
   1.180 -				 * POSITION:     ^
   1.181 -				 */
   1.182 -				/* We don't have to move anything around, just do a straight copy */
   1.183 -				memcpy(ptr, replace, replaceLen);
   1.184 -			}
   1.185 -
   1.186 -			return ptr + replaceLen;
   1.187 -		}
   1.188 -		ptr++;
   1.189 -		browsed++;
   1.190 -	}
   1.191 -
   1.192 -	return NULL;
   1.193 -}
   1.194 -
   1.195  char *UTIL_TrimWhitespace(char *str, size_t &len)
   1.196  {
   1.197  	char *end = str + len - 1;