descriptionAdds support for modern string manipulation to ANSI C.
homepage URLhttps://github.com/carmesim/libstring
repository URLhttps://github.com/carmesim/libstring.git
ownerlemao.vrm07@gmail.com
last changeTue, 24 Nov 2020 04:50:26 +0000 (24 01:50 -0300)
last refreshSat, 27 Apr 2024 06:54:13 +0000 (27 08:54 +0200)
content tags
add:
README.md

libstring Codacy Badge CodeFactor Build Status License: 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(const char * origin);                 // Initializes a new cstr_t *.
void  string_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 token
void string_replace(cstr_t *str, char * old, const char * 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 function
cstr_t * string_to_lower_case(cstr_t * origin);            // Returns a new cstr_t * with the contents of origin lower-cased
cstr_t * string_to_upper_case(cstr_t * origin);            // Returns a new cstr_t * with the contents of origin upper-cased
cstr_t * string_concat(cstr_t * str1, const char * str2);  // Returns a new cstr_t * with the concatenation of str1 and str2
size_t string_concat_to(cstr_t * str1, const char * str2); // Concatenates str1 and str2 to str1.
bool string_contains(cstr_t * str1, const char * str2);    // Returns true if str2 is a substring of str1.
size_t string_update(cstr_t * str, const char * new_val);  // Updates the value of str. Increases its memory reservation if needed.
bool string_swap(cstr_t * str1, cstr_t * str2);            // Swaps the contents of str1 and str2.
bool string_reserve(cstr_t *str, size_t capacity);         // Increases str's memory reservation
size_t string_replace_char(cstr_t *str, char before, char after); // Replaces all instances of a char with another.
cstr_t * string_mid(cstr_t * str, size_t pos, long length); // Returns a substring of a given string starting at position pos with a given length.
cstr_t * string_left(cstr_t * str, size_t length); // Returns the first (length) characters of a string starting from the left.
cstr_t * string_right(cstr_t * str, long length); // 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.c
gcc -fpreprocessed -dD -E src/libstring.h >> src/89libstring.h

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

shortlog
2020-11-24 Vinícius Rodrigues... Fix potential NULL deref and other minor changesmaster
2020-11-24 Vinícius Rodrigues... Better memory safety (and slighty better docs)
2020-10-12 V. R. MiguelRemove TODO badge
2020-08-03 Vinícius R... New .travis.yml
2020-08-02 Vinícius R... Typo on run-test.sh
2020-08-02 Vinícius R... Update Travis-CI build
2020-08-02 Vinícius R... Add proper unit tests, drop QMake
2020-07-25 Vinícius R... Add string_replace
2020-06-23 IvanDSMstring_right: Fixing last commit.
2020-06-22 IvanDSMstring_right: Quick implementation of string_right
2020-06-22 IvanDSMlibstring.h: Fix declaration of string_right, change...
2020-06-22 IvanDSMstring_left: Quick implementation of string_left
2020-06-22 IvanDSMstring_mid: Implementation of string_mid
2020-06-22 IvanDSMstring_free_all: Fix alloc_list_head not being reset
2020-06-08 V. R. MiguelUpdate README.md
2020-06-08 Vinícius R... Merge remote-tracking branch 'origin/master'
...
heads
3 years ago master