#ifndef STRING_H #define STRING_H #include namespace ecci { #define implicit typedef long long Length; /// Wraps a C-string pointer, manages its memory automatically, and eases many /// convenience operations, such as conversion, comparatoin and concatenation. class String { public: /// An invalid position, e.g: a non-found character in the String static const Length npos = -1; private: /// Length of characters stored in text Length length; /// Pointer to the text of this String char* text; public: // Construction, copy, move and destruction /// Default constructor and conversion constructor implicit String(const char* cstring = ""); /// Copy constructor String(const String& other); /// Move constructor String(String&& other); /// Destructor (dtor) ~String(); /// Copy assignment operator String& operator=(const String& other); /// Mover assignment operator String& operator=(String&& other); public: // Getters and setters /// Get length inline Length getLength() const { return length; } /// Conversion operator to const char* (dangerous, explicit recommended) inline explicit operator const char*() const { return text; } /// Get access to the internal string inline const char* c_str() const { return text; } /// Get read-only access to a character in the string. We return a reference /// instead of a copy because caller may need to get the character's address inline const char& operator[](Length index) const { return text[index]; } /// Get read-write access to the character at the given index. Notice that no /// index out of bounds checking is done for efficiency inline char& operator[](Length index) { return text[index]; } public: // Concatenation /// Concatenates two String objects String operator+(const String& other) const; /// Concatenates this object with a char String operator+(char ch) const; /// Concatenates a char with a String object friend String operator+(char ch, const String& string); public: // Input/output /// Allows to write this String to a file friend std::ostream& operator<<(std::ostream& out, const String& string) { return out << string.text; } private: /// Capacity constructor (explicit keyword disables automatic conversion constructor) explicit String(Length capacity); #ifdef TESTING private: /// Count of String instances created static size_t instanceCount; public: /// Get the number of String object created, for testing purposes only static size_t getInstanceCount() { return instanceCount; } #endif #ifdef TESTING2 /// The number of this String instance size_t instanceNumber; #endif }; } // namespace ecci #endif // STRING_H