diff --git a/src/gui/interact/ui-coord.hpp b/src/gui/interact/ui-coord.hpp index b4ddfbfd5..4be04e47d 100644 --- a/src/gui/interact/ui-coord.hpp +++ b/src/gui/interact/ui-coord.hpp @@ -583,6 +583,14 @@ namespace interact { uic_.setTailSequence (UIC_PATH, pathDef); return std::move (*this); } + + /** possibly shorten this path specification to a limited depth */ + Builder + truncateTo (size_t depth) + { + uic_.truncateTo (depth); + return std::move (*this); + } }; diff --git a/src/lib/path-array.hpp b/src/lib/path-array.hpp index 58299cd51..c496e26bd 100644 --- a/src/lib/path-array.hpp +++ b/src/lib/path-array.hpp @@ -477,6 +477,23 @@ namespace lib { *reinterpret_cast (pos) = val; } + void + truncateTo (size_t newSize) + { + if (newSize < chunk_size) + { + Literal* end = elms_.end(); + Literal* pos = getPosition(newSize); + for ( ; pos!=end; ++pos) + setContent (pos, nullptr); + tail_.resizeTo (0); + } + else + if (newSize-chunk_size < tail_.size()) + tail_.resizeTo (newSize - chunk_size); + // otherwise: keep current size, don't grow + } + /** * establish the _contract_ of PathArray * - no null pointer within the valid storage range diff --git a/tests/gui/interact/ui-coord-test.cpp b/tests/gui/interact/ui-coord-test.cpp index 2e25b9a25..69cd2fda8 100644 --- a/tests/gui/interact/ui-coord-test.cpp +++ b/tests/gui/interact/ui-coord-test.cpp @@ -189,6 +189,17 @@ namespace test { CHECK (5 == uic4.size()); CHECK ("UI:?-panel.view.#555" == string(uic4)); VERIFY_ERROR(LOGIC, uic1.prepend("root")); + + // the builder may shorten/truncate the path + uic4 = uic4.append("α/β/γ/δ/ε/λ").truncateTo(9); + CHECK (9 == uic4.size()); + CHECK ("UI:?-panel.view.#555/α/β/γ/δ" == string(uic4)); + uic4 = uic4.path("ε/ε/ε/ε/ε").truncateTo(3); + CHECK (3 == uic4.size()); + CHECK ("UI:?-panel" == string(uic4)); + uic4 = uic4.append("something").truncateTo(2); + CHECK (0 == uic4.size()); // NOTE: normalisation detected absence of any remaining content + CHECK ("UI:?" == string(uic4)); } diff --git a/tests/library/path-array-test.cpp b/tests/library/path-array-test.cpp index db129ef28..a4bbb9bd9 100644 --- a/tests/library/path-array-test.cpp +++ b/tests/library/path-array-test.cpp @@ -451,6 +451,10 @@ namespace test { CHECK ("" == parr[4]); CHECK ("" == parr[5]); CHECK ("Φ" == parr[6]); + + parr = ParrT{"",nullptr,"",nullptr,"",nullptr,""}; + CHECK ("" == join(parr,"")); + CHECK (0 == parr.size()); }