Browse Source

Added string_clear.

David Piuva 5 years ago
parent
commit
f4ee6ad7d7
2 changed files with 9 additions and 0 deletions
  1. 5 0
      Source/DFPSR/base/text.cpp
  2. 4 0
      Source/DFPSR/base/text.h

+ 5 - 0
Source/DFPSR/base/text.cpp

@@ -721,6 +721,11 @@ void String::cloneIfShared() {
 	}
 }
 
+void dsr::string_clear(String& target) {
+	target.cloneIfShared();
+	target.length = 0;
+}
+
 void String::expand(int64_t newLength, bool affectUsedLength) {
 	if (newLength > this->length) {
 		if (newLength > this->capacity()) {

+ 4 - 0
Source/DFPSR/base/text.h

@@ -201,6 +201,10 @@ std::ostream& string_toStream(std::ostream& target, const T& source) {
 // ---------------- Procedural API ----------------
 
 
+// Sets the target string's length to zero.
+// Because this opens up to appending new text where sub-string may already share the buffer,
+//   this operation will reallocate the buffer if shared with other strings.
+void string_clear(String& target);
 // Post-condition: Returns the length of source.
 //   Example: string_length(U"ABC") == 3
 int64_t string_length(const ReadableString& source);