mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
FFstrbuf: fix bugs in ffStrbufAppendS; add more tests
This commit is contained in:
+8
-21
@@ -31,10 +31,8 @@ void ffStrbufInitCopy(FFstrbuf* strbuf, const FFstrbuf* src)
|
||||
|
||||
void ffStrbufInitS(FFstrbuf* strbuf, const char* str)
|
||||
{
|
||||
uint32_t bufSize = (uint32_t)strlen(str) + 1;
|
||||
ffStrbufInitA(strbuf, FASTFETCH_STRBUF_DEFAULT_ALLOC > bufSize ? FASTFETCH_STRBUF_DEFAULT_ALLOC : bufSize);
|
||||
memcpy(strbuf->chars, str, bufSize);
|
||||
strbuf->length = bufSize - 1;
|
||||
ffStrbufInitA(strbuf, 0);
|
||||
ffStrbufAppendS(strbuf, str);
|
||||
}
|
||||
|
||||
uint32_t ffStrbufGetFree(const FFstrbuf* strbuf)
|
||||
@@ -102,14 +100,7 @@ void ffStrbufAppendS(FFstrbuf* strbuf, const char* value)
|
||||
if(value == NULL)
|
||||
return;
|
||||
|
||||
for(uint32_t i = 0; value[i] != '\0'; i++)
|
||||
{
|
||||
if(i % 16 == 0)
|
||||
ffStrbufEnsureFree(strbuf, 16);
|
||||
strbuf->chars[strbuf->length++] = value[i];
|
||||
}
|
||||
|
||||
strbuf->chars[strbuf->length] = '\0';
|
||||
ffStrbufAppendNS(strbuf, (uint32_t)strlen(value), value);
|
||||
}
|
||||
|
||||
void ffStrbufAppendNS(FFstrbuf* strbuf, uint32_t length, const char* value)
|
||||
@@ -118,15 +109,8 @@ void ffStrbufAppendNS(FFstrbuf* strbuf, uint32_t length, const char* value)
|
||||
return;
|
||||
|
||||
ffStrbufEnsureFree(strbuf, length);
|
||||
|
||||
for(uint32_t i = 0; i < length; i++)
|
||||
{
|
||||
if(value[i] == '\0')
|
||||
break;
|
||||
|
||||
strbuf->chars[strbuf->length++] = value[i];
|
||||
}
|
||||
|
||||
memcpy(&strbuf->chars[strbuf->length], value, length);
|
||||
strbuf->length += length;
|
||||
strbuf->chars[strbuf->length] = '\0';
|
||||
}
|
||||
|
||||
@@ -580,5 +564,8 @@ uint16_t ffStrbufToUInt16(const FFstrbuf* strbuf, uint16_t defaultValue)
|
||||
|
||||
void ffStrbufDestroy(FFstrbuf* strbuf)
|
||||
{
|
||||
//Avoid free-after-use. These 3 assignments are cheap so don't remove them
|
||||
strbuf->allocated = strbuf->length = 0;
|
||||
free(strbuf->chars);
|
||||
strbuf->chars = NULL;
|
||||
}
|
||||
|
||||
+52
-6
@@ -26,24 +26,58 @@ int main(int argc, char** argv)
|
||||
|
||||
//initA
|
||||
|
||||
ffStrbufInitA(&strbuf, 64);
|
||||
ffStrbufInitA(&strbuf, 0);
|
||||
|
||||
if(strbuf.allocated != 64)
|
||||
testFailed(&strbuf, "strbuf.allocated != 64");
|
||||
if(strbuf.chars[0] != 0) //make sure chars[0] is accessable
|
||||
testFailed(&strbuf, "strbuf.chars[0] != 0");
|
||||
|
||||
if(strbuf.allocated != 0)
|
||||
testFailed(&strbuf, "strbuf.allocated != 0");
|
||||
|
||||
if(strbuf.length != 0)
|
||||
testFailed(&strbuf, "testSrbuf.length != 0");
|
||||
testFailed(&strbuf, "strbuf.length != 0");
|
||||
|
||||
//appendS
|
||||
|
||||
ffStrbufAppendS(&strbuf, "123456789");
|
||||
ffStrbufAppendS(&strbuf, "12345");
|
||||
|
||||
if(strbuf.length != 5)
|
||||
testFailed(&strbuf, "strbuf.length != 5");
|
||||
|
||||
if(strbuf.allocated < 6)
|
||||
testFailed(&strbuf, "strbuf.allocated < 6");
|
||||
|
||||
if(ffStrbufCompS(&strbuf, "12345") != 0)
|
||||
testFailed(&strbuf, "strbuf.data != \"12345\"");
|
||||
|
||||
//appendNS
|
||||
|
||||
ffStrbufAppendNS(&strbuf, 4, "67890");
|
||||
|
||||
if(strbuf.length != 9)
|
||||
testFailed(&strbuf, "strbuf.length != 9");
|
||||
|
||||
if(strcmp(strbuf.chars, "123456789") != 0)
|
||||
if(strbuf.allocated < 10)
|
||||
testFailed(&strbuf, "strbuf.allocated < 10");
|
||||
|
||||
if(ffStrbufCompS(&strbuf, "123456789") != 0)
|
||||
testFailed(&strbuf, "strbuf.data != \"123456789\"");
|
||||
|
||||
//appendS long
|
||||
|
||||
ffStrbufAppendS(&strbuf, "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
|
||||
|
||||
if(strbuf.length != 109)
|
||||
testFailed(&strbuf, "strbuf.length != 109");
|
||||
|
||||
if(strbuf.allocated < 110)
|
||||
testFailed(&strbuf, "strbuf.allocated < 110");
|
||||
|
||||
if(strbuf.chars[strbuf.length] != 0)
|
||||
testFailed(&strbuf, "strbuf.chars[strbuf.length] != 0");
|
||||
|
||||
strbuf.length = 9;
|
||||
|
||||
//startsWithS
|
||||
|
||||
if(!ffStrbufStartsWithS(&strbuf, "123"))
|
||||
@@ -79,6 +113,18 @@ int main(int argc, char** argv)
|
||||
if(strcmp(strbuf.chars, "12316") != 0)
|
||||
testFailed(&strbuf, "strbuf.chars != \"12316\"");
|
||||
|
||||
//Destroy
|
||||
|
||||
ffStrbufDestroy(&strbuf);
|
||||
if(strbuf.allocated != 0)
|
||||
testFailed(&strbuf, "strbuf.allocated != 0");
|
||||
|
||||
if(strbuf.length != 0)
|
||||
testFailed(&strbuf, "strbuf.length != 0");
|
||||
|
||||
if(strbuf.chars != NULL)
|
||||
testFailed(&strbuf, "strbuf.chars != NULL");
|
||||
|
||||
//Success
|
||||
puts("\033[32mAll tests passed!"FASTFETCH_TEXT_MODIFIER_RESET);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user