Base: Add isNullOrEmpty string helper

This adds isNullOrEmpty string helper that cheks if string is... well
null or empty. It is done to improve readability of the code and better
express intent.
This commit is contained in:
Kacper Donat
2025-02-20 17:47:06 +01:00
parent 8c8179feb9
commit cc2efa90f8
47 changed files with 113 additions and 72 deletions

View File

@@ -35,6 +35,7 @@
#include "ConsoleObserver.h"
#include "Interpreter.h"
#include "Tools.h"
using namespace Base;
@@ -343,7 +344,7 @@ std::stringstream& LogLevel::prefix(std::stringstream& str, const char* src, int
#endif
}
}
if (print_src && src && src[0]) {
if (print_src && !Base::Tools::isNullOrEmpty(src)) {
#ifdef FC_OS_WIN32
const char* _f = std::strrchr(src, '\\');
#else

View File

@@ -45,6 +45,7 @@
#include "Exception.h"
#include "Stream.h"
#include "TimeInfo.h"
#include "Tools.h"
using namespace Base;
@@ -119,7 +120,7 @@ const std::string& FileInfo::getTempPath()
delete[] dest;
#else
const char* tmp = getenv("TMPDIR");
if (tmp && tmp[0] != '\0') {
if (!Base::Tools::isNullOrEmpty(tmp)) {
tempPath = tmp;
FileInfo fi(tempPath);
if (tempPath.empty() || !fi.isDir()) { // still empty or non-existent

View File

@@ -42,6 +42,8 @@
#include "Exception.h"
#include "Interpreter.h"
#include <Tools.h>
namespace Base
{
@@ -73,7 +75,7 @@ public:
Py::Tuple args(2);
args.setItem(0, Py::asObject(GetPyObject(hGrp)));
// A Reason of null indicates to clear the parameter group
if (Reason && Reason[0] != '\0') {
if (!Base::Tools::isNullOrEmpty(Reason)) {
args.setItem(1, Py::String(Reason));
}
method.apply(args);

View File

@@ -301,6 +301,11 @@ struct BaseExport Tools
*/
static std::string quoted(const std::string&);
static constexpr bool isNullOrEmpty(const char* str)
{
return !str || str[0] == '\0';
}
/**
* @brief joinList
* Join the vector of strings \a vec using the separator \a sep