🧵 str
Painfully common string utilities for C
Loading...
Searching...
No Matches
str.h
Go to the documentation of this file.
1/*
2 * 🧵 str — str.h
3 * Copyright (c) 2026 Fawn <rubiefawn@gmail.com>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
19#pragma once
20#include <stdbool.h>
21#include <stddef.h>
22
30typedef struct str {
32 char *data;
33
37 size_t len;
39
41str str_from_cstr(char *sz);
42
45
48
51
54
56str str_strip_prefix_cstr(str it, const char *prefix);
57
59str str_strip_prefix_if(str it, bool (*predicate)(char c));
60
62#define str_strip_prefix(IT, PREFIX) _Generic((PREFIX),\
63 str : str_strip_prefix_str,\
64 char * : str_strip_prefix_cstr,\
65 bool (*)(char c): str_strip_prefix_if\
66)((IT), (PREFIX))
67
69str str_strip_postfix_str(str it, str postfix);
70
72str str_strip_postfix_cstr(str it, const char *postfix);
73
75str str_strip_postfix_if(str it, bool (*predicate)(char c));
76
78#define str_strip_postfix(IT, POSTFIX) _Generic((POSTFIX),\
79 str : str_strip_postfix_str,\
80 char * : str_strip_postfix_cstr,\
81 bool (*)(char c): str_strip_postfix_if\
82)((IT), (POSTFIX))
83
85bool str_is_sz(str it);
86
88int str_cmp_str(str a, str b);
89
91int str_cmp_cstr(str a, const char *b);
92
94int cstr_cmp_str(const char *a, str b);
95
99#define cstr_cmp_cstr strcmp
100
102#define str_cmp(LHS, RHS) _Generic((LHS),\
103 str : _Generic((RHS),\
104 str : str_cmp_str,\
105 char *: str_cmp_cstr\
106 ),\
107 char *: _Generic((RHS),\
108 str : cstr_cmp_str,\
109 char *: strcmp\
110 )\
111)((LHS), (RHS))
112
114bool str_eq_str(str a, str b);
115
117bool str_eq_cstr(str a, const char *b);
118
120bool cstr_eq_str(const char *a, str b);
121
123bool cstr_eq_cstr(const char *a, const char *b);
124
126#define str_eq(LHS, RHS) _Generic((LHS),\
127 str : _Generic((RHS),\
128 str : str_eq_str,\
129 char *: str_eq_cstr\
130 ),\
131 char *: _Generic((RHS),\
132 str : cstr_eq_str,\
133 char *: cstr_eq_cstr\
134 )\
135)((LHS), (RHS))
136
138bool str_eq_str_any(str it, size_t count, ...);
139
141bool str_eq_cstr_any(str it, size_t count, ...);
142
144bool cstr_eq_str_any(const char *it, size_t count, ...);
145
147bool cstr_eq_cstr_any(const char *it, size_t count, ...);
148
150#define str_eq_any(LHS, COUNT, RHS, ...) _Generic((LHS),\
151 str : _Generic((RHS),\
152 str : str_eq_str_any,\
153 char *: str_eq_cstr_any\
154 ),\
155 char *: _Generic((RHS),\
156 str : cstr_eq_str_any,\
157 char *: cstr_eq_cstr_any\
158 )\
159)((LHS), (COUNT), (RHS) __VA_OPT(, __VA_ARGS__))
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.
Definition str.c:151
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.
Definition str.c:127
bool str_eq_cstr(str a, const char *b)
Compares two strings to see if their contents are equal.
Definition str.c:104
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.
Definition str.c:56
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.
Definition str.c:48
bool cstr_eq_str(const char *a, str b)
Compares two strings to see if their contents are equal.
Definition str.c:109
str str_strip_whitespace(str it)
Returns a non-owning copy of a str with all leading and trailing whitespace removed.
Definition str.c:25
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.
Definition str.c:61
int str_cmp_str(str a, str b)
Compares two strings lexicographically.
Definition str.c:81
bool str_eq_str(str a, str b)
Compares two strings to see if their contents are equal.
Definition str.c:100
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.
Definition str.c:139
str str_strip_whitespace_pre(str it)
Returns a non-owning copy of a str with all leading whitespace removed.
Definition str.c:31
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.
Definition str.c:41
int str_cmp_cstr(str a, const char *b)
Compares two strings lexicographically.
Definition str.c:88
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.
Definition str.c:74
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.
Definition str.c:67
int cstr_cmp_str(const char *a, str b)
Compares two strings lexicographically.
Definition str.c:96
str str_from_cstr(char *sz)
Constructs a non-owning str from a null-terminated C-string.
Definition str.c:23
bool str_is_sz(str it)
Checks if a str is null-terminated.
Definition str.c:79
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.
Definition str.c:115
str str_strip_whitespace_post(str it)
Returns a non-owning copy of a str with all trailing whitespace removed.
Definition str.c:36
bool cstr_eq_cstr(const char *a, const char *b)
Compares two strings to see if their contents are equal.
Definition str.c:113
A string "slice", which is a pointer & length pair.
Definition str.h:30
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