A library providing string functions and utilities in C.
Before using it, do cd c-string/ && ./setup.sh
to install libraries.
#include <c-string/lib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
int main(void) {
String str = String_from("Hello, world!");
printf("%s\n", str); // Works, since it is NULL terminated.
String_append(&str, "\nfoo bar");
printf("%s\n", str);
String str2 = String_clone(str);
assert(String_equal(str, str2)); // Identical
String_append(&str2, "\nbaz");
assert(!String_equal(str, str2)); // Different, since one of them changed.
String str3 = String_slice(str2, 0, 5);
assert(!strcmp(str3, "Hello")); // You can use strcmp because it is NULL terminated.
// Always be sure to cleanup(aka. free) the strings to prevent memory leaks.
String_cleanup(str);
String_cleanup(str2);
String_cleanup(str3);
}
A typedef of char *
. Used to represent a vector of characters. Is mainly used to represent the underlying buffer of String but can also be used like StringBuffer
in Java, std::stringstream
in C++ or StringSink
in AssemblyScript. Made with c-vector.
A typedef of char const *
. Used to represent a string. It is NULL
terminated to stay compatitible with the C standrd library.
A vector of Strings, just CVECTOR(String)
, mafe with c-vector. Used by String_split
and String_split_by_char
.
A struct describing an immutable fixed-size string.
int len
: The length.char const *str
: The pointer the content.
Together with the functions declared below, there are other functions that operate on StringBuffer that are generated from the CVECTOR_WITH_NAME macro from c-vector.
Creates a StringBuffer from a string literal without allocating on the heap, valid only inside the scope that it is created.
- Arguments:
char const *
: A C string literal.
- Return(StringBuffer)
Pushes a number of bytes one-by-one into the buffer.
- Arguments:
StringBuffer *sb
: The buffer where the bytes will be pushed.char *bytes
: The pointer to the bytes that will be pushed.int n
: Then number of bytes that will br pushed.
- Return(
CVECTOR_STATUS
): Read silent-mode of c-vector.
Pushes a NULL
terminated string into the buffer.
- Arguments:
StringBuffer *sb
: The buffer where the string will be pushed.char *str
: The string that will be pushed.
- Return(
CVECTOR_STATUS
): Read silent-mode of c-vector.
Creates a new empty String.
- Arguments: None
- Return(
String
): The newly created empty String.
Creates a String from a string literal without allocating on the heap, valid only inside the scope that it is created.
- Arguments:
char const *
: A C string literal.
- Return(String)
Creates a new String from a chunk of bytes.
- Arguments:
char *bytes
: The pointer to the bytes that will be used as content.int n
: The number of bytes that wil be used.
- Return(
String
): The new String with the chunk if bytes as content.
Creates a new String from a plain NULL
terminated string.
- Arguments:
char *str
: TheNULL
terminated string.
- Return(
String
): The new String with theNULL
terminated string as content.
- Arguments:
String str
: The string.
- Return(
int
): The length of the string.
Clones a String. Works like how strdup
duplicates a NULL
terminated string.
Concatenates the contents of two Strings into one.
- Arguments:
String str1
: The first string.String str2
: The second string.
- Return(
String
): A new String with the concatenated contents.
Slices a segment of a String. Negative backward indexing is allowed(i.e. index -1
refers to the last item).
- Arguments:
String str
: The String from which the segment will be sliced.int fi
: The start index.int li
: The end index.
- Return(
String
): The sliced segment. Iffi
is smaller thanli
orfi
/li
are smaller than0
even after they are translated to psitive indices or if they are greater or equal than the length of the string,NULL
will be returned.
Hashes a String using the MurmurHash-2 non-cryptographic hashing function.
- Arguments:
String str
: The String that will be hashed.
- Return(
uint32_t
): The hash ofstr
.
Appends a plain NULL
terminated string at the end.
- Arguments:
String *str
: The pointer to the String variable. It will probably reallocate the string to a bigger memory location, so it needs to reassign the variable too.char *bstr
: TheNULL
terminated string that will be appended.
- Return(
CVECTOR_STATUS
): Read silent-mode of c-vector. On verbose-mode, it will return1
if pushing any of the character fails, otherwise1
.
Compares two Strings.
- Arguments:
String str1
,String str2
: The strings that will be compared.
- Return(
bool
):true
if both Strings are equal in length and content, otherwisefalse
.
Frees the String from memory.
- Arguments:
String str
: The string that will be freed.
- Return(
void
)
Creates and returns the fixed version of a String
- Arguments:
String str
: The source.
- Return(
FixedString
): The fixed string with the contents of the first argument.
Splits a String by a character sequence.
- Arguments:
String str
: The string that will be splitted.char const *seq
: The character sequence.
- Return(
Vector_String
): The new strings that resulted from the split.
Like String_split
, but splits by a character instead of a sequence.
- Arguments:
String str
: The string that will be splitted.char c
: The character.
- Return(
Vector_String
): The new strings that resulted from the split.
Removes the leading \t
,
, \n
and \0
characters from the start of a String.
- Arguments:
String str
: The String that will be operated on.
- Return(
void
)
Works like String_trim_start but operates at the end of the String.
- Arguments:
String str
: The String that will be operated on.
- Return(
void
)
Applies String_trim_start String_trim_end on the String.
- Arguments:
String str
: The String that will be operated on.
- Return(
void
)
Creates and returns a new String with the current collected bytes of the StringBuffer as content.
- Arguments:
StringBuffer sb
: The StringBuffer that holds the contents.
- Return(
String
): The newly created String.
Modifies the StringBuffer and transforms it into a String. Is useful when you are done writing to the StringBuffer and now you want a String with that content, but you want to prevent memory allocations.
- Arguments:
StringBuffer sb
: The StringBuffer that will be transformed.
- Return(
String
): Essentiallysb
.
Creates a FixedString from a plain static string literal.
- Arguments:
char *str
: The string literal.
- Return(
FixedString
): The newly created FixedString.
free()
s the str
field if it isn't NULL
and zeroes all the fields of the struct.
- Arguments:
FixedString *fstr
: The FixedString that will be operated on.
- Return(
void
)