Browse Source

Added calcChecksum() and calcChecksumi() to Readable_String.

release/0.19
JustinAJ 10 years ago
parent
commit
3b0b3a6b75
  1. 21
      Jupiter/Readable_String.h
  2. 41
      Jupiter/Readable_String_Imp.h

21
Jupiter/Readable_String.h

@ -219,6 +219,24 @@ namespace Jupiter
*/
double asDouble() const;
/**
* @brief Sums together all of the elements in the string.
*
* @param T Integral type to return
*
* @return Sum of all the elements in the string
*/
template<typename R = unsigned int> R calcChecksum() const;
/**
* @brief Sums together the uppercase version of all of the elements in the string.
*
* @param T Integral type to return
*
* @return Sum of all the elements in the string
*/
template<typename R = unsigned int> R calcChecksumi() const;
/**
* @brief Outputs the string to a FILE stream.
*
@ -320,6 +338,9 @@ namespace Jupiter
/** Conversion operators */
explicit inline operator std::basic_string<T>() { return std::basic_string<T>(this->ptr(), this->size()); }
private:
template<typename R> R calcChecksumiHelper() const;
};
/** Generic Readable String Type */

41
Jupiter/Readable_String_Imp.h

@ -992,6 +992,47 @@ template<typename T> double Jupiter::Readable_String<T>::asDouble() const
return 0;
}
// calcChecksum
template<typename T> template<typename R> R Jupiter::Readable_String<T>::calcChecksum() const
{
R sum = 0;
size_t index = this->size();
while (index != 0)
sum += this->get(--index);
return sum;
}
// calcChecksumi
template<typename T> template<typename R> R Jupiter::Readable_String<T>::calcChecksumi() const
{
return this->calcChecksumiHelper<R>();
}
template<> template<typename R> R inline Jupiter::Readable_String<char>::calcChecksumiHelper() const
{
R sum = 0;
size_t index = this->size();
while (index != 0)
sum += toupper(this->get(--index));
return sum;
}
template<> template<typename R> R inline Jupiter::Readable_String<wchar_t>::calcChecksumiHelper() const
{
R sum = 0;
size_t index = this->size();
while (index != 0)
sum += towupper(this->get(--index));
return sum;
}
template<typename T> template<typename R> R Jupiter::Readable_String<T>::calcChecksumiHelper() const
{
return this->calcChecksum<R>();
}
// Stream output
template<typename T> size_t Jupiter::Readable_String<T>::print(FILE *out) const

Loading…
Cancel
Save