From 57c323318779fb4be2a83a1aedfc6e34d458bd27 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 22 Dec 2023 19:26:41 +0100 Subject: [PATCH] Optimized path_clean_end_slash in String. --- core/string/ustring.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index f8de2eb57..53f921785 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -4205,8 +4205,17 @@ String String::path_clean_end_slash() const { String ret = *this; - while (ret.length() > 1 && (ret.ends_with("/") || ret.ends_with("\\"))) { - ret.resize(ret.length()); + int strip_to = ret.length() - 1; + + CharType c = ret[strip_to]; + + while (strip_to > 1 && (c == '/' || c == '\\')) { + --strip_to; + c = ret[strip_to]; + } + + if (ret.length() != strip_to + 1) { + ret.set_length(strip_to + 1); } return ret;