Skip to content

Instantly share code, notes, and snippets.

@yhirose
Last active November 29, 2015 16:04
Show Gist options
  • Select an option

  • Save yhirose/b8ebc8205046f5c81c11 to your computer and use it in GitHub Desktop.

Select an option

Save yhirose/b8ebc8205046f5c81c11 to your computer and use it in GitHub Desktop.
replace_all
template <typename T>
std::basic_string<T> replace_all(const std::basic_string<T>& str, const T* from, const T* to)
{
std::basic_string<T> ret;
ret.reserve(str.length());
size_t from_len = 0;
while (from[from_len]) {
from_len++;
}
size_t start_pos = 0, pos;
while ((pos = str.find(from, start_pos)) != std::basic_string<T>::npos) {
ret += str.substr(start_pos, pos - start_pos);
ret += to;
pos += from_len;
start_pos = pos;
}
ret += str.substr(start_pos);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment