diff --git a/String.cpp b/String.cpp index c0e3deb..3ec4b01 100644 --- a/String.cpp +++ b/String.cpp @@ -5,12 +5,13 @@ -String::String() +String::String() noexcept :_str(nullptr) {} // string must be null-terminated String::String(const char* string) +:String() { auto size = std::strlen(string) + 1; // make space for null-terminator auto data = new char[size]; @@ -19,7 +20,7 @@ String::String(const char* string) _str = new StringValue(data); // initialize new StringValue with data } -String::String(const String& other) +String::String(const String& other) noexcept { _str = other._str; _str->operator++(); @@ -32,7 +33,7 @@ String& String::operator=(String other) // take parameter by value: can bind return *this; } -String::~String() +String::~String() noexcept { if (_str) _str->operator--(); // the ref-counting is done by StringValue, no need to do that here @@ -45,7 +46,7 @@ void swap(String& s1, String& s2) noexcept swap(s1._str, s2._str); } -String::String(String&& other) : String() // init via default ctor so _str is a nullptr +String::String(String&& other) noexcept : String() // init via default ctor so _str is a nullptr { swap(*this, other); } String String::operator+(const String& rhs) const @@ -120,7 +121,7 @@ Char String::operator[](size_t index) return Char(_str->index_of(&const_cast(const_cast(this)->operator[](index))), *this); } -size_t String::size() const +size_t String::size() const noexcept { if (_str) return _str->size(); diff --git a/String.h b/String.h index 75eab5f..13a9a9f 100644 --- a/String.h +++ b/String.h @@ -18,12 +18,12 @@ private: public: - String(); + String() noexcept; String(const char*); - String(const String&); + String(const String&) noexcept; String& operator=(String); - String(String&&); - ~String(); + String(String&&) noexcept; + ~String() noexcept; friend void swap(String&, String&) noexcept; String operator+(const String&) const; @@ -34,7 +34,7 @@ public: const char& operator[](size_t) const; Char operator[](size_t); - size_t size() const; // does not include null-terminator + size_t size() const noexcept; // does not include null-terminator const char* c_str() const; friend std::istream& operator>>(std::istream&, String&); diff --git a/StringValue.cpp b/StringValue.cpp index cee8e78..f322f61 100644 --- a/StringValue.cpp +++ b/StringValue.cpp @@ -4,14 +4,14 @@ -StringValue::StringValue(char* data) +StringValue::StringValue(char* data) noexcept :_data(data), _size(std::strlen(data) + 1), _refs(1) {} -void StringValue::operator++() +void StringValue::operator++() noexcept { _refs++; } -void StringValue::operator--() +void StringValue::operator--() noexcept { _refs--; if (_refs == 0) @@ -33,10 +33,10 @@ const char& StringValue::operator[](size_t index) const char& StringValue::operator[](size_t index) { return const_cast(const_cast(this)->operator[](index)); } -StringValue::operator const char*() const +StringValue::operator const char*() const noexcept { return _data; } -size_t StringValue::size() const +size_t StringValue::size() const noexcept { return _size - 1; } size_t StringValue::index_of(char* c) const diff --git a/StringValue.h b/StringValue.h index 5cebb8a..cc8205b 100644 --- a/StringValue.h +++ b/StringValue.h @@ -21,12 +21,12 @@ private: public: - StringValue(char* data); - void operator++(); - void operator--(); + StringValue(char* data) noexcept; + void operator++() noexcept; + void operator--() noexcept; const char& operator[](size_t index) const; char& operator[](size_t index); - operator const char*() const; - size_t size() const; + operator const char*() const noexcept; + size_t size() const noexcept; size_t index_of(char* c) const; }; \ No newline at end of file