skullchap/chadstr logo

skullchap/chadstr

Free

Chad Strings - The Chad way to handle strings in C.

FreeFree tier
Type
Open Source

About skullchap/chadstr

Chad Strings (chadstr) is a single-header C library that provides a minimalist and unconventional approach to string handling through a powerful str() macro. It enables concatenation of multiple strings, integers, shell commands, and substrings in a single expression. The library integrates with OS command-line tools via a cmd struct, allowing shell-like operations directly in C. It also offers a range() function for substring extraction, supports zero-indexed and one-indexed ranges via HUMAN_RANGE, and optional automatic memory deallocation using GCC/Clang's __attribute__((cleanup())). Designed for quick prototyping and short-lived programs, it aims to reduce boilerplate by leveraging the OS to manage memory and perform string operations.

Key Features

Single-header library (chadstr.h) with no external dependencies
Unified `str()` macro for concatenation, copying, and type conversion
Integration with OS shell commands via `cmd` struct for piping and execution
`range()` function for substring extraction with zero or one-indexed modes
Optional automatic memory deallocation via `__attribute__((cleanup()))` on GCC/Clang
Supports embedding file contents using `cmd CAT` pattern

Pros & Cons

Pros
  • Simplifies complex string operations into a single macro call
  • Leverages existing OS tools (echo, cat, cut) for powerful text processing
  • Minimal boilerplate – single header, no build configuration needed
  • Optional automatic memory management reduces manual free() calls
  • Supports both zero-based and one-based indexing for substring ranges
Cons
  • Automatic memory deallocation relies on GCC/Clang extension (`cleanup`), not portable to all compilers
  • Memory is not automatically freed unless using the `autofree` type or `__attribute__((cleanup()))` – leaks possible in long-running applications
  • Heavy reliance on shell commands may introduce security risks if input is untrusted
  • Non-standard approach may be unfamiliar and harder to debug for many C developers
  • Designed for short-lived programs (author states OS cleans memory), unsuitable for production where explicit resource management is required

Best For

Rapid prototyping and quick scripting in CConcatenating strings, integers, and variables in a single expressionBuilding and executing shell commands directly from C codeFile content embedding without extra file I/O functionsEducation and experimentation with unconventional C string idioms

FAQ

What is chadstr?
Chad Strings is a single-header C library that provides a `str()` macro for concatenation, copying, and integrating with shell commands, aiming to simplify string manipulation in C.
How does memory management work in chadstr?
The library allocates memory for the string data along with the `str` struct. Calling `free()` on the struct pointer frees both. Optionally, using `autofree` or `__attribute__((cleanup()))` on GCC/Clang automatically frees memory when the variable goes out of scope.
Can chadstr execute shell commands?
Yes, using the `cmd` struct (e.g., `cmd CMD = { "echo" }`) you can build and execute shell commands by concatenating them with `str()`, producing a string that can be passed to `puts()` or other functions.
What compilers are supported?
The library works with any C compiler, but the automatic cleanup feature requires GCC or Clang due to `__attribute__((cleanup()))`.
Is chadstr suitable for production use?
The library is designed for quick prototyping and learning; the author states 'OS cleans all memory for me' suggesting it is not intended for long-running or security-critical applications.