Skip to content

Commit 9955e9d

Browse files
committed
CPU: remove duplicate whitespaces in CPU name
Eg. `Intel(R) Xeon(R) CPU E5620 @ 2.40GHz`
1 parent 8b41cbd commit 9955e9d

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

src/detection/cpu/cpu.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const char* ffDetectCPU(const FFCPUOptions* options, FFCPUResult* cpu)
1616
ffStrbufRemoveStrings(&cpu->name, ARRAY_SIZE(removeStrings), removeStrings);
1717
ffStrbufSubstrBeforeFirstC(&cpu->name, '@'); //Cut the speed output in the name as we append our own
1818
ffStrbufTrimRight(&cpu->name, ' '); //If we removed the @ in previous step there was most likely a space before it
19+
ffStrbufRemoveDupWhitespaces(&cpu->name);
1920
return NULL;
2021
}
2122

src/util/FFstrbuf.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,25 @@ bool ffStrbufGetline(char** lineptr, size_t* n, FFstrbuf* buffer)
575575
*n = remaining;
576576
return true;
577577
}
578+
579+
bool ffStrbufRemoveDupWhitespaces(FFstrbuf* strbuf)
580+
{
581+
if (strbuf->allocated == 0) return false; // Doesn't work with static strings
582+
583+
bool changed = false;
584+
for (uint32_t i = 0; i < strbuf->length; i++)
585+
{
586+
if (strbuf->chars[i] != ' ') continue;
587+
588+
i++;
589+
uint32_t j = i;
590+
for (; j < strbuf->length && strbuf->chars[j] == ' '; j++);
591+
592+
if (j == i) continue;
593+
memmove(&strbuf->chars[i], &strbuf->chars[j], strbuf->length - j + 1);
594+
strbuf->length -= j - i;
595+
changed = true;
596+
}
597+
598+
return changed;
599+
}

src/util/FFstrbuf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void ffStrbufUpperCase(FFstrbuf* strbuf);
9191
void ffStrbufLowerCase(FFstrbuf* strbuf);
9292

9393
bool ffStrbufGetline(char** lineptr, size_t* n, FFstrbuf* buffer);
94+
bool ffStrbufRemoveDupWhitespaces(FFstrbuf* strbuf);
9495

9596
FF_C_NODISCARD static inline FFstrbuf ffStrbufCreateA(uint32_t allocate)
9697
{

tests/strbuf.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,34 @@ int main(void)
565565
VERIFY(i == 0);
566566
}
567567

568+
ffStrbufSetS(&strbuf, "Hello World");
569+
VERIFY(ffStrbufRemoveDupWhitespaces(&strbuf) == false);
570+
VERIFY(strcmp(strbuf.chars, "Hello World") == 0);
571+
572+
ffStrbufSetS(&strbuf, "Hello World");
573+
VERIFY(ffStrbufRemoveDupWhitespaces(&strbuf) == true);
574+
VERIFY(strcmp(strbuf.chars, "Hello World") == 0);
575+
576+
ffStrbufSetS(&strbuf, " Hello World ");
577+
VERIFY(ffStrbufRemoveDupWhitespaces(&strbuf) == true);
578+
VERIFY(strcmp(strbuf.chars, " Hello World ") == 0);
579+
580+
ffStrbufSetS(&strbuf, " Hello World ");
581+
VERIFY(ffStrbufRemoveDupWhitespaces(&strbuf) == true);
582+
VERIFY(strcmp(strbuf.chars, " Hello World ") == 0);
583+
584+
ffStrbufSetS(&strbuf, " ");
585+
VERIFY(ffStrbufRemoveDupWhitespaces(&strbuf) == true);
586+
VERIFY(strcmp(strbuf.chars, " ") == 0);
587+
588+
ffStrbufClear(&strbuf);
589+
VERIFY(ffStrbufRemoveDupWhitespaces(&strbuf) == false);
590+
VERIFY(strcmp(strbuf.chars, "") == 0);
591+
592+
ffStrbufSetStatic(&strbuf, " ");
593+
VERIFY(ffStrbufRemoveDupWhitespaces(&strbuf) == false);
594+
VERIFY(strcmp(strbuf.chars, " ") == 0);
595+
568596
//Success
569597
puts("\e[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET);
570598
}

0 commit comments

Comments
 (0)