summaryrefslogtreecommitdiffstats
path: root/library/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
parent986452bd918bcba07aff4fe904852a9893129408 (diff)
YT: Make trim helpers noncopying; lesser copies in rich ypath parsing
commit_hash:88244dc0263029f091579dd66bba1e03132449fe
Diffstat (limited to 'library/cpp')
-rw-r--r--library/cpp/yt/string/string.cpp38
-rw-r--r--library/cpp/yt/string/string.h4
-rw-r--r--library/cpp/yt/string/unittests/string_ut.cpp17
3 files changed, 29 insertions, 30 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);
}
////////////////////////////////////////////////////////////////////////////////
diff --git a/library/cpp/yt/string/string.h b/library/cpp/yt/string/string.h
index 9e521204838..6758c25c022 100644
--- a/library/cpp/yt/string/string.h
+++ b/library/cpp/yt/string/string.h
@@ -148,8 +148,8 @@ TString UnderscoreCaseToCamelCase(TStringBuf str);
void CamelCaseToUnderscoreCase(TStringBuilderBase* builder, TStringBuf str);
TString CamelCaseToUnderscoreCase(TStringBuf str);
-TString TrimLeadingWhitespaces(TStringBuf str);
-TString Trim(TStringBuf str, TStringBuf whitespaces);
+[[nodiscard]] TStringBuf TrimLeadingWhitespaces(TStringBuf str Y_LIFETIME_BOUND);
+[[nodiscard]] TStringBuf Trim(TStringBuf str Y_LIFETIME_BOUND, TStringBuf whitespaces = " ");
////////////////////////////////////////////////////////////////////////////////
diff --git a/library/cpp/yt/string/unittests/string_ut.cpp b/library/cpp/yt/string/unittests/string_ut.cpp
index 3e12312af0b..073516006fb 100644
--- a/library/cpp/yt/string/unittests/string_ut.cpp
+++ b/library/cpp/yt/string/unittests/string_ut.cpp
@@ -45,8 +45,23 @@ TEST(TStringTest, CamelCaseToUnderscoreCase)
}
}
+TEST(TStringTest, TrimWhitespaces)
+{
+ EXPECT_EQ("", TrimLeadingWhitespaces(""));
+ EXPECT_EQ("", TrimLeadingWhitespaces(" "));
+ EXPECT_EQ("foo", TrimLeadingWhitespaces(" foo"));
+ EXPECT_EQ("foo ", TrimLeadingWhitespaces(" foo "));
+ EXPECT_EQ("f oo ", TrimLeadingWhitespaces(" f oo "));
+
+ EXPECT_EQ("", Trim("", ""));
+ EXPECT_EQ("", Trim(" ", " "));
+ EXPECT_EQ(" ", Trim(" ", "\t"));
+ EXPECT_EQ("", Trim(" \t", " \t"));
+ EXPECT_EQ("foo", Trim(" foo ", " "));
+ EXPECT_EQ("f", Trim(" f ", " "));
+}
+
////////////////////////////////////////////////////////////////////////////////
} // namespace
} // namespace NYT
-