From 3d981d9996b307cb4721aa47a8f57602395e1e52 Mon Sep 17 00:00:00 2001 From: JustinAJ Date: Tue, 3 Jun 2014 20:58:45 -0400 Subject: [PATCH] Added find() functions --- Jupiter/Readable_String.h | 4 +++- Jupiter/Readable_String_Imp.h | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Jupiter/Readable_String.h b/Jupiter/Readable_String.h index fbc2d30..ced8b70 100644 --- a/Jupiter/Readable_String.h +++ b/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 &in) const; /** * @brief Compares another string against the String. diff --git a/Jupiter/Readable_String_Imp.h b/Jupiter/Readable_String_Imp.h index 876d439..3dde809 100644 --- a/Jupiter/Readable_String_Imp.h +++ b/Jupiter/Readable_String_Imp.h @@ -42,9 +42,29 @@ template bool Jupiter::Readable_String::contains(const T &value) // find -template size_t Jupiter::Readable_String::find(const T &value) const +template size_t Jupiter::Readable_String::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 size_t Jupiter::Readable_String::find(const Jupiter::Readable_String &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; }