Commit 93959cf9 authored by hybrid's avatar hybrid

irrString additions provided by Nalin.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@3254 dfc29bdd-3216-0410-991c-e03cc46cb475
parent b3ab720d
...@@ -491,18 +491,20 @@ public: ...@@ -491,18 +491,20 @@ public:
//! Makes the string lower case. //! Makes the string lower case.
void make_lower() string<T,TAlloc>& make_lower()
{ {
for (u32 i=0; i<used; ++i) for (u32 i=0; i<used; ++i)
array[i] = locale_lower ( array[i] ); array[i] = locale_lower ( array[i] );
return *this;
} }
//! Makes the string upper case. //! Makes the string upper case.
void make_upper() string<T,TAlloc>& make_upper()
{ {
for (u32 i=0; i<used; ++i) for (u32 i=0; i<used; ++i)
array[i] = locale_upper ( array[i] ); array[i] = locale_upper ( array[i] );
return *this;
} }
...@@ -590,7 +592,7 @@ public: ...@@ -590,7 +592,7 @@ public:
//! Appends a character to this string //! Appends a character to this string
/** \param character: Character to append. */ /** \param character: Character to append. */
void append(T character) string<T,TAlloc>& append(T character)
{ {
if (used + 1 > allocated) if (used + 1 > allocated)
reallocate(used + 1); reallocate(used + 1);
...@@ -599,15 +601,18 @@ public: ...@@ -599,15 +601,18 @@ public:
array[used-2] = character; array[used-2] = character;
array[used-1] = 0; array[used-1] = 0;
return *this;
} }
//! Appends a char string to this string //! Appends a char string to this string
/** \param other: Char string to append. */ /** \param other: Char string to append. */
void append(const T* const other) /** \param length: The length of the string to append. */
string<T,TAlloc>& append(const T* const other, u32 length=0xffffffff)
{ {
if (!other) if (!other)
return; return *this;
u32 len = 0; u32 len = 0;
const T* p = other; const T* p = other;
...@@ -616,6 +621,8 @@ public: ...@@ -616,6 +621,8 @@ public:
++len; ++len;
++p; ++p;
} }
if (len > length)
len = length;
if (used + len > allocated) if (used + len > allocated)
reallocate(used + len); reallocate(used + len);
...@@ -627,13 +634,18 @@ public: ...@@ -627,13 +634,18 @@ public:
array[l+used] = *(other+l); array[l+used] = *(other+l);
used += len; used += len;
return *this;
} }
//! Appends a string to this string //! Appends a string to this string
/** \param other: String to append. */ /** \param other: String to append. */
void append(const string<T,TAlloc>& other) string<T,TAlloc>& append(const string<T,TAlloc>& other)
{ {
if (other.size() == 0)
return *this;
--used; --used;
u32 len = other.size()+1; u32 len = other.size()+1;
...@@ -644,18 +656,23 @@ public: ...@@ -644,18 +656,23 @@ public:
array[used+l] = other[l]; array[used+l] = other[l];
used += len; used += len;
return *this;
} }
//! Appends a string of the length l to this string. //! Appends a string of the length l to this string.
/** \param other: other String to append to this string. /** \param other: other String to append to this string.
\param length: How much characters of the other string to add to this one. */ \param length: How much characters of the other string to add to this one. */
void append(const string<T,TAlloc>& other, u32 length) string<T,TAlloc>& append(const string<T,TAlloc>& other, u32 length)
{ {
if (other.size() == 0)
return *this;
if (other.size() < length) if (other.size() < length)
{ {
append(other); append(other);
return; return *this;
} }
if (used + length > allocated) if (used + length > allocated)
...@@ -670,6 +687,8 @@ public: ...@@ -670,6 +687,8 @@ public:
// ensure proper termination // ensure proper termination
array[used]=0; array[used]=0;
++used; ++used;
return *this;
} }
...@@ -704,9 +723,9 @@ public: ...@@ -704,9 +723,9 @@ public:
this should be strlen(c) this should be strlen(c)
\return Position where one of the characters has been found, \return Position where one of the characters has been found,
or -1 if not found. */ or -1 if not found. */
s32 findFirstChar(const T* const c, u32 count) const s32 findFirstChar(const T* const c, u32 count=1) const
{ {
if (!c) if (!c || !count)
return -1; return -1;
for (u32 i=0; i<used; ++i) for (u32 i=0; i<used; ++i)
...@@ -726,8 +745,11 @@ public: ...@@ -726,8 +745,11 @@ public:
\return Position where the character has been found, \return Position where the character has been found,
or -1 if not found. */ or -1 if not found. */
template <class B> template <class B>
s32 findFirstCharNotInList(const B* const c, u32 count) const s32 findFirstCharNotInList(const B* const c, u32 count=1) const
{ {
if (!c || !count)
return -1;
for (u32 i=0; i<used-1; ++i) for (u32 i=0; i<used-1; ++i)
{ {
u32 j; u32 j;
...@@ -750,8 +772,11 @@ public: ...@@ -750,8 +772,11 @@ public:
\return Position where the character has been found, \return Position where the character has been found,
or -1 if not found. */ or -1 if not found. */
template <class B> template <class B>
s32 findLastCharNotInList(const B* const c, u32 count) const s32 findLastCharNotInList(const B* const c, u32 count=1) const
{ {
if (!c || !count)
return -1;
for (s32 i=(s32)(used-2); i>=0; --i) for (s32 i=(s32)(used-2); i>=0; --i)
{ {
u32 j; u32 j;
...@@ -803,9 +828,9 @@ public: ...@@ -803,9 +828,9 @@ public:
this should be strlen(c) this should be strlen(c)
\return Position where one of the characters has been found, \return Position where one of the characters has been found,
or -1 if not found. */ or -1 if not found. */
s32 findLastChar(const T* const c, u32 count) const s32 findLastChar(const T* const c, u32 count=1) const
{ {
if (!c) if (!c || !count)
return -1; return -1;
for (s32 i=used-1; i>=0; --i) for (s32 i=used-1; i>=0; --i)
...@@ -961,17 +986,133 @@ public: ...@@ -961,17 +986,133 @@ public:
//! Replaces all characters of a special type with another one //! Replaces all characters of a special type with another one
/** \param toReplace Character to replace. /** \param toReplace Character to replace.
\param replaceWith Character replacing the old one. */ \param replaceWith Character replacing the old one. */
void replace(T toReplace, T replaceWith) string<T,TAlloc>& replace(T toReplace, T replaceWith)
{ {
for (u32 i=0; i<used; ++i) for (u32 i=0; i<used; ++i)
if (array[i] == toReplace) if (array[i] == toReplace)
array[i] = replaceWith; array[i] = replaceWith;
return *this;
}
//! Replaces all instances of a string with another one.
/** \param toReplace The string to replace.
\param replaceWith The string replacing the old one. */
string<T,TAlloc>& replace(const string<T,TAlloc>& toReplace, const string<T,TAlloc>& replaceWith)
{
if (toReplace.size() == 0)
return *this;
const T* other = toReplace.c_str();
const T* replace = replaceWith.c_str();
const u32 other_size = toReplace.size();
const u32 replace_size = replaceWith.size();
// Determine the delta. The algorithm will change depending on the delta.
s32 delta = replace_size - other_size;
// A character for character replace. The string will not shrink or grow.
if (delta == 0)
{
s32 pos = 0;
while ((pos = find(other, pos)) != -1)
{
for (u32 i = 0; i < replace_size; ++i)
array[pos + i] = replace[i];
++pos;
}
return *this;
}
// We are going to be removing some characters. The string will shrink.
if (delta < 0)
{
u32 i = 0;
for (u32 pos = 0; pos < used; ++i, ++pos)
{
// Is this potentially a match?
if (array[pos] == *other)
{
// Check to see if we have a match.
u32 j;
for (j = 0; j < other_size; ++j)
{
if (array[pos + j] != other[j])
break;
}
// If we have a match, replace characters.
if (j == other_size)
{
for (j = 0; j < replace_size; ++j)
array[i + j] = replace[j];
i += replace_size - 1;
pos += other_size - 1;
continue;
}
}
// No match found, just copy characters.
array[i] = array[pos];
}
array[i] = 0;
used = i;
return *this;
}
// We are going to be adding characters, so the string size will increase.
// Count the number of times toReplace exists in the string so we can allocate the new size.
u32 find_count = 0;
s32 pos = 0;
while ((pos = find(other, pos)) != -1)
{
++find_count;
++pos;
}
// Re-allocate the string now, if needed.
u32 len = replace_size * find_count;
if (used + len > allocated)
reallocate(used + len);
// Don't take the string terminator into account.
--used;
// Start replacing.
pos = 0;
while ((pos = find(other, pos)) != -1)
{
T* start = array + pos + other_size - 1;
T* ptr = array + used;
T* end = array + used + delta;
// Shift characters to make room for the string.
while (ptr != start)
{
*end = *ptr;
--ptr;
--end;
}
// Add the new string now.
for (u32 i = 0; i < replace_size; ++i)
array[pos + i] = replace[i];
pos += replace_size;
used += replace_size;
}
// Terminate the string and return ourself.
++used;
array[used] = 0;
return *this;
} }
//! Removes characters from a string. //! Removes characters from a string.
/** \param c: Character to remove. */ /** \param c: Character to remove. */
void remove(T c) string<T,TAlloc>& remove(T c)
{ {
u32 pos = 0; u32 pos = 0;
u32 found = 0; u32 found = 0;
...@@ -987,14 +1128,17 @@ public: ...@@ -987,14 +1128,17 @@ public:
} }
used -= found; used -= found;
array[used] = 0; array[used] = 0;
return *this;
} }
//! Removes a string from the string. //! Removes a string from the string.
/** \param toRemove: String to remove. */ /** \param toRemove: String to remove. */
void remove(const string<T,TAlloc> toRemove) string<T,TAlloc>& remove(const string<T,TAlloc>& toRemove)
{ {
u32 size = toRemove.size(); u32 size = toRemove.size();
if (size == 0) return *this;
u32 pos = 0; u32 pos = 0;
u32 found = 0; u32 found = 0;
for (u32 i=0; i<used; ++i) for (u32 i=0; i<used; ++i)
...@@ -1017,13 +1161,17 @@ public: ...@@ -1017,13 +1161,17 @@ public:
} }
used -= found; used -= found;
array[used] = 0; array[used] = 0;
return *this;
} }
//! Removes characters from a string. //! Removes characters from a string.
/** \param characters: Characters to remove. */ /** \param characters: Characters to remove. */
void removeChars(const string<T,TAlloc> & characters) string<T,TAlloc>& removeChars(const string<T,TAlloc> & characters)
{ {
if (characters.size() == 0)
return *this;
u32 pos = 0; u32 pos = 0;
u32 found = 0; u32 found = 0;
for (u32 i=0; i<used; ++i) for (u32 i=0; i<used; ++i)
...@@ -1047,6 +1195,8 @@ public: ...@@ -1047,6 +1195,8 @@ public:
} }
used -= found; used -= found;
array[used] = 0; array[used] = 0;
return *this;
} }
...@@ -1070,7 +1220,7 @@ public: ...@@ -1070,7 +1220,7 @@ public:
/** May be slow, because all elements /** May be slow, because all elements
following after the erased element have to be copied. following after the erased element have to be copied.
\param index: Index of element to be erased. */ \param index: Index of element to be erased. */
void erase(u32 index) string<T,TAlloc>& erase(u32 index)
{ {
_IRR_DEBUG_BREAK_IF(index>=used) // access violation _IRR_DEBUG_BREAK_IF(index>=used) // access violation
...@@ -1078,10 +1228,11 @@ public: ...@@ -1078,10 +1228,11 @@ public:
array[i-1] = array[i]; array[i-1] = array[i];
--used; --used;
return *this;
} }
//! verify the existing string. //! verify the existing string.
void validate() string<T,TAlloc>& validate()
{ {
// terminate on existing null // terminate on existing null
for (u32 i=0; i<allocated; ++i) for (u32 i=0; i<allocated; ++i)
...@@ -1089,7 +1240,7 @@ public: ...@@ -1089,7 +1240,7 @@ public:
if (array[i] == 0) if (array[i] == 0)
{ {
used = i + 1; used = i + 1;
return; return *this;
} }
} }
...@@ -1103,6 +1254,8 @@ public: ...@@ -1103,6 +1254,8 @@ public:
{ {
used = 0; used = 0;
} }
return *this;
} }
//! gets the last char of a string or null //! gets the last char of a string or null
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment