Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: handy functions for pointer-length objects #884

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/re_fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ uint64_t pl_u64(const struct pl *pl);
uint64_t pl_x64(const struct pl *pl);
double pl_float(const struct pl *pl);
int pl_bool(bool *val, const struct pl *pl);
int pl_hex(const struct pl *pl, uint8_t *hex, size_t len);
bool pl_isset(const struct pl *pl);
int pl_strcpy(const struct pl *pl, char *str, size_t size);
int pl_strdup(char **dst, const struct pl *src);
Expand All @@ -49,6 +50,7 @@ int pl_cmp(const struct pl *pl1, const struct pl *pl2);
int pl_casecmp(const struct pl *pl1, const struct pl *pl2);
const char *pl_strchr(const struct pl *pl, char c);
const char *pl_strrchr(const struct pl *pl, char c);
const char *pl_strstr(const struct pl *pl, const char *str);
int pl_trim(struct pl *pl);
int pl_ltrim(struct pl *pl);
int pl_rtrim(struct pl *pl);
Expand Down
56 changes: 56 additions & 0 deletions src/fmt/pl.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,29 @@ int pl_bool(bool *val, const struct pl *pl)
}


/**
* Convert an ASCII hex string as a pointer-length object to binary format
*
* @param pl Pointer-length object
* @param hex Destination binary buffer
* @param len Length of binary buffer
*
* @return 0 if success, otherwise errorcode
*/
int pl_hex(const struct pl *pl, uint8_t *hex, size_t len)
{
if (!pl_isset(pl) || !hex || (pl->l != (2 * len)))
return EINVAL;

for (size_t i = 0; i < pl->l; i += 2) {
hex[i/2] = ch_hex(*(pl->p + i)) << 4;
hex[i/2] += ch_hex(*(pl->p + i +1));
}

return 0;
}


/**
* Check if pointer-length object is set
*
Expand Down Expand Up @@ -655,6 +678,39 @@ const char *pl_strrchr(const struct pl *pl, char c)
}


/**
* Locate the fist substring in a pointer-length string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fist -> first

*
* @param pl Pointer-length string
* @param str Substring to locate
*
* @return Pointer to first char if substring is found, otherwise NULL
*/
const char *pl_strstr(const struct pl *pl, const char *str)
{
size_t len = str_len(str);

/*case pl not set & pl is not long enough*/
if (!pl_isset(pl) || pl->l < len)
return NULL;

/*case str is empty or just '\0'*/
if (!len)
return pl->p;

for (size_t i = 0; i < pl->l; ++i) {
/*case rest of pl is not long enough*/
if (pl->l - i < len)
return NULL;

if (!memcmp(pl->p + i, str, len))
return pl->p + i;
}

return NULL;
}


/**
* Trim white space characters at start of pointer-length string
*
Expand Down
16 changes: 16 additions & 0 deletions test/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ int test_fmt_pl(void)
const struct pl pl4 = PL("rattarei4");
const char str0[] = "rattarei";
const char str1[] = "rattaray";
const char str2[] = "foo";
const char str3[] = "bar";
struct pl pl5, pl6;
const struct pl pl7 = PL("hei");
const struct pl pl7_ = PL("Hei");
Expand Down Expand Up @@ -116,6 +118,20 @@ int test_fmt_pl(void)
if (NULL != pl_strrchr(&pl_empty, 'r'))
goto out;

/* pl_strstr() */
if (pl.p != pl_strstr(&pl, str0))
goto out;
if (pl1.p != pl_strstr(&pl1, str2))
goto out;
if (pl1.p + 3 != pl_strstr(&pl1, str3))
goto out;
if (NULL != pl_strstr(&pl, str1))
goto out;
if (pl.p != pl_strstr(&pl, ""))
goto out;
if (NULL != pl_strstr(&pl1, str0))
goto out;

return 0;
out:
return EINVAL;
Expand Down