From dd572ba024a6e71c96104f30998b8caf195ac650 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 30 Jan 2022 12:43:43 +0100 Subject: [PATCH] sys: Return std::nullopt instead of {} for empty optional values --- .../include/hex/pattern_language/ast_node.hpp | 10 ++--- lib/libimhex/source/helpers/magic.cpp | 2 +- lib/libimhex/source/helpers/net.cpp | 2 +- .../source/pattern_language/lexer.cpp | 37 +++++++++---------- .../pattern_language/pattern_language.cpp | 10 ++--- .../source/pattern_language/preprocessor.cpp | 2 +- lib/libimhex/source/providers/provider.cpp | 2 +- .../source/content/pl_builtin_functions.cpp | 12 +++--- .../source/content/providers/gdb_provider.cpp | 2 +- .../builtin/source/content/tools_entries.cpp | 10 ++--- plugins/builtin/source/math_evaluator.cpp | 2 +- 11 files changed, 45 insertions(+), 46 deletions(-) diff --git a/lib/libimhex/include/hex/pattern_language/ast_node.hpp b/lib/libimhex/include/hex/pattern_language/ast_node.hpp index ec4bf9dc6..425245e39 100644 --- a/lib/libimhex/include/hex/pattern_language/ast_node.hpp +++ b/lib/libimhex/include/hex/pattern_language/ast_node.hpp @@ -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 createPatterns(Evaluator *evaluator) const override { this->execute(evaluator); - return { }; + return {}; } FunctionResult execute(Evaluator *evaluator) const override { diff --git a/lib/libimhex/source/helpers/magic.cpp b/lib/libimhex/source/helpers/magic.cpp index 8f0a23a48..eb768b95f 100644 --- a/lib/libimhex/source/helpers/magic.cpp +++ b/lib/libimhex/source/helpers/magic.cpp @@ -32,7 +32,7 @@ namespace hex::magic { } if (error) - return {}; + return std::nullopt; else return magicFiles; } diff --git a/lib/libimhex/source/helpers/net.cpp b/lib/libimhex/source/helpers/net.cpp index 61b2287c5..af8632847 100644 --- a/lib/libimhex/source/helpers/net.cpp +++ b/lib/libimhex/source/helpers/net.cpp @@ -132,7 +132,7 @@ namespace hex { this->m_shouldCancel = false; if (result != CURLE_OK) - return {}; + return std::nullopt; else return responseCode; } diff --git a/lib/libimhex/source/pattern_language/lexer.cpp b/lib/libimhex/source/pattern_language/lexer.cpp index 0bfa6d8a0..db62fbdf2 100644 --- a/lib/libimhex/source/pattern_language/lexer.cpp +++ b/lib/libimhex/source/pattern_language/lexer.cpp @@ -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> 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} }; diff --git a/lib/libimhex/source/pattern_language/pattern_language.cpp b/lib/libimhex/source/pattern_language/pattern_language.cpp index f34206d71..135894db1 100644 --- a/lib/libimhex/source/pattern_language/pattern_language.cpp +++ b/lib/libimhex/source/pattern_language/pattern_language.cpp @@ -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; diff --git a/lib/libimhex/source/pattern_language/preprocessor.cpp b/lib/libimhex/source/pattern_language/preprocessor.cpp index 1a3d32ec4..aa747f968 100644 --- a/lib/libimhex/source/pattern_language/preprocessor.cpp +++ b/lib/libimhex/source/pattern_language/preprocessor.cpp @@ -212,7 +212,7 @@ namespace hex::pl { } } catch (PreprocessorError &e) { this->m_error = e; - return {}; + return std::nullopt; } return output; diff --git a/lib/libimhex/source/providers/provider.cpp b/lib/libimhex/source/providers/provider.cpp index 189e4a301..2eb59eb51 100644 --- a/lib/libimhex/source/providers/provider.cpp +++ b/lib/libimhex/source/providers/provider.cpp @@ -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; } diff --git a/plugins/builtin/source/content/pl_builtin_functions.cpp b/plugins/builtin/source/content/pl_builtin_functions.cpp index fe3eb2935..79ec26695 100644 --- a/plugins/builtin/source/content/pl_builtin_functions.cpp +++ b/plugins/builtin/source/content/pl_builtin_functions.cpp @@ -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; }); } } diff --git a/plugins/builtin/source/content/providers/gdb_provider.cpp b/plugins/builtin/source/content/providers/gdb_provider.cpp index 9830ee6d0..388926369 100644 --- a/plugins/builtin/source/content/providers/gdb_provider.cpp +++ b/plugins/builtin/source/content/providers/gdb_provider.cpp @@ -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); diff --git a/plugins/builtin/source/content/tools_entries.cpp b/plugins/builtin/source/content/tools_entries.cpp index 7f85b063e..c7887a160 100644 --- a/plugins/builtin/source/content/tools_entries.cpp +++ b/plugins/builtin/source/content/tools_entries.cpp @@ -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 { 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); diff --git a/plugins/builtin/source/math_evaluator.cpp b/plugins/builtin/source/math_evaluator.cpp index 1c2540b85..3d9bdd15a 100644 --- a/plugins/builtin/source/math_evaluator.cpp +++ b/plugins/builtin/source/math_evaluator.cpp @@ -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