-
Notifications
You must be signed in to change notification settings - Fork 0
Text Documentation
- these functions don't allocate memory.
- the user must ensure that the buffer passed is big enough for the required operation to be performed (for instance, appending text)
- all strings must be null terminated.
- these functions won't check if the text is a valid utf 8.
size_t letters(char *src);
source code
count letters from src (utf8)
char buf[256] = "𥹷ꌘ";
letters(buf) // -> 2
size_t bytes(char *src);
source code
count bytes used by src including null terminator.
char buf[256] = "𥹷ꌘ";
bytes(buf) // -> 8 (7 from letters + null terminator)
unsigned int letter(char *src, char **next);
source code
get utf8 letter, and set the pointer to the following letter in next. if there are no more letters, next won't be updated and the result will be 0.
char buf[256] = "𥹷ꌘ";
char *next_letter = 0;
unsigned int utf8_letter = letter(buf, &next_letter);
// 4 to store the letter + 1 for null terminator.
char printable_utf8_letter[5] = {0};
memcpy(printable_utf8_letter, &utf8_letter, 4);
printf("letter %s\n", printable_utf8_letter) // -> 𥹷, next_letter points to the beginning of ꌘ
int begins_withn(char *src, char *what, size_t n);
source code
check if src begins with n bytes of what.
char buf[256] = "lorem ipsum dolor sit amet";
begins_withn(buf, "lorem", strlen("lorem")); // -> 1
begins_withn(buf, "ipsum", strlen("ipsum")); // -> 0
int begins_with(char *src, char *what);
source code
check if src begins with what.
char buf[256] = "lorem ipsum dolor sit amet";
begins_with(buf, "lorem"); // -> 1
begins_with(buf, "ipsum"); // -> 0
int ends_withn(char *src, char *what, size_t n);
source code
check if src ends with n bytes of what.
char buf[256] = "lorem ipsum dolor sit amet";
ends_withn(buf, "amet", strlen("amet")); // -> 1
ends_withn(buf, "ipsum", strlen("ipsum")); // -> 0
int ends_with(char *src, char *what);
source code
check if src ends with what.
char buf[256] = "lorem ipsum dolor sit amet";
ends_with(buf, "amet"); // -> 1
ends_with(buf, "ipsum"); // -> 0
int contains(char *src, char *what);
source code
check if src contains what.
char buf[256] = "lorem ipsum dolor sit amet";
contains(buf, "dolor"); // -> 1
contains(buf, "foo"); // -> 0
char *find(char *src, char *what);
source code
find what in src. null is returned if no match is found.
char buf[256] = "lorem ipsum dolor sit amet";
find(buf, "dolor"); // -> "dolor sit..."
find(buf, "foo"); // -> 0
int find_index(char *src, char *what);
source code
same as find but instead of returning a pointer, it returns an index (starting from 0) if no match, -1 is returned.
char buf[256] = "lorem ipsum dolor sit amet";
find_index(buf, "ipsum"); // -> 6
find_index(buf, "foo"); // -> -1
char *find_last(char *src, char *what);
source code
find last match of what in src. null is returned if no match is found.
char buf[256] = "lorem ipsum lorem dolor sit amet";
find_last(buf, "lorem"); // -> "lorem dolor ..."
find_last(buf, "foo"); // -> 0
int find_last_index(char *src, char *what);
source code
same as find_last but returns an index starting from 0. if no match, -1 is returned.
char buf[256] = "lorem ipsum lorem dolor sit amet";
find_last_index(buf, "lorem"); // -> 13
find_last_index(buf, "foo"); // -> -1
char *from(char *src, int n);
source code
get pointer from src + n. n can be negative, for example, "lorem", -3, a pointer to "rem" will be returned.
char buf[256] = "lorem ipsum dolor sit amet";
from(buf, 6); // -> "ipsum dolor ..."
from(buf, -4); // -> "amet"
void insertn(char *src, char *what, size_t max, int n);
source code
insert max bytes of what into src at index n.
char buf[256] = "lorem dolor sit amet";
insertn(buf, "ipsum ", strlen("ipsum "), find_index(buf, "dolor")); // -> "lorem ipsum dolor ..."
void insert(char *src, char *what, int n);
source code
insert what into src starting at index n.
char buf[256] = "lorem dolor sit amet";
insert(buf, "ipsum ", find_index(buf, "dolor")); // -> "lorem ipsum dolor ..."
void erase_bytes(char *src, int n, size_t bytes);
source code
erase bytes amount from src at index n.
char buf[256] = "lorem ipsum dolor sit amet";
erase_bytes(buf, 0, 6); // -> "ipsum dolor ..."
void erase(char *src, char *what);
source code
erase what from src.
char buf[256] = "lorem ipsum lorem dolor sit amet";
erase(buf, "lorem "); // -> "ipsum dolor sit amet"
void erase_first(char *src, char *what);
source code
erase only the first match of what from src.
char buf[256] = "lorem ipsum lorem dolor sit amet";
erase_first(buf, "lorem "); // -> "ipsum lorem dolor..."
void erase_last(char *src, char *what);
source code
erase only last match of what from src.
char buf[256] = "lorem ipsum lorem dolor sit amet";
erase_last(buf, "lorem "); // -> "lorem ipsum dolor..."
void replace(char *src, char *original, char *replacement);
source code
replace original from src with replacement.
char buf[256] = "lorem ipsum lorem dolor sit amet";
replace(buf, "lorem", "foo"); // -> "foo ipsum foo dolor..."
void replace_first(char *src, char *original, char *replacement);
source code
replace only first match of original with replacement from src.
char buf[256] = "lorem ipsum lorem dolor sit amet";
replace_first(buf, "lorem", "foo"); // -> "foo ipsum lorem dolor..."
void replace_last(char *src, char *original, char *replacement);
source code
replace only last match of original with replacement from src.
char buf[256] = "lorem ipsum lorem dolor sit amet";
replace_last(buf, "lorem", "foo"); // -> "lorem ipsum foo dolor..."
int empty(char *src);
source code
check if src is an empty string.
empty(""); // -> 1
empty("foo"); // -> 0
void appendn(char *src, char *what, size_t n);
source code
append n bytes from what to src.
char buf[256] = "lorem ipsum";
appendn(buf, " foo", strlen(" foo")); // -> "lorem ipsum foo"
appendn(buf, ".!", 1); // "lorem ipsum foo.", notice no "!"
void append(char *src, char *what);
source code
append what to src.
char buf[256] = "lorem ipsum";
append(buf, " foo"); // -> "lorem ipsum foo"
void prependn(char *src, char *what, size_t n);
source code
insert n bytes from what at the beginning of src.
char buf[256] = "lorem ipsum";
prependn(buf, "foo ", strlen("foo ")); // -> "foo lorem ipsum"
prependn(buf, "!!", 1); // -> "!foo lorem ipsum"
void prepend(char *src, char *what);
source code
insert what at the beginning of src.
char buf[256] = "lorem ipsum";
prepend(buf, "foo "); // -> "foo lorem ipsum"
void trim(char *src);
source code
remove all the white space at the beginning and the end from src.
char buf[256] = " lorem ipsum ";
trim(buf); // -> "lorem ipsum"
void substr(char *src, int n, size_t max);
source code
update src to be max bytes starting at index n.
char buf[256] = "lorem ipsum";
substr(buf, 0, 5); // -> "lorem"
void repeat(char *src, char *what, int n, size_t times);
source code
repeat what times to src starting at index n.
char buf[256] = "lorem ipsum";
repeat(buf, "foo ", 0, 3); // -> "foo foo foo lorem ..."