#include "String.h" #include namespace ecci { String::String(const wchar_t* initial) : str(nullptr) , length( wcslen(initial) ) { std::wcerr << L"default/conversion constructor\n"; str = (wchar_t*) malloc( (length + 1) * sizeof(wchar_t) ); for ( LengthType index = 0; index <= length; ++index ) str[index] = initial[index]; } String::String(const String& other) : str(nullptr) , length( other.length ) { std::wcerr << L"copy constructor\n"; str = (wchar_t*) malloc( (length + 1) * sizeof(wchar_t) ); for ( LengthType index = 0; index <= length; ++index ) str[index] = other.str[index]; } String::String(String&& temp) : str(temp.str) , length( temp.length ) { std::wcerr << L"move constructor\n"; temp.str = nullptr; temp.length = 0; } String::~String() { std::wcerr << L"Destroying [" << (str ? str : L"nullptr") << "]\n"; free(str); } String& String::operator=(const String& other) { std::wcerr << L"assignment operator=\n"; if ( this != &other ) { if ( length != other.length ) { free(str); length = other.length; str = (wchar_t*) malloc( (length + 1) * sizeof(wchar_t) ); } for ( LengthType index = 0; index <= length; ++index ) str[index] = other.str[index]; } return *this; } String& String::operator=(String&& temp) { std::wcerr << L"move assignment operator=\n"; if ( this != &temp ) { free(str); str = temp.str; length = temp.length; temp.str = nullptr; temp.length = 0; } return *this; } std::basic_ostream& operator<<(std::basic_ostream& out, const String& string) { return out << string.str; } FILE* String::print(FILE* out) const { fwprintf(stdout, L"%ls", str); return out; } } // namespace ecci