Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringUtils.h cleanup #553

Merged
merged 17 commits into from
Jan 27, 2022
Merged
8 changes: 4 additions & 4 deletions src/TurtleParserMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void writeLabel(const std::string& filename) {
}
}
for (const auto& t : entities) {
if (ad_utility::startsWith(t, "<")) {
if (t.starts_with('<')) {
std::cout << t << " <qlever_label> \"" << t.substr(1, t.size() - 2)
<< ".\n";
}
Expand Down Expand Up @@ -179,13 +179,13 @@ int main(int argc, char** argv) {

if (fileFormat.empty()) {
bool filetypeDeduced = false;
if (ad_utility::endsWith(inputFile, ".tsv")) {
if (inputFile.ends_with(".tsv")) {
fileFormat = "tsv";
filetypeDeduced = true;
} else if (ad_utility::endsWith(inputFile, ".nt")) {
} else if (inputFile.ends_with(".nt")) {
fileFormat = "nt";
filetypeDeduced = true;
} else if (ad_utility::endsWith(inputFile, ".ttl")) {
} else if (inputFile.ends_with(".ttl")) {
fileFormat = "ttl";
filetypeDeduced = true;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/engine/Filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ void Filter::computeFilterFixedValue(
if (!entity) {
return true;
}
return ad_utility::endsWith(entity.value(), _rhs);
return entity.value().ends_with(_rhs);
},
res);
break;
Expand Down
8 changes: 4 additions & 4 deletions src/engine/QueryExecutionTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ QueryExecutionTree::generateResults(const vector<string>& selectVars,
LOG(DEBUG) << "Resolving strings for finished binary result...\n";
vector<std::optional<pair<size_t, ResultTable::ResultType>>> validIndices;
for (auto var : selectVars) {
if (ad_utility::startsWith(var, "TEXT(")) {
if (var.starts_with("TEXT(")) {
var = var.substr(5, var.rfind(')') - 5);
}
auto it = getVariableColumns().find(var);
Expand Down Expand Up @@ -140,7 +140,7 @@ QueryExecutionTree::selectedVariablesToColumnIndices(
const ResultTable& resultTable) const {
ColumnIndicesAndTypes exportColumns;
for (auto var : selectVariables) {
if (ad_utility::startsWith(var, "TEXT(")) {
if (var.starts_with("TEXT(")) {
var = var.substr(5, var.rfind(')') - 5);
}
if (getVariableColumns().contains(var)) {
Expand Down Expand Up @@ -332,7 +332,7 @@ QueryExecutionTree::toStringAndXsdType(Id id, ResultTable::ResultType type,
if (!entity.has_value()) {
return std::nullopt;
}
if (ad_utility::startsWith(entity.value(), VALUE_PREFIX)) {
if (entity.value().starts_with(VALUE_PREFIX)) {
return ad_utility::convertIndexWordToLiteralAndType(entity.value());
}
return std::pair{std::move(entity.value()), nullptr};
Expand Down Expand Up @@ -439,7 +439,7 @@ ad_utility::stream_generator::stream_generator QueryExecutionTree::writeTable(
string entity = _qec->getIndex()
.idToOptionalString(idTable(i, val.first))
.value_or("");
if (ad_utility::startsWith(entity, VALUE_PREFIX)) {
if (entity.starts_with(VALUE_PREFIX)) {
co_yield ad_utility::convertIndexWordToValueLiteral(entity);
} else {
co_yield entity;
Expand Down
3 changes: 1 addition & 2 deletions src/engine/QueryPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,7 @@ vector<QueryPlanner::SubtreePlan> QueryPlanner::getDistinctRow(
keepIndices.push_back(ind);
indDone.insert(ind);
}
} else if (ad_utility::startsWith(var, "SCORE(") ||
ad_utility::startsWith(var, "TEXT(")) {
} else if (var.starts_with("SCORE(") || var.starts_with("TEXT(")) {
auto varInd = var.find('?');
auto cVar = var.substr(varInd, var.rfind(')') - varInd);
const auto it = colMap.find(cVar);
Expand Down
2 changes: 1 addition & 1 deletion src/engine/RuntimeInformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class RuntimeInformation {
} else {
out << el.value();
}
if (ad_utility::endsWith(el.key(), "Time")) {
if (el.key().ends_with("Time")) {
out << " ms";
}
out << '\n';
Expand Down
8 changes: 4 additions & 4 deletions src/engine/sparqlExpressions/SparqlExpressionValueGetters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ double NumericValueGetter::operator()(StrongIdWithResultType strongId,
// load the string, parse it as an xsd::int or float
std::string entity =
context->_qec.getIndex().idToOptionalString(id).value_or("");
if (!ad_utility::startsWith(entity, VALUE_FLOAT_PREFIX)) {
if (!entity.starts_with(VALUE_FLOAT_PREFIX)) {
return std::numeric_limits<float>::quiet_NaN();
} else {
return ad_utility::convertIndexWordToFloat(entity);
Expand Down Expand Up @@ -67,7 +67,7 @@ bool EffectiveBooleanValueGetter::operator()(StrongIdWithResultType strongId,
return false;
}
// Non-numeric non-empty values are always true
if (!ad_utility::startsWith(entity, VALUE_FLOAT_PREFIX)) {
if (!entity.starts_with(VALUE_FLOAT_PREFIX)) {
return true;
} else {
// 0 and nan values are considered false.
Expand Down Expand Up @@ -99,9 +99,9 @@ string StringValueGetter::operator()(StrongIdWithResultType strongId,
// load the string, parse it as an xsd::int or float
std::string entity =
context->_qec.getIndex().idToOptionalString(id).value_or("");
if (ad_utility::startsWith(entity, VALUE_FLOAT_PREFIX)) {
if (entity.starts_with(VALUE_FLOAT_PREFIX)) {
return std::to_string(ad_utility::convertIndexWordToFloat(entity));
} else if (ad_utility::startsWith(entity, VALUE_DATE_PREFIX)) {
} else if (entity.starts_with(VALUE_DATE_PREFIX)) {
return ad_utility::convertDateToIndexWord(entity);
} else {
return entity;
Expand Down
4 changes: 2 additions & 2 deletions src/index/CompressedRelation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ template void CompressedRelationMetaData::scan<Permutation::OSP_T, IdTable>(

// ___________________________________________________________________________
void CompressedRelationWriter::addRelation(
Id col0Id, const ad_utility::BufferedVector<array<Id, 2>>& data,
Id col0Id, const ad_utility::BufferedVector<std::array<Id, 2>>& data,
size_t numDistinctCol1, bool functional) {
LOG(TRACE) << "Writing a relation ...\n";
AD_CHECK_GT(data.size(), 0);
Expand Down Expand Up @@ -413,7 +413,7 @@ void CompressedRelationWriter::addRelation(

// _____________________________________________________________________________
void CompressedRelationWriter::writeRelationToExclusiveBlocks(
Id col0Id, const ad_utility::BufferedVector<array<Id, 2>>& data) {
Id col0Id, const ad_utility::BufferedVector<std::array<Id, 2>>& data) {
RobinTF marked this conversation as resolved.
Show resolved Hide resolved
constexpr size_t NUM_ROWS_PER_BLOCK =
BLOCKSIZE_COMPRESSED_METADATA / (2 * sizeof(Id));
for (size_t i = 0; i < data.size(); i += NUM_ROWS_PER_BLOCK) {
Expand Down
4 changes: 2 additions & 2 deletions src/index/CompressedRelation.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class CompressedRelationWriter {
* have a function from the occurring Ys to the Zs).
*/
void addRelation(Id col0Id,
const ad_utility::BufferedVector<array<Id, 2>>& data,
const ad_utility::BufferedVector<std::array<Id, 2>>& data,
size_t numDistinctCol1, bool functional);

/// Finish writing all relations which have previously been added, but might
Expand Down Expand Up @@ -215,7 +215,7 @@ class CompressedRelationWriter {
// its size. Write the blocks to `_outfile` and append all the created
// block meta data to `_blockBuffer`.
void writeRelationToExclusiveBlocks(
Id col0Id, const ad_utility::BufferedVector<array<Id, 2>>& data);
Id col0Id, const ad_utility::BufferedVector<std::array<Id, 2>>& data);
RobinTF marked this conversation as resolved.
Show resolved Hide resolved
};

#endif // QLEVER_COMPRESSEDRELATION_H
10 changes: 4 additions & 6 deletions src/index/Index.Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,10 @@ void Index::calculateBlockBoundariesImpl(
}
auto forcedBlockStartSortKey = locManager.getSortKey(
*forcedBlockStartsIt, LocaleManager::Level::PRIMARY);
if (forcedBlockStartSortKey.get() >= prefixSortKey.get()) {
if (forcedBlockStartSortKey >= prefixSortKey) {
break;
}
if (ad_utility::startsWith(prefixSortKey.get(),
forcedBlockStartSortKey.get())) {
if (prefixSortKey.starts_with(forcedBlockStartSortKey)) {
prefixSortKey = std::move(forcedBlockStartSortKey);
prefixLength = MIN_WORD_PREFIX_SIZE;
return;
Expand Down Expand Up @@ -515,12 +514,11 @@ void Index::calculateBlockBoundariesImpl(

bool tooShortButNotEqual =
(currentLen < MIN_WORD_PREFIX_SIZE || nextLen < MIN_WORD_PREFIX_SIZE) &&
(prefixSortKey.get() != nextPrefixSortKey.get());
(prefixSortKey != nextPrefixSortKey);
// The `startsWith` also correctly handles the case where
RobinTF marked this conversation as resolved.
Show resolved Hide resolved
// `nextPrefixSortKey` is "longer" than `MIN_WORD_PREFIX_SIZE`, e.g. because
// of unicode ligatures.
bool samePrefix =
ad_utility::startsWith(nextPrefixSortKey.get(), prefixSortKey.get());
bool samePrefix = nextPrefixSortKey.starts_with(prefixSortKey);
if (tooShortButNotEqual || !samePrefix) {
blockBoundaryAction(i);
numBlocks++;
Expand Down
6 changes: 3 additions & 3 deletions src/index/IndexBuilderMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ int main(int argc, char** argv) {
<< ad_utility::getUppercase(filetype) << std::endl;
} else {
bool filetypeDeduced = false;
if (ad_utility::endsWith(inputFile, ".tsv")) {
if (inputFile.ends_with(".tsv")) {
filetype = "tsv";
filetypeDeduced = true;
} else if (ad_utility::endsWith(inputFile, ".nt")) {
} else if (inputFile.ends_with(".nt")) {
filetype = "nt";
filetypeDeduced = true;
} else if (ad_utility::endsWith(inputFile, ".ttl")) {
} else if (inputFile.ends_with(".ttl")) {
filetype = "ttl";
filetypeDeduced = true;
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/index/IndexBuilderTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,13 @@ auto getIdMapLambdas(std::array<ItemMapManager, Parallelism>* itemArrayPtr,
map.assignNextId(ad_utility::convertToLanguageTaggedPredicate(
lt._triple[1], lt._langtag));
auto& spoIds = *(res[0]); // ids of original triple
// TODO replace the std::array by an explicit IdTriple class,
// then the emplace calls don't need the explicit type.
// extra triple <subject> @language@<predicate> <object>
res[1].emplace(array<Id, 3>{spoIds[0], langTaggedPredId, spoIds[2]});
res[1].emplace(
std::array<Id, 3>{spoIds[0], langTaggedPredId, spoIds[2]});
// extra triple <object> ql:language-tag <@language>
res[2].emplace(array<Id, 3>{
res[2].emplace(std::array<Id, 3>{
RobinTF marked this conversation as resolved.
Show resolved Hide resolved
spoIds[2], map.assignNextId(LANGUAGE_PREDICATE), langTagId});
}
return res;
Expand Down
6 changes: 3 additions & 3 deletions src/index/PrefixHeuristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TreeNode* TreeNode::insertAfter(string_view value) {
// we now know that _value is a real prefix of value
// check if one of the children also is a prefix of value
for (auto& c : _children) {
if (startsWith(value, c->_value)) {
if (value.starts_with(c->_value)) {
return c->insertAfter(value);
}
}
Expand All @@ -52,7 +52,7 @@ TreeNode* TreeNode::insertAfter(string_view value) {

// find children of current node which have to become children of the new node
auto pred = [&value](const NodePtr& s) {
return startsWith(s->_value, value);
return s->_value.starts_with(value);
};
auto itChildren = std::remove_if(_children.begin(), _children.end(), pred);

Expand All @@ -71,7 +71,7 @@ TreeNode* TreeNode::insertAfter(string_view value) {

// ______________________________________________________________________
TreeNode* TreeNode::insert(string_view value) {
if (startsWith(value, _value)) {
if (value.starts_with(_value)) {
// this node is a prefix of value, insert in subtree
return insertAfter(value);
}
Expand Down
8 changes: 3 additions & 5 deletions src/index/StringSortComparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ class LocaleManager {
size_t prefixLengthSoFar = 1;
SortKey completeSortKey = getSortKey(s, Level::PRIMARY);
while (numContributingCodepoints < prefixLength ||
!ad_utility::startsWith(completeSortKey.get(), sortKey.get())) {
!completeSortKey.starts_with(sortKey)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 221 the get() can also go

auto [numCodepoints, prefix] =
ad_utility::getUTF8Prefix(s, prefixLengthSoFar);
auto nextLongerSortKey = getSortKey(prefix, Level::PRIMARY);
if (nextLongerSortKey.get() != sortKey.get()) {
if (nextLongerSortKey != sortKey) {
// The `SortKey` changed by adding a codepoint, so that codepoint
// was contributing.
numContributingCodepoints++;
Expand Down Expand Up @@ -675,9 +675,7 @@ class TripleComponentComparator {
std::string_view res = a;
const char first = a.empty() ? char(0) : a[0];
std::string_view langtag;
if (ad_utility::startsWith(res, "\"") ||
ad_utility::startsWith(res,
std::string{EXTERNALIZED_LITERALS_PREFIX})) {
if (res.starts_with('"') || res.starts_with(EXTERNALIZED_LITERALS_PREFIX)) {
// only remove the first character in case of literals that always start
// with a quotation mark. For all other types we need this. <TODO> rework
// the vocabulary's data type to remove ALL of those hacks
Expand Down
6 changes: 3 additions & 3 deletions src/index/Vocabulary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ bool Vocabulary<S, C>::isLiteral(const string& word) {
template <class S, class C>
bool Vocabulary<S, C>::isExternalizedLiteral(const string& word) {
return word.size() > 1 &&
ad_utility::startsWith(word, EXTERNALIZED_LITERALS_PREFIX + '\"');
word.starts_with(EXTERNALIZED_LITERALS_PREFIX + '\"');
}

// _____________________________________________________________________________
Expand All @@ -169,7 +169,7 @@ bool Vocabulary<S, C>::shouldBeExternalized(const string& word) const {
template <class S, class C>
bool Vocabulary<S, C>::shouldEntityBeExternalized(const string& word) const {
for (const auto& p : _externalizedPrefixes) {
if (ad_utility::startsWith(word, p)) {
if (word.starts_with(p)) {
return true;
}
}
Expand Down Expand Up @@ -226,7 +226,7 @@ template <class S, class C>
template <typename, typename>
CompressedString Vocabulary<S, C>::compressPrefix(const string& word) const {
for (const auto& p : _prefixVec) {
if (ad_utility::startsWith(word, p._fulltext)) {
if (word.starts_with(p._fulltext)) {
auto res = CompressedString::fromString(
p._prefix + std::string_view(word).substr(p._fulltext.size()));
return res;
Expand Down
2 changes: 1 addition & 1 deletion src/index/Vocabulary.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class Vocabulary {

// maps (numeric) keys to the prefix they encode.
// currently only 128 prefixes are supported.
array<std::string, NUM_COMPRESSION_PREFIXES> _prefixMap{""};
std::array<std::string, NUM_COMPRESSION_PREFIXES> _prefixMap{""};

// If a word starts with one of those prefixes it will be externalized
vector<std::string> _externalizedPrefixes;
Expand Down
2 changes: 1 addition & 1 deletion src/index/VocabularyGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using IdPairMMapVec = ad_utility::MmapVector<std::pair<Id, Id>>;
using IdPairMMapVecView = ad_utility::MmapVectorView<std::pair<Id, Id>>;
using std::string;

using TripleVec = stxxl::vector<array<Id, 3>>;
using TripleVec = stxxl::vector<std::array<Id, 3>>;

/**
* Class for merging the partial vocabularies. The main function is still in the
Expand Down
2 changes: 1 addition & 1 deletion src/index/VocabularyGeneratorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ void writeMappedIdsToExtVec(const TripleVec& input,
}

// update the Element
writer << array<Id, 3>{
writer << std::array<Id, 3>{
{iterators[0]->second, iterators[1]->second, iterators[2]->second}};
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/parser/ParsedQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,10 @@ void ParsedQuery::expandPrefix(
// _____________________________________________________________________________
void ParsedQuery::expandPrefix(
string& item, const ad_utility::HashMap<string, string>& prefixMap) {
if (!ad_utility::startsWith(item, "?") &&
!ad_utility::startsWith(item, "<")) {
if (!item.starts_with("?") && !item.starts_with("<")) {
std::optional<string> langtag = std::nullopt;
if (ad_utility::startsWith(item, "@")) {
auto secondPos = item.find("@", 1);
if (item.starts_with("@")) {
auto secondPos = item.find('@', 1);
if (secondPos == string::npos) {
throw ParseException(
"langtaged predicates must have form @lang@ActualPredicate. Second "
Expand Down
10 changes: 4 additions & 6 deletions src/parser/ParsedQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ class PropertyPath {
bool _can_be_null;
};

inline bool isVariable(const string& elem) {
return ad_utility::startsWith(elem, "?");
}
inline bool isVariable(const string& elem) { return elem.starts_with("?"); }

inline bool isVariable(const PropertyPath& elem) {
return elem._operation == PropertyPath::Operation::IRI &&
Expand Down Expand Up @@ -119,7 +117,7 @@ class OrderKey {
explicit OrderKey(const string& textual) {
std::string lower = ad_utility::getLowercaseUtf8(textual);
size_t pos = 0;
_desc = ad_utility::startsWith(lower, "desc(");
_desc = lower.starts_with("desc(");
// skip the 'desc('
if (_desc) {
pos += 5;
Expand All @@ -130,7 +128,7 @@ class OrderKey {
}
}
// skip 'asc('
if (ad_utility::startsWith(lower, "asc(")) {
if (lower.starts_with("asc(")) {
pos += 4;
// skip any whitespace after the opening bracket
while (pos < textual.size() &&
Expand All @@ -157,7 +155,7 @@ class OrderKey {
_key = textual.substr(pos, end - pos);
} else if (lower[pos] == 's') {
// key is a text score
if (!ad_utility::startsWith(lower.substr(pos), "score(")) {
if (!lower.substr(pos).starts_with("score(")) {
throw ParseException("Expected keyword score in order by key: " +
textual);
}
Expand Down
3 changes: 1 addition & 2 deletions src/parser/PropertyPathParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ bool PropertyPathParser::accept(const std::string& token) {
// _____________________________________________________________________________
bool PropertyPathParser::acceptPrefix(const std::string& tokenPrefix,
std::string_view* token) {
if (_position != _end &&
ad_utility::startsWith(_position->text, tokenPrefix)) {
if (_position != _end && _position->text.starts_with(tokenPrefix)) {
if (token != nullptr) {
*token = _position->text;
}
Expand Down