From 3ba899261f9d1730428f00b14da1a776283d54e3 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Tue, 28 May 2024 15:34:59 +0800 Subject: [PATCH] Util: add `ffCharIsDigit` and use it --- src/detection/brightness/brightness_linux.c | 5 ++--- src/detection/cpu/cpu_linux.c | 3 +-- src/detection/displayserver/linux/wmde.c | 3 +-- src/detection/editor/editor.c | 2 +- src/detection/gamepad/gamepad_linux.c | 5 +---- src/detection/os/os_windows.cpp | 5 +++-- src/detection/packages/packages_linux.c | 6 +++--- src/detection/processes/processes_linux.c | 5 ++--- src/logo/logo.c | 2 +- src/util/stringUtils.h | 5 +++++ 10 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/detection/brightness/brightness_linux.c b/src/detection/brightness/brightness_linux.c index adc1d3923..de105504b 100644 --- a/src/detection/brightness/brightness_linux.c +++ b/src/detection/brightness/brightness_linux.c @@ -4,7 +4,6 @@ #include "util/stringUtils.h" #include -#include #include static const char* detectWithBacklight(FFlist* result) @@ -58,14 +57,14 @@ static const char* detectWithBacklight(FFlist* result) { ffStrbufSubstrBeforeLastC(&brightness->name, '/'); // remove "/edid" ffStrbufSubstrAfterLastC(&brightness->name, '/'); // try getting DRM connector name - if(isdigit(brightness->name.chars[0])) + if(ffCharIsDigit(brightness->name.chars[0])) { // PCI address or some unknown path, give up ffStrbufSetS(&brightness->name, entry->d_name); } else { - if(ffStrbufStartsWithS(&brightness->name, "card") && isdigit(brightness->name.chars[4])) + if(ffStrbufStartsWithS(&brightness->name, "card") && ffCharIsDigit(brightness->name.chars[4])) ffStrbufSubstrAfterFirstC(&brightness->name, '-'); } } diff --git a/src/detection/cpu/cpu_linux.c b/src/detection/cpu/cpu_linux.c index ac5c895e4..53aa739f1 100644 --- a/src/detection/cpu/cpu_linux.c +++ b/src/detection/cpu/cpu_linux.c @@ -9,7 +9,6 @@ #include #include #include -#include #ifdef __ANDROID__ #include "common/settings.h" @@ -139,7 +138,7 @@ static bool detectFrequency(FFCPUResult* cpu, const FFCPUOptions* options) struct dirent* entry; while ((entry = readdir(dir)) != NULL) { - if (ffStrStartsWith(entry->d_name, "policy") && isdigit(entry->d_name[strlen("policy")])) + if (ffStrStartsWith(entry->d_name, "policy") && ffCharIsDigit(entry->d_name[strlen("policy")])) { ffStrbufAppendS(&path, entry->d_name); uint32_t fbase = getFrequency(&path, "/base_frequency", NULL, &buffer); diff --git a/src/detection/displayserver/linux/wmde.c b/src/detection/displayserver/linux/wmde.c index 64f16db03..4ee96cd48 100644 --- a/src/detection/displayserver/linux/wmde.c +++ b/src/detection/displayserver/linux/wmde.c @@ -4,7 +4,6 @@ #include "util/mallocHelper.h" #include -#include #include #include @@ -320,7 +319,7 @@ static const char* getFromProcesses(FFDisplayServerResult* result) while((dirent = readdir(procdir)) != NULL) { //Match only folders starting with a number (the pid folders) - if(dirent->d_type != DT_DIR || !isdigit(dirent->d_name[0])) + if(dirent->d_type != DT_DIR || !ffCharIsDigit(dirent->d_name[0])) continue; ffStrbufAppendS(&procPath, dirent->d_name); diff --git a/src/detection/editor/editor.c b/src/detection/editor/editor.c index 942435cfb..be6ee4a9c 100644 --- a/src/detection/editor/editor.c +++ b/src/detection/editor/editor.c @@ -114,7 +114,7 @@ const char* ffDetectEditor(FFEditorResult* result) for (uint32_t iStart = 0; iStart < result->version.length; ++iStart) { char c = result->version.chars[iStart]; - if (c >= '0' && c <= '9') + if (ffCharIsDigit(c)) { for (uint32_t iEnd = iStart + 1; iEnd < result->version.length; ++iEnd) { diff --git a/src/detection/gamepad/gamepad_linux.c b/src/detection/gamepad/gamepad_linux.c index 791412a70..50d04df5e 100644 --- a/src/detection/gamepad/gamepad_linux.c +++ b/src/detection/gamepad/gamepad_linux.c @@ -2,9 +2,6 @@ #include "common/io/io.h" #include "util/stringUtils.h" -#include -#include - static void detectGamepad(FFlist* devices, FFstrbuf* name, FFstrbuf* path) { uint32_t baseLen = path->length; @@ -71,7 +68,7 @@ const char* ffDetectGamepad(FFlist* devices /* List of FFGamepadDevice */) { if (!ffStrStartsWith(entry->d_name, "js")) continue; - if (!isdigit(entry->d_name[2])) + if (!ffCharIsDigit(entry->d_name[2])) continue; ffStrbufAppendS(&path, entry->d_name); diff --git a/src/detection/os/os_windows.cpp b/src/detection/os/os_windows.cpp index bd442da7d..8734f74c1 100644 --- a/src/detection/os/os_windows.cpp +++ b/src/detection/os/os_windows.cpp @@ -4,6 +4,7 @@ extern "C" { } #include "util/windows/unicode.hpp" #include "util/windows/wmi.hpp" +#include "util/stringUtils.h" static const char* getOsNameByWmi(FFstrbuf* osName) { @@ -73,8 +74,8 @@ void ffDetectOSImpl(FFOSResult* os) if(ffStrbufEndsWithC(&os->prettyName, 'r')) { if(os->variant.chars[0] == 'R' && - isdigit(os->variant.chars[1]) && - (os->variant.chars[2] == '\0' || os->variant.chars[2] == ' ')) + ffCharIsDigit(os->variant.chars[1]) && + (os->variant.chars[2] == '\0' || os->variant.chars[2] == ' ')) { ffStrbufAppendF(&os->version, " R%c", os->variant.chars[1]); ffStrbufSubstrAfter(&os->variant, strlen("Rx ") - 1); diff --git a/src/detection/packages/packages_linux.c b/src/detection/packages/packages_linux.c index 2665ce69b..e3178d0e6 100644 --- a/src/detection/packages/packages_linux.c +++ b/src/detection/packages/packages_linux.c @@ -141,11 +141,11 @@ static bool isValidNixPkg(FFstrbuf* pkg) switch (state) { case START: - if (c >= '0' && c <= '9') + if (ffCharIsDigit(c)) state = DIGIT; break; case DIGIT: - if (c >= '0' && c <= '9') + if (ffCharIsDigit(c)) continue; if (c == '.') state = DOT; @@ -153,7 +153,7 @@ static bool isValidNixPkg(FFstrbuf* pkg) state = START; break; case DOT: - if (c >= '0' && c <= '9') + if (ffCharIsDigit(c)) state = MATCH; else state = START; diff --git a/src/detection/processes/processes_linux.c b/src/detection/processes/processes_linux.c index cff71038c..25e3a84af 100644 --- a/src/detection/processes/processes_linux.c +++ b/src/detection/processes/processes_linux.c @@ -1,8 +1,7 @@ #include "processes.h" #include "common/io/io.h" - -#include +#include "util/stringUtils.h" const char* ffDetectProcesses(uint32_t* result) { @@ -15,7 +14,7 @@ const char* ffDetectProcesses(uint32_t* result) struct dirent* entry; while ((entry = readdir(dir)) != NULL) { - if (entry->d_type == DT_DIR && isdigit(entry->d_name[0])) + if (entry->d_type == DT_DIR && ffCharIsDigit(entry->d_name[0])) ++num; } diff --git a/src/logo/logo.c b/src/logo/logo.c index a7a346ae7..01dfb1de3 100644 --- a/src/logo/logo.c +++ b/src/logo/logo.c @@ -113,7 +113,7 @@ void ffLogoPrintChars(const char* data, bool doColorReplacement) ffStrbufAppendS(&result, "\e["); data += 2; - while(isdigit(*data) || *data == ';') + while(ffCharIsDigit(*data) || *data == ';') ffStrbufAppendC(&result, *data++); // number //We have a valid control sequence, print it and continue with next char diff --git a/src/util/stringUtils.h b/src/util/stringUtils.h index ad24057b5..a7dea3fbe 100644 --- a/src/util/stringUtils.h +++ b/src/util/stringUtils.h @@ -74,3 +74,8 @@ static inline bool ffCharIsEnglishAlphabet(char c) { return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); } + +static inline bool ffCharIsDigit(char c) +{ + return '0' <= c && c <= '9'; +}