1
0
Fork 0

Implemented string append function.

This commit is contained in:
Wael Karram 2022-03-26 12:16:46 +03:00
parent d3562af7ea
commit c680d07ba4
Signed by: wael
GPG Key ID: 3B356038CCB10808
2 changed files with 89 additions and 0 deletions

48
strings/strapp.c Normal file
View File

@ -0,0 +1,48 @@
/**
* 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 STRAPP_C
#define STRAPP_C
/* Include statements. */
#include "strapp.h"
#endif /* STRAPP_C */
/**
* This function takes two strings and appends source to destination, resizing destination if needed.
* Returns the destination address on success and NULL on failure.
* Assumes the size of each buffer to be (length + 1L) * sizeof(char).
* User should free the source strings.
*/
static inline char *strapp(char *restrict dest, const char *restrict src, const size_t dest_len, const size_t src_len) {
/* Sanity check. */
assert(src && src_len && ((!dest && !dest_len) || (dest && dest_len)));
/* Start by allocating the new array. */
char *result = calloc(dest_len + src_len + 1L, sizeof(char));
/* Check if the allocation failed. */
if (!result)
return NULL;
/* Copy the first string. */
strncpy(result, dest, dest_len);
/* Append the second string. */
strncat(result, src, src_len);
/* Return the buffer. */
return result;
}

41
strings/strapp.h Normal file
View File

@ -0,0 +1,41 @@
/**
* 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 strapp function, for general string appending. */
/* Include guards + includes. */
#ifndef STRAPP_H
#define STRAPP_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 two strings and appends source to destination, resizing destination if needed.
* Returns the destination address on success and NULL on failure.
*/
static inline char *strapp(char *restrict dest, const char *restrict src, const size_t dest_len, const size_t src_len);
#endif /* STRAPP_H */