diff options
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/slang-string.cpp | 47 | ||||
| -rw-r--r-- | source/core/slang-string.h | 17 |
2 files changed, 52 insertions, 12 deletions
diff --git a/source/core/slang-string.cpp b/source/core/slang-string.cpp index df711218c..bcf5853d5 100644 --- a/source/core/slang-string.cpp +++ b/source/core/slang-string.cpp @@ -439,4 +439,51 @@ namespace Slang sprintf_s(data, kCount, format, val); m_buffer->length += strnlen_s(data, kCount); } + + // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! UnownedStringSlice !!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + Index UnownedStringSlice::indexOf(char c) const + { + const Index size = Index(m_end - m_begin); + for (Index i = 0; i < size; ++i) + { + if (m_begin[i] == c) + { + return i; + } + } + return -1; + } + + Index UnownedStringSlice::indexOf(const UnownedStringSlice& in) const + { + const Index len = getLength(); + const Index inLen = in.getLength(); + if (inLen > len) + { + return -1; + } + + const char* inChars = in.m_begin; + switch (inLen) + { + case 0: return 0; + case 1: return indexOf(inChars[0]); + default: break; + } + + const char* chars = m_begin; + const char firstChar = inChars[0]; + + for (Int i = 0; i < len - inLen; ++i) + { + if (chars[i] == firstChar && in == UnownedStringSlice(chars + i, inLen)) + { + return i; + } + } + + return -1; + } + } diff --git a/source/core/slang-string.h b/source/core/slang-string.h index 10bc76048..5eb113dc1 100644 --- a/source/core/slang-string.h +++ b/source/core/slang-string.h @@ -104,18 +104,11 @@ namespace Slang return Index(m_end - m_begin); } - Index indexOf(char c) const - { - const Index size = int(m_end - m_begin); - for (Index i = 0; i < size; ++i) - { - if (m_begin[i] == c) - { - return i; - } - } - return -1; - } + /// Finds first index of char 'c'. If not found returns -1. + Index indexOf(char c) const; + /// Find first index of slice. If not found returns -1 + Index indexOf(const UnownedStringSlice& slice) const; + Index lastIndexOf(char c) const { const Index size = Index(m_end - m_begin); |
