Skip to content

cally72jhb/vstr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

vstr/vstring - vector string

simple dynamic strings for c
The library can be used header-only by moving vstr.h to a directory in your program.

vstr contains just about all the functions you'll need.
In total there are 128 functions ranging from searching algorithms to advanced formatting
to number/float to string conversation.

what is the difference between vstr and vstring?

vstr and vstring are separated by different files, but can be used together in one project.
vstr is designed like c-strings and is compatible with them. It does not store the length of the string and must
go through the entire string to determine it's length.

+-------------------------------+------------------+
| String Data (built like vstr) | Null Termination |
+-------------------------------+------------------+

vstring is a string of known length. The length is stored in the 4 bytes (unsigned int) before
the pointer that is returned to the user.
Since the length of the string is always known, vstring is faster than vstr, but requires more memory.

+---------------+----------------------------------+------------------+
| String Length | String Data (designed like vstr) | Null Termination |
+---------------+----------------------------------+------------------+
                |
             pointer

how to decide between vstr and vstring

To decide between vstr and vstring some things have to be considered:

vstr vstring
requires less memory requires more memory than vstr
has to query the whole string
to obtain it's length
length is always known and can
be accessed very fast
fast implementation takes time to implement
made for small strings and few memory made for big applications with long strings

For fast implementation it's better to use vstr. For projects that work
with long strings, the speed of the application is better with vstring.

creating new string

str string = vstr_alloc("abc"); // new string from const char*
str string = vstr_alloc_len("abc", 3); // new string from const char* with length 3
str string = vstr_alloc_empty(); // new empty string

concatenating strings

str string1 = vstr_alloc("abc"); // new string with content "abc" and length 3
str string2 = vstr_alloc("def"); // new string with content "def" and length 3

vstr_cat(&string1, string2); // @string1 needs to be passed as reference here

printf("%s\n", string1); // prints: "abcdef"

utility functions

vstr_len(string) - returns the length of @string
vstr_equ(string1, string2) - returns true if @string1 and @string2 are identical
vstr_cont_s(string1, string2) - returns true if @string1 contains @string2

vstr_find_fs(string1, string2) - returns the index of the first occurence of @string2 in @string1
vstr_find_ls(string1, string2) - returns the index of the last occurence of @string2 in @string1

vstr_upper(string) - makes @string uppercase
vstr_lower(string) - makes @string lowercase