From ba4ff8bf10c7e5b16056a932d2c466a0051bdb91 Mon Sep 17 00:00:00 2001 From: JustinAJ Date: Fri, 13 Jun 2014 00:37:24 -0400 Subject: [PATCH] Expanded and simplified equals() and equalsi(). --- Jupiter/Readable_String.h | 2 + Jupiter/Readable_String_Imp.h | 84 +++++++++++++++-------------------- 2 files changed, 37 insertions(+), 49 deletions(-) diff --git a/Jupiter/Readable_String.h b/Jupiter/Readable_String.h index 28df9c5..96c39ac 100644 --- a/Jupiter/Readable_String.h +++ b/Jupiter/Readable_String.h @@ -129,6 +129,7 @@ namespace Jupiter */ bool equals(const Readable_String &in) const; bool equals(const std::basic_string &in) const; + bool equals(const T *in, size_t len) const; bool equals(const T *in) const; bool equals(const T &in) const; bool equals(const std::nullptr_t) const; @@ -142,6 +143,7 @@ namespace Jupiter */ bool equalsi(const Readable_String &in) const; bool equalsi(const std::basic_string &in) const; + bool equalsi(const T *in, size_t len) const; bool equalsi(const T *in) const; bool equalsi(const T &in) const; bool equalsi(const std::nullptr_t) const; diff --git a/Jupiter/Readable_String_Imp.h b/Jupiter/Readable_String_Imp.h index 9b59afd..966de52 100644 --- a/Jupiter/Readable_String_Imp.h +++ b/Jupiter/Readable_String_Imp.h @@ -233,34 +233,30 @@ template int Jupiter::Readable_String::compare(const std::nullptr template bool Jupiter::Readable_String::equals(const Jupiter::Readable_String &in) const { - if (this->size() != in.size()) return false; - - // rewrite to compare multiple bytes at a time. - size_t index = 0; - while (index != this->size()) - { - if (this->get(index) != in.get(index)) return false; - index++; - } - return true; + return this->equals(in.ptr(), in.size()); } template bool Jupiter::Readable_String::equals(const std::basic_string &in) const { - if (this->size() != in.size()) return false; + return this->equals(in.data(), in.size()); +} + +template bool Jupiter::Readable_String::equals(const T *in, size_t len) const +{ + if (this->size() != len) + return false; // rewrite to compare multiple bytes at a time. - size_t index = 0; - while (index != this->size()) - { - if (this->get(index) != in.at(index)) return false; - index++; - } + in += len; + while (len != 0) + if (this->get(--len) != *(--in)) + return false; + return true; } template bool Jupiter::Readable_String::equals(const T *in) const { - if (in == nullptr) return this->size() == 0; + if (in == nullptr) return this->isEmpty(); for (size_t index = 0; index != this->size(); index++) { if (*in == 0 || this->get(index) != *in) return false; @@ -281,52 +277,42 @@ template bool Jupiter::Readable_String::equals(std::nullptr_t) co // equalsi() -template<> bool inline Jupiter::Readable_String::equalsi(const Jupiter::Readable_String &in) const +template bool Jupiter::Readable_String::equalsi(const Jupiter::Readable_String &in) const { - if (this->size() != in.size()) return false; - for (size_t index = 0; index != this->size(); index++) - { - if (toupper(this->get(index)) != toupper(in.get(index))) return false; - } - return true; + return this->equalsi(in.ptr(), in.size()); } -template<> bool inline Jupiter::Readable_String::equalsi(const Jupiter::Readable_String &in) const +template bool Jupiter::Readable_String::equalsi(const std::basic_string &in) const { - if (this->size() != in.size()) return false; - for (size_t index = 0; index != this->size(); index++) - { - if (towupper(this->get(index)) != towupper(in.get(index))) return false; - } - return true; + return this->equalsi(in.data(), in.size()); } -template bool Jupiter::Readable_String::equalsi(const Jupiter::Readable_String &in) const +template<> bool inline Jupiter::Readable_String::equalsi(const char *in, size_t len) const { - return this->equals(in); // Concept of "case" not supported for type. -} + if (this->size() != len) + return false; + + in += len; + while (len != 0) + if (toupper(this->get(--len)) != toupper(*(--in))) + return false; -template<> bool inline Jupiter::Readable_String::equalsi(const std::basic_string &in) const -{ - if (this->size() != in.size()) return false; - for (size_t index = 0; index != this->size(); index++) - { - if (toupper(this->get(index)) != toupper(in.at(index))) return false; - } return true; } -template<> bool inline Jupiter::Readable_String::equalsi(const std::basic_string &in) const +template<> bool inline Jupiter::Readable_String::equalsi(const wchar_t *in, size_t len) const { - if (this->size() != in.size()) return false; - for (size_t index = 0; index != this->size(); index++) - { - if (towupper(this->get(index)) != towupper(in.at(index))) return false; - } + if (this->size() != len) + return false; + + while (len != 0) + if (towupper(this->get(--len)) != towupper(*(--in))) + return false; + return true; } -template bool Jupiter::Readable_String::equalsi(const std::basic_string &in) const +template bool Jupiter::Readable_String::equalsi(const T *in, size_t len) const { return this->equals(in); // Concept of "case" not supported for type. }