1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-03-03 14:52:21 +02:00

Add srtCatZN().

Append N characters from a zero-terminated string.

Note that the string does not actually need to be zero-terminated as long as N is <= the end of the string being concatenated.
This commit is contained in:
David Steele 2020-03-25 18:37:35 -04:00
parent b10270eee8
commit 88d7ee6215
3 changed files with 32 additions and 1 deletions

View File

@ -322,6 +322,32 @@ strCat(String *this, const char *cat)
FUNCTION_TEST_RETURN(this);
}
String *
strCatZN(String *this, const char *cat, size_t size)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRINGZ, cat);
FUNCTION_TEST_PARAM(SIZE, size);
FUNCTION_TEST_END();
ASSERT(this != NULL);
ASSERT(cat != NULL);
// Ensure there is enough space to grow the string
strResize(this, size);
// Append the string
strncpy(this->buffer + this->size, cat, size);
this->buffer[this->size + size] = '\0';
// Update size/extra
this->size += (unsigned int)size;
this->extra -= (unsigned int)size;
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Append a character
***********************************************************************************************************************************/

View File

@ -51,6 +51,11 @@ bool strBeginsWithZ(const String *this, const char *beginsWith);
String *strCat(String *this, const char *cat);
String *strCatChr(String *this, char cat);
String *strCatFmt(String *this, const char *format, ...) __attribute__((format(printf, 2, 3)));
// Append N characters from a zero-terminated string. Note that the string does not actually need to be zero-terminated as long as
// N is <= the end of the string being concatenated.
String *strCatZN(String *this, const char *cat, size_t size);
int strCmp(const String *this, const String *compare);
int strCmpZ(const String *this, const char *compare);
String *strDup(const String *this);

View File

@ -106,7 +106,7 @@ testRun(void)
TEST_RESULT_STR_Z(strCatChr(string, '!'), "XXXXYYYY00777!", "cat chr");
TEST_RESULT_UINT(string->extra, 54, "check extra");
TEST_RESULT_STR_Z(
strCat(string, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"),
strCatZN(string, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*", 55),
"XXXXYYYY00777!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", "cat chr");
TEST_RESULT_UINT(string->extra, 34, "check extra");