Implemented strcatarr function to concatinate an array of strings into one string.
This commit is contained in:
parent
c680d07ba4
commit
27046e6bbb
|
@ -0,0 +1,63 @@
|
|||
/**
|
||||
* C_lib Copyright (C) 2021 Wael Karram.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/* This file contains the function implementation for string append. */
|
||||
/* Include guards + includes. */
|
||||
#ifndef STRCATARR_C
|
||||
#define STRCATARR_C
|
||||
/* Include statements. */
|
||||
#include "strcatarr.h"
|
||||
#endif /* STRCATARR_C */
|
||||
|
||||
/**
|
||||
* This function takes an array of strings, array of lengths (optional) and length of arrays.
|
||||
* Allocates a new string which is the result of copying and appending all the provided strings.
|
||||
* Returns NULL on failure.
|
||||
* If no lengths specified, falls back to strlen.
|
||||
* Assumes all strings to not be null.
|
||||
*/
|
||||
static inline char *strcatarr(char **strings, size_t *lens, size_t len) {
|
||||
/* Sanity check. */
|
||||
assert(strings && len);
|
||||
|
||||
/* Local variables. */
|
||||
size_t total_length = 0L;
|
||||
size_t i;
|
||||
char *result;
|
||||
|
||||
/* Calculate the total length. */
|
||||
if (!lens)
|
||||
for (i = 0L; i < len; i++) /* O(nk) */
|
||||
total_length += strlen(strings[i]);
|
||||
else
|
||||
for (i = 0L; i < len; i++) /* O(n) */
|
||||
total_length += lens[i];
|
||||
|
||||
/* For null terminator. */
|
||||
total_length++;
|
||||
|
||||
/* Attempt to allocate the result buffer. */
|
||||
result = (char*) malloc(sizeof(char) * total_length);
|
||||
if (!result)
|
||||
return NULL;
|
||||
|
||||
/* Concatinate the strings. */
|
||||
for (i = 0L; i < len; i++)
|
||||
strcat(result, strings[i]);
|
||||
|
||||
/* Return the result. */
|
||||
return result;
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* C_lib Copyright (C) 2021 Wael Karram.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/* This header file defines a strcatarr function, for general string array concatination. */
|
||||
/* Include guards + includes. */
|
||||
#ifndef STRCATARR_H
|
||||
#define STRCATARR_H
|
||||
/* Include string.h */
|
||||
#include <string.h>
|
||||
/* Include assert.h to allow code generation, but only run with debug flag. */
|
||||
#ifdef DEBUG
|
||||
#ifndef NDEBUG
|
||||
#define NDEBUG
|
||||
#endif /* NDEBUG. */
|
||||
#endif /* DEBUG. */
|
||||
#include <assert.h>
|
||||
|
||||
/* Constants. */
|
||||
#define NULL_CHAR '\0'
|
||||
|
||||
/**
|
||||
* This function takes an array of strings, array of lengths (optional) and length of arrays.
|
||||
* Allocates a new string which is the result of copying and appending all the provided strings.
|
||||
* Returns NULL on failure.
|
||||
* If no lengths specified, falls back to strlen.
|
||||
*/
|
||||
static inline char *strcatarr(char **strings, size_t *lens, size_t len);
|
||||
|
||||
#endif /* STRCATARR_H */
|
Loading…
Reference in New Issue