1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-18 04:58:51 +02:00

Add strReplaceChr() to String object.

This commit is contained in:
David Steele 2018-08-14 16:49:38 -04:00
parent 4a176681c3
commit cb4b715533
5 changed files with 36 additions and 1 deletions

View File

@ -154,6 +154,10 @@
<release-item>
<p>Add <code>cvtCharToZ()</code> and macro for debugging <code>char</code> params.</p>
</release-item>
<release-item>
<p>Add <code>strReplaceChr()</code> to <code>String</code> object.</p>
</release-item>
</release-development-list>
</release-core-list>

View File

@ -526,6 +526,29 @@ strQuoteZ(const String *this, const char *quote)
FUNCTION_TEST_RESULT(STRING, strNewFmt("%s%s%s", quote, strPtr(this), quote));
}
/***********************************************************************************************************************************
Replace a character with another character
***********************************************************************************************************************************/
String *
strReplaceChr(String *this, char find, char replace)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(CHAR, find);
FUNCTION_TEST_PARAM(CHAR, replace);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
for (unsigned int stringIdx = 0; stringIdx < this->size; stringIdx++)
{
if (this->buffer[stringIdx] == find)
this->buffer[stringIdx] = replace;
}
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Return a substring given only the start position
***********************************************************************************************************************************/
@ -636,6 +659,7 @@ strChr(const String *this, char chr)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(CHAR, chr);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();

View File

@ -39,6 +39,7 @@ String *strPath(const String *this);
const char *strPtr(const String *this);
String *strQuote(const String *this, const String *quote);
String *strQuoteZ(const String *this, const char *quote);
String *strReplaceChr(String *this, char find, char replace);
size_t strSize(const String *this);
String *strSub(const String *this, size_t start);
String *strSubN(const String *this, size_t start, size_t size);

View File

@ -152,7 +152,7 @@ unit:
# ----------------------------------------------------------------------------------------------------------------------------
- name: type-string
total: 13
total: 14
coverage:
common/type/string: full

View File

@ -149,6 +149,12 @@ testRun(void)
TEST_RESULT_STR(strPtr(strQuote(strNew("abcd"), strNew("'"))), "'abcd'", "quote string");
}
// *****************************************************************************************************************************
if (testBegin("strReplaceChr()"))
{
TEST_RESULT_STR(strPtr(strReplaceChr(strNew("ABCD"), 'B', 'R')), "ARCD", "replace chr");
}
// *****************************************************************************************************************************
if (testBegin("strSub() and strSubN()"))
{