From f1fdd2e2a94c29609f534ece21503ea6f114e7d1 Mon Sep 17 00:00:00 2001 From: Ladislav Michl Date: Sat, 20 Jan 2024 18:18:09 +0100 Subject: [PATCH] Base: Replace if else with switch statement --- src/Base/Console.h | 30 +++++------ src/Base/Persistence.cpp | 54 ++++++++++--------- src/Base/QuantityPyImp.cpp | 108 +++++++++++++++++-------------------- src/Base/Tools.cpp | 42 ++++++++------- src/Base/VectorPyImp.cpp | 2 +- 5 files changed, 115 insertions(+), 121 deletions(-) diff --git a/src/Base/Console.h b/src/Base/Console.h index ab2a2b3616..ed350bd5ca 100644 --- a/src/Base/Console.h +++ b/src/Base/Console.h @@ -579,23 +579,19 @@ public: */ bool isActive(Base::LogStyle category) const { - if (category == Base::LogStyle::Log) { - return bLog; - } - if (category == Base::LogStyle::Warning) { - return bWrn; - } - if (category == Base::LogStyle::Error) { - return bErr; - } - if (category == Base::LogStyle::Message) { - return bMsg; - } - if (category == Base::LogStyle::Critical) { - return bCritical; - } - if (category == Base::LogStyle::Notification) { - return bNotification; + switch (category) { + case Base::LogStyle::Log: + return bLog; + case Base::LogStyle::Warning: + return bWrn; + case Base::LogStyle::Error: + return bErr; + case Base::LogStyle::Message: + return bMsg; + case Base::LogStyle::Critical: + return bCritical; + case Base::LogStyle::Notification: + return bNotification; } return false; diff --git a/src/Base/Persistence.cpp b/src/Base/Persistence.cpp index 094b85767f..7dc3f7d42e 100644 --- a/src/Base/Persistence.cpp +++ b/src/Base/Persistence.cpp @@ -76,32 +76,34 @@ std::string Persistence::encodeAttribute(const std::string& str) { std::string tmp; for (char it : str) { - if (it == '<') { - tmp += "<"; - } - else if (it == '\"') { - tmp += """; - } - else if (it == '\'') { - tmp += "'"; - } - else if (it == '&') { - tmp += "&"; - } - else if (it == '>') { - tmp += ">"; - } - else if (it == '\r') { - tmp += " "; - } - else if (it == '\n') { - tmp += " "; - } - else if (it == '\t') { - tmp += " "; - } - else { - tmp += it; + switch (it) { + case '<': + tmp += "<"; + break; + case '\"': + tmp += """; + break; + case '\'': + tmp += "'"; + break; + case '&': + tmp += "&"; + break; + case '>': + tmp += ">"; + break; + case '\r': + tmp += " "; + break; + case '\n': + tmp += " "; + break; + case '\t': + tmp += " "; + break; + default: + tmp += it; + break; } } diff --git a/src/Base/QuantityPyImp.cpp b/src/Base/QuantityPyImp.cpp index 527c01e2d4..4a59496f42 100644 --- a/src/Base/QuantityPyImp.cpp +++ b/src/Base/QuantityPyImp.cpp @@ -541,35 +541,31 @@ PyObject* QuantityPy::richCompare(PyObject* v, PyObject* w, int op) const Quantity* u2 = static_cast(w)->getQuantityPtr(); PyObject* res = nullptr; - if (op == Py_NE) { - res = (!(*u1 == *u2)) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - else if (op == Py_LT) { - res = (*u1 < *u2) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - else if (op == Py_LE) { - res = (*u1 < *u2) || (*u1 == *u2) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - else if (op == Py_GT) { - res = (!(*u1 < *u2)) && (!(*u1 == *u2)) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - else if (op == Py_GE) { - res = (!(*u1 < *u2)) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - else if (op == Py_EQ) { - res = (*u1 == *u2) ? Py_True : Py_False; - Py_INCREF(res); - return res; + switch (op) { + case Py_NE: + res = (!(*u1 == *u2)) ? Py_True : Py_False; + Py_INCREF(res); + return res; + case Py_LT: + res = (*u1 < *u2) ? Py_True : Py_False; + Py_INCREF(res); + return res; + case Py_LE: + res = (*u1 < *u2) || (*u1 == *u2) ? Py_True : Py_False; + Py_INCREF(res); + return res; + case Py_GT: + res = (!(*u1 < *u2)) && (!(*u1 == *u2)) ? Py_True : Py_False; + Py_INCREF(res); + return res; + case Py_GE: + res = (!(*u1 < *u2)) ? Py_True : Py_False; + Py_INCREF(res); + return res; + case Py_EQ: + res = (*u1 == *u2) ? Py_True : Py_False; + Py_INCREF(res); + return res; } } else if (PyNumber_Check(v) && PyNumber_Check(w)) { @@ -577,35 +573,31 @@ PyObject* QuantityPy::richCompare(PyObject* v, PyObject* w, int op) double u1 = PyFloat_AsDouble(v); double u2 = PyFloat_AsDouble(w); PyObject* res = nullptr; - if (op == Py_NE) { - res = (u1 != u2) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - else if (op == Py_LT) { - res = (u1 < u2) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - else if (op == Py_LE) { - res = (u1 <= u2) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - else if (op == Py_GT) { - res = (u1 > u2) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - else if (op == Py_GE) { - res = (u1 >= u2) ? Py_True : Py_False; - Py_INCREF(res); - return res; - } - else if (op == Py_EQ) { - res = (u1 == u2) ? Py_True : Py_False; - Py_INCREF(res); - return res; + switch (op) { + case Py_NE: + res = (u1 != u2) ? Py_True : Py_False; + Py_INCREF(res); + return res; + case Py_LT: + res = (u1 < u2) ? Py_True : Py_False; + Py_INCREF(res); + return res; + case Py_LE: + res = (u1 <= u2) ? Py_True : Py_False; + Py_INCREF(res); + return res; + case Py_GT: + res = (u1 > u2) ? Py_True : Py_False; + Py_INCREF(res); + return res; + case Py_GE: + res = (u1 >= u2) ? Py_True : Py_False; + Py_INCREF(res); + return res; + case Py_EQ: + res = (u1 == u2) ? Py_True : Py_False; + Py_INCREF(res); + return res; } } diff --git a/src/Base/Tools.cpp b/src/Base/Tools.cpp index 1f315ce721..9e313be85e 100644 --- a/src/Base/Tools.cpp +++ b/src/Base/Tools.cpp @@ -264,17 +264,19 @@ std::string Base::Tools::escapeEncodeString(const std::string& s) std::string result; size_t len = s.size(); for (size_t i = 0; i < len; ++i) { - if (s.at(i) == '\\') { - result += "\\\\"; - } - else if (s.at(i) == '\"') { - result += "\\\""; - } - else if (s.at(i) == '\'') { - result += "\\\'"; - } - else { - result += s.at(i); + switch (s.at(i)) { + case '\\': + result += "\\\\"; + break; + case '\"': + result += "\\\""; + break; + case '\'': + result += "\\\'"; + break; + default: + result += s.at(i); + break; } } return result; @@ -305,14 +307,16 @@ std::string Base::Tools::escapeEncodeFilename(const std::string& s) std::string result; size_t len = s.size(); for (size_t i = 0; i < len; ++i) { - if (s.at(i) == '\"') { - result += "\\\""; - } - else if (s.at(i) == '\'') { - result += "\\\'"; - } - else { - result += s.at(i); + switch (s.at(i)) { + case '\"': + result += "\\\""; + break; + case '\'': + result += "\\\'"; + break; + default: + result += s.at(i); + break; } } return result; diff --git a/src/Base/VectorPyImp.cpp b/src/Base/VectorPyImp.cpp index bf2b66181b..80d730c39d 100644 --- a/src/Base/VectorPyImp.cpp +++ b/src/Base/VectorPyImp.cpp @@ -250,7 +250,7 @@ PyObject* VectorPy::mapping_subscript(PyObject* self, PyObject* item) return PyTuple_New(0); } if (start == 0 && step == 1 && slicelength == sequence_length(self) - && PyObject_TypeCheck(self, &(VectorPy::Type))) { + && PyObject_TypeCheck(self, &(VectorPy::Type))) { Base::Vector3d v = static_cast(self)->value(); Py::Tuple xyz(3); xyz.setItem(0, Py::Float(v.x));