[Scummvm-git-logs] scummvm master -> 3adb30e4bf20634ef111ec810206bb91b3c06820

sev- noreply at scummvm.org
Sun May 14 21:07:36 UTC 2023


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

Summary:
3adb30e4bf COMMON: Avoid makeUnique on string if trim wouldn't modify it.


Commit: 3adb30e4bf20634ef111ec810206bb91b3c06820
    https://github.com/scummvm/scummvm/commit/3adb30e4bf20634ef111ec810206bb91b3c06820
Author: elasota (ejlasota at gmail.com)
Date: 2023-05-14T23:07:32+02:00

Commit Message:
COMMON: Avoid makeUnique on string if trim wouldn't modify it.

Changed paths:
    common/str-base.cpp


diff --git a/common/str-base.cpp b/common/str-base.cpp
index c38f33b5192..54d02b20865 100644
--- a/common/str-base.cpp
+++ b/common/str-base.cpp
@@ -742,22 +742,36 @@ TEMPLATE void BASESTRING::trim() {
 	if (_size == 0)
 		return;
 
+	uint numLeading = 0;
+	while (numLeading < _size) {
+		if (isSpace(_str[numLeading]))
+			numLeading++;
+		else
+			break;
+	}
+
+	uint numTrailing = 0;
+	if (numLeading != _size) {
+		while (numTrailing < _size) {
+			if (isSpace(_str[_size - 1 - numTrailing]))
+				numTrailing++;
+			else
+				break;
+		}
+	}
+
+	if (numLeading == 0 && numTrailing == 0)
+		return;
+
 	makeUnique();
 
-	// Trim trailing whitespace
-	while (_size >= 1 && isSpace(_str[_size - 1]))
-		--_size;
-	_str[_size] = 0;
+	uint newSize = _size - numLeading - numTrailing;
 
-	// Trim leading whitespace
-	value_type *t = _str;
-	while (isSpace(*t))
-		t++;
+	if (numLeading > 0)
+		memmove(_str, _str + numLeading, newSize);
 
-	if (t != _str) {
-		_size -= t - _str;
-		memmove(_str, t, (_size + 1) * sizeof(_str[0]));
-	}
+	_str[newSize] = 0;
+	_size = newSize;
 }
 #endif
 




More information about the Scummvm-git-logs mailing list