diff options
author | Alexander Tkachev | 2016-07-06 15:37:13 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 627bda9d82c178641e5b5253379c21ce556eb3c2 (patch) | |
tree | fd82f68f1380450bee225f6b0adf62eff2894f15 /common | |
parent | e47b6c04b30565b1db238c2784e562f3f4472d70 (diff) | |
download | scummvm-rg350-627bda9d82c178641e5b5253379c21ce556eb3c2.tar.gz scummvm-rg350-627bda9d82c178641e5b5253379c21ce556eb3c2.tar.bz2 scummvm-rg350-627bda9d82c178641e5b5253379c21ce556eb3c2.zip |
COMMON: Add replace(String, String, String)
Searches for a substring in the string and replaces it with the other
string.
Diffstat (limited to 'common')
-rw-r--r-- | common/str.cpp | 9 | ||||
-rw-r--r-- | common/str.h | 9 |
2 files changed, 18 insertions, 0 deletions
diff --git a/common/str.cpp b/common/str.cpp index 326b4a80d1..90bd539790 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -838,6 +838,15 @@ bool matchString(const char *str, const char *pat, bool ignoreCase, bool pathMod } } +void replace(Common::String &source, const Common::String &what, const Common::String &with) { + const char *cstr = source.c_str(); + const char *position = strstr(cstr, what.c_str()); + if (position) { + uint32 index = position - cstr; + source.replace(index, what.size(), with); + } +} + String tag2string(uint32 tag) { char str[5]; str[0] = (char)(tag >> 24); diff --git a/common/str.h b/common/str.h index 89b832d57e..1293d89508 100644 --- a/common/str.h +++ b/common/str.h @@ -390,6 +390,15 @@ String normalizePath(const String &path, const char sep); */ bool matchString(const char *str, const char *pat, bool ignoreCase = false, bool pathMode = false); +/** + * Function which replaces substring with the other. It happens in place. + * If there is no substring found, original string is not changed. + * + * @param source String to search and replace substring in. + * @param what Substring to replace. + * @param with String to replace with. + */ +void replace(Common::String &source, const Common::String &what, const Common::String &with); /** * Take a 32 bit value and turn it into a four character string, where each of |