Warning
This page is located in archive.

HW 04 - String library

Your task is to implement your own simplified string library, of course, without the help of string.h library or similar. Below, you are given the header file with declarations of all the required functions. Each declaration has a description of the desired functionality, arguments, and return values. You can freely implement custom helper functions if needed, but keep the provided header file unchanged.

Description

  • You should consider all strings to be dynamically allocated!
  • All strings are expected to be standard null-terminated character arrays.
  • The functions are expected to behave the same as their standard library equivalents unless custom modifications are stated.
  • Brief function description with links. You can find the detailed description from cppreference.com, which is also the same in the header file.
      • strtok() - only the first variant of C99 standard is considered
      • The return value is through the first argument, which is mutated. Only variant the C99 variant is considered.
      • There is a change compared to the standard in terms of reallocating the first string.
      • The second argument is a simplified regexp pattern with [] character group and [^] group prohibition operators.
        • Examples of patterns He[^abEr-]p A[lp]ex or C is great[!?]

You get the header file my_string.h here.

Evaluation

Upload your solution into BRUTE as a zip archive containing only your my_string.c source files and either a Makefile or readme explaining how to compile it into a portable shared library.
Your submission must compile without errors with the following compiler flags

-Wall -Werror -pedantic -std=c99 -O2

Also, it must not have memory leaks.

The maximum number of points is 10.

  • 3 points - my_strstr_match function
  • 3 points - my_strtok function
  • 2 points - my_strspn function
  • 1 point - my_strcat function
  • 1 point - coding style (structured, commented functions and arguments (e.g. by Doxygen style), separated into functions …)

Bonus (2 points)

Prepare a Makefile with targets `shared` and `static` to create either a shared or static library out of you implementation. The libraries will be created in a prepared folder called `build` by calling the Makefile in the top level of the source tree. Please, have a look on the GCC flags and the AR program, especially on the flags `-fPIC` and `-rdynamic` and others.

my_string.h

//----------------------------------------------------------------------------------------
/**
 * \file       my_string.h
 * \author     Jiri Ulrich
 * \date       2023/11/01
 * \brief      Simplified string library.
 *
 *  Simplified string library. Only a selected subset of string manipulation functions.
 *  They differ from the standard in certain parts like return vals and mutability of args.
 *
*/
//----------------------------------------------------------------------------------------
 
#ifndef MY_STRING_H
#define MY_STRING_H
 
/// Length of the maximum initial segment
/**
  Returns the length of the maximum initial segment (span) of the null-terminated byte string pointed to by dest,
  that consists of only the characters found in the null-terminated byte string pointed to by src.
 
  \param[in] dest    pointer to the null-terminated byte string to be analyzed
  \param[in] src     pointer to the null-terminated byte string that contains the characters to search for
  \return The length of the maximum initial segment that contains only characters from the null-terminated byte string pointed to by src.
*/
unsigned long my_strspn(const char *dest, const char *src);
 
/// Finds the next token in a byte string
/**
  Finds the next token in a null-terminated byte string pointed to by str. The separator characters are identified by null-terminated byte string
  pointed to by delim. This function is designed to be called multiple times to obtain successive tokens from the same string.
 
    - If str is not a null pointer, the call is treated as the first call to strtok for this particular string.
      The function searches for the first character which is not contained in delim. 
      - If no such character was found, there are no tokens in str at all, and the function returns a null pointer.
        If such character was found, it is the beginning of the token. The function then searches from that point on
        for the first character that is contained in delim. 
        - If no such character was found, str has only one token, and future calls to strtok will return a null pointer
        - If such character was found, it is replaced by the null character '\0' and the pointer to the following character
          is stored in a static location for subsequent invocations. 
      - The function then returns the pointer to the beginning of the token 
 
    - If str is a null pointer, the call is treated as a subsequent call to strtok: the function continues from where it left
      in previous invocation. The behavior is the same as if the previously stored pointer is passed as str. 
 
  \param[in] str    pointer to the null-terminated byte string to tokenize
  \param[in] delim         pointer to the null-terminated byte string to copy from
  \return Returns pointer to the beginning of the next token or a null pointer if there are no more tokens.
*/
char *my_strtok(char *str, const char *delim);
 
/// String concatenation
/**
  Appends a copy of the null-terminated byte string pointed to by src to the end of the null-terminated byte string pointed to by dest.
  The character src[0] replaces the null terminator at the end of dest. The resulting byte string is null-terminated.
  The dest string will be reallocated if needed to fit the src string.
 
  \param[in,out] dest    pointer to the null-terminated byte string to append to
  \param[in] src         pointer to the null-terminated byte string identifying delimiters
*/
void my_strcat(char **dest, const char *src);
 
/// Substring matching
/**
  Finds the first occurrence of the null-terminated byte string pointed to by substr in the null-terminated byte string pointed to by str.
  The terminating null characters are not compared. The substr may contain the following subset of the regex syntax:
    []        - character groups
    [^]       - group prohibition
 
  \param[in] str         pointer to the null-terminated byte string to examine
  \param[in] substr      pointer to the null-terminated byte string to search for
  \param[out] begin      pointer to the beginning of the found substring or null pointer if not found
  \return The length of the found substring segment.
*/
unsigned long my_strstr_match(const char *str, const char *substr, char **begin);
 
#endif // MY_STRING_H


courses/be5b99cpl/hw/hw04.txt · Last modified: 2023/11/13 22:56 by ulricji1