From 54123d3ffd393903bd5211cfc76cecb51a0b671b Mon Sep 17 00:00:00 2001 From: dgolear Date: Tue, 5 Aug 2025 13:50:26 +0300 Subject: YT: Make trim helpers noncopying; lesser copies in rich ypath parsing commit_hash:88244dc0263029f091579dd66bba1e03132449fe --- library/cpp/yt/string/string.cpp | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) (limited to 'library/cpp/yt/string/string.cpp') diff --git a/library/cpp/yt/string/string.cpp b/library/cpp/yt/string/string.cpp index a346219144c..d155da578e4 100644 --- a/library/cpp/yt/string/string.cpp +++ b/library/cpp/yt/string/string.cpp @@ -65,42 +65,26 @@ TString CamelCaseToUnderscoreCase(TStringBuf str) //////////////////////////////////////////////////////////////////////////////// -TString TrimLeadingWhitespaces(TStringBuf str) +[[nodiscard]] TStringBuf TrimLeadingWhitespaces(TStringBuf str Y_LIFETIME_BOUND) { - for (size_t i = 0; i < str.size(); ++i) { - if (str[i] != ' ') { - return TString(str.substr(i)); - } - } - return ""; + auto begin = str.find_first_not_of(' '); + return begin == TStringBuf::npos ? "" : str.substr(begin); } -TString Trim(TStringBuf str, TStringBuf whitespaces) +[[nodiscard]] TStringBuf Trim(TStringBuf str Y_LIFETIME_BOUND, TStringBuf whitespaces) { - size_t end = str.size(); - while (end > 0) { - size_t i = end - 1; - bool isWhitespace = false; - for (auto c : whitespaces) { - if (str[i] == c) { - isWhitespace = true; - break; - } - } - if (!isWhitespace) { - break; - } - --end; + if (whitespaces.empty()) { + return str; } + auto end = str.find_last_not_of(whitespaces); - if (end == 0) { + if (end == TStringBuf::npos) { return ""; } - size_t begin = str.find_first_not_of(whitespaces); - YT_VERIFY(begin != TString::npos); - YT_VERIFY(begin < end); - return TString(str.substr(begin, end - begin)); + auto begin = str.find_first_not_of(whitespaces); + YT_VERIFY(begin != TStringBuf::npos); + return str.substr(begin, end - begin + 1); } //////////////////////////////////////////////////////////////////////////////// -- cgit v1.3