aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorrichiesams2013-06-19 17:27:05 -0500
committerrichiesams2013-06-20 15:40:25 -0500
commite1ff60da7aa311793cc424f23495442756762ee8 (patch)
treeff1895515adf97eae9934a9a7026ee6db68ac706 /common
parent4401f40f7203f7aaa78c2e1c904c70902325436c (diff)
downloadscummvm-rg350-e1ff60da7aa311793cc424f23495442756762ee8.tar.gz
scummvm-rg350-e1ff60da7aa311793cc424f23495442756762ee8.tar.bz2
scummvm-rg350-e1ff60da7aa311793cc424f23495442756762ee8.zip
COMMON: Add erase method to String class
Diffstat (limited to 'common')
-rw-r--r--common/str.cpp19
-rw-r--r--common/str.h5
2 files changed, 24 insertions, 0 deletions
diff --git a/common/str.cpp b/common/str.cpp
index 5d647ee4f0..4a10792373 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 5039130707..6b4475e1c4 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);