summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/core/core.vcxproj2
-rw-r--r--source/core/list.h2
-rw-r--r--source/core/slang-string-util.cpp29
-rw-r--r--source/core/slang-string-util.h17
-rw-r--r--source/core/slang-string.h29
5 files changed, 75 insertions, 4 deletions
diff --git a/source/core/core.vcxproj b/source/core/core.vcxproj
index 77e91b32f..803c1ab12 100644
--- a/source/core/core.vcxproj
+++ b/source/core/core.vcxproj
@@ -36,6 +36,7 @@
<ClInclude Include="slang-io.h" />
<ClInclude Include="slang-math.h" />
<ClInclude Include="slang-result.h" />
+ <ClInclude Include="slang-string-util.h" />
<ClInclude Include="slang-string.h" />
<ClInclude Include="smart-pointer.h" />
<ClInclude Include="stream.h" />
@@ -47,6 +48,7 @@
<ClCompile Include="platform.cpp" />
<ClCompile Include="slang-free-list.cpp" />
<ClCompile Include="slang-io.cpp" />
+ <ClCompile Include="slang-string-util.cpp" />
<ClCompile Include="slang-string.cpp" />
<ClCompile Include="stream.cpp" />
<ClCompile Include="text-io.cpp" />
diff --git a/source/core/list.h b/source/core/list.h
index 6c4a04fc7..3a325955a 100644
--- a/source/core/list.h
+++ b/source/core/list.h
@@ -504,7 +504,7 @@ namespace Slang
template<typename T2>
UInt IndexOf(const T2 & val) const
{
- for (int i = 0; i < _count; i++)
+ for (UInt i = 0; i < _count; i++)
{
if (buffer[i] == val)
return i;
diff --git a/source/core/slang-string-util.cpp b/source/core/slang-string-util.cpp
new file mode 100644
index 000000000..c60ad9683
--- /dev/null
+++ b/source/core/slang-string-util.cpp
@@ -0,0 +1,29 @@
+#include "slang-string-util.h"
+
+namespace Slang {
+
+/* static */void StringUtil::split(const UnownedStringSlice& in, char splitChar, List<UnownedStringSlice>& slicesOut)
+{
+ slicesOut.Clear();
+
+ const char* start = in.begin();
+ const char* end = in.end();
+
+ while (start < end)
+ {
+ // Move cur so it's either at the end or at next split character
+ const char* cur = start;
+ while (cur < end && *cur != splitChar)
+ {
+ cur++;
+ }
+
+ // Add to output
+ slicesOut.Add(UnownedStringSlice(start, cur));
+
+ // Skip the split character, if at end we are okay anyway
+ start = cur + 1;
+ }
+}
+
+} // namespace Slang
diff --git a/source/core/slang-string-util.h b/source/core/slang-string-util.h
new file mode 100644
index 000000000..bae576370
--- /dev/null
+++ b/source/core/slang-string-util.h
@@ -0,0 +1,17 @@
+#ifndef SLANG_STRING_UTIL_H
+#define SLANG_STRING_UTIL_H
+
+#include "slang-string.h"
+#include "list.h"
+
+namespace Slang {
+
+struct StringUtil
+{
+ static void split(const UnownedStringSlice& in, char splitChar, List<UnownedStringSlice>& slicesOut);
+};
+
+} // namespace Slang
+
+
+#endif // SLANG_STRING_UTIL_H
diff --git a/source/core/slang-string.h b/source/core/slang-string.h
index 7d8d3eb81..9f18554f6 100644
--- a/source/core/slang-string.h
+++ b/source/core/slang-string.h
@@ -135,10 +135,14 @@ namespace Slang
{
public:
UnownedStringSlice()
- : beginData(0)
+ : beginData(nullptr)
, endData(0)
{}
+ explicit UnownedStringSlice(char const* a):
+ beginData(a),
+ endData(a + strlen(a))
+ {}
UnownedStringSlice(char const* b, char const* e)
: beginData(b)
, endData(e)
@@ -159,13 +163,32 @@ namespace Slang
return endData - beginData;
}
- bool operator==(UnownedStringSlice const& other)
+ int indexOf(char c) const
+ {
+ const int size = int(endData - beginData);
+ for (int i = 0; i < size; ++i)
+ {
+ if (beginData[i] == c)
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ const char& operator[](int i) const
+ {
+ assert(i >= 0 && i < int(endData - beginData));
+ return beginData[i];
+ }
+
+ bool operator==(UnownedStringSlice const& other) const
{
return size() == other.size()
&& memcmp(begin(), other.begin(), size()) == 0;
}
- bool operator==(char const* str)
+ bool operator==(char const* str) const
{
return (*this) == UnownedStringSlice(str, str + strlen(str));
}