Added missing substr() method needed by gtest. Collapsed 3 copy constructors into one with default values for the pos and n values. string(const string&, size_t pos=0, size_t n=npos) Extra test for substr and the copy constructor.
diff --git a/include/string b/include/string index f1cfe93..552c37b 100644 --- a/include/string +++ b/include/string
@@ -61,17 +61,12 @@ // Constructors string(); - string(const string& str); - // Construct a string from a source's substring. // @param str The source string. // @param pos The index of the character to start the copy at. // @param n The number of characters to copy. Use string::npos for the // remainder. - string(const string& str, size_t pos, size_type n); - - // Same as above but implicitly copy from pos to the end of the str. - string(const string& str, size_type pos); + string(const string& str, size_t pos = 0, size_type n = npos); // Construct a string from a C string. // @param str The source string, must be '\0' terminated. @@ -241,6 +236,11 @@ // starting position. size_type find(const value_type *str, size_type pos = 0) const; + // @return a substring of this one. + string substr(size_type pos = 0, size_type n = npos) const { + return string(*this, pos, n); + } + private: bool SafeMalloc(size_type n); void SafeRealloc(size_type n);
diff --git a/src/string.cpp b/src/string.cpp index bb9e35a..b80fd78 100644 --- a/src/string.cpp +++ b/src/string.cpp
@@ -136,12 +136,6 @@ mCapacity = 0; } -void string::Constructor(const value_type *str, size_type n) -{ - Constructor(str, 0, n); -} - - void string::Constructor(const value_type *str, size_type pos, size_type n) { // Enough data and no overflow @@ -174,40 +168,29 @@ ConstructEmptyString(); } -string::string(const string& str) -{ - Constructor(str.mData, str.mLength); -} - string::string(const string& str, size_type pos, size_type n) { - if (pos < str.mLength && n <= (str.mLength - pos)) - { - Constructor(str.mData + pos , n); - } - else - { - ConstructEmptyString(); - } -} - -string::string(const string& str, size_type pos) -{ if (pos < str.mLength) { - Constructor(str.mData, pos, str.mLength - pos); + if (npos == n) + { + Constructor(str.mData, pos , str.mLength - pos); + return; + } + else if (n <= (str.mLength - pos)) + { + Constructor(str.mData, pos , n); + return; + } } - else - { - ConstructEmptyString(); - } + ConstructEmptyString(); } string::string(const value_type *str) { if (NULL != str) { - Constructor(str, strlen(str)); + Constructor(str, 0, strlen(str)); } else { @@ -217,7 +200,14 @@ string::string(const value_type *str, size_type n) { - Constructor(str, n); + if (npos != n) + { + Constructor(str, 0, n); + } + else + { + ConstructEmptyString(); // standard requires we throw length_error here. + } } // Char repeat constructor. @@ -230,7 +220,7 @@ { if (begin < end) { - Constructor(begin, end - begin); + Constructor(begin, 0, end - begin); } else { @@ -455,7 +445,7 @@ string& string::assign(const string& str) { clear(); - Constructor(str.mData, str.mLength); + Constructor(str.mData, 0, str.mLength); return *this; } @@ -480,7 +470,7 @@ return *this; } clear(); - Constructor(str, strlen(str)); + Constructor(str, 0, strlen(str)); return *this; } @@ -491,7 +481,7 @@ return *this; } clear(); - Constructor(array, n); + Constructor(array, 0, n); return *this; } @@ -529,10 +519,7 @@ { return string::npos; } - - const std::ptrdiff_t delta = idx - mData; - - return static_cast<size_type>(delta); + return static_cast<size_type>(idx - mData); } } // namespace std
diff --git a/tests/test_string.cpp b/tests/test_string.cpp index bd69a46..ecff20e 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp
@@ -96,7 +96,10 @@ string str2 (str1); EXPECT_TRUE(str1.size() == 21); - const string str3("scott mills cracks me up"); + const char literal[] = "scott mills cracks me up"; + const string str3(literal); + EXPECT_TRUE(str3 == literal); + string str4(str3, 12); EXPECT_TRUE(strcmp("cracks me up", str4.c_str()) == 0); @@ -115,6 +118,12 @@ string str9(str3, 24, 1); EXPECT_TRUE(strcmp("", str9.c_str()) == 0); + string str10(str3, 0); + EXPECT_TRUE(strcmp(literal, str10.c_str()) == 0); + + string str11(str3, 6); + EXPECT_TRUE(strcmp("mills cracks me up", str11.c_str()) == 0); + return true; } @@ -867,6 +876,32 @@ return true; } +bool testSubstr() +{ + const char literal[] = "basement jaxx"; + const string str01(literal); + string str02; + + str02 = str01.substr(0, 5); + EXPECT_TRUE(str02 == "basem"); + + str02 = str01.substr(0, 8); + EXPECT_TRUE(str02 == "basement"); + + str02 = str01.substr(0, string::npos); + EXPECT_TRUE(str02 == "basement jaxx"); + + str02 = str01.substr(); + EXPECT_TRUE(str02 == "basement jaxx"); + + str02 = str01.substr(9); + EXPECT_TRUE(str02 == "jaxx"); + + str02 = str01.substr(9, string::npos); + EXPECT_TRUE(str02 == "jaxx"); + return true; +} + } // namespace android int main(int argc, char **argv) @@ -891,5 +926,6 @@ FAIL_UNLESS(testCapacity); FAIL_UNLESS(testClear); FAIL_UNLESS(testErase); + FAIL_UNLESS(testSubstr); return kPassed; }