coco_string.c File Reference

Definitions of functions that manipulate strings. More...

#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include "coco.h"

Macros

#define COCO_VSTRDUPF_BUFLEN   444
 The length of the buffer used in the coco_vstrdupf function. More...
 

Functions

static size_t * coco_allocate_vector_size_t (const size_t number_of_elements)
 
static char * coco_allocate_string (const size_t number_of_elements)
 
static char * coco_strdup (const char *string)
 Creates a duplicate copy of string and returns a pointer to it. More...
 
static char * coco_vstrdupf (const char *str, va_list args)
 Formatted string duplication, with va_list arguments. More...
 
char * coco_strdupf (const char *str,...)
 Formatted string duplication. More...
 
static char * coco_strconcat (const char *s1, const char *s2)
 Returns a concatenate copy of string1 + string2. More...
 
static long coco_strfind (const char *base, const char *seq)
 Returns the first index where seq occurs in base and -1 if it doesn't. More...
 
static char ** coco_string_split (const char *string, const char delimiter)
 Splits a string based on the given delimiter. More...
 
static char * coco_remove_from_string (const char *string, const char *from, const char *to)
 Creates and returns a string with removed characters between from and to. More...
 
static size_t * coco_string_parse_ranges (const char *string, const size_t min, const size_t max, const char *name, const size_t max_count)
 Returns the numbers defined by the ranges. More...
 
static char * coco_string_trim (char *string)
 Trims the given string (removes any leading and trailing spaces). More...
 

Detailed Description

Definitions of functions that manipulate strings.

Macro Definition Documentation

#define COCO_VSTRDUPF_BUFLEN   444

The length of the buffer used in the coco_vstrdupf function.

Note
This should be handled differently!

Function Documentation

static char* coco_allocate_string ( const size_t  number_of_elements)
static
static size_t* coco_allocate_vector_size_t ( const size_t  number_of_elements)
static
static char* coco_remove_from_string ( const char *  string,
const char *  from,
const char *  to 
)
static

Creates and returns a string with removed characters between from and to.

If you wish to remove characters from the beginning of the string, set from to "". If you wish to remove characters until the end of the string, set to to "".

The caller is responsible for freeing the allocated memory using coco_free_memory().

static char* coco_strconcat ( const char *  s1,
const char *  s2 
)
static

Returns a concatenate copy of string1 + string2.

The caller is responsible for freeing the allocated memory using coco_free_memory().

static char* coco_strdup ( const char *  string)
static

Creates a duplicate copy of string and returns a pointer to it.

The caller is responsible for freeing the allocated memory using coco_free_memory().

char* coco_strdupf ( const char *  str,
  ... 
)

Formatted string duplication.

Optional arguments are used like in sprintf.

static long coco_strfind ( const char *  base,
const char *  seq 
)
static

Returns the first index where seq occurs in base and -1 if it doesn't.

Note
If there is an equivalent standard C function, this can/should be removed.
static size_t* coco_string_parse_ranges ( const char *  string,
const size_t  min,
const size_t  max,
const char *  name,
const size_t  max_count 
)
static

Returns the numbers defined by the ranges.

Reads ranges from a string of positive ranges separated by commas. For example: "-3,5-6,8-". Returns the numbers that are defined by the ranges if min and max are used as their extremes. If the ranges with open beginning/end are not allowed, use 0 as min/max. The returned string has an appended 0 to mark its end. A maximum of max_count values is returned. If there is a problem with one of the ranges, the parsing stops and the current result is returned. The memory of the returned object needs to be freed by the caller.

static char** coco_string_split ( const char *  string,
const char  delimiter 
)
static

Splits a string based on the given delimiter.

Returns a pointer to the resulting substrings with NULL as the last one. The caller is responsible for freeing the allocated memory using:

for (i = 0; *(result + i); i++) coco_free_memory(*(result + i)); coco_free_memory(*(result + i)); <- This is needed! coco_free_memory(result);

static char* coco_string_trim ( char *  string)
static

Trims the given string (removes any leading and trailing spaces).

If the string contains any leading spaces, the contents are shifted so that if it was dynamically allocated, it can be still freed on the returned pointer.

static char* coco_vstrdupf ( const char *  str,
va_list  args 
)
static

Formatted string duplication, with va_list arguments.