summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAttila Molnar <attilamolnar@hush.com>2016-08-22 17:10:41 +0200
committerAttila Molnar <attilamolnar@hush.com>2016-08-22 17:10:41 +0200
commit5378b913a69f0f8f527eff3d77862cb29df3476e (patch)
tree3072d37358ad4ad689647b39198b93445dc245f8 /include
parent78b6ad984b846de7367e3d6042f0779906d5ab70 (diff)
Add stdalgo::string::replace() and replace_all()
Diffstat (limited to 'include')
-rw-r--r--include/stdalgo.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/include/stdalgo.h b/include/stdalgo.h
index 12ec76275..f4465963a 100644
--- a/include/stdalgo.h
+++ b/include/stdalgo.h
@@ -94,6 +94,41 @@ namespace stdalgo
{
return (!strcasecmp(tocstr(str1), tocstr(str2)));
}
+
+ /** Replace first occurrence of a substring ('target') in a string ('str') with another string ('replacement').
+ * @param str String to perform replacement in
+ * @param target String to replace
+ * @param replacement String to put in place of 'target'
+ * @return True if 'target' was replaced with 'replacement', false if it was not found in 'str'.
+ */
+ template<typename CharT, typename Traits, typename Alloc>
+ inline bool replace(std::basic_string<CharT, Traits, Alloc>& str, const std::basic_string<CharT, Traits, Alloc>& target, const std::basic_string<CharT, Traits, Alloc>& replacement)
+ {
+ const typename std::basic_string<CharT, Traits, Alloc>::size_type p = str.find(target);
+ if (p == std::basic_string<CharT, Traits, Alloc>::npos)
+ return false;
+ str.replace(p, target.size(), replacement);
+ return true;
+ }
+
+ /** Replace all occurrences of a string ('target') in a string ('str') with another string ('replacement').
+ * @param str String to perform replacement in
+ * @param target String to replace
+ * @param replacement String to put in place of 'target'
+ */
+ template<typename CharT, typename Traits, typename Alloc>
+ inline void replace_all(std::basic_string<CharT, Traits, Alloc>& str, const std::basic_string<CharT, Traits, Alloc>& target, const std::basic_string<CharT, Traits, Alloc>& replacement)
+ {
+ if (target.empty())
+ return;
+
+ typename std::basic_string<CharT, Traits, Alloc>::size_type p = 0;
+ while ((p = str.find(target, p)) != std::basic_string<CharT, Traits, Alloc>::npos)
+ {
+ str.replace(p, target.size(), replacement);
+ p += replacement.size();
+ }
+ }
}
/**