Skip to content

Add const version of some array operation #37

@yanis-fourel

Description

@yanis-fourel

Some array/strings utility function cannot take a T const** as argument because they need to possibly reallocate and return the new pointer to the array.
For example:

// Appends the given string `string` to the end of the given string array `dest`.
t_char** StringArray_Add(t_char** dest, t_char const* str);

This forces T const** type array to be casted to T**.
One should be able to pass a T const** type array because the function does not modify the elements.

I suggest adding another version of these functions and wrapping them like so:

t_char const** StringArray_Add_Const(t_char const** dest, t_char const* str)
{
        // The current implementation of `StringArray_Add` which doesn't modify the content of strings anyway
    	t_char const**	result;
	t_uint		length;

	HANDLE_ERROR(NULLPOINTER, (dest == NULL), return (NULL);)
	HANDLE_ERROR(NULLPOINTER, (str  == NULL), return (NULL);)
	length = (StringArray_Length((t_char const**)dest));
	result = (t_char const**)Memory_Reallocate(dest, (length + 1) * sizeof(t_char*));
	HANDLE_ERROR(ALLOCFAILURE, (result == NULL), return (NULL);)
	result[length] = String_Duplicate(str);
	return (result);
}

t_char** StringArray_Add(t_char** dest, t_char const* str)
{
        return (t_char**)StringArray_Add_Const((t_char const**)dest, str);
}

Here is a list of all the functions I think this applies:

  • PointerArray_Add
  • PointerArray_Insert
  • PointerArray_Wedge
  • PointerArray_Replace
  • PointerArray_ReplaceFirst
  • PointerArray_ReplaceLast
  • PointerArray_Concat
  • PointerArray_Append
  • PointerArray_Prepend
  • PointerArray_Join
  • PointerArray_Find
  • PointerArray_Find_F
  • PointerArray_Iterate
  • PointerArray_Iterate_I
  • PointerArray_Map (need to double check if this applies)
  • PointerArray_Map_I (need to double check if this applies)
  • PointerArray_Filter
  • PointerArray_Filter_I
  • StringArray_Add
  • StringArray_Insert
  • StringArray_Wedge
  • StringArray_Append (that would imply we free some t_char const*, does that make sense?)
  • StringArray_Prepend (same ^^^)
  • StringArray_Iterate
  • StringArray_Iterate_I

Metadata

Metadata

Assignees

No one assigned

    Labels

    ++cWill be eventually fixed, using ++C

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions