1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

Add cvtZToUInt() to convert string to unsigned int.

This commit is contained in:
David Steele 2018-08-24 17:08:00 -04:00
parent eb30d88b6a
commit 4c3cf435c0
4 changed files with 29 additions and 1 deletions

View File

@ -167,6 +167,10 @@
<p>Add <code>cvtBoolToConstZ()</code> to simplify conversion of boolean to string.</p>
</release-item>
<release-item>
<p>Add <code>cvtZToUInt()</code> to convert string to unsigned int.</p>
</release-item>
<release-item>
<p>Enable <id>-Wstrict-prototypes</id>, <id>-Wpointer-arith</id>, <id>-Wduplicated-branches</id>, <id>-Wvla</id>, and <id>-Wduplicated-cond</id> and update code to conform.</p>
</release-item>

View File

@ -343,6 +343,24 @@ cvtUIntToZ(unsigned int value, char *buffer, size_t bufferSize)
FUNCTION_TEST_RESULT(SIZE, result);
}
unsigned int
cvtZToUInt(const char *value)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(CHARP, value);
FUNCTION_TEST_ASSERT(value != NULL);
FUNCTION_TEST_END();
uint64_t result = cvtZToUInt64Internal(value, "unsigned int");
// Don't allow negative numbers even though strtoull() does and check max value
if (*value == '-' || result > UINT_MAX)
THROW_FMT(FormatError, "unable to convert string '%s' to unsigned int", value);
FUNCTION_TEST_RESULT(UINT, (unsigned int)result);
}
/***********************************************************************************************************************************
Convert uint64 to zero-terminated string and visa versa
***********************************************************************************************************************************/

View File

@ -26,6 +26,7 @@ size_t cvtModeToZ(mode_t value, char *buffer, size_t bufferSize);
size_t cvtSizeToZ(size_t value, char *buffer, size_t bufferSize);
size_t cvtUIntToZ(unsigned int value, char *buffer, size_t bufferSize);
unsigned int cvtZToUInt(const char *value);
size_t cvtUInt64ToZ(uint64_t value, char *buffer, size_t bufferSize);
uint64_t cvtZToUInt64(const char *value);

View File

@ -98,7 +98,7 @@ testRun(void)
}
// *****************************************************************************************************************************
if (testBegin("cvtUIntToZ()"))
if (testBegin("cvtUIntToZ() and cvtZToUInt()"))
{
char buffer[STACK_TRACE_PARAM_MAX];
@ -106,6 +106,11 @@ testRun(void)
TEST_RESULT_INT(cvtUIntToZ(4294967295, buffer, STACK_TRACE_PARAM_MAX), 10, "convert unsigned int to string");
TEST_RESULT_STR(buffer, "4294967295", " check buffer");
TEST_ERROR(cvtZToUInt("-1"), FormatError, "unable to convert string '-1' to unsigned int");
TEST_ERROR(cvtZToUInt("5000000000"), FormatError, "unable to convert string '5000000000' to unsigned int");
TEST_RESULT_UINT(cvtZToUInt("3333333333"), 3333333333U, "convert string to unsigned int");
}
// *****************************************************************************************************************************