Add is_word_at helper methods to string.

This commit is contained in:
Relintai 2021-11-21 12:32:02 +01:00
parent 35fd8d7894
commit 78f09b5fab
2 changed files with 42 additions and 0 deletions

View File

@ -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);

View File

@ -33,6 +33,9 @@ public:
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);
void replace(const String &find_str, const String &with, const int count);