From bdfc14357a6ccbc28dbacb6b66959b72438d5f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sat, 29 Oct 2016 20:08:55 +0200 Subject: [PATCH] made some final changes to the comments --- String.cpp | 4 +++- StringValue.cpp | 2 +- manualtest.cpp | 3 ++- test.cpp | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/String.cpp b/String.cpp index 757078d..aab3b94 100644 --- a/String.cpp +++ b/String.cpp @@ -151,8 +151,9 @@ std::istream& operator>>(std::istream& is, String& str) { constexpr size_t BUFSIZE = 50; // set some reasonable buffer size char buf[BUFSIZE]; - std::unique_ptr data(new char[1]); // istream data - I use unique_ptr for exception safety + std::unique_ptr data(new char[1]); // istream data - unique_ptr for exception safety data.get()[0] = '\0'; // init it with a null terminator + while (is.get(buf, BUFSIZE)) // while there is still some data in the stream { std::unique_ptr newdata(new char[strlen(data.get()) + BUFSIZE]); // make some space @@ -160,6 +161,7 @@ std::istream& operator>>(std::istream& is, String& str) data.swap(newdata); // make the old data go out of scope instead of newdata std::strcat(data.get(), buf); // append new input } + std::unique_ptr newdata(new char[strlen(data.get()) + 1]); // shrink the array std::strcpy(newdata.get(), data.get()); // to fit the data data.swap(newdata); diff --git a/StringValue.cpp b/StringValue.cpp index eb013d5..7ae1e70 100644 --- a/StringValue.cpp +++ b/StringValue.cpp @@ -28,7 +28,7 @@ const char& StringValue::operator[](size_t index) const return _data[index]; } -// reuse const operator[], just as Scott Meyers would +// reuse const operator[] char& StringValue::operator[](size_t index) { return const_cast(const_cast(this)->operator[](index)); } diff --git a/manualtest.cpp b/manualtest.cpp index 535d8bb..ab5537e 100644 --- a/manualtest.cpp +++ b/manualtest.cpp @@ -7,7 +7,8 @@ /* This is a demonstration, that the String class and it's components work - * correctly, basically a non-unit test. Perfect for usage with valgrind */ + * correctly, basically a non-unit test. Perfect for usage with valgrind. + * Note that these tests are only used in case GTest is not present on the system. */ int main() { if (std::is_default_constructible::value) diff --git a/test.cpp b/test.cpp index 3394c4c..bc79be5 100644 --- a/test.cpp +++ b/test.cpp @@ -10,7 +10,8 @@ /* These unit tests were written using the Google Test framework, - * and check everything stated in the specification. */ + * and check everything stated in the specification. Note that + * these tests are only used in case GTest is present on the system.*/ /////////////////////////////////////////////////////////////// /// Testing helper classes /// ///////////////////////////////////////////////////////////////