UI-Coordinates: support for truncating a given spec

...implemented within the builder.
Will shorten and discard extraneous storage,
but not expand and allocate new one
This commit is contained in:
Fischlurch 2017-10-30 02:59:56 +01:00
parent 5530bbede8
commit e035dbc54a
4 changed files with 40 additions and 0 deletions

View file

@ -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);
}
};

View file

@ -477,6 +477,23 @@ namespace lib {
*reinterpret_cast<const char**> (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

View file

@ -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));
}

View file

@ -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());
}