Browse Source

Added find() functions

release/0.19
JustinAJ 10 years ago
parent
commit
3d981d9996
  1. 4
      Jupiter/Readable_String.h
  2. 24
      Jupiter/Readable_String_Imp.h

4
Jupiter/Readable_String.h

@ -75,9 +75,11 @@ namespace Jupiter
* @brief Returns the index of the first element in the string with the specified value.
*
* @param value Value of the element to search for.
* @param index Index of the match to return (i.e: 0 returns the first match).
* @return The index of an element if one is found, INVALID_INDEX otherwise.
*/
size_t find(const T &value) const;
size_t find(const T &value, size_t index = 0) const;
size_t find(const Readable_String<T> &in) const;
/**
* @brief Compares another string against the String.

24
Jupiter/Readable_String_Imp.h

@ -42,9 +42,29 @@ template<typename T> bool Jupiter::Readable_String<T>::contains(const T &value)
// find
template<typename T> size_t Jupiter::Readable_String<T>::find(const T &value) const
template<typename T> size_t Jupiter::Readable_String<T>::find(const T &value, size_t index) const
{
for (size_t i = 0; i != this->size(); i++) if (this->get(i) == value) return i;
for (size_t i = 0; i != this->size(); i++)
{
if (this->get(i) == value)
{
if (index == 0) return i;
else index--;
}
}
return Jupiter::INVALID_INDEX;
}
template<typename T> size_t Jupiter::Readable_String<T>::find(const Jupiter::Readable_String<T> &in) const
{
if (in.size() > this->size()) return Jupiter::INVALID_INDEX;
size_t j;
for (size_t i = 0; i != this->size() - in.size() + 1; i++)
{
j = 0;
while (this->get(i + j) == in.get(j))
if (++j == in.size()) return i;
}
return Jupiter::INVALID_INDEX;
}

Loading…
Cancel
Save