summaryrefslogtreecommitdiffstats
path: root/source/core
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2023-10-06 14:03:18 -0700
committerGitHub <noreply@github.com>2023-10-06 14:03:18 -0700
commit17c7163c2ae8fc290e70b43d8700b68ef18b1ee1 (patch)
tree09df040039fb1221810f956bb83871430cbac47f /source/core
parent4547125ce945140dc10542e9606b225dd06159b8 (diff)
Small type system fixes. (#3265)
Diffstat (limited to 'source/core')
-rw-r--r--source/core/slang-string-util.cpp25
-rw-r--r--source/core/slang-string-util.h3
2 files changed, 28 insertions, 0 deletions
diff --git a/source/core/slang-string-util.cpp b/source/core/slang-string-util.cpp
index fb062de76..a6a18d4a0 100644
--- a/source/core/slang-string-util.cpp
+++ b/source/core/slang-string-util.cpp
@@ -392,6 +392,31 @@ ComPtr<ISlangBlob> StringUtil::createStringBlob(const String& string)
return (fromChar == toChar || string.indexOf(fromChar) == Index(-1)) ? string : calcCharReplaced(string.getUnownedSlice(), fromChar, toChar);
}
+String StringUtil::replaceAll(UnownedStringSlice text, UnownedStringSlice subStr, UnownedStringSlice replacement)
+{
+ StringBuilder builder;
+ for (Index i = 0; i < text.getLength();)
+ {
+ if (i + subStr.getLength() >= text.getLength())
+ {
+ builder.append(text.subString(i, text.getLength() - i));
+ break;
+ }
+ if (text.subString(i, subStr.getLength()) == subStr)
+ {
+ builder.append(replacement);
+ i += subStr.getLength();
+ }
+ else
+ {
+ builder.append(text[i]);
+ i++;
+ }
+ }
+ return builder.produceString();
+}
+
+
/* static */void StringUtil::appendStandardLines(const UnownedStringSlice& text, StringBuilder& out)
{
const char* cur = text.begin();
diff --git a/source/core/slang-string-util.h b/source/core/slang-string-util.h
index 4b9a4d5d1..7e5733cbb 100644
--- a/source/core/slang-string-util.h
+++ b/source/core/slang-string-util.h
@@ -87,6 +87,9 @@ struct StringUtil
static String calcCharReplaced(const UnownedStringSlice& slice, char fromChar, char toChar);
static String calcCharReplaced(const String& string, char fromChar, char toChar);
+ /// Replaces all occurrances of subStr with replacement.
+ static String replaceAll(UnownedStringSlice text, UnownedStringSlice subStr, UnownedStringSlice replacement);
+
/// Create a blob from a string
static ComPtr<ISlangBlob> createStringBlob(const String& string);