diff options
author | Nicolas Catania <niko@google.com> | 2010-02-09 09:32:14 -0800 |
---|---|---|
committer | Nicolas Catania <niko@google.com> | 2010-02-09 09:32:14 -0800 |
commit | 464136e01a1facf09ce3befccbfc04f2d1da8d5b (patch) | |
tree | 90b9487e7a883841c42c47e4d742f59747a00a48 | |
parent | 6309a85f3be27b49451e37d0b31446e0cf727f23 (diff) | |
download | astl-464136e01a1facf09ce3befccbfc04f2d1da8d5b.tar.gz |
Added insert(iterator, char); method.
-rw-r--r-- | include/string | 7 | ||||
-rw-r--r-- | src/string.cpp | 25 | ||||
-rw-r--r-- | tests/test_string.cpp | 27 |
3 files changed, 59 insertions, 0 deletions
diff --git a/include/string b/include/string index dccd5ac..428d1db 100644 --- a/include/string +++ b/include/string @@ -235,6 +235,13 @@ class string return string(&left, 1).append(right); } + // Insert a copy of c before the character referred to by pos. + // @param pos A valid iterator on *this. + // @return An iterator which refers to the copy of the inserted + // character. Because internally some reallocation may occur, the + // returned iterator may be different from 'pos'. + iterator insert(iterator pos, char c); + // Find the position of a sub-string. The empty string is always // found at the requested position except when it's beyond the // string's end. diff --git a/src/string.cpp b/src/string.cpp index d2457fc..5c04c8e 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -516,6 +516,31 @@ string& string::assign(const value_type *array, size_type n) return *this; } +string::iterator string::insert(iterator iter, char c) { + const size_type new_len = mLength + 1; + char *base = iter.base(); + + if (base < mData || base > mData + mLength || new_len < mLength) { + return iterator(&sDummy); // out of bound || overflow + } + + const size_type pos = base - mData; + if (new_len > mCapacity) { + reserve(new_len); + if (new_len > mCapacity) { + return iterator(&sDummy); // not enough memory? + } + } + // At this point 'iter' and 'base' are not valid anymore since + // realloc could have taken place. + base = mData + pos; + std::memmove(base + 1, base, mLength - pos); + *base = c; + mLength = new_len; + mData[mLength] = 0; + return iterator(base); +} + string& string::operator=(char c) { clear(); diff --git a/tests/test_string.cpp b/tests/test_string.cpp index c2405ae..0a9c1cc 100644 --- a/tests/test_string.cpp +++ b/tests/test_string.cpp @@ -1038,6 +1038,32 @@ bool testCharSearch() { return true; } + +bool testInsert() { + { + string::iterator res; + string str("zzzzzz"); + res = str.insert(str.begin(), 'a'); + EXPECT_TRUE(str == "azzzzzz"); + EXPECT_TRUE(*res == 'a'); + + res = str.insert(str.begin() + 3, 'b'); + EXPECT_TRUE(str == "azzbzzzz"); + EXPECT_TRUE(*res == 'b'); + + res = str.insert(str.end(), 'c'); + EXPECT_TRUE(str == "azzbzzzzc"); + EXPECT_TRUE(*res == 'c'); + } + { + string str; + string::iterator res = str.insert(str.begin(), 'a'); + EXPECT_TRUE(str == "a"); + EXPECT_TRUE(*res == 'a'); + } + return true; +} + } // namespace android int main(int argc, char **argv) @@ -1067,5 +1093,6 @@ int main(int argc, char **argv) FAIL_UNLESS(testForwardIterator); FAIL_UNLESS(testSubstr); FAIL_UNLESS(testCharSearch); + FAIL_UNLESS(testInsert); return kPassed; } |