pgbackrest.git / summary / log / commit / refs
commit 4c3cf435c036037ff32b7a76e53753e765358173
Author: David Steele <david@pgmasters.net>
Date: Fri Aug 24 21:08:00 2018 +0000
Add cvtZToUInt() to convert string to unsigned int.
doc/xml/release.xml | 4 ++++
src/common/type/convert.c | 18 ++++++++++++++++++
src/common/type/convert.h | 1 +
test/src/module/common/typeConvertTest.c | 7 ++++++-
4 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/doc/xml/release.xml b/doc/xml/release.xml
index a91592d63..e01b810d3 100644
--- a/doc/xml/release.xml
+++ b/doc/xml/release.xml
@@ -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>
diff --git a/src/common/type/convert.c b/src/common/type/convert.c
index bf6b2490d..679397a6a 100644
--- a/src/common/type/convert.c
+++ b/src/common/type/convert.c
@@ -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
***********************************************************************************************************************************/
diff --git a/src/common/type/convert.h b/src/common/type/convert.h
index 56d19547f..7f196b0b0 100644
--- a/src/common/type/convert.h
+++ b/src/common/type/convert.h
@@ -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);
diff --git a/test/src/module/common/typeConvertTest.c b/test/src/module/common/typeConvertTest.c
index b46010ccf..f6da1d509 100644
--- a/test/src/module/common/typeConvertTest.c
+++ b/test/src/module/common/typeConvertTest.c
@@ -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");
}
// *****************************************************************************************************************************
[parent: eb30d88b6a60]