From aa2a4edf7e17c0825ffee2e6b508d40d9a6591ac Mon Sep 17 00:00:00 2001 From: michael Date: Wed, 17 Jul 2024 13:12:15 +0200 Subject: [PATCH] Update Helper.cpp --- code/components/jomjol_helper/Helper.cpp | 104 +++++++++++++++++++++-- 1 file changed, 98 insertions(+), 6 deletions(-) diff --git a/code/components/jomjol_helper/Helper.cpp b/code/components/jomjol_helper/Helper.cpp index d61631552..c4112ecf2 100644 --- a/code/components/jomjol_helper/Helper.cpp +++ b/code/components/jomjol_helper/Helper.cpp @@ -1198,7 +1198,7 @@ bool isInString(std::string &s, std::string const &toFind) { std::size_t pos = s.find(toFind); - if (pos == std::string::npos) + if (pos == std::string::npos) { // Not found return false; @@ -1210,9 +1210,101 @@ bool isInString(std::string &s, std::string const &toFind) // from https://stackoverflow.com/a/14678800 void replaceAll(std::string& s, const std::string& toReplace, const std::string& replaceWith) { - size_t pos = 0; - while ((pos = s.find(toReplace, pos)) != std::string::npos) { - s.replace(pos, toReplace.length(), replaceWith); - pos += replaceWith.length(); - } + size_t pos = 0; + + while ((pos = s.find(toReplace, pos)) != std::string::npos) + { + s.replace(pos, toReplace.length(), replaceWith); + pos += replaceWith.length(); + } +} + +bool isStringNumeric(std::string &input) +{ + int start = 0; + int punkt_existiert_schon = 0; + + if (input[0] == '-') + { + start = 1; + } + + for (int i = start; i < input.size(); i++) + { + // if ((ispunct(input[i])) && (i > 0) && (punkt_existiert_schon == 0)) + if (((input[i] == '.') || (input[i] == ',')) && (i > 0) && (punkt_existiert_schon == 0)) + { + punkt_existiert_schon = 1; + i++; + } + else if (!isdigit(input[i])) + { + return false; + } + } + + return true; +} + +bool isStringAlphabetic(std::string &input) +{ + for (int i = 0; i < input.size(); i++) + { + if (!isalpha(input[i])) + { + return false; + } + } + + return true; +} + +bool isStringAlphanumeric(std::string &input) +{ + for (int i = 0; i < input.size(); i++) + { + if (!isalnum(input[i])) + { + return false; + } + } + + return true; +} + +bool alphanumericToBoolean(std::string &input) +{ + if (isStringAlphabetic(input)) + { + return stringToBoolean(toUpper(input)); + } + else if (isStringNumeric(input)) + { + return numericStrToBool(input); + } + + return false; +} + +int clipInt(int input, int high, int low) +{ + if (input < low) + { + input = low; + } + else if (input > high) + { + input = high; + } + return input; +} + +bool numericStrToBool(std::string input) +{ + return (std::stoi(input) != 0); +} + +bool stringToBoolean(std::string input) +{ + return (input == "TRUE"); }