Base: Replace if else with switch statement

This commit is contained in:
Ladislav Michl
2024-01-20 18:18:09 +01:00
committed by 3x380V
parent 95b37fa806
commit f1fdd2e2a9
5 changed files with 115 additions and 121 deletions

View File

@@ -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;

View File

@@ -76,32 +76,34 @@ std::string Persistence::encodeAttribute(const std::string& str)
{
std::string tmp;
for (char it : str) {
if (it == '<') {
tmp += "&lt;";
}
else if (it == '\"') {
tmp += "&quot;";
}
else if (it == '\'') {
tmp += "&apos;";
}
else if (it == '&') {
tmp += "&amp;";
}
else if (it == '>') {
tmp += "&gt;";
}
else if (it == '\r') {
tmp += "&#13;";
}
else if (it == '\n') {
tmp += "&#10;";
}
else if (it == '\t') {
tmp += "&#9;";
}
else {
tmp += it;
switch (it) {
case '<':
tmp += "&lt;";
break;
case '\"':
tmp += "&quot;";
break;
case '\'':
tmp += "&apos;";
break;
case '&':
tmp += "&amp;";
break;
case '>':
tmp += "&gt;";
break;
case '\r':
tmp += "&#13;";
break;
case '\n':
tmp += "&#10;";
break;
case '\t':
tmp += "&#9;";
break;
default:
tmp += it;
break;
}
}

View File

@@ -541,35 +541,31 @@ PyObject* QuantityPy::richCompare(PyObject* v, PyObject* w, int op)
const Quantity* u2 = static_cast<QuantityPy*>(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;
}
}

View File

@@ -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;

View File

@@ -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<VectorPy*>(self)->value();
Py::Tuple xyz(3);
xyz.setItem(0, Py::Float(v.x));