#include "String.h" #include #include String::String(const char* str) : str(strdup(str)) , len(strlen(str)) { } String::String(const String& other) : str(strdup(other.str)) , len(other.len) { } const String& String::operator=(const String& other) { if ( this != &other ) { free(str); str = strdup(other.str); len = other.len; } return *this; } String::~String() { free(str); } String operator+(const String& s1, const String& s2) { String::length_t result_length = s1.len + s2.len; String result(result_length + 1); strcpy(result.str, s1.str); strcpy(result.str + s1.len, s2.str); result.len = result_length; return result; } String String::operator+(char ch) const { length_t result_length = len + 1; String result(result_length + 1); strcpy(result.str, str); result.str[len] = ch; result.str[result_length] = '\0'; result.len = result_length; return result; } String::String(length_t capacity) : str( (char*) malloc(capacity * sizeof(char)) ) , len(0) { *str = '\0'; } String String::operator()(length_t pos, length_t len) const { len = len > 0 ? len : this->len - pos; String result(len); strncpy(result.str, str, len); result.len = len; return result; } std::ostream& operator<<(std::ostream& output, const String& str) { return output << str.c_str(); }