##// END OF EJS Templates
Introduced opaque library from Kyle Markley and improved DateTimeRange...
Introduced opaque library from Kyle Markley and improved DateTimeRange class Opaque library allow safer manipulation of numeric values and gives tem a meaning. This should help to prevent stupid mistakes. A first usage in SciQLop is Seconds which are double but have a specific meaning, a DateTimeRange can only be multiplied by a double (-> zoom) but only seconds can be added to it (-> pan). Even a zoom could be an opaque type in the future: Range/Range->Zoom, Range*Zoom->Range, Zoom*Range->Error. DateTimeRange class has now many arithmetic operators implemented this will allow to implement zoom/pan operations in only one place and test them. Signed-off-by: Alexis Jeandet <alexis.jeandet@member.fsf.org>

File last commit:

r0:86b06c4cec3c
r4:96a6baa9f92b
Show More
StringUtils.cpp
30 lines | 1.1 KiB | text/x-c | CppLexer
#include "Common/StringUtils.h"
#include <QRegExp>
#include <QString>
#include <set>
QString StringUtils::uniqueName(const QString &defaultName,
const std::vector<QString> &forbiddenNames) noexcept
{
// Gets the base of the unique name to generate, by removing trailing number (for example, base
// name of "FGM12" is "FGM")
auto baseName = defaultName;
baseName.remove(QRegExp{QStringLiteral("\\d*$")});
// Finds the unique name by adding an index to the base name and stops when the generated name
// isn't forbidden
QString newName{};
auto forbidden = true;
for (auto i = 0; forbidden; ++i) {
newName = (i == 0) ? baseName : baseName + QString::number(i);
forbidden = newName.isEmpty()
|| std::any_of(forbiddenNames.cbegin(), forbiddenNames.cend(),
[&newName](const auto &name) {
return name.compare(newName, Qt::CaseInsensitive) == 0;
});
}
return newName;
}