diff --git a/Jupiter/CString.h b/Jupiter/CString.h index 26623f4..149e6b9 100644 --- a/Jupiter/CString.h +++ b/Jupiter/CString.h @@ -60,7 +60,7 @@ namespace Jupiter * @param value Value of the element to remove. * @return True if an element was removed, false otherwise. */ - bool remove(T &value); + bool remove(const T &value); /** Assignment Operators */ inline CString_Type &operator=(const CString_Type &right) { this->set(right); return *this; }; diff --git a/Jupiter/CString_Imp.h b/Jupiter/CString_Imp.h index d34e898..bd9d83e 100644 --- a/Jupiter/CString_Imp.h +++ b/Jupiter/CString_Imp.h @@ -50,37 +50,16 @@ template const T *Jupiter::CString_Type::c_str() const template size_t Jupiter::CString_Type::truncate(size_t n) { - if (n >= Jupiter::String_Type::length) - { - Jupiter::String_Type::str = Jupiter::Shift_String_Type::base; - *Jupiter::String_Type::str = 0; - Jupiter::String_Type::length = 0; - return 0; - } - Jupiter::String_Type::length -= n; - Jupiter::String_Type::str[Jupiter::String_Type::length] = 0; + Jupiter::String_Type::str[Jupiter::String_Type::truncate(n)] = 0; return Jupiter::String_Type::length; } -template bool Jupiter::CString_Type::remove(T &value) +template bool Jupiter::CString_Type::remove(const T &value) { - for (unsigned int i = 0; i < Jupiter::String_Type::length; i++) + if (Jupiter::Shift_String_Type::remove(value)) { - if (Jupiter::String_Type::str[i] == value) - { - if (i == Jupiter::String_Type::length - 1) Jupiter::CString_Type::truncate(1); - else if (i == 0) - { - if (Jupiter::String_Type::length == 1) Jupiter::CString_Type::truncate(1); - else Jupiter::CString_Type::shiftRight(1); - } - else - { - Jupiter::strcpy(Jupiter::String_Type::str + i, Jupiter::String_Type::str + i + 1); - Jupiter::String_Type::length--; - } - return true; - } + Jupiter::String_Type::str[Jupiter::String_Type::length] = 0; + return true; } return false; } diff --git a/Jupiter/Shift_String.h b/Jupiter/Shift_String.h index 5f28bd7..2314936 100644 --- a/Jupiter/Shift_String.h +++ b/Jupiter/Shift_String.h @@ -54,6 +54,14 @@ namespace Jupiter */ size_t shiftRight(size_t length); + /** + * @brief Removes the first instance of an element from the string. + * + * @param value Value of the element to remove. + * @return True if an element was removed, false otherwise. + */ + virtual bool remove(const T &value); + protected: T *base; /** Base pointer for the underlying String's memory allocation */ }; diff --git a/Jupiter/Shift_String_Imp.h b/Jupiter/Shift_String_Imp.h index 958cb86..e80fda3 100644 --- a/Jupiter/Shift_String_Imp.h +++ b/Jupiter/Shift_String_Imp.h @@ -41,4 +41,16 @@ template size_t Jupiter::Shift_String_Type::shiftRight(size_t len return len; } +template bool Jupiter::Shift_String_Type::remove(const T &value) +{ + if (Jupiter::String_Type::length == 0) return false; + if (*Jupiter::String_Type::str == value) + { + if (Jupiter::String_Type::length == 1) this->truncate(1); + else this->shiftRight(1); + return true; + } + return Jupiter::String_Type::remove(value); +} + #endif // _SHIFT_STRING_IMP_H_HEADER \ No newline at end of file diff --git a/Jupiter/String_Type.h b/Jupiter/String_Type.h index 1ad5ba6..a7de4d4 100644 --- a/Jupiter/String_Type.h +++ b/Jupiter/String_Type.h @@ -210,6 +210,22 @@ namespace Jupiter size_t aformat(const T *format, ...); virtual size_t avformat(const T *format, va_list args) = 0; + /** + * @brief Truncates the string by a specified number of elements. + * + * @param n Number of elements to remove from the tail. + * @return New size of the String. + */ + virtual size_t truncate(size_t n); + + /** + * @brief Removes the first instance of an element from the string. + * + * @param value Value of the element to remove. + * @return True if an element was removed, false otherwise. + */ + virtual bool remove(const T &value); + /** * @brief Copies the data from the input string to the String. * @@ -232,14 +248,6 @@ namespace Jupiter virtual size_t concat(const T *in) = 0; virtual size_t concat(const T in) = 0; - /** - * @brief Truncates the string by a specified number of elements. - * - * @param n Number of elements to remove from the tail. - * @return New size of the String. - */ - virtual size_t truncate(size_t n) = 0; - /** Access operator */ inline T &operator[](size_t index) { return Jupiter::String_Type::get(index); }; diff --git a/Jupiter/String_Type_Imp.h b/Jupiter/String_Type_Imp.h index 8fc7e61..09abe8d 100644 --- a/Jupiter/String_Type_Imp.h +++ b/Jupiter/String_Type_Imp.h @@ -659,4 +659,37 @@ template size_t Jupiter::String_Type::aformat(const T *format, .. return r; } +// truncate base + +template size_t Jupiter::String_Type::truncate(size_t n) +{ + if (n >= Jupiter::String_Type::length) return (Jupiter::String_Type::length = 0); + return (Jupiter::String_Type::length -= n); +} + +// remove base + +template bool Jupiter::String_Type::remove(const T &value) +{ + for (unsigned int i = 0; i < Jupiter::String_Type::length - 1; i++) + { + if (Jupiter::String_Type::str[i] == value) + { + while (i < Jupiter::String_Type::length) + { + i++; + Jupiter::String_Type::str[i - 1] = Jupiter::String_Type::str[i]; + } + Jupiter::String_Type::length--; + return true; + } + } + if (Jupiter::String_Type::str[Jupiter::String_Type::length - 1] == value) + { + this->truncate(1); + return true; + } + return false; +} + #endif // _STRING_TYPE_IMP_H_HEADER \ No newline at end of file