Browse Source

Fixed a bug in tokenCount().

release/0.19
JustinAJ 11 years ago
parent
commit
69936d32b2
  1. 4
      Jupiter/Readable_String.h
  2. 18
      Jupiter/Readable_String_Imp.h
  3. BIN
      Release/Jupiter.lib

4
Jupiter/Readable_String.h

@ -179,10 +179,10 @@ namespace Jupiter
unsigned int wordCount(const T *whitespace) const; unsigned int wordCount(const T *whitespace) const;
/** /**
* @brief Counts the number of occurances for a specific token. * @brief Counts the number of token-deliminated strings in the string.
* *
* @param token Token to scan for. * @param token Token to scan for.
* @return Number of occurances of the specified token in the string. * @return Number of token-deliminated strings in the string.
*/ */
size_t tokenCount(const T &token) const; size_t tokenCount(const T &token) const;
size_t tokenCount(const Readable_String<T> &token) const; size_t tokenCount(const Readable_String<T> &token) const;

18
Jupiter/Readable_String_Imp.h

@ -793,7 +793,10 @@ template<typename T> unsigned int Jupiter::Readable_String<T>::wordCount(const T
template<typename T> size_t Jupiter::Readable_String<T>::tokenCount(const T &token) const template<typename T> size_t Jupiter::Readable_String<T>::tokenCount(const T &token) const
{ {
size_t total = 0; if (this->size() == 0)
return 0;
size_t total = 1;
for (size_t i = 0; i != this->size(); i++) for (size_t i = 0; i != this->size(); i++)
if (this->get(i) == token) if (this->get(i) == token)
total++; total++;
@ -812,23 +815,28 @@ template<typename T> size_t Jupiter::Readable_String<T>::tokenCount(const std::b
template<typename T> size_t Jupiter::Readable_String<T>::tokenCount(const T *token, size_t tokenLength) const template<typename T> size_t Jupiter::Readable_String<T>::tokenCount(const T *token, size_t tokenLength) const
{ {
if (tokenLength == 0 || tokenLength > this->size())
return 0;
if (tokenLength == 1) if (tokenLength == 1)
return this->tokenCount(*token); return this->tokenCount(*token);
if (tokenLength == 0 || tokenLength > this->size())
return 0;
if (tokenLength == this->size()) if (tokenLength == this->size())
return this->equals(token, tokenLength) ? 1 : 0; return this->equals(token, tokenLength) ? 1 : 0;
size_t total = 0; size_t total = 1;
for (size_t i = 0, j; i != this->size() - tokenLength + 1; i++) for (size_t i = 0, j; i != this->size() - tokenLength + 1; i++)
{ {
Jupiter_Readable_String_tokenCount_skip_increment:
j = 0; j = 0;
while (this->get(i + j) == token[j]) while (this->get(i + j) == token[j])
{ {
if (++j == tokenLength) if (++j == tokenLength)
{ {
i += j;
total++; total++;
break; if (i >= this->size() - tokenLength + 1)
return total;
goto Jupiter_Readable_String_tokenCount_skip_increment;
} }
} }
} }

BIN
Release/Jupiter.lib

Binary file not shown.
Loading…
Cancel
Save