Skip to content

Commit

Permalink
[glibutil] Added gutil_parse_uint()
Browse files Browse the repository at this point in the history
  • Loading branch information
monich committed May 5, 2021
1 parent 7621553 commit 75f1a27
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/gutil_misc.h
Expand Up @@ -69,6 +69,12 @@ gutil_parse_int(
int base,
int* value); /* Since 1.0.30 */

gboolean
gutil_parse_uint(
const char* str,
int base,
unsigned int* value); /* Since 1.0.53 */

gboolean
gutil_data_equal(
const GUtilData* data1,
Expand Down
24 changes: 24 additions & 0 deletions src/gutil_misc.c
Expand Up @@ -194,6 +194,30 @@ gutil_parse_int(
return ok;
}

gboolean
gutil_parse_uint(
const char* str,
int base,
unsigned int* value) /* Since 1.0.53 */
{
gboolean ok = FALSE;

if (str && str[0]) {
char* tmp = NULL;
char* end = NULL;
const char* stripped = gutil_strstrip(str, &tmp);
guint64 ull;

ull = g_ascii_strtoull(stripped, &end, base);
ok = !*end && ull <= UINT_MAX;
if (ok && value) {
*value = (unsigned int)ull;
}
g_free(tmp);
}
return ok;
}

gboolean
gutil_data_equal(
const GUtilData* data1,
Expand Down
36 changes: 36 additions & 0 deletions test/test_misc/test_misc.c
Expand Up @@ -192,6 +192,41 @@ test_parse_int(
g_assert(!gutil_parse_int("0xffffffff", 0, &value));
}

/*==========================================================================*
* parse_uint
*==========================================================================*/

static
void
test_parse_uint(
void)
{
unsigned int value;

g_assert(!gutil_parse_uint(NULL, 0, NULL));
g_assert(!gutil_parse_uint("", 0, NULL));
g_assert(!gutil_parse_uint("garbage", 0, NULL));
g_assert(!gutil_parse_uint("0 trailing garbage", 0, NULL));
g_assert(gutil_parse_uint("0", 0, NULL));
g_assert(gutil_parse_uint("0", 0, &value));
g_assert_cmpuint(value, == ,0);
g_assert(gutil_parse_uint("42", 0, &value));
g_assert_cmpuint(value, == ,42);
g_assert(!gutil_parse_uint("0x10000000000000000", 0, &value));
g_assert(!gutil_parse_uint("-2147483649", 0, &value));
g_assert(!gutil_parse_uint("-1", 0, &value));
g_assert(gutil_parse_uint("4294967295", 0, &value));
g_assert_cmpuint(value, == ,4294967295);
g_assert(gutil_parse_uint(" 0x7fffffff ", 0, &value));
g_assert_cmpuint(value, == ,0x7fffffff);
g_assert(gutil_parse_uint(" 7fffffff ", 16, &value));
g_assert_cmpuint(value, == ,0x7fffffff);
g_assert(gutil_parse_uint("7ffffffe ", 16, &value));
g_assert_cmpuint(value, == ,0x7ffffffe);
g_assert(gutil_parse_uint("0xffffffff", 0, &value));
g_assert_cmpuint(value, == ,0xffffffff);
}

/*==========================================================================*
* data_equal
*==========================================================================*/
Expand Down Expand Up @@ -567,6 +602,7 @@ int main(int argc, char* argv[])
g_test_add_func(TEST_("hex2bin"), test_hex2bin);
g_test_add_func(TEST_("hexdump"), test_hexdump);
g_test_add_func(TEST_("parse_int"), test_parse_int);
g_test_add_func(TEST_("parse_uint"), test_parse_uint);
g_test_add_func(TEST_("data_equal"), test_data_equal);
g_test_add_func(TEST_("data_prefix"), test_data_prefix);
g_test_add_func(TEST_("data_suffix"), test_data_suffix);
Expand Down

0 comments on commit 75f1a27

Please sign in to comment.