mUPnP for C
string_function.c File Reference
#include <mupnp/util/log.h>
#include <mupnp/util/string.h>
#include <ctype.h>

Functions

char * mupnp_strdup (const char *str)
 Duplicate a string (NULL-safe wrapper for strdup)
 
size_t mupnp_strlen (const char *str)
 Get the length of a string (NULL-safe wrapper for strlen)
 
char * mupnp_strcpy (char *dest, const char *src)
 Copy a string (NULL-safe wrapper for strcpy)
 
char * mupnp_strcat (char *dest, const char *src)
 Concatenate strings (NULL-safe wrapper for strcat)
 
int mupnp_strcmp (const char *str1, const char *str2)
 Compare two strings (NULL-safe wrapper for strcmp)
 
int mupnp_strncmp (const char *str1, const char *str2, int nchars)
 Compare first n characters of two strings (NULL-safe)
 
int mupnp_strcasecmp (const char *str1, const char *str2)
 Compare two strings case-insensitively (NULL-safe)
 
bool mupnp_streq (const char *str1, const char *str2)
 Test if two strings are equal (NULL-safe)
 
bool mupnp_strcaseeq (const char *str1, const char *str2)
 Test if two strings are equal case-insensitively (NULL-safe)
 
ssize_t mupnp_strstr (const char *haystack, const char *needle)
 Find substring (NULL-safe wrapper for strstr)
 
ssize_t mupnp_strchr (const char *str, const char *chars, size_t nchars)
 Find first occurrence of any character from a set (strcspn variant)
 
ssize_t mupnp_strrchr (const char *str, const char *chars, size_t nchars)
 Find last occurrence of any character from a set.
 
char * mupnp_strtrimwhite (char *str)
 Trim whitespace from both ends of a string (in-place)
 
char * mupnp_strtrim (char *str, char *delim, size_t ndelim)
 Trim specified delimiter characters from both ends (in-place)
 
char * mupnp_strltrim (char *str, char *delim, size_t ndelim)
 Trim delimiter characters from the left (start) of string (in-place)
 
char * mupnp_strrtrim (char *str, char *delim, size_t ndelim)
 Trim delimiter characters from the right (end) of string (in-place)
 
char * mupnp_strncpy (char *str1, const char *str2, size_t cnt)
 Copy at most n characters from one string to another (NULL-safe)
 
char * mupnp_strncat (char *str1, const char *str2, size_t cnt)
 Append at most n characters from one string to another (NULL-safe)
 
const char * mupnp_int2str (int value, char *buf, size_t bufSize)
 Convert integer to string.
 
const char * mupnp_long2str (long value, char *buf, size_t bufSize)
 Convert long integer to string.
 
const char * mupnp_float2str (float value, char *buf, size_t bufSize)
 Convert float to string.
 
const char * mupnp_double2str (double value, char *buf, size_t bufSize)
 Convert double to string.
 
const char * mupnp_sizet2str (size_t value, char *buf, size_t bufSize)
 Convert size_t to string.
 
const char * mupnp_ssizet2str (ssize_t value, char *buf, size_t bufSize)
 Convert ssize_t to string.
 

Function Documentation

◆ mupnp_double2str()

const char * mupnp_double2str ( double value,
char * buf,
size_t bufSize )

Convert double to string.

Parameters
valueThe double value to convert.
bufBuffer to store the result. Must not be NULL.
bufSizeSize of buf in bytes. Should be at least MUPNP_STRING_DOUBLE_BUFLEN (64) to safely hold any double.
Returns
Pointer to buf containing the converted string.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_float2str()

const char * mupnp_float2str ( float value,
char * buf,
size_t bufSize )

Convert float to string.

Parameters
valueThe float value to convert.
bufBuffer to store the result. Must not be NULL.
bufSizeSize of buf in bytes. Should be at least MUPNP_STRING_FLOAT_BUFLEN (64) to safely hold any float.
Returns
Pointer to buf containing the converted string.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_int2str()

const char * mupnp_int2str ( int value,
char * buf,
size_t bufSize )

Convert integer to string.

Parameters
valueThe integer value to convert.
bufBuffer to store the result. Must not be NULL.
bufSizeSize of buf in bytes. Should be at least MUPNP_STRING_INTEGER_BUFLEN (16) to safely hold any int.
Returns
Pointer to buf containing the converted string.
Note
Thread-safe: Can be called from any thread.
The buffer is always null-terminated if bufSize > 0.

◆ mupnp_long2str()

const char * mupnp_long2str ( long value,
char * buf,
size_t bufSize )

Convert long integer to string.

Parameters
valueThe long value to convert.
bufBuffer to store the result. Must not be NULL.
bufSizeSize of buf in bytes. Should be at least MUPNP_STRING_LONG_BUFLEN (32) to safely hold any long.
Returns
Pointer to buf containing the converted string.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_sizet2str()

const char * mupnp_sizet2str ( size_t value,
char * buf,
size_t bufSize )

Convert size_t to string.

Parameters
valueThe size_t value to convert.
bufBuffer to store the result. Must not be NULL.
bufSizeSize of buf in bytes. Should be at least MUPNP_STRING_LONG_BUFLEN (32) bytes.
Returns
Pointer to buf containing the converted string.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_ssizet2str()

const char * mupnp_ssizet2str ( ssize_t value,
char * buf,
size_t bufSize )

Convert ssize_t to string.

Parameters
valueThe ssize_t value to convert.
bufBuffer to store the result. Must not be NULL.
bufSizeSize of buf in bytes. Should be at least MUPNP_STRING_LONG_BUFLEN (32) bytes.
Returns
Pointer to buf containing the converted string.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_strcasecmp()

int mupnp_strcasecmp ( const char * str1,
const char * str2 )

Compare two strings case-insensitively (NULL-safe)

Parameters
str1First string. May be NULL.
str2Second string. May be NULL.
Returns
Negative if str1 < str2, 0 if equal, positive if str1 > str2 (case-insensitive comparison).
Note
Thread-safe: Can be called from any thread.

◆ mupnp_strcaseeq()

bool mupnp_strcaseeq ( const char * str1,
const char * str2 )

Test if two strings are equal case-insensitively (NULL-safe)

Parameters
str1First string. May be NULL.
str2Second string. May be NULL.
Returns
true if strings are equal (case-insensitive), false otherwise.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_strcat()

char * mupnp_strcat ( char * dest,
const char * src )

Concatenate strings (NULL-safe wrapper for strcat)

Parameters
destDestination buffer. Must not be NULL and must have sufficient space.
srcSource string to append. May be NULL (no-op if NULL).
Returns
dest pointer.
Note
Thread-safe: Can be called from any thread.
Warning
No bounds checking is performed. Ensure dest has enough space for the concatenated result plus null terminator.

◆ mupnp_strchr()

ssize_t mupnp_strchr ( const char * str,
const char * chars,
size_t nchars )

Find first occurrence of any character from a set (strcspn variant)

Parameters
strThe string to search. Must not be NULL.
charsArray of characters to search for. Must not be NULL.
ncharsNumber of characters in chars array.
Returns
Index of first matching character, or -1 if not found.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_strcmp()

int mupnp_strcmp ( const char * str1,
const char * str2 )

Compare two strings (NULL-safe wrapper for strcmp)

Parameters
str1First string. May be NULL.
str2Second string. May be NULL.
Returns
Negative if str1 < str2, 0 if equal, positive if str1 > str2. NULL is considered less than any non-NULL string.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_strcpy()

char * mupnp_strcpy ( char * dest,
const char * src )

Copy a string (NULL-safe wrapper for strcpy)

Parameters
destDestination buffer. Must not be NULL and must be large enough.
srcSource string. May be NULL (dest will be set to empty string).
Returns
dest pointer.
Note
Thread-safe: Can be called from any thread.
Warning
No bounds checking is performed. Ensure dest is large enough to hold src plus null terminator.

◆ mupnp_strdup()

char * mupnp_strdup ( const char * str)

Duplicate a string (NULL-safe wrapper for strdup)

Allocates memory and creates a copy of the input string. The caller is responsible for freeing the returned string with free().

Parameters
strThe string to duplicate. May be NULL.
Returns
A newly-allocated copy of str, or NULL if str is NULL or memory allocation fails.
Note
Thread-safe: Can be called from any thread.
The returned string must be freed with free() to avoid memory leaks.
See also
free()

◆ mupnp_streq()

bool mupnp_streq ( const char * str1,
const char * str2 )

Test if two strings are equal (NULL-safe)

Parameters
str1First string. May be NULL.
str2Second string. May be NULL.
Returns
true if strings are equal, false otherwise. Two NULL strings are considered equal.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_strlen()

size_t mupnp_strlen ( const char * str)

Get the length of a string (NULL-safe wrapper for strlen)

Parameters
strThe string to measure. May be NULL.
Returns
The length of str (excluding null terminator), or 0 if str is NULL.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_strltrim()

char * mupnp_strltrim ( char * str,
char * delim,
size_t ndelim )

Trim delimiter characters from the left (start) of string (in-place)

Parameters
strThe string to trim. Must not be NULL. Will be modified.
delimArray of delimiter characters to remove. Must not be NULL.
ndelimNumber of characters in delim array.
Returns
Pointer to the trimmed string.
Note
Thread-safe: Can be called from any thread.
Side effect: Modifies the input string in place.

◆ mupnp_strncat()

char * mupnp_strncat ( char * str1,
const char * str2,
size_t cnt )

Append at most n characters from one string to another (NULL-safe)

Parameters
str1Destination buffer. Must not be NULL and must have sufficient space.
str2Source string to append. May be NULL (no-op if NULL).
cntMaximum number of characters to append (excluding null terminator).
Returns
str1 pointer.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_strncmp()

int mupnp_strncmp ( const char * str1,
const char * str2,
int nchars )

Compare first n characters of two strings (NULL-safe)

Parameters
str1First string. May be NULL.
str2Second string. May be NULL.
ncharsMaximum number of characters to compare.
Returns
Negative if str1 < str2, 0 if equal, positive if str1 > str2.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_strncpy()

char * mupnp_strncpy ( char * str1,
const char * str2,
size_t cnt )

Copy at most n characters from one string to another (NULL-safe)

Parameters
str1Destination buffer. Must not be NULL and must have space for cnt+1 bytes.
str2Source string. May be NULL (str1 will be set to empty string).
cntMaximum number of characters to copy (excluding null terminator).
Returns
str1 pointer.
Note
Thread-safe: Can be called from any thread.
Always null-terminates the destination string.

◆ mupnp_strrchr()

ssize_t mupnp_strrchr ( const char * str,
const char * chars,
size_t nchars )

Find last occurrence of any character from a set.

Parameters
strThe string to search. Must not be NULL.
charsArray of characters to search for. Must not be NULL.
ncharsNumber of characters in chars array.
Returns
Index of last matching character, or -1 if not found.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_strrtrim()

char * mupnp_strrtrim ( char * str,
char * delim,
size_t ndelim )

Trim delimiter characters from the right (end) of string (in-place)

Parameters
strThe string to trim. Must not be NULL. Will be modified.
delimArray of delimiter characters to remove. Must not be NULL.
ndelimNumber of characters in delim array.
Returns
Pointer to the trimmed string (same as str).
Note
Thread-safe: Can be called from any thread.
Side effect: Modifies the input string in place.

◆ mupnp_strstr()

ssize_t mupnp_strstr ( const char * haystack,
const char * needle )

Find substring (NULL-safe wrapper for strstr)

Parameters
haystackThe string to search in. May be NULL.
needleThe substring to search for. May be NULL.
Returns
Index of first occurrence of needle in haystack, or -1 if not found or if either parameter is NULL.
Note
Thread-safe: Can be called from any thread.

◆ mupnp_strtrim()

char * mupnp_strtrim ( char * str,
char * delim,
size_t ndelim )

Trim specified delimiter characters from both ends (in-place)

Parameters
strThe string to trim. Must not be NULL. Will be modified.
delimArray of delimiter characters to remove. Must not be NULL.
ndelimNumber of characters in delim array.
Returns
Pointer to the trimmed string.
Note
Thread-safe: Can be called from any thread.
Side effect: Modifies the input string in place.

◆ mupnp_strtrimwhite()

char * mupnp_strtrimwhite ( char * str)

Trim whitespace from both ends of a string (in-place)

Removes leading and trailing whitespace characters (space, tab, newline, carriage return) from the string by modifying it in place.

Parameters
strThe string to trim. Must not be NULL. Will be modified.
Returns
Pointer to the trimmed string (same as str, but offset to start of non-whitespace).
Note
Thread-safe: Can be called from any thread.
Side effect: Modifies the input string in place.