diff --git a/src/common/detectGTK.c b/src/common/detectGTK.c index cdf0f3b87..7c1475f6e 100644 --- a/src/common/detectGTK.c +++ b/src/common/detectGTK.c @@ -152,25 +152,23 @@ static void detectGTK(FFinstance* instance, const char* version, const char* env FFstrbuf buffer; ffStrbufInitA(&buffer, 128); - uint32_t lastIndex; - // From ENV: GTK*_RC_FILES ffStrbufSetS(&buffer, getenv(envVariable)); - lastIndex = 0; - while (lastIndex < buffer.length) + uint32_t startIndex = 0; + while (startIndex < buffer.length) { - uint32_t colonIndex = ffStrbufFirstIndexAfterC(&buffer, lastIndex, ':'); + uint32_t colonIndex = ffStrbufNextIndexC(&buffer, startIndex, ':'); buffer.chars[colonIndex] = '\0'; - detectGTKFromConfigFile(buffer.chars + lastIndex, result); + detectGTKFromConfigFile(buffer.chars + startIndex, result); if(allPropertiesSet(result)) { ffStrbufDestroy(&buffer); return; } - lastIndex = colonIndex + 1; + startIndex = colonIndex + 1; } //From DConf / GSettings diff --git a/src/common/format.c b/src/common/format.c index 1c618b894..92a46a727 100644 --- a/src/common/format.c +++ b/src/common/format.c @@ -210,7 +210,7 @@ void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, const FFst } // fastforward to the end of the if without printing the in between - i = ffStrbufFirstIndexAfterS(formatstr, i, "{?}") + 2; // 2 is the length of "{?}" -1 because the loop will increament it again directly after continue + i = ffStrbufNextIndexS(formatstr, i, "{?}") + 2; // 2 is the length of "{?}" - 1 because the loop will increament it again directly after continue ffStrbufDestroy(&placeholderValue); continue; } @@ -247,7 +247,7 @@ void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, const FFst } // fastforward to the end of the if without printing the in between - i = ffStrbufFirstIndexAfterS(formatstr, i, "{/}") + 2; // 2 is the length of "{/}" -1 because the loop will increament it again directly after continue + i = ffStrbufNextIndexS(formatstr, i, "{/}") + 2; // 2 is the length of "{/}" - 1 because the loop will increament it again directly after continue ffStrbufDestroy(&placeholderValue); continue; } diff --git a/src/common/init.c b/src/common/init.c index d78965ac9..9564e4441 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -22,19 +22,21 @@ static void initConfigDirs(FFstate* state) } FFstrbuf xdgConfigDirs; - ffStrbufInitAS(&xdgConfigDirs, 64, getenv("XDG_CONFIG_DIRS")); - uint32_t lastIndex = 0; - while (lastIndex < xdgConfigDirs.length) + ffStrbufInitA(&xdgConfigDirs, 64); + ffStrbufAppendS(&xdgConfigDirs, getenv("XDG_CONFIG_DIRS")); + + uint32_t startIndex = 0; + while (startIndex < xdgConfigDirs.length) { - uint32_t colonIndex = ffStrbufFirstIndexAfterC(&xdgConfigDirs, lastIndex, ':'); + uint32_t colonIndex = ffStrbufNextIndexC(&xdgConfigDirs, startIndex, ':'); xdgConfigDirs.chars[colonIndex] = '\0'; FFstrbuf* buffer = (FFstrbuf*) ffListAdd(&state->configDirs); ffStrbufInitA(buffer, 64); - ffStrbufAppendS(buffer, xdgConfigDirs.chars + lastIndex); + ffStrbufAppendS(buffer, xdgConfigDirs.chars + startIndex); ffStrbufTrimRight(buffer, '/'); - lastIndex = colonIndex + 1; + startIndex = colonIndex + 1; } #define FF_ENSURE_ONLY_ONCE_IN_LIST(element) \ diff --git a/src/common/io.c b/src/common/io.c index ed44f8fb6..0e8272fa5 100644 --- a/src/common/io.c +++ b/src/common/io.c @@ -128,14 +128,14 @@ static bool printCachedValue(FFinstance* instance, const char* moduleName, const uint8_t moduleCounter = 1; - uint32_t lastIndex = 0; - while(lastIndex < content.length) + uint32_t startIndex = 0; + while(startIndex < content.length) { - uint32_t nullByteIndex = ffStrbufFirstIndexAfterC(&content, lastIndex, '\0'); + uint32_t nullByteIndex = ffStrbufNextIndexC(&content, startIndex, '\0'); uint8_t moduleIndex = (moduleCounter == 1 && nullByteIndex == content.length) ? 0 : moduleCounter; ffPrintLogoAndKey(instance, moduleName, moduleIndex, customKeyFormat); - puts(content.chars + lastIndex); - lastIndex = nullByteIndex + 1; + puts(content.chars + startIndex); + startIndex = nullByteIndex + 1; ++moduleCounter; } @@ -160,14 +160,14 @@ static bool printCachedFormat(FFinstance* instance, const char* moduleName, cons FFformatarg* arguments = calloc(numArgs, sizeof(FFformatarg)); uint32_t argumentCounter = 0; - uint32_t lastIndex = 0; - while(lastIndex < content.length) + uint32_t startIndex = 0; + while(startIndex < content.length) { arguments[argumentCounter].type = FF_FORMAT_ARG_TYPE_STRING; - arguments[argumentCounter].value = &content.chars[lastIndex]; + arguments[argumentCounter].value = &content.chars[startIndex]; ++argumentCounter; - uint32_t nullByteIndex = ffStrbufFirstIndexAfterC(&content, lastIndex, '\0'); + uint32_t nullByteIndex = ffStrbufNextIndexC(&content, startIndex, '\0'); if(argumentCounter == numArgs) { @@ -177,7 +177,7 @@ static bool printCachedFormat(FFinstance* instance, const char* moduleName, cons argumentCounter = 0; } - lastIndex = nullByteIndex + 1; + startIndex = nullByteIndex + 1; } free(arguments); diff --git a/src/fastfetch.c b/src/fastfetch.c index 5e0acfdaf..a894db471 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -1079,15 +1079,15 @@ static void run(FFinstance* instance, FFdata* data) ffStart(instance); - uint32_t lastIndex = 0; - while (lastIndex < data->structure.length) + uint32_t startIndex = 0; + while (startIndex < data->structure.length) { - uint32_t colonIndex = ffStrbufFirstIndexAfterC(&data->structure, lastIndex, ':'); + uint32_t colonIndex = ffStrbufNextIndexC(&data->structure, startIndex, ':'); data->structure.chars[colonIndex] = '\0'; - parseStructureCommand(instance, data, data->structure.chars + lastIndex); + parseStructureCommand(instance, data, data->structure.chars + startIndex); - lastIndex = colonIndex + 1; + startIndex = colonIndex + 1; } ffFinish(instance); diff --git a/src/modules/disk.c b/src/modules/disk.c index e5bafce21..16c730b13 100644 --- a/src/modules/disk.c +++ b/src/modules/disk.c @@ -114,15 +114,15 @@ void ffPrintDisk(FFinstance* instance) return; } - uint32_t lastIndex = 0; - while (lastIndex < instance->config.diskFolders.length) + uint32_t startIndex = 0; + while (startIndex < instance->config.diskFolders.length) { - uint32_t colonIndex = ffStrbufFirstIndexAfterC(&instance->config.diskFolders, lastIndex, ':'); + uint32_t colonIndex = ffStrbufNextIndexC(&instance->config.diskFolders, startIndex, ':'); instance->config.diskFolders.chars[colonIndex] = '\0'; - printFolder(instance, instance->config.diskFolders.chars + lastIndex); + printFolder(instance, instance->config.diskFolders.chars + startIndex); - lastIndex = colonIndex + 1; + startIndex = colonIndex + 1; } } } diff --git a/src/util/FFstrbuf.c b/src/util/FFstrbuf.c index 186d67fa4..fbcdcead3 100644 --- a/src/util/FFstrbuf.c +++ b/src/util/FFstrbuf.c @@ -381,7 +381,7 @@ void ffStrbufRemoveStrings(FFstrbuf* strbuf, uint32_t numStrings, ...) va_end(argp); } -uint32_t ffStrbufFirstIndexAfterC(const FFstrbuf* strbuf, uint32_t start, const char c) +uint32_t ffStrbufNextIndexC(const FFstrbuf* strbuf, uint32_t start, char c) { for(uint32_t i = start; i < strbuf->length; i++) { @@ -391,14 +391,14 @@ uint32_t ffStrbufFirstIndexAfterC(const FFstrbuf* strbuf, uint32_t start, const return strbuf->length; } -uint32_t ffStrbufFirstIndexC(const FFstrbuf* strbuf, const char c) +uint32_t ffStrbufFirstIndexC(const FFstrbuf* strbuf, char c) { - return ffStrbufFirstIndexAfterC(strbuf, 0, c); + return ffStrbufNextIndexC(strbuf, 0, c); } -uint32_t ffStrbufFirstIndexAfterS(const FFstrbuf* strbuf, uint32_t start, const char* str) +uint32_t ffStrbufNextIndexS(const FFstrbuf* strbuf, uint32_t start, const char* str) { - for(uint32_t i = start + 1; i < strbuf->length; i++) + for(uint32_t i = start; i < strbuf->length; i++) { bool found = true; @@ -417,18 +417,22 @@ uint32_t ffStrbufFirstIndexAfterS(const FFstrbuf* strbuf, uint32_t start, const if(found) return i; } + return strbuf->length; } uint32_t ffStrbufFirstIndexS(const FFstrbuf* strbuf, const char* str) { - return ffStrbufFirstIndexAfterS(strbuf, 0, str); + return ffStrbufNextIndexS(strbuf, 0, str); } -uint32_t ffStrbufLastIndexC(const FFstrbuf* strbuf, const char c) +uint32_t ffStrbufPreviousIndexC(const FFstrbuf* strbuf, uint32_t start, char c) { + if(start >= strbuf->length) + return strbuf->length; + //We need to loop one higher than the actual index, because uint32_t is guranteed to be >= 0, so this statement would always be true - for(uint32_t i = strbuf->length; i > 0; i--) + for(uint32_t i = start + 1; i > 0; i--) { if(strbuf->chars[i - 1] == c) return i - 1; @@ -436,6 +440,11 @@ uint32_t ffStrbufLastIndexC(const FFstrbuf* strbuf, const char c) return strbuf->length; } +uint32_t ffStrbufLastIndexC(const FFstrbuf* strbuf, char c) +{ + return ffStrbufPreviousIndexC(strbuf, strbuf->length - 1, c); +} + void ffStrbufSubstrBefore(FFstrbuf* strbuf, uint32_t index) { if(strbuf->length <= index) diff --git a/src/util/FFstrbuf.h b/src/util/FFstrbuf.h index 93f04244e..78f2c3452 100644 --- a/src/util/FFstrbuf.h +++ b/src/util/FFstrbuf.h @@ -72,11 +72,12 @@ void ffStrbufRemoveStringsA(FFstrbuf* strbuf, uint32_t numStrings, const char* s void ffStrbufRemoveStringsV(FFstrbuf* strbuf, uint32_t numStrings, va_list arguments); void ffStrbufRemoveStrings(FFstrbuf* strbuf, uint32_t numStrings, ...); -uint32_t ffStrbufFirstIndexAfterC(const FFstrbuf* strbuf, uint32_t start, const char c); -uint32_t ffStrbufFirstIndexC(const FFstrbuf* strbuf, const char c); -uint32_t ffStrbufFirstIndexAfterS(const FFstrbuf* strbuf, uint32_t start, const char* str); +uint32_t ffStrbufNextIndexC(const FFstrbuf* strbuf, uint32_t start, char c); +uint32_t ffStrbufFirstIndexC(const FFstrbuf* strbuf, char c); +uint32_t ffStrbufNextIndexS(const FFstrbuf* strbuf, uint32_t start, const char* str); uint32_t ffStrbufFirstIndexS(const FFstrbuf* strbuf, const char* str); -uint32_t ffStrbufLastIndexC(const FFstrbuf* strbuf, const char c); +uint32_t ffStrbufPreviousIndexC(const FFstrbuf* strbuf, uint32_t start, char c); +uint32_t ffStrbufLastIndexC(const FFstrbuf* strbuf, char c); void ffStrbufSubstrBefore(FFstrbuf* strbuf, uint32_t index); void ffStrbufSubstrBeforeFirstC(FFstrbuf* strbuf, const char c);