Skip to content
This repository was archived by the owner on Oct 15, 2021. It is now read-only.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

60 Commits

Repository files navigation

libstring Codacy BadgeCodeFactorBuild StatusLicense: MIT

*Early work in progress. ⚠️

libstring aims to be a highly portable, Valgrind-passing, single-header library that brings better support for strings and string manipulation to ANSI C. This library does not depend on string.h or strings.h, using only stdio.h, stdlib.h and stdbool.h (when available, falls back to the usual typedef when not on C99 or newer).

Currently implemented public functions

cstr_t*string_init(constchar*origin); // Initializes a new cstr_t *.voidstring_free_all (void); // Frees all heap allocations made by libstring.char*string_first_token(char*str, char*delim); // Sets up tokenization and returns the first tokenvoidstring_replace(cstr_t*str, char*old, constchar*new); // Replaces all instances of `old` to `new` within `str`char*string_get_token(char*delim); // Returns a token from the char * str passed onto the previous functioncstr_t*string_to_lower_case(cstr_t*origin); // Returns a new cstr_t * with the contents of origin lower-casedcstr_t*string_to_upper_case(cstr_t*origin); // Returns a new cstr_t * with the contents of origin upper-casedcstr_t*string_concat(cstr_t*str1, constchar*str2); // Returns a new cstr_t * with the concatenation of str1 and str2size_tstring_concat_to(cstr_t*str1, constchar*str2); // Concatenates str1 and str2 to str1.boolstring_contains(cstr_t*str1, constchar*str2); // Returns true if str2 is a substring of str1.size_tstring_update(cstr_t*str, constchar*new_val); // Updates the value of str. Increases its memory reservation if needed.boolstring_swap(cstr_t*str1, cstr_t*str2); // Swaps the contents of str1 and str2.boolstring_reserve(cstr_t*str, size_tcapacity); // Increases str's memory reservationsize_tstring_replace_char(cstr_t*str, charbefore, charafter); // Replaces all instances of a char with another.cstr_t*string_mid(cstr_t*str, size_tpos, longlength); // Returns a substring of a given string starting at position pos with a given length.cstr_t*string_left(cstr_t*str, size_tlength); // Returns the first (length) characters of a string starting from the left.cstr_t*string_right(cstr_t*str, longlength); // Returns the first (length) characters of a string starting from the right.

The other functions defined in libstring.c are internal and not accessible.

Building the test file

The unit tests are done using Criterion. Running the run-tests.sh file builds the test file and runs it through Valgrind.

For C89

The only thing deterring builds for C89 are the C++-style comments.

You can solve this problem by removing all comments and then building:

gcc -fpreprocessed -dD -E src/libstring.c >> src/89libstring.cgcc -fpreprocessed -dD -E src/libstring.h >> src/89libstring.h

You can then use 89libstring.c and 89libstring.h on C89 projects.

About

Concise library for modern string manipulation in ANSI C.

Topics

Resources

Stars

6 stars

Watchers

0 watching

Forks

Contributors

Languages