#ifndef String_h #define String_h #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 = static_cast(-1); protected: /// The internal null terminated string char* str = nullptr; /// Length of the internal null terminated string size_t len = 0; public: // Construction, copy, move, and deletion /// Conversion and default constructor implicit String(const char* s = ""); /// Builds a String with @a capacity bytes /// Internal string is blank with length zero explicit String(size_t capacity); /// 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); /// Copies a null terminated string with assignment operator const String& operator=(const char* cstr); /// Destructor ~String(); public: // Member access /// Get access to the internal string vector inline const char* c_str() const { return str; } inline size_t getLength() const { return len; } inline bool isEmpty() const { return len == 0; } inline bool operator!() const { return len == 0; } inline operator const char*() const = delete; // { return str; } inline char& operator[](size_t i) { return str[i]; } inline const char& operator[](size_t i) const { return str[i]; } public: // Substring String substr(size_t pos, size_t length = npos) const; inline String operator()(size_t pos, size_t length) const { return substr(pos, length); } public: // Concatenation /// Concatenates with a char String operator+(char ch) const; /// Concatenates a char with a string friend String operator+(char ch, const String& text); /// Concatenates with integers inline String operator+(int num) const { return *this + (long long)num; } /// Concatenates with long integers String operator+(long long num) const; /// Concatenates a signed integer with a string friend String operator+(long long num, const String& text); /// Concatenates with long usigned integers String operator+(unsigned long long num) const; /// Concatenates an unsigned integer with a string friend String operator+(unsigned long long num, const String& text); /// Concatenates with double precission floats using the default formatting String operator+(double num) const; /// Concatenates a double with a string using the default formatting friend String operator+(double num, const String& text); /// Concatenates a string object with a null terminated string friend String operator+(const String& text, const char* cstr); /// Concatenates a null terminated string with a string object friend String operator+(const char* cstr, const String& text); /// Concatenates two String objects String operator+(const String& other) const; public: // Comparing /// Returns true if str2 has only a char same as ch in case sensitive way friend bool operator==(char ch, const String& str2); /// Returns true if str1 has only a char same as ch in case sensitive way friend bool operator==(const String& str1, char ch); /// Returns true if str1 and str2 have the same chars, in case sensitive way friend bool operator==(const char* str1, const String& str2); /// Returns true if str1 and str2 have the same chars, in case sensitive way friend bool operator==(const String& str1, const char* str2); /// Returns true if str1 and str2 have the same chars, in case sensitive way friend bool operator==(const String& str1, const String& str2); public: // Searching /// Returns the position of the first occurrence of the char ch in the string starting from start /// Search is case sensitive /// @return The position of ch, @a String::npos if ch is not found size_t find(char ch, size_t start = 0) const; /// Returns the position of the first occurrence of the char ch in the string starting from start /// Search is not case sensitive, therefore lower and uppercase letters are considered equals /// @return The position of ch, @a String::npos if ch is not found size_t findNoCase(char ch, size_t start = 0) const; /// Returns the position of the first occurrence of @a what in the string starting from start /// Search is case sensitive /// @return The position where @what starts, @a String::npos if @what is not found size_t find(const char* what, size_t start = 0) const; /// Returns the position of the first occurrence of the char ch in the string starting from start /// Search is not case sensitive, therefore lower and uppercase letters are considered equals /// @return The position of ch, @a String::npos if ch is not found size_t findNoCase(const char* what, size_t start = 0) const; }; /// Concatenates a char with a string String operator+(char ch, const String& text); /// Concatenates a signed integer with a string inline String operator+(int num, const String& text) { return (long long)num + text; } /// Concatenates a signed long integer with a string String operator+(long long num, const String& text); /// Concatenates an unsigned integer with a string String operator+(unsigned long long num, const String& text); /// Concatenates a double with a string using the default formatting String operator+(double num, const String& text); /// Concatenates a string object with a null terminated string String operator+(const String& text, const char* cstr); /// Concatenates a null terminated string with a string object String operator+(const char* cstr, const String& text); /// Returns true if str2 has only a char same as ch in case sensitive way bool operator==(char ch, const String& str2); /// Returns true if str1 has only a char same as ch in case sensitive way bool operator==(const String& str1, char ch); /// Returns true if str1 and str2 have the same chars, in case sensitive way bool operator==(const char* str1, const String& str2); /// Returns true if str1 and str2 have the same chars, in case sensitive way bool operator==(const String& str1, const char* str2); /// Returns true if str1 and str2 have the same chars, in case sensitive way bool operator==(const String& str1, const String& str2); } // namespace ecci #include /// Allows printing strings in standard output or files, e.g: cout << mystring; inline std::ostream& operator<<(std::ostream& out, const ecci::String& text) { return out << text.c_str(); } #endif // String_h