diff --git a/Jupiter/String_Type.h b/Jupiter/String_Type.h index cbd9b14..5a9a1ea 100644 --- a/Jupiter/String_Type.h +++ b/Jupiter/String_Type.h @@ -113,6 +113,34 @@ namespace Jupiter virtual size_t set(const T *in); virtual size_t set(const T in); + /** + * @brief Inserts data into a position in the string. + * + * @param index Index to insert data to. + * @param value Value to insert. + */ + virtual size_t insert(size_t index, const T &value); + virtual size_t insert(size_t index, const Jupiter::Readable_String &value); + + /** + * @brief Replaces all instances of one value with another value. + * + * @param target Target to search for and replace. + * @param value Value to replace the target with. + * @return New size of the string. + */ + virtual size_t replace(const T &target, const T &value); + virtual size_t replace(const T *target, size_t targetSize, const T &value); + virtual size_t replace(const Jupiter::Readable_String &target, const T &value); + + //virtual size_t replace(const T &target, const T *value, size_t valueSize); + //virtual size_t replace(const T &target, const Jupiter::Readable_String &value); + + //virtual size_t replace(const T *target, size_t targetSize, const T *value, size_t valueSize); + //virtual size_t replace(const T *target, size_t targetSize, const Jupiter::Readable_String &value); + //virtual size_t replace(const Jupiter::Readable_String &target, const T *value, size_t valueSize); + //virtual size_t replace(const Jupiter::Readable_String &target, const Jupiter::Readable_String &value); + /** * @brief Copies the data from the input string and concatenates it to the end of String. * diff --git a/Jupiter/String_Type_Imp.h b/Jupiter/String_Type_Imp.h index 636ba44..bfad1d7 100644 --- a/Jupiter/String_Type_Imp.h +++ b/Jupiter/String_Type_Imp.h @@ -163,6 +163,103 @@ template size_t Jupiter::String_Type::set(const T in) return Jupiter::String_Type::length = 1; } +// replace + +template size_t Jupiter::String_Type::insert(size_t index, const T &value) +{ + if (index >= this->size()) + return this->concat(value); + + this->setBufferSize(this->size() + 1); + for (size_t i = this->size(); i != index; i--) + Jupiter::String_Type::str[i] = this->get(i-1); + + Jupiter::String_Type::str[index] = value; + return ++Jupiter::String_Type::length; +} + +template size_t Jupiter::String_Type::insert(size_t index, const Jupiter::Readable_String &value) +{ + if (index >= this->size()) + return this->concat(value); + + if (value.size() == 0) + return Jupiter::String_Type::length; + + if (value.size() == 1) + return this->insert(index, value.get(0)); + + this->setBufferSize(this->size() + value.size()); + size_t i; + for (i = this->size() + value.size() - 1; i != index + value.size() - 1; i--) + Jupiter::String_Type::str[i] = this->get(i - value.size()); + + while (i != index) + { + Jupiter::String_Type::str[i] = value.get(i - index); + i--; + } + Jupiter::String_Type::str[index] = value.get(0); + + return Jupiter::String_Type::length += value.size(); +} + +// replace + +template size_t Jupiter::String_Type::replace(const T &target, const T &value) +{ + for (size_t i = 0; i != this->size(); i++) + { + if (this->get(i) == target) + Jupiter::String_Type::str[i] = value; + } + return Jupiter::String_Type::length; +} + +template size_t Jupiter::String_Type::replace(const T *target, size_t targetSize, const T &value) +{ + if (targetSize != 0) + { + if (targetSize == 1) + return this->replace(*target, value); + + if (targetSize < this->size()) + { + size_t i = 0, j = 0, k; + while (j <= this->size() - targetSize) + { + k = 0; + while (this->get(j + k) == target[k]) + { + if (++k == targetSize) // match found + { + Jupiter::String_Type::str[i] = value; + i += 1; + j += k; + break; + } + } + if (k != targetSize) + Jupiter::String_Type::str[i++] = Jupiter::String_Type::str[j++]; + } + while (j < this->size()) + Jupiter::String_Type::str[i++] = Jupiter::String_Type::str[j++]; + + Jupiter::String_Type::length = i; + } + else if (targetSize == this->size() && this->equals(target, targetSize)) + return this->set(value); + } + return Jupiter::String_Type::length; +} + +template size_t Jupiter::String_Type::replace(const Jupiter::Readable_String &target, const T &value) +{ + return this->replace(target.ptr(), target.size(), value); +} + +// concat + template size_t Jupiter::String_Type::concat(const Jupiter::Readable_String &in) { size_t nSize = Jupiter::String_Type::length + in.size(); diff --git a/Release/Jupiter.lib b/Release/Jupiter.lib index bc40130..a5d082f 100644 Binary files a/Release/Jupiter.lib and b/Release/Jupiter.lib differ