Signature
stock replace_all(string[], len, const what[], const with[]){ new pos = 0; if((pos = contain(string, what)) == -1) { return 0; } new total = 0; new with_len = strlen(with); new diff = strlen(what) - with_len; new total_len = strlen(string); new temp_pos = 0; while(replace(string[pos], len - pos, what, with) != 0) { total++; /* jump to position after replacement */ pos += with_len; /* update cached length of string */ total_len -= diff; /* will the next call be operating on the last character? */ if(pos >= total_len) { break; } /* find the next position from our offset */ temp_pos = contain(string[pos], what); /* if it's invalid, we're done */ if(temp_pos == -1) { break; } /* otherwise, reposition and update counters */ pos += temp_pos; } return total;}
Description
Replaces a contained string iteratively.
Notes
- Consider using replace_string() instead.
-
This ensures that no infinite replacements will take place by
intelligently moving to the next string position each iteration.
Parameters
- string — String to perform search and replacements on.
- len — Maximum length of the string buffer.
- what — String to search for.
- with — String to replace the search string with.
Returns
Number of replacements on success, otherwise 0.