#ifndef _INCLUDED_L3_STR_H #define _INCLUDED_L3_STR_H // Copyright (C) Krzysztof Bosak, 1998-12-25...1999-11-12. // All rights reserved. // kbosak@box43.pl // http://www.kbosak.prv.pl // *TextString* #include #include #include #include "l3_port.h" ////////////////////////////////////////////////////////////////////////////// /* // Unused, but possible: #include #ifdef WIN32 typedef std::string TextString; #else class TextString: public std::string { public: inline TextString() { } inline TextString(const char * const ptr) : std::string(ptr) { } inline char& operator[](int i) { assert(i>=0); assert(i(length())); return at(i); } inline const char & operator[](int i) const { assert(i>=0); assert(i(length())); return at(i); } }; #endif */ ////////////////////////////////////////////////////////////////////////////// #include "l3_array.h" class TextString { // Small subset of std::string, without reference counting (often faster) // and with strict access control. basearray_s _string; public: inline TextString(): _string(1) // EXCEPTION NEUTRAL { _string[0]='\0'; } inline TextString(const char * const str) // EXCEPTION NEUTRAL // Implicit. : _string(static_cast(strlen(str)+1)) { // Makes sense to use explicit here: str+=56+"abc"; // Should be explicit because of l3_arrayheap assert(str!=NULL); if(str!=NULL) { strcpy(&_string[0], str); } else { _string.setsize(1); _string[0]=0; } } inline void resize(int requested_size) // EXCEPTION NEUTRAL { _string.resize(requested_size); } /* inline int size() const // NOTHROW { return length(); } */ inline int length() const // NOTHROW { return static_cast(strlen(&_string[0])); } inline int capacity() const // NOTHROW { return static_cast(_string.size()); } inline const char * c_str() const // NOTHROW { return &_string[0]; } inline const char * data() const // NOTHROW { return &_string[0]; } inline char& operator[](int i) // EXCEPTION NEUTRAL { assert(i>=0); assert(i < static_cast(strlen(&_string[0]))); return _string[i]; } inline char operator[](int i) const // EXCEPTION NEUTRAL { assert(i>=0); assert(i < static_cast(strlen(&_string[0]))); return _string[i]; } friend inline bool operator<(const TextString &a, const TextString &b) // NOTHROW { return strcmp(&a._string[0], &b._string[0])>0; } friend inline bool operator>(const TextString &a, const TextString &b) // NOTHRO { return strcmp(&a._string[0], &b._string[0])<0; } friend inline bool operator<=(const TextString &a, const TextString &b) // NOTHROW { return strcmp(&a._string[0], &b._string[0])>=0; } friend inline bool operator>=(const TextString &a, const TextString &b) // NOTHROW { return strcmp(&a._string[0], &b._string[0])<=0; } friend inline bool operator==(const TextString &a, const TextString &b) // NOTHROW { return strcmp(&a._string[0], &b._string[0])==0; } friend inline bool operator==(const TextString &a, const char * const b) // NOTHROW { assert(b!=NULL); return strcmp(&a._string[0], b)==0; } friend inline bool operator!=(const TextString &a, const TextString &b) // NOTHROW { return strcmp(&a._string[0], &b._string[0])!=0; } friend inline bool operator!=(const TextString &a, const char * const b) // NOTHROW { assert(b!=NULL); return strcmp(&a._string[0], b)!=0; } TextString& operator+=(const TextString& s); // EXCEPTION NEUTRAL inline TextString& operator+=(int number) // EXCEPTION NEUTRAL { char buffer[36]; buffer[35]='\0'; sprintf(buffer, "%d", number); assert(buffer[35]=='\0'); buffer[35]='\0'; return operator+=(TextString(&buffer[0]));// OK to return by reference. } inline TextString& operator+=(double number) // EXCEPTION NEUTRAL { char buffer[36]; buffer[35]='\0'; sprintf(buffer, "%f", number); assert(buffer[35]=='\0'); buffer[35]='\0'; return operator+=(TextString(&buffer[0]));// OK to return by reference. } friend inline const TextString operator+(const TextString& s1, const TextString& s2) // EXCEPTION NEUTRAL { TextString result(s1); result.operator+=(s2); return result; } friend inline const TextString operator+(const TextString& s1, int number) // EXCEPTION NEUTRAL { TextString result(s1); result.operator+=(number); return result; } friend inline const TextString operator+(const TextString& s1, double number) // EXCEPTION NEUTRAL { TextString result(s1); result.operator+=(number); return result; } }; TextString& TextString::operator+=(const TextString& s) // EXCEPTION NEUTRAL { if(&s==this) { const int len=length(); const int len_pow2=len<<1; _string.resize(len_pow2+1); _string[len_pow2]='\0'; if(len>0) { memcpy(&_string[len], &_string[0], len*sizeof(char)); } } else { const int len=length(); const int s_len=s.length(); const int total_len=len+s_len; _string.resize(total_len+1); _string[total_len]='\0'; if(s_len>0) { memcpy(&_string[len], &s._string[0], s_len*sizeof(char)); } } return *this; } inline ostream& operator<<(ostream& str, const TextString& text) { return str<