🧵 str
Painfully common string utilities for C
Loading...
Searching...
No Matches
str.h File Reference
#include <stdbool.h>
#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  str
 A string "slice", which is a pointer & length pair. More...

Macros

#define str_strip_prefix(IT, PREFIX)
 Returns a non-owning copy of a str with a given prefix removed, if it is present.
#define str_strip_postfix(IT, POSTFIX)
 Returns a non-owning copy of a str with a given postfix removed, if it is present.
#define cstr_cmp_cstr   strcmp
 Compares two strings lexicographically.
#define str_cmp(LHS, RHS)
 Compares two strings lexicographically.
#define str_eq(LHS, RHS)
 Compares two strings to see if their contents are equal.
#define str_eq_any(LHS, COUNT, RHS, ...)
 Compares a string to several others to see if its contents are equal to any of them.

Typedefs

typedef struct str str
 A string "slice", which is a pointer & length pair.

Functions

str str_from_cstr (char *sz)
 Constructs a non-owning str from a null-terminated C-string.
str str_strip_whitespace (str it)
 Returns a non-owning copy of a str with all leading and trailing whitespace removed.
str str_strip_whitespace_pre (str it)
 Returns a non-owning copy of a str with all leading whitespace removed.
str str_strip_whitespace_post (str it)
 Returns a non-owning copy of a str with all trailing whitespace removed.
str str_strip_prefix_str (str it, str prefix)
 Returns a non-owning copy of a str with a given prefix removed, if it is present.
str str_strip_prefix_cstr (str it, const char *prefix)
 Returns a non-owning copy of a str with a given prefix removed, if it is present.
str str_strip_prefix_if (str it, bool(*predicate)(char c))
 Returns a non-owning copy of a str with all leading characters satisfying predicate removed.
str str_strip_postfix_str (str it, str postfix)
 Returns a non-owning copy of a str with a given postfix removed, if it is present.
str str_strip_postfix_cstr (str it, const char *postfix)
 Returns a non-owning copy of a str with a given postfix removed, if it is present.
str str_strip_postfix_if (str it, bool(*predicate)(char c))
 Returns a non-owning copy of a str with all trailing characters satisfying predicate removed.
bool str_is_sz (str it)
 Checks if a str is null-terminated.
int str_cmp_str (str a, str b)
 Compares two strings lexicographically.
int str_cmp_cstr (str a, const char *b)
 Compares two strings lexicographically.
int cstr_cmp_str (const char *a, str b)
 Compares two strings lexicographically.
bool str_eq_str (str a, str b)
 Compares two strings to see if their contents are equal.
bool str_eq_cstr (str a, const char *b)
 Compares two strings to see if their contents are equal.
bool cstr_eq_str (const char *a, str b)
 Compares two strings to see if their contents are equal.
bool cstr_eq_cstr (const char *a, const char *b)
 Compares two strings to see if their contents are equal.
bool str_eq_str_any (str it, size_t count,...)
 Compares a string to several others to see if its contents are equal to any of them.
bool str_eq_cstr_any (str it, size_t count,...)
 Compares a string to several others to see if its contents are equal to any of them.
bool cstr_eq_str_any (const char *it, size_t count,...)
 Compares a string to several others to see if its contents are equal to any of them.
bool cstr_eq_cstr_any (const char *it, size_t count,...)
 Compares a string to several others to see if its contents are equal to any of them.

Macro Definition Documentation

◆ cstr_cmp_cstr

#define cstr_cmp_cstr   strcmp

Compares two strings lexicographically.

This is an alias to strcmp() for convenience.

◆ str_cmp

#define str_cmp ( LHS,
RHS )
Value:
_Generic((LHS),\
str : _Generic((RHS),\
str : str_cmp_str,\
char *: str_cmp_cstr\
),\
char *: _Generic((RHS),\
str : cstr_cmp_str,\
char *: strcmp\
)\
)((LHS), (RHS))
A string "slice", which is a pointer & length pair.
Definition str.h:30

Compares two strings lexicographically.

◆ str_eq

#define str_eq ( LHS,
RHS )
Value:
_Generic((LHS),\
str : _Generic((RHS),\
str : str_eq_str,\
char *: str_eq_cstr\
),\
char *: _Generic((RHS),\
str : cstr_eq_str,\
char *: cstr_eq_cstr\
)\
)((LHS), (RHS))

Compares two strings to see if their contents are equal.

◆ str_eq_any

#define str_eq_any ( LHS,
COUNT,
RHS,
... )
Value:
_Generic((LHS),\
str : _Generic((RHS),\
str : str_eq_str_any,\
char *: str_eq_cstr_any\
),\
char *: _Generic((RHS),\
str : cstr_eq_str_any,\
char *: cstr_eq_cstr_any\
)\
)((LHS), (COUNT), (RHS) __VA_OPT(, __VA_ARGS__))

Compares a string to several others to see if its contents are equal to any of them.

◆ str_strip_postfix

#define str_strip_postfix ( IT,
POSTFIX )
Value:
_Generic((POSTFIX),\
str : str_strip_postfix_str,\
char * : str_strip_postfix_cstr,\
bool (*)(char c): str_strip_postfix_if\
)((IT), (POSTFIX))

Returns a non-owning copy of a str with a given postfix removed, if it is present.

◆ str_strip_prefix

#define str_strip_prefix ( IT,
PREFIX )
Value:
_Generic((PREFIX),\
str : str_strip_prefix_str,\
char * : str_strip_prefix_cstr,\
bool (*)(char c): str_strip_prefix_if\
)((IT), (PREFIX))

Returns a non-owning copy of a str with a given prefix removed, if it is present.

Typedef Documentation

◆ str

typedef struct str str

A string "slice", which is a pointer & length pair.

A str can be printed using the "%.*s" format specifier:

str s = str_from_cstr("World!");
printf("Hello %.*s", (int)(s.len), s.data);
str str_from_cstr(char *sz)
Constructs a non-owning str from a null-terminated C-string.
Definition str.c:23
char * data
Pointer to the start of the string.
Definition str.h:32
size_t len
The length of the string.
Definition str.h:37