Skip to content

Commit

Permalink
Addressed sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Flixtastic committed Jan 10, 2025
1 parent c412983 commit 46fbb98
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/index/IndexImpl.Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

// _____________________________________________________________________________
cppcoro::generator<WordsFileLine> IndexImpl::wordsInTextRecords(
const std::string& contextFile, bool addWordsFromLiterals) {
const std::string& contextFile, bool addWordsFromLiterals) const {
auto localeManager = textVocab_.getLocaleManager();
// ROUND 1: If context file aka wordsfile is not empty, read words from there.
// Remember the last context id for the (optional) second round.
Expand Down
2 changes: 1 addition & 1 deletion src/index/IndexImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ class IndexImpl {
// testing phase, once it works, it should be easy to include the IRIs and
// literals from the external vocabulary as well).
cppcoro::generator<WordsFileLine> wordsInTextRecords(
const std::string& contextFile, bool addWordsFromLiterals);
const std::string& contextFile, bool addWordsFromLiterals) const;

size_t processWordsForVocabulary(const string& contextFile,
bool addWordsFromLiterals);
Expand Down
28 changes: 15 additions & 13 deletions src/parser/WordsAndDocsFileParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,32 @@
#include "util/StringUtils.h"

// _____________________________________________________________________________
WordsAndDocsFileParser::WordsAndDocsFileParser(const string& wordsOrDocsFile,
LocaleManager localeManager)
: in_(wordsOrDocsFile), localeManager_(std::move(localeManager)) {}
WordsAndDocsFileParser::WordsAndDocsFileParser(
const string& wordsOrDocsFile, const LocaleManager& localeManager)
: in_(wordsOrDocsFile), localeManager_(localeManager) {}

// _____________________________________________________________________________
ad_utility::InputRangeFromGet<WordsFileLine>::Storage WordsFileParser::get() {
WordsFileLine line;
string l;
if (!std::getline(in_, l)) {
if (!std::getline(getInputStream(), l)) {
return std::nullopt;
};
size_t i = l.find('\t');
std::string_view lineView(l);
size_t i = lineView.find('\t');
assert(i != string::npos);
size_t j = i + 2;
assert(j + 3 < l.size());
size_t k = l.find('\t', j + 2);
assert(j + 3 < lineView.size());
size_t k = lineView.find('\t', j + 2);
assert(k != string::npos);
line.isEntity_ = (l[i + 1] == '1');
line.isEntity_ = (lineView[i + 1] == '1');
line.word_ =
(line.isEntity_ ? l.substr(0, i)
: localeManager_.getLowercaseUtf8(l.substr(0, i)));
(line.isEntity_
? lineView.substr(0, i)
: getLocaleManager().getLowercaseUtf8(lineView.substr(0, i)));
line.contextId_ =
TextRecordIndex::make(atol(l.substr(j + 1, k - j - 1).c_str()));
line.score_ = static_cast<Score>(atol(l.substr(k + 1).c_str()));
TextRecordIndex::make(atol(lineView.substr(j + 1, k - j - 1).data()));
line.score_ = static_cast<Score>(atol(lineView.substr(k + 1).data()));
#ifndef NDEBUG
if (lastCId_ > line.contextId_) {
AD_THROW("ContextFile has to be sorted by context Id.");
Expand All @@ -48,7 +50,7 @@ ad_utility::InputRangeFromGet<WordsFileLine>::Storage WordsFileParser::get() {
ad_utility::InputRangeFromGet<DocsFileLine>::Storage DocsFileParser::get() {
DocsFileLine line;
string l;
if (!std::getline(in_, l)) {
if (!std::getline(getInputStream(), l)) {
return std::nullopt;
};
size_t i = l.find('\t');
Expand Down
10 changes: 7 additions & 3 deletions src/parser/WordsAndDocsFileParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct DocsFileLine {
// The `Find` function returns the next delimiter in `text` after the given
// `pos` or an empty substring if there is no next delimiter.
struct LiteralsTokenizationDelimiter {
absl::string_view Find(absl::string_view text, size_t pos) {
absl::string_view Find(absl::string_view text, size_t pos) const {
auto isWordChar = [](char c) -> bool { return std::isalnum(c); };
auto found = std::find_if_not(text.begin() + pos, text.end(), isWordChar);
if (found == text.end()) return text.substr(text.size());
Expand Down Expand Up @@ -138,12 +138,16 @@ inline auto tokenizeAndNormalizeText(std::string_view text,
class WordsAndDocsFileParser {
public:
explicit WordsAndDocsFileParser(const string& wordsOrDocsFile,
LocaleManager localeManager);
const LocaleManager& localeManager);
explicit WordsAndDocsFileParser(const WordsAndDocsFileParser& other) = delete;
WordsAndDocsFileParser& operator=(const WordsAndDocsFileParser& other) =
delete;

protected:
std::ifstream& getInputStream() { return in_; }
const LocaleManager& getLocaleManager() { return localeManager_; }

private:
std::ifstream in_;
LocaleManager localeManager_;
};
Expand All @@ -165,8 +169,8 @@ class WordsFileParser : public WordsAndDocsFileParser,
using WordsAndDocsFileParser::WordsAndDocsFileParser;
Storage get() override;

private:
#ifndef NDEBUG
private:
// Only used for sanity checks in debug builds
TextRecordIndex lastCId_ = TextRecordIndex::make(0);
#endif
Expand Down

0 comments on commit 46fbb98

Please sign in to comment.