#ifndef STRING_H #define STRING_H #include #include #define implicit namespace ecci { /** A convenient class for managing null terminated strings. Internal string is stored in simple char arrays, therefore it is not suited for Unicode or other character sets than ASCII. */ class String { public: /// No valid position marker static const size_t npos; private: /// Points to the first char in dynamic memory where the C-style string is stored char* str; /// The length of the stored string size_t len; public: // Construction, copy, move, and deletion /// Default and conversion constructor (ctor) implicit String(const char* cstr = ""); /// Builds a String with enoguh capacity to allocate a text of the given length explicit String(size_t initialLength); /// Copy constructor String(const String& other); /// Move constructor String(String&& temp); /// Copy assignment operator const String& operator=(const String& other); /// Move assignment operator const String& operator=(String&& temp); /// Destructor (dtor) ~String(); public: // Member access /// Get access to the internal c-style string inline const char* c_str() const { return str; } /// Conversion operator: Converts a String to const char* /// Automatic conversions are dangerous, therefore this operator is deleted //inline operator const char*() const = delete; /// Returns the lenght of this string inline size_t getLength() const { return len; } /// Get read-and-write access to the i-th character of the string /// @remarks This method assumes i is valid for efficiency purporses /// @param i A valid index between 0 and lenght - 1, otherwise the program may crash inline char& operator[](size_t i) { return str[i]; } /// Get read-only access to the i-th character of the string /// @remarks This method assumes i is valid for efficiency purporses /// @param i A valid index between 0 and lenght - 1, otherwise the program may crash inline const char& operator[](size_t i) const { return str[i]; } public: // Comparison /// Returns true if the contents of this String equals the contents of the @a other String /// Comparison considers lowercase and uppercase as different, for example: /// String("hi") == String("Hi") returns false bool operator==(const String& other) const; public: // Concatenation /// Concatenates this string with a c-style string /// @return A new string object containing the result of the concatenation String operator+(const char* cstr) const; /// Produces a new string object by concatenating two existing String objects friend String operator+(const String& a, const String& b); /// Produces a new string object by concatenating a C-style string with a String object friend String operator+(const char* cstr, const String& string); /// Produces a new string object by concatenating a String object with a int number friend String operator+(const String& string, int num); /// Produces a new string object by concatenating an integer with a String object friend String operator+(int num, const String& string); #ifdef TESTING private: static size_t instanceCount; public: static size_t getInstanceCount() { return instanceCount; } #endif }; /// Produces a new string object by concatenating two existing String objects String operator+(const String& a, const String& b); /// Produces a new string object by concatenating a C-style string with a String object String operator+(const char* cstr, const String& string); /// Produces a new string object by concatenating a String object with a int number String operator+(const String& string, int num); /// Produces a new string object by concatenating an integer with a String object String operator+(int num, const String& string); } // namespace ecci /// Allows printng a String object to standard output or file, e.g: std::cout << mystring inline std::ostream& operator<<(std::ostream& out, const ecci::String& str) { return out << str.c_str(); } #endif // STRING_H