[Scummvm-cvs-logs] scummvm master -> a00c3e75b0921207bc386de4310899cf964517bd

lordhoto lordhoto at gmail.com
Mon Jul 1 14:19:41 CEST 2013


This automated email contains information about 2 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
e1ff60da7a COMMON: Add erase method to String class
a00c3e75b0 Merge pull request #342 from RichieSams/addDeleteRangeStringMethod


Commit: e1ff60da7aa311793cc424f23495442756762ee8
    https://github.com/scummvm/scummvm/commit/e1ff60da7aa311793cc424f23495442756762ee8
Author: richiesams (adastley at gmail.com)
Date: 2013-06-20T13:40:25-07:00

Commit Message:
COMMON: Add erase method to String class

Changed paths:
    common/str.cpp
    common/str.h
    test/common/str.h



diff --git a/common/str.cpp b/common/str.cpp
index 5d647ee..4a10792 100644
--- a/common/str.cpp
+++ b/common/str.cpp
@@ -361,6 +361,25 @@ void String::deleteChar(uint32 p) {
 	_size--;
 }
 
+void String::erase(uint32 p, uint32 len) {
+	assert(p < _size);
+
+	makeUnique();
+	// If len == npos or p + len is over the end, remove all the way to the end
+	if (len == npos || p + len >= _size) {
+		// Delete char at p as well. So _size = (p - 1) + 1
+		_size = p;
+		// Null terminate
+		_str[_size] = 0;
+		return;
+	}
+
+	for ( ; p + len <= _size; p++) {
+		_str[p] = _str[p + len];
+	}
+	_size -= len;
+}
+
 void String::clear() {
 	decRefCount(_extern._refCount);
 
diff --git a/common/str.h b/common/str.h
index 5039130..6b4475e 100644
--- a/common/str.h
+++ b/common/str.h
@@ -43,6 +43,8 @@ namespace Common {
  * behavior in some operations.
  */
 class String {
+public:
+	static const uint32 npos = 0xFFFFFFFF;
 protected:
 	/**
 	 * The size of the internal storage. Increasing this means less heap
@@ -191,6 +193,9 @@ public:
 	/** Remove the character at position p from the string. */
 	void deleteChar(uint32 p);
 
+	/** Remove all characters from position p to the p + len. If len = String::npos, removes all characters to the end */
+	void erase(uint32 p, uint32 len = npos);
+
 	/** Set character c at position p, replacing the previous character there. */
 	void setChar(char c, uint32 p);
 
diff --git a/test/common/str.h b/test/common/str.h
index 2c563f3..adc6a09 100644
--- a/test/common/str.h
+++ b/test/common/str.h
@@ -243,6 +243,14 @@ class StringTestSuite : public CxxTest::TestSuite
 		TS_ASSERT_EQUALS(str, "012345678923456789012345678901");
 	}
 
+	void test_erase() {
+		Common::String str("01234567890123456789012345678901");
+		str.erase(18);
+		TS_ASSERT_EQUALS(str, "012345678901234567");
+		str.erase(7, 5);
+		TS_ASSERT_EQUALS(str, "0123456234567");
+	}
+
 	void test_sharing() {
 		Common::String str("01234567890123456789012345678901");
 		Common::String str2(str);


Commit: a00c3e75b0921207bc386de4310899cf964517bd
    https://github.com/scummvm/scummvm/commit/a00c3e75b0921207bc386de4310899cf964517bd
Author: Johannes Schickel (lordhoto at gmail.com)
Date: 2013-07-01T05:18:49-07:00

Commit Message:
Merge pull request #342 from RichieSams/addDeleteRangeStringMethod

COMMON: Add erase method to String class

Changed paths:
    common/str.cpp
    common/str.h
    test/common/str.h









More information about the Scummvm-git-logs mailing list