summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/string/string.cpp
diff options
context:
space:
mode:
authordgolear <[email protected]>2025-08-05 13:50:26 +0300
committerdgolear <[email protected]>2025-08-05 14:03:29 +0300
commit54123d3ffd393903bd5211cfc76cecb51a0b671b (patch)
treeb509377fac592e5aa559cbb65abe720082380944 /library/cpp/yt/string/string.cpp
parent986452bd918bcba07aff4fe904852a9893129408 (diff)
YT: Make trim helpers noncopying; lesser copies in rich ypath parsing
commit_hash:88244dc0263029f091579dd66bba1e03132449fe
Diffstat (limited to 'library/cpp/yt/string/string.cpp')
-rw-r--r--library/cpp/yt/string/string.cpp38
1 files changed, 11 insertions, 27 deletions
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);
}
////////////////////////////////////////////////////////////////////////////////