Orcania: string functions

Orcania Potluck with different functions for different purposes that can be shared among C programs

Loading...
Searching...
No Matches
string functions

## Functions
char * | str_replace (const char *source, const char *str_old, const char *str_new)
char * | o_strdup (const char *source)
char * | o_strndup (const char *source, size_t len)
int | o_strcmp (const char *p1, const char *p2)
int | o_strncmp (const char *p1, const char *p2, size_t n)
char * | o_strcpy (char *p1, const char *p2)
char * | o_strncpy (char *p1, const char *p2, size_t n)
int | o_strcasecmp (const char *p1, const char *p2)
int | o_strncasecmp (const char *p1, const char *p2, size_t n)
char * | o_strstr (const char *haystack, const char *needle)
char * | o_strnstr (const char *haystack, const char *needle, size_t len)
char * | o_strcasestr (const char *haystack, const char *needle)
char * | o_strchr (const char *haystack, int c)
const char * | o_strnchr (const char *haystack, size_t len, char c)
char * | o_strrchr (const char *haystack, int c)
const char * | o_strrnchr (const char *haystack, size_t len, char c)
int | o_strnullempty (const char *s)
size_t | o_strlen (const char *s)
const char * | trimwhitespace (char *str)
char * | trimcharacter (char *str, char to_remove)
char * | msprintf (const char *message,...)
char * | mstrcatf (char *source, const char *message,...)

## Detailed Description

These functions are used for string manipulation Types of functions available:

- crash safe version of <string.h> library
- build heap allocated strings using printf-like format

## Function Documentation

## ◆ msprintf()

char * msprintf | ( | const char * | message,
|  |  | ... )

char * msprintf(const char * message, ...) Implementation of sprintf that return a malloc'd char * with the string construction because life is too short to use 3 lines instead of 1 but don't forget to free the returned value after use! Disclaimer: When I made this function, I didn't know asprintf existed. If I did I surely wouldn't make msprintf although, msprintf has the advantage to work with o_malloc and o_free so it can use specific memory allocation functions

Parameters

  message | the message format to concat to source, printf format

Returns
  a heap-allocated char * containing the result string

## ◆ mstrcatf()

char * mstrcatf | ( | char * | source,
|  | const char * | message,
|  |  | ... )

char * mstrcatf((char * source, const char * message, ...) A combination of strcat and msprintf that will concat source and message formatted and return the combination as a new allocated char * and will o_free source but don't forget to free the returned value after use!

Parameters

  source | the source string to concat message, source will be free'd during the process
  message | the message format to concat to source, printf format

Returns
  a heap-allocated char * containing the result string

## ◆ o_strcasecmp()

int o_strcasecmp | ( | const char * | p1,
|  | const char * | p2 )

o_strcasecmp a modified strcasecmp function that don't crash when p1 is NULL or p2 us NULL

Parameters

  p1 | the first string to compare
  p2 | the second string to compare

Returns
  0 if p1 is equal to p2, negative number if p1 is inferior to p2, positive number otherwise

## ◆ o_strcasestr()

char * o_strcasestr | ( | const char * | haystack,
|  | const char * | needle )

o_strcasestr a modified strcasestr function that don't crash when haystack is NULL or needle us NULL

Parameters

  haystack | the string to be looked for
  needle | the string to search in haystack

Returns
  the pointer to the first occurence of needle in haystack or NULL if not found

## ◆ o_strchr()

char * o_strchr | ( | const char * | haystack,
|  | int | c )

o_strchr a modified strchr function that don't crash when haystack is NULL

Parameters

  haystack | the string to be looked for
  c | the character to look for in haystack

Returns
  the pointer to the first occurence of c in haystack or NULL if not found

## ◆ o_strcmp()

int o_strcmp | ( | const char * | p1,
|  | const char * | p2 )

o_strcmp a modified strcmp function that don't crash when p1 is NULL or p2 us NULL

Parameters

  p1 | the first string to compare
  p2 | the second string to compare

Returns
  0 if p1 is equal to p2, negative number if p1 is inferior to p2, positive number otherwise

## ◆ o_strcpy()

char * o_strcpy | ( | char * | p1,
|  | const char * | p2 )

o_strcpy a modified strcpy function that don't crash when p1 is NULL or p2 us NULL

Parameters

  p1 | the string to copy p2
  p2 | the source string to be copied

Returns
  p1

## ◆ o_strdup()

char * o_strdup | ( | const char * | source | ) |

o_strdup a modified strdup function that don't crash when source is NULL, instead return NULL

Parameters

  source | the source string to duplicate

Returns
  a heap-allocated string Returned value must be free'd after use

## ◆ o_strlen()

size_t o_strlen | ( | const char * | s | ) |

o_strlen a modified version of strlen that don't crash when s is NULL

Parameters

  s | the string to calculate length

Returns
  the length of s, 0 if s is NULL

## ◆ o_strncasecmp()

int o_strncasecmp | ( | const char * | p1,
|  | const char * | p2,
|  | size_t | n )

o_strncasecmp a modified strncasecmp function that don't crash when p1 is NULL or p2 us NULL

Parameters

  p1 | the first string to compare
  p2 | the second string to compare
  n | the maximum number of char to compare

Returns
  0 if p1 is equal to p2, negative number if p1 is inferior to p2, positive number otherwise

## ◆ o_strnchr()

const char * o_strnchr | ( | const char * | haystack,
|  | size_t | len,
|  | char | c )

o_strnchr a modified strnchr function that don't crash when haystack is NULL

Parameters

  haystack | the string to be looked for
  len | the maxmimum length of characters to look for in haystack
  c | the character to look for in haystack

Returns
  the pointer to the first occurence of c in haystack or NULL if not found

## ◆ o_strncmp()

int o_strncmp | ( | const char * | p1,
|  | const char * | p2,
|  | size_t | n )

o_strncmp a modified strncmp function that don't crash when p1 is NULL or p2 us NULL

Parameters

  p1 | the first string to compare
  p2 | the second string to compare
  n | the maximum number of char to compare

Returns
  0 if p1 is equal to p2, negative number if p1 is inferior to p2, positive number otherwise

## ◆ o_strncpy()

char * o_strncpy | ( | char * | p1,
|  | const char * | p2,
|  | size_t | n )

o_strncpy

Parameters

  p1 | the string to copy p2
  p2 | the source string to be copied
  n | the maximum characters to copy from p2 to p1

Returns
  p1 a modified strncpy function that don't crash when p1 is NULL or p2 us NULL

## ◆ o_strndup()

char * o_strndup | ( | const char * | source,
|  | size_t | len )

o_strndup a modified strndup function that don't crash when source is NULL, instead return NULL

Parameters

  source | the source string to duplicate
  len | the maximum length of the string source to duplicate

Returns
  a heap-allocated string Returned value must be free'd after use

## ◆ o_strnstr()

char * o_strnstr | ( | const char * | haystack,
|  | const char * | needle,
|  | size_t | len )

o_strnstr a modified strnstr function that don't crash when haystack is NULL or needle us NULL

Parameters

  haystack | the string to be looked for
  needle | the string to search in haystack
  len | the maximum length to look for in haystack

Returns
  the pointer to the first occurence of needle in haystack or NULL if not found

## ◆ o_strnullempty()

int o_strnullempty | ( | const char * | s | ) |

o_strnullempty return true if s is NULL or empty string, false otherwise

## ◆ o_strrchr()

char * o_strrchr | ( | const char * | haystack,
|  | int | c )

o_strrchr a modified strrchr function that don't crash when haystack is NULL

Parameters

  haystack | the string to be looked for
  c | the character to look for in haystack

Returns
  the pointer to the last occurence of c in haystack or NULL if not found

o_strrchr a modified strrchr function that don't crash when haystack is NULL

## ◆ o_strrnchr()

const char * o_strrnchr | ( | const char * | haystack,
|  | size_t | len,
|  | char | c )

o_strrnchr a modified strrnchr function that don't crash when haystack is NULL

Parameters

  haystack | the string to be looked for
  len | the maxmimum length of characters to look for in haystack
  c | the character to look for in haystack

Returns
  the pointer to the last occurence of c in haystack or NULL if not found

## ◆ o_strstr()

char * o_strstr | ( | const char * | haystack,
|  | const char * | needle )

o_strstr a modified strstr function that don't crash when haystack is NULL or needle us NULL

Parameters

  haystack | the string to be looked for
  needle | the string to search in haystack

Returns
  the pointer to the first occurence of needle in haystack or NULL if not found

## ◆ str_replace()

char * str_replace | ( | const char * | source,
|  | const char * | str_old,
|  | const char * | str_new )

char * str_replace(const char * source, char * old, char * new) replace all occurences of old by new in the string source return a char * with the new value

Parameters

  source | the source string
  str_old | string to be replaced in source
  str_new | new string to replace

Returns
  a heap-allocated string with the replacements applied return NULL on error returned value must be free'd after use

Orcania library

Different functions for different purposes but that can be shared between other projects

## ◆ trimcharacter()

char * trimcharacter | ( | char * | str,
|  | char | to_remove )

Remove string of beginning and ending given character the string str will be modified

Parameters

  string functions | the string to trim
  to_remove | the character to trim

Returns
  the trimmed str

## ◆ trimwhitespace()

const char * trimwhitespace | ( | char * | str | ) |

Remove string of beginning and ending whitespaces the string str will be modified

Parameters

  string functions | the string to trim

Returns
  the trimmed str