Browse Source

Added case-insensitive version of find() named findi().

release/0.19
JustinAJ 10 years ago
parent
commit
4fb5035e2a
  1. 11
      Jupiter/Readable_String.h
  2. 73
      Jupiter/Readable_String_Imp.h

11
Jupiter/Readable_String.h

@ -88,6 +88,17 @@ namespace Jupiter
size_t find(const T &value, size_t index = 0) const; size_t find(const T &value, size_t index = 0) const;
size_t find(const Readable_String<T> &in) const; size_t find(const Readable_String<T> &in) const;
/**
* @brief Returns the index of the first element in the string with the specified value.
* Note: Case insensitive. Returns false for any type other than char and wchar_t.
*
* @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 findi(const T &value, size_t index = 0) const;
size_t findi(const Readable_String<T> &in) const;
/** /**
* @brief Returns the number of elements of the string which match the input string. * @brief Returns the number of elements of the string which match the input string.
* *

73
Jupiter/Readable_String_Imp.h

@ -68,8 +68,7 @@ template<typename T> size_t Jupiter::Readable_String<T>::find(const Jupiter::Rea
if (in.size() == this->size()) return this->equals(in) ? 0 : Jupiter::INVALID_INDEX; if (in.size() == this->size()) return this->equals(in) ? 0 : Jupiter::INVALID_INDEX;
if (in.size() == 0) return 0; if (in.size() == 0) return 0;
size_t j; for (size_t i = 0, j; i != this->size() - in.size() + 1; i++)
for (size_t i = 0; i != this->size() - in.size() + 1; i++)
{ {
j = 0; j = 0;
while (this->get(i + j) == in.get(j)) while (this->get(i + j) == in.get(j))
@ -78,6 +77,76 @@ template<typename T> size_t Jupiter::Readable_String<T>::find(const Jupiter::Rea
return Jupiter::INVALID_INDEX; return Jupiter::INVALID_INDEX;
} }
// findi
template<> size_t inline Jupiter::Readable_String<char>::findi(const char &value, size_t index) const
{
const char upperValue = (const char) toupper(value);
for (size_t i = 0; i != this->size(); i++)
{
if (toupper(this->get(i)) == upperValue)
{
if (index == 0) return i;
else index--;
}
}
return Jupiter::INVALID_INDEX;
}
template<> size_t inline Jupiter::Readable_String<wchar_t>::findi(const wchar_t &value, size_t index) const
{
const wchar_t upperValue = towupper(value);
for (size_t i = 0; i != this->size(); i++)
{
if (towupper(this->get(i)) == upperValue)
{
if (index == 0) return i;
else index--;
}
}
return Jupiter::INVALID_INDEX;
}
template<typename T> size_t Jupiter::Readable_String<T>::findi(const T &value, size_t index) const
{
return Jupiter::INVALID_INDEX;
}
template<> size_t inline Jupiter::Readable_String<char>::findi(const Jupiter::Readable_String<char> &in) const
{
if (in.size() > this->size()) return Jupiter::INVALID_INDEX;
if (in.size() == this->size()) return this->equalsi(in) ? 0 : Jupiter::INVALID_INDEX;
if (in.size() == 0) return 0;
for (size_t i = 0, j; i != this->size() - in.size() + 1; i++)
{
j = 0;
while (toupper(this->get(i + j)) == toupper(in.get(j)))
if (++j == in.size()) return i;
}
return Jupiter::INVALID_INDEX;
}
template<> size_t inline Jupiter::Readable_String<wchar_t>::findi(const Jupiter::Readable_String<wchar_t> &in) const
{
if (in.size() > this->size()) return Jupiter::INVALID_INDEX;
if (in.size() == this->size()) return this->equalsi(in) ? 0 : Jupiter::INVALID_INDEX;
if (in.size() == 0) return 0;
for (size_t i = 0, j; i != this->size() - in.size() + 1; i++)
{
j = 0;
while (towupper(this->get(i + j)) == towupper(in.get(j)))
if (++j == in.size()) return i;
}
return Jupiter::INVALID_INDEX;
}
template<typename T> size_t Jupiter::Readable_String<T>::findi(const Jupiter::Readable_String<T> &) const
{
return Jupiter::INVALID_INDEX;
}
// span() // span()
template<typename T> size_t Jupiter::Readable_String<T>::span(const Jupiter::Readable_String<T> &in) const template<typename T> size_t Jupiter::Readable_String<T>::span(const Jupiter::Readable_String<T> &in) const

Loading…
Cancel
Save