From 78f09b5fabf9df33c901b6c67ce6dadda95b08fc Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 21 Nov 2021 12:32:02 +0100 Subject: [PATCH] Add is_word_at helper methods to string. --- core/string.cpp | 39 +++++++++++++++++++++++++++++++++++++++ core/string.h | 3 +++ 2 files changed, 42 insertions(+) diff --git a/core/string.cpp b/core/string.cpp index f6a65be..0335ab5 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -207,6 +207,45 @@ bool String::contains(const String &val) const { return find(val) != -1; } +bool String::is_word_at(const int index, const char *str) const { + ERR_FAIL_INDEX_V(index, _size, false); + + int i = 0; + + while (str[i] != '\0') { + int iind = index + i; + + if (iind >= _size) { + return false; + } + + if (_data[iind] != str[i]) { + return false; + } + + ++i; + } + + return true; +} +bool String::is_word_at(const int index, const String &val) const { + ERR_FAIL_INDEX_V(index, _size, false); + + if (index + val.size() >= _size) { + return false; + } + + for (int i = 0; i < val.size(); ++i) { + int iind = index + i; + + if (_data[iind] != val[i]) { + return false; + } + } + + return true; +} + void String::replace_from(const int start_index, const int length, const String &with) { ERR_FAIL_INDEX(start_index, _size); diff --git a/core/string.h b/core/string.h index 43d145d..081aac9 100644 --- a/core/string.h +++ b/core/string.h @@ -32,6 +32,9 @@ public: String substr_index(const int start_index, const int end_index) const; bool contains(const char val) const; bool contains(const String &val) const; + + bool is_word_at(const int index, const char* str) const; + bool is_word_at(const int index, const String &val) const; void replace_from(const int start_index, const int length, const String &with); void replace(const String &find_str, const String &with);