diff --git a/Jupiter/Readable_String.h b/Jupiter/Readable_String.h index 431ca7a..5459e50 100644 --- a/Jupiter/Readable_String.h +++ b/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 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 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() { return std::basic_string(this->ptr(), this->size()); } + + private: + template R calcChecksumiHelper() const; }; /** Generic Readable String Type */ diff --git a/Jupiter/Readable_String_Imp.h b/Jupiter/Readable_String_Imp.h index 7ede4d3..ecb29db 100644 --- a/Jupiter/Readable_String_Imp.h +++ b/Jupiter/Readable_String_Imp.h @@ -992,6 +992,47 @@ template double Jupiter::Readable_String::asDouble() const return 0; } +// calcChecksum + +template template R Jupiter::Readable_String::calcChecksum() const +{ + R sum = 0; + size_t index = this->size(); + while (index != 0) + sum += this->get(--index); + return sum; +} + +// calcChecksumi + +template template R Jupiter::Readable_String::calcChecksumi() const +{ + return this->calcChecksumiHelper(); +} + +template<> template R inline Jupiter::Readable_String::calcChecksumiHelper() const +{ + R sum = 0; + size_t index = this->size(); + while (index != 0) + sum += toupper(this->get(--index)); + return sum; +} + +template<> template R inline Jupiter::Readable_String::calcChecksumiHelper() const +{ + R sum = 0; + size_t index = this->size(); + while (index != 0) + sum += towupper(this->get(--index)); + return sum; +} + +template template R Jupiter::Readable_String::calcChecksumiHelper() const +{ + return this->calcChecksum(); +} + // Stream output template size_t Jupiter::Readable_String::print(FILE *out) const