Browse Source

Added more set() functions; simplified and optimized concat() and set() functions

release/0.19
JustinAJ 10 years ago
parent
commit
46ce3548d2
  1. 22
      Jupiter/String_Type.h
  2. 132
      Jupiter/String_Type_Imp.h

22
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. * This license must be preserved.
* Any applications, libraries, or code which make any use of any * Any applications, libraries, or code which make any use of any
@ -102,16 +102,31 @@ namespace Jupiter
*/ */
virtual bool remove(const T &value); 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<T> &in);
virtual size_t set(size_t index, const std::basic_string<T> &in);
virtual size_t set(size_t index, const T *in);
/** /**
* @brief Copies the data from the input string to the String. * @brief Copies the data from the input string to the String.
* *
* @param in String containing the data to be copied. * @param in String containing the data to be copied.
* @return New size of the String. * @return New size of the String.
*/ */
virtual size_t set(const T *in, size_t inSize);
virtual size_t set(const Jupiter::Readable_String<T> &in); virtual size_t set(const Jupiter::Readable_String<T> &in);
virtual size_t set(const std::basic_string<T> &in); virtual size_t set(const std::basic_string<T> &in);
virtual size_t set(const T *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. * @brief Inserts data into a position in the string.
@ -147,10 +162,11 @@ namespace Jupiter
* @param in String containing the data to be concatenated. * @param in String containing the data to be concatenated.
* @return New size of the String. * @return New size of the String.
*/ */
virtual size_t concat(const T *in, size_t inSize);
virtual size_t concat(const Jupiter::Readable_String<T> &in); virtual size_t concat(const Jupiter::Readable_String<T> &in);
virtual size_t concat(const std::basic_string<T> &in); virtual size_t concat(const std::basic_string<T> &in);
virtual size_t concat(const T *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. * @brief Copies a part of an input string and returns it in an output type.

132
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. * This license must be preserved.
* Any applications, libraries, or code which make any use of any * Any applications, libraries, or code which make any use of any
@ -132,31 +132,93 @@ template<typename T> bool Jupiter::String_Type<T>::remove(const T &value)
return false; return false;
} }
template<typename T> size_t Jupiter::String_Type<T>::set(const Jupiter::Readable_String<T> &in) // set
template<typename T> size_t Jupiter::String_Type<T>::set(size_t index, const T &in)
{
if (index == this->size())
return this->concat(in);
if (index > this->size())
{ {
this->setBufferSizeNoCopy(in.size()); this->setBufferSize(index + 1);
for (Jupiter::String_Type<T>::length = 0; Jupiter::String_Type<T>::length < in.size() != 0; Jupiter::String_Type<T>::length++) while (Jupiter::String_Type<T>::length != index)
Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length] = in.get(Jupiter::String_Type<T>::length); Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length++] = 0;
Jupiter::String_Type<T>::length++;
}
Jupiter::String_Type<T>::str[index] = in;
return Jupiter::String_Type<T>::length; return Jupiter::String_Type<T>::length;
} }
template<typename T> size_t Jupiter::String_Type<T>::set(const std::basic_string<T> &in) template<typename T> size_t Jupiter::String_Type<T>::set(size_t index, const T *in, size_t inSize)
{ {
this->setBufferSizeNoCopy(in.size()); if (index == this->size())
for (Jupiter::String_Type<T>::length = 0; Jupiter::String_Type<T>::length < in.size(); Jupiter::String_Type<T>::length++) return this->concat(in);
Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length] = in.at(Jupiter::String_Type<T>::length);
if (index > this->size())
{
this->setBufferSize(index + inSize);
while (Jupiter::String_Type<T>::length != index)
Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length++] = 0;
index = 0;
while (index != inSize)
Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length++] = in[index];
}
else
{
index += inSize;
if (index > Jupiter::String_Type<T>::length)
{
this->setBufferSize(index);
Jupiter::String_Type<T>::length = index;
}
while (inSize != 0)
Jupiter::String_Type<T>::str[--index] = in[--inSize];
}
return Jupiter::String_Type<T>::length; return Jupiter::String_Type<T>::length;
} }
template<typename T> size_t Jupiter::String_Type<T>::set(const T *in) template<typename T> size_t Jupiter::String_Type<T>::set(size_t index, const Jupiter::Readable_String<T> &in)
{ {
size_t nLen = Jupiter::strlen<T>(in); return this->set(index, in.ptr(), in.size());
this->setBufferSizeNoCopy(nLen); }
for (Jupiter::String_Type<T>::length = 0; *in != 0; Jupiter::String_Type<T>::length++, in++) Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length] = *in;
template<typename T> size_t Jupiter::String_Type<T>::set(size_t index, const std::basic_string<T> &in)
{
return this->set(index, in.c_str(), in.size());
}
template<typename T> size_t Jupiter::String_Type<T>::set(size_t index, const T *in)
{
return this->set(index, in, Jupiter::strlen<T>(in));
}
template<typename T> size_t Jupiter::String_Type<T>::set(const T *in, size_t inSize)
{
this->setBufferSizeNoCopy(inSize);
Jupiter::String_Type<T>::length = inSize;
while (inSize-- != 0)
*Jupiter::String_Type<T>::str++ = *in++;
Jupiter::String_Type<T>::str -= Jupiter::String_Type<T>::length;
return Jupiter::String_Type<T>::length; return Jupiter::String_Type<T>::length;
} }
template<typename T> size_t Jupiter::String_Type<T>::set(const T in) template<typename T> size_t Jupiter::String_Type<T>::set(const Jupiter::Readable_String<T> &in)
{
return this->set(in.ptr(), in.size());
}
template<typename T> size_t Jupiter::String_Type<T>::set(const std::basic_string<T> &in)
{
return this->set(in.c_str(), in.size());
}
template<typename T> size_t Jupiter::String_Type<T>::set(const T *in)
{
return this->set(in, Jupiter::strlen<T>(in));
}
template<typename T> size_t Jupiter::String_Type<T>::set(const T &in)
{ {
this->setBufferSizeNoCopy(1); this->setBufferSizeNoCopy(1);
*Jupiter::String_Type<T>::str = in; *Jupiter::String_Type<T>::str = in;
@ -373,48 +435,30 @@ template<typename T> size_t Jupiter::String_Type<T>::replace(const Jupiter::Read
// concat // concat
template<typename T> size_t Jupiter::String_Type<T>::concat(const Jupiter::Readable_String<T> &in) template<typename T> size_t Jupiter::String_Type<T>::concat(const T *in, size_t inSize)
{
size_t nSize = Jupiter::String_Type<T>::length + in.size();
const T *inData = in.ptr();
this->setBufferSize(nSize);
while (Jupiter::String_Type<T>::length != nSize)
{ {
Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length] = *inData; this->setBufferSize(Jupiter::String_Type<T>::length + inSize);
Jupiter::String_Type<T>::length++; while (inSize-- != 0)
inData++; Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length++] = *in++;
}
return Jupiter::String_Type<T>::length; return Jupiter::String_Type<T>::length;
} }
template<typename T> size_t Jupiter::String_Type<T>::concat(const std::basic_string<T> &in) template<typename T> size_t Jupiter::String_Type<T>::concat(const Jupiter::Readable_String<T> &in)
{
size_t nSize = Jupiter::String_Type<T>::length + in.size();
const T *inData = in.data();
this->setBufferSize(nSize);
while (Jupiter::String_Type<T>::length != nSize)
{ {
Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length] = *inData; return this->concat(in.ptr(), in.size());
Jupiter::String_Type<T>::length++;
inData++;
} }
return Jupiter::String_Type<T>::length;
template<typename T> size_t Jupiter::String_Type<T>::concat(const std::basic_string<T> &in)
{
return this->concat(in.c_str(), in.size());
} }
template<typename T> size_t Jupiter::String_Type<T>::concat(const T *in) template<typename T> size_t Jupiter::String_Type<T>::concat(const T *in)
{ {
size_t nSize = Jupiter::String_Type<T>::length + Jupiter::strlen<T>(in); return this->concat(in, Jupiter::strlen<T>(in));
this->setBufferSize(nSize);
while (*in != 0)
{
Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length] = *in;
Jupiter::String_Type<T>::length++;
in++;
}
return Jupiter::String_Type<T>::length;
} }
template<typename T> size_t Jupiter::String_Type<T>::concat(const T c) template<typename T> size_t Jupiter::String_Type<T>::concat(const T &c)
{ {
this->setBufferSize(Jupiter::String_Type<T>::length + 1); this->setBufferSize(Jupiter::String_Type<T>::length + 1);
Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length] = c; Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length] = c;

Loading…
Cancel
Save