1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-03-05 15:05:48 +02:00

Add strLstExists(), strLstExistsZ(), strSub(), and strSubN() to String and StringList objects.

This commit is contained in:
David Steele 2018-04-29 10:32:46 -04:00
parent 89d3476e32
commit d44848baa0
8 changed files with 87 additions and 3 deletions

View File

@ -76,9 +76,10 @@
<release-item>
<release-item-contributor-list>
<release-item-contributor id="shang.cynthia"/>
<release-item-contributor id="steele.david"/>
</release-item-contributor-list>
<p>String object improvements. Add <code>strUpper</code>, <code>strLower</code>, <code>strChr</code>, and <code>strTrunc</code>.</p>
<p>Improve <code>String</code> and <code>StringList</code> objects. Add <code>strUpper()</code>, <code>strLower()</code>, <code>strLstExists()</code>, <code>strLstExistsZ()</code>, <code>strChr()</code>, <code>strSub()</code>, <code>strSubN()</code>, and <code>strTrunc()</code>.</p>
</release-item>
<release-item>

View File

@ -7,6 +7,7 @@ String Handler
#include <stdlib.h>
#include <string.h>
#include "common/assert.h"
#include "common/memContext.h"
#include "common/type/string.h"
@ -347,6 +348,27 @@ strPtr(const String *this)
return result;
}
/***********************************************************************************************************************************
Return a substring given only the start position
***********************************************************************************************************************************/
String *
strSub(const String *this, size_t start)
{
return strSubN(this, start, this->size - start);
}
/***********************************************************************************************************************************
Return a substring given the start position and size
***********************************************************************************************************************************/
String *
strSubN(const String *this, size_t start, size_t size)
{
ASSERT(start < this->size);
ASSERT(start + size <= this->size);
return strNewN(this->buffer + start, size);
}
/***********************************************************************************************************************************
Return string size
***********************************************************************************************************************************/

View File

@ -38,6 +38,8 @@ String *strLower(String *this);
String *strPath(const String *this);
const char *strPtr(const String *this);
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);
String *strTrim(String *this);
int strChr(const String *this, char chr);
String *strTrunc(String *this, int idx);

View File

@ -178,6 +178,43 @@ strLstDup(const StringList *sourceList)
return this;
}
/***********************************************************************************************************************************
Does the specified string exist in the list?
***********************************************************************************************************************************/
bool
strLstExists(const StringList *this, const String *string)
{
bool result = false;
for (unsigned int listIdx = 0; listIdx < strLstSize(this); listIdx++)
{
if (strEq(strLstGet(this, listIdx), string))
{
result = true;
break;
}
}
return result;
}
bool
strLstExistsZ(const StringList *this, const char *cstring)
{
bool result = false;
for (unsigned int listIdx = 0; listIdx < strLstSize(this); listIdx++)
{
if (strEqZ(strLstGet(this, listIdx), cstring))
{
result = true;
break;
}
}
return result;
}
/***********************************************************************************************************************************
Wrapper for lstAdd()
***********************************************************************************************************************************/

View File

@ -35,6 +35,8 @@ StringList *strLstDup(const StringList *sourceList);
StringList *strLstAdd(StringList *this, const String *string);
StringList *strLstAddZ(StringList *this, const char *string);
bool strLstExists(const StringList *this, const String *string);
bool strLstExistsZ(const StringList *this, const char *cstring);
String *strLstGet(const StringList *this, unsigned int listIdx);
String *strLstJoin(const StringList *this, const char *separator);
StringList * strLstMove(StringList *this, MemContext *parentNew);

View File

@ -144,14 +144,14 @@ unit:
# ----------------------------------------------------------------------------------------------------------------------------
- name: type-string
total: 10
total: 11
coverage:
common/type/string: full
# ----------------------------------------------------------------------------------------------------------------------------
- name: type-string-list
total: 7
total: 8
coverage:
common/type/stringList: full

View File

@ -123,6 +123,19 @@ testRun()
strLstFree(list);
}
// *****************************************************************************************************************************
if (testBegin("strLstExists() and strLstExistsZ()"))
{
StringList *list = strLstNew();
strLstAddZ(list, "A");
strLstAddZ(list, "C");
TEST_RESULT_BOOL(strLstExists(list, strNew("B")), false, "string does not exist");
TEST_RESULT_BOOL(strLstExists(list, strNew("C")), true, "string exists");
TEST_RESULT_BOOL(strLstExistsZ(list, "B"), false, "string does not exist");
TEST_RESULT_BOOL(strLstExistsZ(list, "C"), true, "string exists");
}
// *****************************************************************************************************************************
if (testBegin("strLstJoin()"))
{

View File

@ -140,6 +140,13 @@ testRun()
TEST_RESULT_STR(strPtr(strUpper(strNew(""))), "", "empty upper");
}
// *****************************************************************************************************************************
if (testBegin("strSub() and strSubN()"))
{
TEST_RESULT_STR(strPtr(strSub(strNew("ABCD"), 2)), "CD", "sub string");
TEST_RESULT_STR(strPtr(strSubN(strNew("ABCD"), 1, 2)), "BC", "sub string with length");
}
// *****************************************************************************************************************************
if (testBegin("strTrim()"))
{