diff --git a/Jupiter/String_Type.h b/Jupiter/String_Type.h index 3ca600f..56c4eba 100644 --- a/Jupiter/String_Type.h +++ b/Jupiter/String_Type.h @@ -1,5 +1,5 @@ /** - * Copyright (C) 2013-2014 Justin James. + * Copyright (C) 2013-2015 Justin James. * * This license must be preserved. * Any applications, libraries, or code which make any use of any @@ -102,16 +102,31 @@ namespace Jupiter */ virtual bool remove(const T &value); + /** + * @brief Sets the value of an element at the specified index. + * Note: If the index is not in the string, it will be added. + * + * @param index Index of element to replace. + * @param in Value to set element to. + * @return New size of the String. + */ + virtual size_t set(size_t index, const T &in); + virtual size_t set(size_t index, const T *in, size_t inSize); + virtual size_t set(size_t index, const Jupiter::Readable_String &in); + virtual size_t set(size_t index, const std::basic_string &in); + virtual size_t set(size_t index, const T *in); + /** * @brief Copies the data from the input string to the String. * * @param in String containing the data to be copied. * @return New size of the String. */ + virtual size_t set(const T *in, size_t inSize); virtual size_t set(const Jupiter::Readable_String &in); virtual size_t set(const std::basic_string &in); virtual size_t set(const T *in); - virtual size_t set(const T in); + virtual size_t set(const T &in); /** * @brief Inserts data into a position in the string. @@ -147,10 +162,11 @@ namespace Jupiter * @param in String containing the data to be concatenated. * @return New size of the String. */ + virtual size_t concat(const T *in, size_t inSize); virtual size_t concat(const Jupiter::Readable_String &in); virtual size_t concat(const std::basic_string &in); virtual size_t concat(const T *in); - virtual size_t concat(const T in); + virtual size_t concat(const T &in); /** * @brief Copies a part of an input string and returns it in an output type. diff --git a/Jupiter/String_Type_Imp.h b/Jupiter/String_Type_Imp.h index 04627e8..2785793 100644 --- a/Jupiter/String_Type_Imp.h +++ b/Jupiter/String_Type_Imp.h @@ -1,5 +1,5 @@ /** - * Copyright (C) 2014 Justin James. + * Copyright (C) 2014-2015 Justin James. * * This license must be preserved. * Any applications, libraries, or code which make any use of any @@ -132,31 +132,93 @@ template bool Jupiter::String_Type::remove(const T &value) return false; } -template size_t Jupiter::String_Type::set(const Jupiter::Readable_String &in) +// set + +template size_t Jupiter::String_Type::set(size_t index, const T &in) { - this->setBufferSizeNoCopy(in.size()); - for (Jupiter::String_Type::length = 0; Jupiter::String_Type::length < in.size() != 0; Jupiter::String_Type::length++) - Jupiter::String_Type::str[Jupiter::String_Type::length] = in.get(Jupiter::String_Type::length); + if (index == this->size()) + return this->concat(in); + + if (index > this->size()) + { + this->setBufferSize(index + 1); + while (Jupiter::String_Type::length != index) + Jupiter::String_Type::str[Jupiter::String_Type::length++] = 0; + Jupiter::String_Type::length++; + } + Jupiter::String_Type::str[index] = in; return Jupiter::String_Type::length; } -template size_t Jupiter::String_Type::set(const std::basic_string &in) +template size_t Jupiter::String_Type::set(size_t index, const T *in, size_t inSize) { - this->setBufferSizeNoCopy(in.size()); - for (Jupiter::String_Type::length = 0; Jupiter::String_Type::length < in.size(); Jupiter::String_Type::length++) - Jupiter::String_Type::str[Jupiter::String_Type::length] = in.at(Jupiter::String_Type::length); + if (index == this->size()) + return this->concat(in); + + if (index > this->size()) + { + this->setBufferSize(index + inSize); + while (Jupiter::String_Type::length != index) + Jupiter::String_Type::str[Jupiter::String_Type::length++] = 0; + index = 0; + while (index != inSize) + Jupiter::String_Type::str[Jupiter::String_Type::length++] = in[index]; + } + else + { + index += inSize; + if (index > Jupiter::String_Type::length) + { + this->setBufferSize(index); + Jupiter::String_Type::length = index; + } + while (inSize != 0) + Jupiter::String_Type::str[--index] = in[--inSize]; + } return Jupiter::String_Type::length; } -template size_t Jupiter::String_Type::set(const T *in) +template size_t Jupiter::String_Type::set(size_t index, const Jupiter::Readable_String &in) { - size_t nLen = Jupiter::strlen(in); - this->setBufferSizeNoCopy(nLen); - for (Jupiter::String_Type::length = 0; *in != 0; Jupiter::String_Type::length++, in++) Jupiter::String_Type::str[Jupiter::String_Type::length] = *in; + return this->set(index, in.ptr(), in.size()); +} + +template size_t Jupiter::String_Type::set(size_t index, const std::basic_string &in) +{ + return this->set(index, in.c_str(), in.size()); +} + +template size_t Jupiter::String_Type::set(size_t index, const T *in) +{ + return this->set(index, in, Jupiter::strlen(in)); +} + +template size_t Jupiter::String_Type::set(const T *in, size_t inSize) +{ + this->setBufferSizeNoCopy(inSize); + Jupiter::String_Type::length = inSize; + while (inSize-- != 0) + *Jupiter::String_Type::str++ = *in++; + Jupiter::String_Type::str -= Jupiter::String_Type::length; return Jupiter::String_Type::length; } -template size_t Jupiter::String_Type::set(const T in) +template size_t Jupiter::String_Type::set(const Jupiter::Readable_String &in) +{ + return this->set(in.ptr(), in.size()); +} + +template size_t Jupiter::String_Type::set(const std::basic_string &in) +{ + return this->set(in.c_str(), in.size()); +} + +template size_t Jupiter::String_Type::set(const T *in) +{ + return this->set(in, Jupiter::strlen(in)); +} + +template size_t Jupiter::String_Type::set(const T &in) { this->setBufferSizeNoCopy(1); *Jupiter::String_Type::str = in; @@ -373,48 +435,30 @@ template size_t Jupiter::String_Type::replace(const Jupiter::Read // concat -template size_t Jupiter::String_Type::concat(const Jupiter::Readable_String &in) +template size_t Jupiter::String_Type::concat(const T *in, size_t inSize) { - size_t nSize = Jupiter::String_Type::length + in.size(); - const T *inData = in.ptr(); - this->setBufferSize(nSize); - while (Jupiter::String_Type::length != nSize) - { - Jupiter::String_Type::str[Jupiter::String_Type::length] = *inData; - Jupiter::String_Type::length++; - inData++; - } + this->setBufferSize(Jupiter::String_Type::length + inSize); + while (inSize-- != 0) + Jupiter::String_Type::str[Jupiter::String_Type::length++] = *in++; return Jupiter::String_Type::length; } +template size_t Jupiter::String_Type::concat(const Jupiter::Readable_String &in) +{ + return this->concat(in.ptr(), in.size()); +} + template size_t Jupiter::String_Type::concat(const std::basic_string &in) { - size_t nSize = Jupiter::String_Type::length + in.size(); - const T *inData = in.data(); - this->setBufferSize(nSize); - while (Jupiter::String_Type::length != nSize) - { - Jupiter::String_Type::str[Jupiter::String_Type::length] = *inData; - Jupiter::String_Type::length++; - inData++; - } - return Jupiter::String_Type::length; + return this->concat(in.c_str(), in.size()); } template size_t Jupiter::String_Type::concat(const T *in) { - size_t nSize = Jupiter::String_Type::length + Jupiter::strlen(in); - this->setBufferSize(nSize); - while (*in != 0) - { - Jupiter::String_Type::str[Jupiter::String_Type::length] = *in; - Jupiter::String_Type::length++; - in++; - } - return Jupiter::String_Type::length; + return this->concat(in, Jupiter::strlen(in)); } -template size_t Jupiter::String_Type::concat(const T c) +template size_t Jupiter::String_Type::concat(const T &c) { this->setBufferSize(Jupiter::String_Type::length + 1); Jupiter::String_Type::str[Jupiter::String_Type::length] = c;