Function rcutils_repl_str
Defined in File repl_str.h
Function Documentation
-
char *rcutils_repl_str(const char *str, const char *from, const char *to, const rcutils_allocator_t *allocator)
Replace all the occurrences of one string for another in the given string.
Documentation copied from the source with minor tweaks:
Description:
Replaces in the string
str
all the occurrences of the source stringfrom
with the destination stringto
. The lengths of the stringsfrom
andto
may differ. The stringto
may be of any length, but the stringfrom
must be of non-zero length - the penalty for providing an empty string for thefrom
parameter is an infinite loop. In addition, none of the three parameters may be NULL.Returns:
The post-replacement string, or NULL if memory for the new string could not be allocated. Does not modify the original string. The memory for the returned post-replacement string may be deallocated with given allocator’s deallocate function when it is no longer required.
Performance:
In case you are curious enough to want to compare this implementation with alternative implementations, you are welcome to compile and run the code in the benchmarking file, replacebench.c. In that file, the above function is included as
replace_cache
, and the functions originally published on this page asreplace_str2
andreplace_str
are included asreplace_opt2
andreplace_opt
. Code/functions are included from the comp.lang.c thread, how to replace a substring in a string using C?, from answers to the stackoverflow question, What is the function to replace string in C?, and also from private correspondence. See the comments top of file for instructions on compiling and running it.In most scenarios, the fastest replacement function, by around 10-20%, is Albert Chan’s
replace_smart
, followed by the above function:repl_str
akareplace_cache
. There are some scenarios, though, whererepl_str
is faster thanreplace_smart
, sometimes by up to 200%. These scenarios involve long strings with few matches. Why, if Albert’s function is generally slightly faster than the aboverepl_str
function, is it not the focus of this page? Because it generally uses much more memory thanrepl_str
.The third fastest implementation is typically
replace_str2
akareplace_opt2
. For longer strings in the case in which the lengths of the “from” and “to” strings differ,repl_str
akareplace_cache
beats it by margins of up to about 80%. For smaller strings, and in the case where the lengths of the “from” and “to” strings are identical,replace_str2
akareplace_opt2
is faster, by a maximum margin of about 35%, sometimes in those scenarios beatingreplace_smart
too. Some of the other functions are also faster for smaller strings. The even-match point betweenreplace_str2
andrepl_str
(assuming “from” and “to” string lengths differ) depends on how far into the string the final match occurs, how many matches there are, and the comparative lengths of the old and “to” strings, but roughly it occurs for strings of 700-800 bytes in length.This analysis is based on compiling with GCC and testing on a 64-bit Intel platform running Linux, however brief testing with Microsoft Visual C++ 2010 Express (scroll down to “Additional information” at that link) on Windows 7 seemed to produce similar results.
Here continues additional documentation added by OSRF.
The allocator must not be NULL.
- Parameters:
str – [in] string to have substrings found and replaced within
from – [in] string to match for replacement
to – [in] string to replace matched strings with
allocator – [in] structure defining functions to be used for allocation
- Returns:
duplicated
str
with all matches offrom
replaced withto
.