From 3b0b3a6b75dd5448b11139a2ac09f43f4a15587f Mon Sep 17 00:00:00 2001 From: JustinAJ Date: Sat, 18 Oct 2014 07:16:35 -0400 Subject: [PATCH] Added calcChecksum() and calcChecksumi() to Readable_String. --- Jupiter/Readable_String.h | 21 ++++++++++++++++++ Jupiter/Readable_String_Imp.h | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) 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