1
0
mirror of synced 2025-02-17 18:59:21 +01:00

sys: Return std::nullopt instead of {} for empty optional values

This commit is contained in:
WerWolv 2022-01-30 12:43:43 +01:00
parent 4bc074fa84
commit dd572ba024
11 changed files with 45 additions and 46 deletions

View File

@ -661,7 +661,7 @@ namespace hex::pl {
continue;
}
return {};
return std::nullopt;
}
[[nodiscard]] bool evaluateCondition(Evaluator *evaluator) const {
@ -823,7 +823,7 @@ namespace hex::pl {
FunctionResult execute(Evaluator *evaluator) const override {
evaluator->createVariable(this->getName(), this->getType());
return {};
return std::nullopt;
}
private:
@ -1260,7 +1260,7 @@ namespace hex::pl {
evaluator->createVariable(variableDecl->getName(), variableDecl->getType()->evaluate(evaluator));
}
return {};
return std::nullopt;
}
private:
@ -1912,7 +1912,7 @@ namespace hex::pl {
}
}
return {};
return std::nullopt;
}
private:
@ -2118,7 +2118,7 @@ namespace hex::pl {
[[nodiscard]] std::vector<PatternData *> createPatterns(Evaluator *evaluator) const override {
this->execute(evaluator);
return { };
return {};
}
FunctionResult execute(Evaluator *evaluator) const override {

View File

@ -32,7 +32,7 @@ namespace hex::magic {
}
if (error)
return {};
return std::nullopt;
else
return magicFiles;
}

View File

@ -132,7 +132,7 @@ namespace hex {
this->m_shouldCancel = false;
if (result != CURLE_OK)
return {};
return std::nullopt;
else
return responseCode;
}

View File

@ -55,42 +55,42 @@ namespace hex::pl {
base = 16;
if (Token::isFloatingPoint(type))
return {};
return std::nullopt;
if (numberData.find_first_not_of("0123456789ABCDEFabcdef") != std::string_view::npos)
return {};
return std::nullopt;
} else if (numberData.starts_with("0b")) {
numberData = numberData.substr(2);
base = 2;
if (Token::isFloatingPoint(type))
return {};
return std::nullopt;
if (numberData.find_first_not_of("01") != std::string_view::npos)
return {};
return std::nullopt;
} else if (numberData.find('.') != std::string_view::npos || Token::isFloatingPoint(type)) {
base = 10;
if (type == Token::ValueType::Any)
type = Token::ValueType::Double;
if (std::count(numberData.begin(), numberData.end(), '.') > 1 || numberData.find_first_not_of("0123456789.") != std::string_view::npos)
return {};
return std::nullopt;
if (numberData.ends_with('.'))
return {};
return std::nullopt;
} else if (isdigit(numberData[0])) {
base = 10;
if (numberData.find_first_not_of("0123456789") != std::string_view::npos)
return {};
} else return {};
return std::nullopt;
} else return std::nullopt;
if (type == Token::ValueType::Any)
type = Token::ValueType::Signed128Bit;
if (numberData.length() == 0)
return {};
return std::nullopt;
if (Token::isUnsigned(type) || Token::isSigned(type)) {
u128 integer = 0;
@ -103,7 +103,7 @@ namespace hex::pl {
integer += 10 + (c - 'A');
else if (c >= 'a' && c <= 'f')
integer += 10 + (c - 'a');
else return {};
else return std::nullopt;
}
switch (type) {
@ -112,7 +112,7 @@ namespace hex::pl {
case Token::ValueType::Signed128Bit:
return { i128(integer) };
default:
return {};
return std::nullopt;
}
} else if (Token::isFloatingPoint(type)) {
double floatingPoint = strtod(numberData.data(), nullptr);
@ -123,24 +123,23 @@ namespace hex::pl {
case Token::ValueType::Double:
return { double(floatingPoint) };
default:
return {};
return std::nullopt;
}
}
return {};
return std::nullopt;
}
std::optional<std::pair<char, size_t>> getCharacter(const std::string &string) {
if (string.length() < 1)
return {};
return std::nullopt;
// Escape sequences
if (string[0] == '\\') {
if (string.length() < 2)
return {};
return std::nullopt;
// Handle simple escape sequences
switch (string[1]) {
@ -189,10 +188,10 @@ namespace hex::pl {
// Hexadecimal number
if (string[1] == 'x') {
if (string.length() != 4)
return {};
return std::nullopt;
if (!isxdigit(string[2]) || !isxdigit(string[3]))
return {};
return std::nullopt;
return {
{std::strtoul(&string[2], nullptr, 16), 4}
@ -212,7 +211,7 @@ namespace hex::pl {
};
}
return {};
return std::nullopt;
} else return {
{string[0], 1}
};

View File

@ -98,19 +98,19 @@ namespace hex::pl {
auto preprocessedCode = this->m_preprocessor->preprocess(code);
if (!preprocessedCode.has_value()) {
this->m_currError = this->m_preprocessor->getError();
return {};
return std::nullopt;
}
auto tokens = this->m_lexer->lex(preprocessedCode.value());
if (!tokens.has_value()) {
this->m_currError = this->m_lexer->getError();
return {};
return std::nullopt;
}
auto ast = this->m_parser->parse(tokens.value());
if (!ast.has_value()) {
this->m_currError = this->m_parser->getError();
return {};
return std::nullopt;
}
return ast;
@ -136,14 +136,14 @@ namespace hex::pl {
auto ast = this->parseString(code);
if (!ast)
return {};
return std::nullopt;
this->m_currAST = ast.value();
auto patterns = this->m_evaluator->evaluate(ast.value());
if (!patterns.has_value()) {
this->m_currError = this->m_evaluator->getConsole().getLastHardError();
return {};
return std::nullopt;
}
return patterns;

View File

@ -212,7 +212,7 @@ namespace hex::pl {
}
} catch (PreprocessorError &e) {
this->m_error = e;
return {};
return std::nullopt;
}
return output;

View File

@ -136,7 +136,7 @@ namespace hex::prv {
u32 page = std::floor((address - this->getBaseAddress()) / double(PageSize));
if (page >= this->getPageCount())
return {};
return std::nullopt;
return page;
}

View File

@ -305,7 +305,7 @@ namespace hex::plugin::builtin {
openFiles.erase(file);
return {};
return std::nullopt;
});
/* read(file, size) */
@ -329,7 +329,7 @@ namespace hex::plugin::builtin {
openFiles[file].write(data);
return {};
return std::nullopt;
});
/* seek(file, offset) */
@ -342,7 +342,7 @@ namespace hex::plugin::builtin {
openFiles[file].seek(offset);
return {};
return std::nullopt;
});
/* size(file) */
@ -365,7 +365,7 @@ namespace hex::plugin::builtin {
openFiles[file].setSize(size);
return {};
return std::nullopt;
});
/* flush(file) */
@ -377,7 +377,7 @@ namespace hex::plugin::builtin {
openFiles[file].flush();
return {};
return std::nullopt;
});
/* remove(file) */
@ -389,7 +389,7 @@ namespace hex::plugin::builtin {
openFiles[file].remove();
return {};
return std::nullopt;
});
}
}

View File

@ -96,7 +96,7 @@ namespace hex::plugin::builtin::prv {
auto ack = socket.readString(1);
if (ack.empty() || ack[0] != '+')
return {};
return false;
auto receivedPacket = socket.readString(6);

View File

@ -150,7 +150,7 @@ namespace hex::plugin::builtin {
mathEvaluator.registerStandardVariables();
mathInput.clear();
return {};
return std::nullopt;
},
0,
0);
@ -161,7 +161,7 @@ namespace hex::plugin::builtin {
auto provider = ImHexApi::Provider::get();
if (!ImHexApi::Provider::isValid() || !provider->isReadable() || args[0] >= provider->getActualSize())
return {};
return std::nullopt;
provider->read(args[0], &value, sizeof(u8));
@ -174,15 +174,15 @@ namespace hex::plugin::builtin {
"write", [](auto args) -> std::optional<long double> {
auto provider = ImHexApi::Provider::get();
if (!ImHexApi::Provider::isValid() || !provider->isWritable() || args[0] >= provider->getActualSize())
return {};
return std::nullopt;
if (args[1] > 0xFF)
return {};
return std::nullopt;
u8 value = args[1];
provider->write(args[0], &value, sizeof(u8));
return {};
return std::nullopt;
},
2,
2);

View File

@ -313,7 +313,7 @@ namespace hex {
}
if (evaluationStack.empty())
return {};
return std::nullopt;
else if (evaluationStack.size() > 1)
throw std::invalid_argument("Undigested input left!");
else