🧵 str
Painfully common string utilities for C
Loading...
Searching...
No Matches
cstr.h
Go to the documentation of this file.
1/*
2 * 🧵 str — cstr.h
3 * Copyright (c) 2025–2026 Fawn <rubiefawn@proton.me>
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
20#pragma once
21#include <stddef.h>
22
23// MSVC has memccpy(), though it will complain about using the POSIX
24// name `memccpy()` rather than the MSVC-specific `_memccpy()`.
25// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/memccpy
26#if !defined(STR_NO_MEMCCPY) && __STDC_VERSION__ < 202311L && _POSIX_C_SOURCE < 200809L && !defined(_MSC_VER)
40void *memccpy(void *restrict dest, const void *restrict src, int ch, size_t len);
41#endif
42
43// MSVC has strdup(), though it will complain about using the POSIX
44// name `strdup()` rather than the MSVC-specific `_strdup()`.
45// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strdup-wcsdup-mbsdup
46#if !defined(STR_NO_STRDUP) && __STDC_VERSION__ < 202311L && _POSIX_C_SOURCE < 200809L && !defined(_MSC_VER)
57char *strdup(const char *src);
58#endif
59
60#if !defined(STR_NO_STRNDUP) && __STDC_VERSION__ < 202311L && _POSIX_C_SOURCE < 200809L
75char *strndup(const char *src, size_t size);
76#endif
77
78// MSVC has strnlen()
79// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strnlen-strnlen-s
80// TODO C2y: Replace "203013L" with the actual constant for C2y
81#if !defined(STR_NO_STRNLEN) && __STDC_VERSION__ < 203013L && _POSIX_C_SOURCE < 200809L && !defined(_MSC_VER)
91size_t strnlen(const char *s, size_t n);
92#endif
93
146char *strzcpy(char dest[restrict static 1], const char src[restrict static 1], size_t len);
char * strzcpy(char dest[restrict static 1], const char src[restrict static 1], size_t len)
Similar to strncpy(), except result is always nil-terminated.
void * memccpy(void *restrict dest, const void *restrict src, int ch, size_t len)
Copies up to len characters from src into dest, stopping early if the character ch is encountered.
char * strdup(const char *src)
Duplicates a nil-terminated string.
size_t strnlen(const char *s, size_t n)
Calculates the length of a string excluding its nil terminator, up to a maximum length.
char * strndup(const char *src, size_t size)
Duplicates a nil-terminated string, up to a maximum length.