// Copyright (C) 2002-2005 Nikolaus Gebhardt // This file is part of the "Irrlicht Engine" and the "irrXML" project. // For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h #ifndef __IRR_STRING_H_INCLUDED__ #define __IRR_STRING_H_INCLUDED__ #include "irrTypes.h" namespace irr { namespace core { //! Very simple string class with some useful features. /** string and string work both with unicode AND ascii, so you can assign unicode to string and ascii to string (and the other way round) if your ever would want to. Note that the conversation between both is not done using an encoding. Known bugs: Special characters like 'Ä', 'Ü' and 'Ö' are ignored in the methods make_upper, make_lower and equals_ignore_case. */ template class string { public: //! Default constructor string() : allocated(1), used(1), array(0) { array = new T[1]; array[0] = 0x0; } //! Constructor string(const string& other) : allocated(0), used(0), array(0) { *this = other; } //! Constructs a string from an int string(int number) : allocated(0), used(0), array(0) { // store if negative and make positive bool negative = false; if (number < 0) { number *= -1; negative = true; } // temporary buffer for 16 numbers c8 tmpbuf[16]; tmpbuf[15] = 0; s32 idx = 15; // special case '0' if (!number) { tmpbuf[14] = '0'; *this = &tmpbuf[14]; return; } // add numbers while(number && idx) { idx--; tmpbuf[idx] = (c8)('0' + (number % 10)); number = number / 10; } // add sign if (negative) { idx--; tmpbuf[idx] = '-'; } *this = &tmpbuf[idx]; } //! Constructor for copying a string from a pointer with a given lenght template string(const B* c, s32 lenght) : allocated(0), used(0), array(0) { if (!c) return; allocated = used = lenght+1; array = new T[used]; for (s32 l = 0; l string(const B* c) : allocated(0), used(0), array(0) { *this = c; } //! destructor ~string() { delete [] array; } //! Assignment operator string& operator=(const string& other) { if (this == &other) return *this; delete [] array; allocated = used = other.size()+1; array = new T[used]; const T* p = other.c_str(); for (s32 i=0; i string& operator=(const B* c) { if (!c) { if (!array) { array = new T[1]; allocated = 1; used = 1; } array[0] = 0x0; return *this; } if ((void*)c == (void*)array) return *this; s32 len = 0; const B* p = c; while(*p) { ++len; ++p; } // we'll take the old string for a while, because the new string could be // a part of the current string. T* oldArray = array; allocated = used = len+1; array = new T[used]; for (s32 l = 0; l operator+(const string& other) { string str(*this); str.append(other); return str; } //! Add operator for strings, ascii and unicode template string operator+(const B* c) { string str(*this); str.append(c); return str; } //! Direct access operator T& operator [](const s32 index) const { _IRR_DEBUG_BREAK_IF(index>=used) // bad index return array[index]; } //! Comparison operator bool operator ==(const T* str) const { int i; for(i=0; array[i] && str[i]; ++i) if (array[i] != str[i]) return false; return !array[i] && !str[i]; } //! Comparison operator bool operator ==(const string& other) const { for(s32 i=0; array[i] && other.array[i]; ++i) if (array[i] != other.array[i]) return false; return used == other.used; } //! Is smaller operator bool operator <(const string& other) const { for(s32 i=0; array[i] && other.array[i]; ++i) if (array[i] != other.array[i]) return (array[i] < other.array[i]); return used < other.used; } //! Equals not operator bool operator !=(const string& other) const { return !(*this == other); } //! Returns length of string /** \return Returns length of the string in characters. */ s32 size() const { return used-1; } //! Returns character string /** \return Returns pointer to C-style zero terminated string. */ const T* c_str() const { return array; } //! Makes the string lower case. void make_lower() { const T A = (T)'A'; const T Z = (T)'Z'; const T diff = (T)'a' - A; for (s32 i=0; i=A && array[i]<=Z) array[i] += diff; } } //! Makes the string upper case. void make_upper() { const T a = (T)'a'; const T z = (T)'z'; const T diff = (T)'A' - a; for (s32 i=0; i=a && array[i]<=z) array[i] += diff; } } //! Compares the string ignoring case. /** \param other: Other string to compare. \return Returns true if the string are equal ignoring case. */ bool equals_ignore_case(const string& other) const { for(s32 i=0; array[i] && other[i]; ++i) if (toLower(array[i]) != toLower(other[i])) return false; return used == other.used; } //! compares the first n characters of the strings bool equalsn(const string& other, int len) { int i; for(i=0; array[i] && other[i] && i < len; ++i) if (array[i] != other[i]) return false; // if one (or both) of the strings was smaller then they // are only equal if they have the same lenght return (i == len) || (used == other.used); } //! compares the first n characters of the strings bool equalsn(const T* str, int len) { int i; for(i=0; array[i] && str[i] && i < len; ++i) if (array[i] != str[i]) return false; // if one (or both) of the strings was smaller then they // are only equal if they have the same lenght return (i == len) || (array[i] == 0 && str[i] == 0); } //! Appends a character to this string /** \param character: Character to append. */ void append(T character) { if (used + 1 > allocated) reallocate((s32)used + 1); used += 1; array[used-2] = character; array[used-1] = 0; } //! Appends a string to this string /** \param other: String to append. */ void append(const string& other) { --used; s32 len = other.size(); if (used + len + 1 > allocated) reallocate((s32)used + (s32)len + 1); for (s32 l=0; l& other, s32 length) { s32 len = other.size(); if (len < length) { append(other); return; } len = length; --used; if (used + len > allocated) reallocate((s32)used + (s32)len); for (s32 l=0; l s32 findFirstCharNotInList(B* c, int count) const { for (int i=0; i s32 findLastCharNotInList(B* c, int count) const { for (int i=used-2; i>=0; --i) { int j; for (j=0; j=0; --i) if (array[i] == c) return i; return -1; } //! Returns a substring //! \param begin: Start of substring. //! \param length: Length of substring. string subString(s32 begin, s32 length) { if (length <= 0) return string(""); string o; o.reserve(length+1); for (s32 i=0; i& other) { append(other); } void operator += (int i) { append(string(i)); } //! replaces all characters of a special type with another one void replace(T toReplace, T replaceWith) { for (s32 i=0; i=used || index<0) // access violation for (int i=index+1; i=(T)'A' && t<=(T)'Z') return t + ((T)'a' - (T)'A'); else return t; } //! Reallocate the array, make it bigger or smaler void reallocate(s32 new_size) { T* old_array = array; array = new T[new_size]; allocated = new_size; s32 amount = used < new_size ? used : new_size; for (s32 i=0; i stringc; //! Typedef for wide character strings typedef string stringw; } // end namespace core } // end namespace irr #endif