aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordimdim11 <dimdim11@yandex-team.com>2023-06-08 19:08:24 +0300
committerdimdim11 <dimdim11@yandex-team.com>2023-06-08 19:08:24 +0300
commit8642bc755ab5b51752619694224388b2d247c737 (patch)
tree92308ad5dc9d264b67a817a6eb2ab83050a0d36e
parentbb56a84dcf62e1f592b6be5a8fbc8298ee9478b0 (diff)
downloadydb-8642bc755ab5b51752619694224388b2d247c737.tar.gz
Refactor and fix constructors TString as std::string
Fix move constructors when used std::string instead of TString
-rw-r--r--util/folder/path.cpp56
-rw-r--r--util/folder/path.h5
-rw-r--r--util/folder/path_ut.cpp76
3 files changed, 115 insertions, 22 deletions
diff --git a/util/folder/path.cpp b/util/folder/path.cpp
index cecef71971..f2bc2a033a 100644
--- a/util/folder/path.cpp
+++ b/util/folder/path.cpp
@@ -14,16 +14,16 @@ struct TFsPath::TSplit: public TAtomicRefCount<TSplit>, public TPathSplit {
: TPathSplit(path)
{
}
- inline TSplit(const TSplit& that, const TString& path, const TString& other) {
- for (const auto& part : that) {
- emplace_back(path.begin() + (part.data() - other.begin()), part.size());
+ inline TSplit(const TString& path, const TSimpleIntrusivePtr<TSplit>& thatSplit, const TString::char_type* thatPathData) {
+ for (const auto& thatPart : *thatSplit) {
+ emplace_back(path.data() + (thatPart.data() - thatPathData), thatPart.size());
}
- if (!that.Drive.empty()) {
- Drive = TStringBuf(path.begin() + (that.Drive.data() - other.begin()), that.Drive.size());
+ if (!thatSplit->Drive.empty()) {
+ Drive = TStringBuf(path.data() + (thatSplit->Drive.data() - thatPathData), thatSplit->Drive.size());
}
- IsAbsolute = that.IsAbsolute;
+ IsAbsolute = thatSplit->IsAbsolute;
}
};
@@ -198,14 +198,6 @@ TFsPath::TSplit& TFsPath::GetSplit() const {
return *Split_;
}
-void TFsPath::CopySplitFrom(const TFsPath& that) const {
- if (that.Split_) {
- Split_ = new TSplit(*that.Split_, Path_, that.Path_);
- } else {
- Split_ = that.Split_;
- }
-}
-
static Y_FORCE_INLINE void VerifyPath(const TStringBuf path) {
Y_VERIFY(!path.Contains('\0'), "wrong format of TFsPath: %s", EscapeC(path).c_str());
}
@@ -230,15 +222,41 @@ TFsPath::TFsPath(const char* path)
{
}
-TFsPath::TFsPath(const TFsPath& that)
- : Path_(that.Path_)
-{
- CopySplitFrom(that);
+TFsPath::TFsPath(const TFsPath& that) {
+ *this = that;
+}
+
+TFsPath::TFsPath(TFsPath&& that) {
+ *this = std::move(that);
}
TFsPath& TFsPath::operator=(const TFsPath& that) {
Path_ = that.Path_;
- CopySplitFrom(that);
+ if (that.Split_) {
+ Split_ = new TSplit(Path_, that.Split_, that.Path_.begin());
+ } else {
+ Split_ = nullptr;
+ }
+ return *this;
+}
+
+TFsPath& TFsPath::operator=(TFsPath&& that) {
+#ifdef TSTRING_IS_STD_STRING
+ const auto thatPathData = that.Path_.data();
+ Path_ = std::move(that.Path_);
+ if (that.Split_) {
+ if (Path_.data() == thatPathData) { // Path_ moved, can move Split_
+ Split_ = std::move(that.Split_);
+ } else { // Path_ copied, rebuild Split_ using that.Split_
+ Split_ = new TSplit(Path_, that.Split_, that.Path_.data());
+ }
+ } else {
+ Split_ = nullptr;
+ }
+#else
+ Path_ = std::move(that.Path_);
+ Split_ = std::move(that.Split_);
+#endif
return *this;
}
diff --git a/util/folder/path.h b/util/folder/path.h
index e1615a947a..15532c6252 100644
--- a/util/folder/path.h
+++ b/util/folder/path.h
@@ -35,10 +35,10 @@ public:
}
TFsPath(const TFsPath& that);
- TFsPath(TFsPath&& that) = default;
+ TFsPath(TFsPath&& that);
TFsPath& operator=(const TFsPath& that);
- TFsPath& operator=(TFsPath&& that) = default;
+ TFsPath& operator=(TFsPath&& that);
~TFsPath() = default;
@@ -213,7 +213,6 @@ public:
private:
void InitSplit() const;
TSplit& GetSplit() const;
- void CopySplitFrom(const TFsPath& that) const;
private:
TString Path_;
diff --git a/util/folder/path_ut.cpp b/util/folder/path_ut.cpp
index f06fec78ee..3c0e875a07 100644
--- a/util/folder/path_ut.cpp
+++ b/util/folder/path_ut.cpp
@@ -830,4 +830,80 @@ Y_UNIT_TEST_SUITE(TFsPathTests) {
path1 = path2;
UNIT_ASSERT_VALUES_EQUAL(path1.PathSplit().at(1), "file_in_folder_2");
}
+
+ static constexpr TStringBuf a("a");
+ static constexpr TStringBuf b("b");
+ static constexpr TStringBuf c("c");
+ static constexpr size_t n = 64;
+ static const TString an(n, 'a');
+ static const TString bn(n, 'b');
+ static const TString cn(n, 'c');
+
+ Y_UNIT_TEST(TestCopySplitSSOSupport) {
+ TFsPath path1 = TFsPath(a) / TFsPath(b);
+ const auto& split1 = path1.PathSplit();
+ UNIT_ASSERT_VALUES_EQUAL(split1.size(), 2);
+ UNIT_ASSERT_VALUES_EQUAL(split1.at(0), a);
+ UNIT_ASSERT_VALUES_EQUAL(split1.at(1), b);
+ TFsPath path2(static_cast<const TFsPath&>(path1)); // copy constructor
+ const auto& split2 = path2.PathSplit();
+ path1 = TFsPath(c); // invalidate previous Path_ in path1
+ const auto& newsplit1 = path1.PathSplit();
+ UNIT_ASSERT_VALUES_EQUAL(newsplit1.size(), 1);
+ UNIT_ASSERT_VALUES_EQUAL(newsplit1.at(0), c);
+ UNIT_ASSERT_VALUES_EQUAL(split2.size(), 2);
+ UNIT_ASSERT_VALUES_EQUAL(split2.at(0), a);
+ UNIT_ASSERT_VALUES_EQUAL(split2.at(1), b);
+ }
+
+ Y_UNIT_TEST(TestMoveSplitSSOSupport) {
+ TFsPath path1 = TFsPath(a) / TFsPath(b);
+ const auto& split1 = path1.PathSplit();
+ UNIT_ASSERT_VALUES_EQUAL(split1.size(), 2);
+ UNIT_ASSERT_VALUES_EQUAL(split1.at(0), a);
+ UNIT_ASSERT_VALUES_EQUAL(split1.at(1), b);
+ TFsPath path2(std::move(path1)); // move constructor
+ const auto& split2 = path2.PathSplit();
+ path1 = TFsPath(c); // invalidate previous Path_ in path1
+ const auto& newsplit1 = path1.PathSplit();
+ UNIT_ASSERT_VALUES_EQUAL(newsplit1.size(), 1);
+ UNIT_ASSERT_VALUES_EQUAL(newsplit1.at(0), c);
+ UNIT_ASSERT_VALUES_EQUAL(split2.size(), 2);
+ UNIT_ASSERT_VALUES_EQUAL(split2.at(0), a);
+ UNIT_ASSERT_VALUES_EQUAL(split2.at(1), b);
+ }
+
+ Y_UNIT_TEST(TestCopySplitNoneSSO) {
+ TFsPath path1 = TFsPath(an) / TFsPath(bn);
+ const auto& split1 = path1.PathSplit();
+ UNIT_ASSERT_VALUES_EQUAL(split1.size(), 2);
+ UNIT_ASSERT_VALUES_EQUAL(split1.at(0), an);
+ UNIT_ASSERT_VALUES_EQUAL(split1.at(1), bn);
+ TFsPath path2(static_cast<const TFsPath&>(path1)); // copy constructor
+ const auto& split2 = path2.PathSplit();
+ path1 = TFsPath(cn); // invalidate previous Path_ in path1
+ const auto& newsplit1 = path1.PathSplit();
+ UNIT_ASSERT_VALUES_EQUAL(newsplit1.size(), 1);
+ UNIT_ASSERT_VALUES_EQUAL(newsplit1.at(0), cn);
+ UNIT_ASSERT_VALUES_EQUAL(split2.size(), 2);
+ UNIT_ASSERT_VALUES_EQUAL(split2.at(0), an);
+ UNIT_ASSERT_VALUES_EQUAL(split2.at(1), bn);
+ }
+
+ Y_UNIT_TEST(TestMoveSplitNoneSSO) {
+ TFsPath path1 = TFsPath(an) / TFsPath(bn);
+ const auto& split1 = path1.PathSplit();
+ UNIT_ASSERT_VALUES_EQUAL(split1.size(), 2);
+ UNIT_ASSERT_VALUES_EQUAL(split1.at(0), an);
+ UNIT_ASSERT_VALUES_EQUAL(split1.at(1), bn);
+ TFsPath path2(std::move(path1)); // move constructor
+ const auto& split2 = path2.PathSplit();
+ path1 = TFsPath(cn); // invalidate previous Path_ in path1
+ const auto& newsplit1 = path1.PathSplit();
+ UNIT_ASSERT_VALUES_EQUAL(newsplit1.size(), 1);
+ UNIT_ASSERT_VALUES_EQUAL(newsplit1.at(0), cn);
+ UNIT_ASSERT_VALUES_EQUAL(split2.size(), 2);
+ UNIT_ASSERT_VALUES_EQUAL(split2.at(0), an);
+ UNIT_ASSERT_VALUES_EQUAL(split2.at(1), bn);
+ }
}