aboutsummaryrefslogtreecommitdiffstats
path: root/util/string
diff options
context:
space:
mode:
authorppavel96 <ppavel96@yandex-team.com>2023-06-22 11:25:00 +0300
committerppavel96 <ppavel96@yandex-team.com>2023-06-22 11:25:00 +0300
commit6e1cc98994e9cfbd532fbc9ab707bd56373245f5 (patch)
treeeda42f6fa64a7120f930ca1f940248a050fd572f /util/string
parentd845680738f3051ec12fac59188c70ee1a623a88 (diff)
downloadydb-6e1cc98994e9cfbd532fbc9ab707bd56373245f5.tar.gz
[util] Turn all Collapse / CollapseInPlace overloads into template functions to support arbitrary string types
Diffstat (limited to 'util/string')
-rw-r--r--util/string/strip.h13
-rw-r--r--util/string/strip_ut.cpp17
2 files changed, 25 insertions, 5 deletions
diff --git a/util/string/strip.h b/util/string/strip.h
index 599fdd93ad..666300e0e7 100644
--- a/util/string/strip.h
+++ b/util/string/strip.h
@@ -246,12 +246,14 @@ std::enable_if_t<std::is_invocable_v<TWhitespaceFunc, typename TStringType::valu
return CollapseImpl(from, to, maxLen, isWhitespace);
}
-inline bool Collapse(const TString& from, TString& to, size_t maxLen = 0) {
- return Collapse(from, to, IsAsciiSpace<typename TString::value_type>, maxLen);
+template <class TStringType>
+inline bool Collapse(const TStringType& from, TStringType& to, size_t maxLen = 0) {
+ return Collapse(from, to, IsAsciiSpace<typename TStringType::value_type>, maxLen);
}
/// Replaces several consequtive space symbols with one (processing is limited to maxLen bytes)
-inline TString& CollapseInPlace(TString& s, size_t maxLen = 0) {
+template <class TStringType>
+inline TStringType& CollapseInPlace(TStringType& s, size_t maxLen = 0) {
Collapse(s, s, maxLen);
return s;
}
@@ -262,8 +264,9 @@ inline TStringType& CollapseInPlace(TStringType& s, TWhitespaceFunc isWhitespace
}
/// Replaces several consequtive space symbols with one (processing is limited to maxLen bytes)
-[[nodiscard]] inline TString Collapse(const TString& s, size_t maxLen = 0) {
- TString ret;
+template <class TStringType>
+[[nodiscard]] inline TStringType Collapse(const TStringType& s, size_t maxLen = 0) {
+ TStringType ret;
Collapse(s, ret, maxLen);
return ret;
}
diff --git a/util/string/strip_ut.cpp b/util/string/strip_ut.cpp
index 0b1687ffa7..283ab66fdf 100644
--- a/util/string/strip_ut.cpp
+++ b/util/string/strip_ut.cpp
@@ -139,6 +139,23 @@ Y_UNIT_TEST_SUITE(TStripStringTest) {
#endif
}
+ Y_UNIT_TEST(TestCollapseUtf16) {
+ TUtf16String s;
+ Collapse(UTF8ToWide<true>(" 123 456 "), s);
+ UNIT_ASSERT(s == UTF8ToWide<true>(" 123 456 "));
+ Collapse(UTF8ToWide<true>(" 123 456 "), s, 10);
+ UNIT_ASSERT(s == UTF8ToWide<true>(" 123 456 "));
+
+ s = UTF8ToWide<true>(" a b c ");
+ TUtf16String s2 = s;
+ CollapseInPlace(s2);
+
+ UNIT_ASSERT(s == s2);
+#ifndef TSTRING_IS_STD_STRING
+ UNIT_ASSERT(s.c_str() == s2.c_str()); // Collapse() does not change the string at all
+#endif
+ }
+
Y_UNIT_TEST(TestCollapse) {
TString s;
Collapse(TString(" 123 456 "), s);