Skip to content

lucky017/cstring

Repository files navigation

cstring library

A string library in C with C++ standard features for string manipulation, designed to mimic the behavior of C++'s std::string (specifically std::basic_string<char>).

I don't have a documentation for now but for reference check: cppreference

Quick Start

#include "cstring.h"

int main(void)
{
    // Initialization
    cstring s = cstr_init("Hello");
    
    // Modifiers
    cstr_append(&s, " World");
    cstr_insert(&s, 5, " amazing");
    
    // Access & Info
    printf("String: %s\n", s);
    printf("Length: %zu\n", cstr_length(s));
    
    // Operations
    if (cstr_contains(s, "amazing")) {
        printf("It's amazing!\n");
    }
    
    // Cleanup
    cstr_free(&s);
    return 0;
}

Core API Overview

Initialization

  • cstr_init(): Default constructor.
  • cstr_init("literal"): Copy constructor.
  • cstr_init(count, 'c'): Create a string with count characters of 'c'.
  • cstr_literal(name, "content"): Create a stack-allocated, literal as const_cstring.

Element Access

  • cstr_at(s, index): Bounds-checked access(supports [] but not recommended).
  • cstr_front(s), cstr_back(s): First and last characters.

Modifiers

  • cstr_append(&s, ...): Append strings, chars, or substrings.
  • cstr_insert(&s, pos, ...): Insert at a specific position.
  • cstr_erase(&s, pos, count): Remove a range of characters.
  • cstr_replace(&s, pos, count, ...): Replace a range with new content.
  • cstr_push_back(&s, 'c'), cstr_pop_back(&s): Stack-like operations.

Search & Compare

  • cstr_find(s, ...): Find first occurrence of a char or string.
  • cstr_rfind(s, ...): Find last occurrence of a char or string.
  • cstr_find_first_of(s, ...): Find first occurrence of any char in a set.
  • cstr_find_last_of(s, ...): Find last occurrence of any char in a set.
  • cstr_find_first_not_of(s, ...): Find first char not in a set.
  • cstr_find_last_not_of(s, ...): Find last char not in a set.
  • cstr_contains(s, ...): Check for existence.
  • cstr_starts_with(s, ...) / cstr_ends_with(s, ...): Prefix/Suffix checks.
  • cstr_compare(s, other): Lexicographical comparison.

Build Instructions

Prerequisites

  • CMake 3.16.0+
  • A C compiler (GCC, Clang, or MSVC)
  • Git

Build

git clone --recursive https://github.com/lucky017/cstring.git
cd cstring

Linux / macOS

cmake -B build
cmake --build build

Windows (MSVC)

cmake -B build
cmake --build build --config Release

Options

Option Default Description
-DBUILD_SHARED=ON OFF Build as a shared library instead of static
-DBUILD_TESTS=ON OFF Build and run the test suite

Linking

Add to your CMakeLists.txt:

add_subdirectory(cstring)
target_link_libraries(your_target PRIVATE cstring)

7 Things I have to do

  • Input & Output
  • Numeric conversions
  • Hash support for cstring
  • String View
  • UTF-8 support
  • Support for wchar_t, char16_t and char32_t
  • Issues of performance and designs

Note: This library is byte-based and does not natively support multi-byte encodings like UTF-8.

About

A string library in C with C++ standard features.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors