Skip to content

Commit

Permalink
feature(libccc/stringarray): implemented StringArray_Find|IndexOf() f…
Browse files Browse the repository at this point in the history
…amily of functions
  • Loading branch information
LexouDuck committed Jan 26, 2022
1 parent e37b1ed commit cd96b37
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 16 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
libccc@0.9.53-2022-01-26_9db813490cf69cc29db86711776b5aa32c25c20c
libccc@0.9.54-2022-01-26_e37b1edb88e4309274413975f581420ec7d756f7
30 changes: 15 additions & 15 deletions hdr/libccc/pointerarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,52 +526,52 @@ t_sint PointerArray_Compare_N(void const* const* ptrarr1, void const* const*
** ************************************************************************** *|
*/

//!@doc Returns the first encountered item in the given `array` matching the given `item`
//!@doc Returns the first encountered item in the given `ptrarr` matching the given `ptr`
/*!
** @param ptrarr The linked array to look through
** @param item The `item` pointer to match against
** @param ptrarr The pointer array to look through
** @param ptr The `ptr` pointer to match against
** @returns
** The first encountered item of the given linked `array` which is equal to `item`,
** The first encountered item of the given `ptrarr` which is equal to `ptr`,
** or `NULL` if no such item was found.
*/
//!@{
void* const* PointerArray_Find(void* const* ptrarr, void const* ptr);
#define c_ptrarrfind PointerArray_Find
//!@}

//!@doc Returns the first encountered item in the given `array` matching the given `item`
//!@doc Returns the first encountered item in the given `ptrarr` matching the given `ptr`
/*!
** @param ptrarr The linked array to look through
** @param ptrarr The pointer array to look through
** @param match The function used to compare items to the target value
** @returns
** The first encountered item of the given linked `array` for which the given `match` function
** The first encountered item of the given `ptrarr` for which the given `match` function
** returned `TRUE`. Otherwise, returns `NULL` if no such item was found.
*/
//!@{
void* const* PointerArray_Find_F(void* const* ptrarr, t_bool (*match)(void const* ptr));
#define c_ptrarrffind PointerArray_Find_F
//!@}

//!@doc Returns the index of the first encountered item in the given `array` matching the given `item`
//!@doc Returns the index of the first encountered item in the given `ptrarr` matching the given `ptr`
/*!
** @param ptrarr the linked array to look through
** @param item the `item` pointer to match against
** @param ptrarr The pointer array to look through
** @param ptr The `ptr` pointer to match against
** @returns
** The first encountered item of the given linked `array` which is equal to `item`,
** The first encountered item of the given `ptrarr` which is equal to `ptr`,
** or `-1` if no such item was found.
*/
//!@{
t_sint PointerArray_IndexOf(void const* const* ptrarr, void const* ptr);
#define c_ptrarrfindi PointerArray_IndexOf
//!@}

//!@doc Returns the index of the first encountered item in the given `array` matching the given `item`
//!@doc Returns the index of the first encountered item in the given `ptrarr` matching the given `ptr`
/*!
** @param ptrarr the linked array to look through
** @param ptrarr The pointer array to look through
** @param match The function used to compare items to the target value
** @returns
** The first encountered item of the given linked `array` which is equal to `item`,
** or `-1` if no such item was found.
** The first encountered item of the given `ptrarr` for which the given `match` function
** returned `TRUE`. Otherwise, `-1` if no such item was found.
*/
//!@{
t_sint PointerArray_IndexOf_F(void const* const* ptrarr, t_bool (*match)(void const* ptr));
Expand Down
54 changes: 54 additions & 0 deletions hdr/libccc/stringarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,60 @@ t_sint StringArray_Compare_N (t_char const* const* strarr1, t_char const* co
** ************************************************************************** *|
*/

//!@doc Returns the first encountered item in the given `strarr` matching the given `str`
/*!
** @param strarr The string array to look through
** @param str The `str` pointer to match against
** @returns
** The first encountered item of the given `strarr` which is equal to `item`,
** or `NULL` if no such item was found.
*/
//!@{
t_char* const* StringArray_Find(t_char* const* strarr, t_char const* str);
#define c_strarrfind StringArray_Find
//!@}

//!@doc Returns the first encountered item in the given `strarr` matching the given `str`
/*!
** @param strarr The string array to look through
** @param match The function used to compare items to the target value
** @returns
** The first encountered item of the given `strarr` for which the given `match` function
** returned `TRUE`. Otherwise, returns `NULL` if no such item was found.
*/
//!@{
t_char* const* StringArray_Find_F(t_char* const* strarr, t_bool (*match)(t_char const* str));
#define c_strarrffind StringArray_Find_F
//!@}

//!@doc Returns the index of the first encountered item in the given `strarr` matching the given `str`
/*!
** @param strarr the string array to look through
** @param str the `str` pointer to match against
** @returns
** The first encountered item of the given `strarr` which is equal to `str`,
** or `-1` if no such item was found.
*/
//!@{
t_sint StringArray_IndexOf(t_char const* const* strarr, t_char const* str);
#define c_strarrfindi StringArray_IndexOf
//!@}

//!@doc Returns the index of the first encountered item in the given `strarr` matching the given `str`
/*!
** @param strarr the string array to look through
** @param match The function used to compare items to the target value
** @returns
** The first encountered item of the given `strarr` for which the given `match` function
** returned `TRUE`. Otherwise, `-1` if no such item was found.
*/
//!@{
t_sint StringArray_IndexOf_F(t_char const* const* strarr, t_bool (*match)(t_char const* str));
#define c_strarrffindi StringArray_IndexOf_F
//!@}



//! Counts the amount of occurences of the char `c` in the given string array `strarr`.
/*!
** TODO document this
Expand Down
1 change: 1 addition & 0 deletions mkfile/lists/srcs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ stringarray/divide.c
stringarray/duplicate.c
stringarray/equals.c
stringarray/filter.c
stringarray/find.c
stringarray/insert.c
stringarray/iterate.c
stringarray/join.c
Expand Down
65 changes: 65 additions & 0 deletions src/stringarray/find.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

#include "libccc/stringarray.h"

#include LIBCONFIG_ERROR_INCLUDE



t_char* const* StringArray_Find(t_char* const* strarr, t_char const* str)
{
t_uint length;

HANDLE_ERROR(NULLPOINTER, (strarr == NULL), return (NULL);)
length = StringArray_Length((t_char const* const*)strarr);
for (t_uint i = 0; i < length; ++i)
{
if (String_Equals(strarr[i], str))
return (&(strarr[i]));
}
HANDLE_ERROR(NOTFOUND, (TRUE), return (NULL);)
}


t_char* const* StringArray_Find_F(t_char* const* strarr, t_bool (*match)(t_char const* str))
{
t_uint length;

HANDLE_ERROR(NULLPOINTER, (strarr == NULL), return (NULL);)
length = StringArray_Length((t_char const* const*)strarr);
for (t_uint i = 0; i < length; ++i)
{
if (match(strarr[i]))
return (&(strarr[i]));
}
HANDLE_ERROR(NOTFOUND, (TRUE), return (NULL);)
}


t_sint StringArray_IndexOf(t_char const* const* strarr, t_char const* str)
{
t_uint length;

HANDLE_ERROR(NULLPOINTER, (strarr == NULL), return (ERROR);)
length = StringArray_Length(strarr);
for (t_uint i = 0; i < length; ++i)
{
if (String_Equals(strarr[i], str))
return (i);
}
HANDLE_ERROR(NOTFOUND, (TRUE), return (ERROR);)
}


t_sint StringArray_IndexOf_F(t_char const* const* strarr, t_bool (*match)(t_char const* str))
{
t_uint length;

HANDLE_ERROR(NULLPOINTER, (strarr == NULL), return (ERROR);)
length = StringArray_Length(strarr);
for (t_uint i = 0; i < length; ++i)
{
if (match(strarr[i]))
return (i);
}
HANDLE_ERROR(NOTFOUND, (TRUE), return (ERROR);)
}

0 comments on commit cd96b37

Please sign in to comment.