Type casting fixes and bounding array access

This commit is contained in:
Imbus 2024-11-21 08:46:55 +01:00
parent 8c8930f5c5
commit 94d807fc67

View file

@ -1,7 +1,6 @@
#include "dictionary.h" #include "dictionary.h"
#include "word.h" #include "word.h"
#include <algorithm> #include <algorithm>
#include <filesystem>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -13,7 +12,7 @@ using std::vector;
Dictionary::Dictionary() {} Dictionary::Dictionary() {}
bool Dictionary::contains(const string &word) const { bool Dictionary::contains(const string &word) const {
int l = word.length(); auto l = word.length();
Word w = Word(word); Word w = Word(word);
if (std::find(this->words[l].begin(), this->words[l].end(), w) != if (std::find(this->words[l].begin(), this->words[l].end(), w) !=
std::end(this->words[l])) { std::end(this->words[l])) {
@ -60,9 +59,9 @@ int Dictionary::slurp(path p) {
std::string line; std::string line;
while (std::getline(file, line)) { while (std::getline(file, line)) {
if (line.size() > MAXLEN) // Words larger than max gets placed in the topmost bucket
continue; words[std::min(line.size(), static_cast<size_t>(MAXLEN) - 1)].push_back(
words[line.size()].push_back(Word(line)); Word(line));
} }
file.close(); file.close();