string
Utilities for null-terminated byte string (NTBS) manipulation. Plus memcpy, because those C guys are weird.
atoi
pub fn atoi(char* str)
Parse the passed NTBS as a decimal (base-10) integer. Overflow is silently ignored. If str is not a pointer to a NTBS containing only characters '0' though '9' with an optional leading '+' or '-', results are undefined.
isdigit
pub fn isdigit(char c)
is the passed character a decimal digit?
isspace
pub fn isspace(char c)
is the passed character whitespace?
isxdigit
pub fn isxdigit(char c)
is the passed character a hexidecimal digit?
itoa
pub fn itoa(int i)
Format the passed int into a new NTBS, and return it.
memcpy
pub fn memcpy(void* dest, void* src, int count)
copy bytes between non-overlapping memory regions.
strchr
pub fn strchr(char* str, char ch)
I return a pointer to the first occurrence of ch in str, or null if no occurrence was found. The terminating null is considered part of str.
strclone
pub fn strclone(char* src)
clone the passed string into a new allocation.
strcmp
pub fn strcmp(char* lhs, char* rhs)
I compare two null-terminated byte strings and return a negative number if lhs sorts lexicographically first, a positive number if rhs is first, and zero if they are equal.
strhash
pub fn strhash(char* str)
I return an int hash value for the passed string. The hash of both the empty string and the null pointer is zero. I'm not a good hash function, just a good-enough default.
strjoin
pub fn strjoin(ArrayList* parts, char* sep)
I take each char* element of parts and concatenate them together with sep between, returning the newly allocated string. I am the inverse of strsplit. This program prints abc:
ArrayList* parts = ArrayList__new(2);
parts.push("a"); parts.push("c");
char* s = strjoin(parts, "b");
puts(s);
free(s); parts.drop();
strlen
pub fn strlen(char* str)
I return the length of the passed string, not including the terminating null byte.
strsplit
pub fn strsplit(char* str, char* sep)
I return an ArrayList containing owned substrings from str, delimited by sep. If sep is the empty string, panic. I am the inverse of strjoin. This program prints 2: '', 'b'.
ArrayList* parts = strsplit("ab", "a");
printf("%d: '%s', '%s'\n",
parts.size(),
parts.get(0),
parts.get(1));
parts.drop();
strsplit_after
pub fn strsplit_after(char* str, char* sep)
I return an ArrayList containing owned substrings from str, each ending with sep, except the last. If sep is the empty string, panic. This program prints 2: 'a', 'b'.
ArrayList* parts = strsplit_after("ab", "a");
printf("%d: '%s', '%s'\n",
parts.size(),
parts.get(0),
parts.get(1));
parts.drop();
strstr
pub fn strstr(char* str, char* substr)
I return a pointer to the first instance of substr within str, or null if no occurrence was found. If substr is the empty string, str is returned.
substr
pub fn substr(char* str, int start, int end)
I create a new null-terminated byte string from the given half-open range of the passed str. No bounds checking is performed.