From c2e90416924681152450843eb10343cac7818e4d Mon Sep 17 00:00:00 2001 From: IvanDSM Date: Mon, 22 Jun 2020 20:35:10 -0300 Subject: [PATCH] string_mid: Implementation of string_mid Adds an implementation of string_mid. When length is -1, copies until the end of the source string. --- README.md | 1 + src/libstring.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/libstring.h | 2 +- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 52b8f99..e720f30 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ size_t string_update(cstr_t * str, const char * new_val); // Updates the value bool string_swap(cstr_t * str1, cstr_t * str2); // Swaps the contents of str1 and str2. bool string_reserve(cstr_t *str, size_t capacity); // Increases str's memory reservation size_t string_replace_char(cstr_t *str, char before, char after); // Replaces all instances of a char with another. +cstr_t * string_mid(cstr_t * str, size_t pos, long length); // Returns a substring of a given string starting at position pos with a given length. ``` The other functions defined in `libstring.c` are internal and not accessible. diff --git a/src/libstring.c b/src/libstring.c index e923421..f015974 100644 --- a/src/libstring.c +++ b/src/libstring.c @@ -193,6 +193,34 @@ size_t __strcat(char *dest, const char *src, size_t size) return(dest_len + __strlen(src)); } +//! +//! \brief __cstr_min Quick internal implementation of a min function for size_t. +//! \param x One of the elements to be compared. +//! \param y The other element to be compared. +//! \return The lesser element. +//! +size_t __cstr_min(size_t x, size_t y) +{ + if (x < y) + return x; + else + return y; +} + +//! +//! \brief __cstr_max Quick internal implementation of a max function for size_t. +//! \param x One of the elements to be compared. +//! \param y The other element to be compared. +//! \return The greater element. +//! +size_t __cstr_max(size_t x, size_t y) +{ + if (x > y) + return x; + else + return y; +} + /*! * \struct alloc_node A single node of the allocation linked list. * \param nbytes The number of bytes to be allocated. @@ -385,7 +413,7 @@ bool string_contains(cstr_t * str1, const char * str2) } /*! - * \brief Initializes a new string, a poitner to cstr_t + * \brief Initializes a new string, a pointer to cstr_t * \param str The cstr_t * whose capacity will be altered. * \param capacity The new memory reserve of the cstr_t *. * \retval bool Returns true if the operation was succesful, false otherwise. @@ -549,3 +577,36 @@ bool string_swap(cstr_t * str1, cstr_t * str2) return true; } + +//! +//! \brief string_mid Returns a substring of a given string starting at position pos with a given length. +//! \param str Source string for the substring. +//! \param pos Position of the starting character. +//! \param length Desired length of the substring. -1 for everything after pos. +//! \return Generated substring. +//! +cstr_t *string_mid(cstr_t *str, size_t pos, long length) +{ + if (!str) + { + fprintf(stderr, "In string_mid: str is uninitialized.\n"); + return 0; + } + + if (str->size <= pos || str->size == 0 || (length < 1 && length != -1)) + return string_init(""); + + size_t stop_element; + + if (length == -1) + stop_element = str->size; + else + stop_element = __cstr_min(str->size, pos + (size_t) length); + + cstr_t *result = string_init(""); + string_reserve(result, stop_element - pos + 1); + + __strcpy(result->value, &str->value[pos], stop_element - pos); + + return result; +} diff --git a/src/libstring.h b/src/libstring.h index 38c2b9f..2cda5be 100644 --- a/src/libstring.h +++ b/src/libstring.h @@ -67,7 +67,7 @@ char string_get_char_at(cstr_t * str, size_t pos); // undef, bound-check size_t string_index_of(cstr_t *str, char c); // undef size_t string_last_index_of(cstr_t *str, char c); // undef cstr_t * string_left(cstr_t * str, size_t length); // undef -cstr_t * string_mid(cstr_t * str, size_t pos, size_t length); // undef +cstr_t * string_mid(cstr_t * str, size_t pos, long length);// def, somewhat tested size_t string_replace_char(cstr_t *str, char before, char after); // def, tested, see related to-do inside function. cstr_t * string_reverse(cstr_t * str); // undef cstr_t string_right(cstr_t * str, size_t length); // undef -- 2.11.4.GIT