Browse Source

Optimized remove(). Spread truncate() and remove() responsibility out of CString_Type.

release/0.19
JustinAJ 11 years ago
parent
commit
48e7fe9de3
  1. 2
      Jupiter/CString.h
  2. 29
      Jupiter/CString_Imp.h
  3. 8
      Jupiter/Shift_String.h
  4. 12
      Jupiter/Shift_String_Imp.h
  5. 24
      Jupiter/String_Type.h
  6. 33
      Jupiter/String_Type_Imp.h

2
Jupiter/CString.h

@ -60,7 +60,7 @@ namespace Jupiter
* @param value Value of the element to remove. * @param value Value of the element to remove.
* @return True if an element was removed, false otherwise. * @return True if an element was removed, false otherwise.
*/ */
bool remove(T &value); bool remove(const T &value);
/** Assignment Operators */ /** Assignment Operators */
inline CString_Type<T> &operator=(const CString_Type<T> &right) { this->set(right); return *this; }; inline CString_Type<T> &operator=(const CString_Type<T> &right) { this->set(right); return *this; };

29
Jupiter/CString_Imp.h

@ -50,38 +50,17 @@ template<typename T> const T *Jupiter::CString_Type<T>::c_str() const
template<typename T> size_t Jupiter::CString_Type<T>::truncate(size_t n) template<typename T> size_t Jupiter::CString_Type<T>::truncate(size_t n)
{ {
if (n >= Jupiter::String_Type<T>::length) Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::truncate(n)] = 0;
{
Jupiter::String_Type<T>::str = Jupiter::Shift_String_Type<T>::base;
*Jupiter::String_Type<T>::str = 0;
Jupiter::String_Type<T>::length = 0;
return 0;
}
Jupiter::String_Type<T>::length -= n;
Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length] = 0;
return Jupiter::String_Type<T>::length; return Jupiter::String_Type<T>::length;
} }
template<typename T> bool Jupiter::CString_Type<T>::remove(T &value) template<typename T> bool Jupiter::CString_Type<T>::remove(const T &value)
{ {
for (unsigned int i = 0; i < Jupiter::String_Type<T>::length; i++) if (Jupiter::Shift_String_Type<T>::remove(value))
{
if (Jupiter::String_Type<T>::str[i] == value)
{ {
if (i == Jupiter::String_Type<T>::length - 1) Jupiter::CString_Type<T>::truncate(1); Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length] = 0;
else if (i == 0)
{
if (Jupiter::String_Type<T>::length == 1) Jupiter::CString_Type<T>::truncate(1);
else Jupiter::CString_Type<T>::shiftRight(1);
}
else
{
Jupiter::strcpy<T>(Jupiter::String_Type<T>::str + i, Jupiter::String_Type<T>::str + i + 1);
Jupiter::String_Type<T>::length--;
}
return true; return true;
} }
}
return false; return false;
} }

8
Jupiter/Shift_String.h

@ -54,6 +54,14 @@ namespace Jupiter
*/ */
size_t shiftRight(size_t length); 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: protected:
T *base; /** Base pointer for the underlying String's memory allocation */ T *base; /** Base pointer for the underlying String's memory allocation */
}; };

12
Jupiter/Shift_String_Imp.h

@ -41,4 +41,16 @@ template<typename T> size_t Jupiter::Shift_String_Type<T>::shiftRight(size_t len
return len; return len;
} }
template<typename T> bool Jupiter::Shift_String_Type<T>::remove(const T &value)
{
if (Jupiter::String_Type<T>::length == 0) return false;
if (*Jupiter::String_Type<T>::str == value)
{
if (Jupiter::String_Type<T>::length == 1) this->truncate(1);
else this->shiftRight(1);
return true;
}
return Jupiter::String_Type<T>::remove(value);
}
#endif // _SHIFT_STRING_IMP_H_HEADER #endif // _SHIFT_STRING_IMP_H_HEADER

24
Jupiter/String_Type.h

@ -210,6 +210,22 @@ namespace Jupiter
size_t aformat(const T *format, ...); size_t aformat(const T *format, ...);
virtual size_t avformat(const T *format, va_list args) = 0; 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. * @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;
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 */ /** Access operator */
inline T &operator[](size_t index) { return Jupiter::String_Type<T>::get(index); }; inline T &operator[](size_t index) { return Jupiter::String_Type<T>::get(index); };

33
Jupiter/String_Type_Imp.h

@ -659,4 +659,37 @@ template<typename T> size_t Jupiter::String_Type<T>::aformat(const T *format, ..
return r; return r;
} }
// truncate base
template<typename T> size_t Jupiter::String_Type<T>::truncate(size_t n)
{
if (n >= Jupiter::String_Type<T>::length) return (Jupiter::String_Type<T>::length = 0);
return (Jupiter::String_Type<T>::length -= n);
}
// remove base
template<typename T> bool Jupiter::String_Type<T>::remove(const T &value)
{
for (unsigned int i = 0; i < Jupiter::String_Type<T>::length - 1; i++)
{
if (Jupiter::String_Type<T>::str[i] == value)
{
while (i < Jupiter::String_Type<T>::length)
{
i++;
Jupiter::String_Type<T>::str[i - 1] = Jupiter::String_Type<T>::str[i];
}
Jupiter::String_Type<T>::length--;
return true;
}
}
if (Jupiter::String_Type<T>::str[Jupiter::String_Type<T>::length - 1] == value)
{
this->truncate(1);
return true;
}
return false;
}
#endif // _STRING_TYPE_IMP_H_HEADER #endif // _STRING_TYPE_IMP_H_HEADER
Loading…
Cancel
Save