diff --git a/Jupiter/Readable_String.h b/Jupiter/Readable_String.h index 96c39ac..46e2038 100644 --- a/Jupiter/Readable_String.h +++ b/Jupiter/Readable_String.h @@ -178,6 +178,17 @@ namespace Jupiter */ unsigned int wordCount(const T *whitespace) const; + /** + * @brief Counts the number of occurances for a specific token. + * + * @param token Token to scan for. + * @return Number of occurances of the specified token in the string. + */ + size_t tokenCount(const T &token) const; + size_t tokenCount(const Readable_String &token) const; + size_t tokenCount(const std::basic_string &token) const; + size_t tokenCount(const T *token, size_t tokenLength) const; + /** * @brief Interprets the string as a bool. * diff --git a/Jupiter/Readable_String_Imp.h b/Jupiter/Readable_String_Imp.h index 966de52..25a63e1 100644 --- a/Jupiter/Readable_String_Imp.h +++ b/Jupiter/Readable_String_Imp.h @@ -789,6 +789,50 @@ template unsigned int Jupiter::Readable_String::wordCount(const T return result; } +// tokenCount() + +template size_t Jupiter::Readable_String::tokenCount(const T &token) const +{ + size_t total = 0; + for (size_t i = 0; i != this->size(); i++) + if (this->get(i) == token) + total++; + return total; +} + +template size_t Jupiter::Readable_String::tokenCount(const Jupiter::Readable_String &token) const +{ + return this->tokenCount(token.ptr(), token.size()); +} + +template size_t Jupiter::Readable_String::tokenCount(const std::basic_string &token) const +{ + return this->tokenCount(token.data(), token.size()); +} + +template size_t Jupiter::Readable_String::tokenCount(const T *token, size_t tokenLength) const +{ + if (tokenLength == 0 || tokenLength > this->size()) + return 0; + if (tokenLength == this->size()) + return this->equals(token, tokenLength) ? 1 : 0; + + size_t total = 0; + for (size_t i = 0, j; i != this->size() - tokenLength + 1; i++) + { + j = 0; + while (this->get(i + j) == token[j]) + { + if (++j == tokenLength) + { + total++; + break; + } + } + } + return total; +} + // as template<> bool inline Jupiter::Readable_String::asBool() const diff --git a/Release/Jupiter.lib b/Release/Jupiter.lib index a5d082f..46e0580 100644 Binary files a/Release/Jupiter.lib and b/Release/Jupiter.lib differ