strutils#
Cross-platform string utility functions.
Typedefs and structs
-
enum StrquoteFlags#
Flags for strquote()
Values:
-
enumerator strquoteInitialBlanks#
Do not skip initial blanks
-
enumerator strquoteNoQuote#
Input is not expected to start and end with double quote
-
enumerator strquoteNoEscape#
Do not escape embedded double quotes
-
enumerator strquoteRaw#
Copy the input without conversions
-
enumerator strquoteInitialBlanks#
-
enum StrCategory#
Character categories, from, RFC 3986
Values:
-
enumerator strcatUpper#
A-Z.
-
enumerator strcatLower#
a-z
-
enumerator strcatDigit#
0-9
-
enumerator strcatUnreserved#
“-._~” (in addition to upper + lower + digit)
-
enumerator strcatSubDelims#
“!$&’()*+,;=”
-
enumerator strcatGenDelims#
“:/?#[]@”
-
enumerator strcatReserved#
strcatSubDelims | strcatGenDelims
-
enumerator strcatPercent#
“%”
-
enumerator strcatCExtra#
“"\<>^{}|” (extra characters in the C standard)
-
enumerator strcatSpace#
“ \f\n\r\t\v”
-
enumerator strcatOther#
anything else, except NUL
-
enumerator strcatNul#
NUL.
-
enumerator strcatUpper#
Print allocated string
-
char *aprintf(const char *fmt, ...)#
A convinient variant of asprintf() that returns the allocated string, or NULL on error.
Functions for writing characters to a buffer
-
int strsetc(char *dest, long size, int c)#
Writes character
cto bufferdestof sizesize. If there is space, the buffer will always be NUL-terminated.Returns always 1 (number of characters written to
dest, or would have been written todestif it had been large enough).
-
int strsets(char *dest, long size, const char *src)#
Copies
srctodest.At most
sizebytes will be written todest. Ifsizeis larger than zero,destwill always be NUL-terminated. No, partly UTF-8 code point will be written to dest.Returns number of bytes written to
destor the number of bytes that would have been written todestif it had been large enough.
-
int strsetn(char *dest, long size, const char *src, int len)#
Like strset(), but copies at most
lenbytes fromsrc.
-
int strput(char **destp, size_t *sizep, size_t pos, const char *src)#
Copies
srcstring to malloc’ed memory pointed to by*destp.The string pointed to by
*destpmay be reallocated. It will always be NUL-terminated.If
sizepis not NULL, the value it points to should be the allocated size of*destp. It will be updated on return.posis the position of*destpthatsrcwill be copied to.Returns number of characters written (excluding terminating NUL). On allocation error, a negative number is returned and
destpandsizepwill not be touched.
-
int strnput(char **destp, size_t *sizep, size_t pos, const char *src, int n)#
Like strput(), but at most
nbytes fromsrcwill be copied. Ifnis negative, all ofsrcwill be copited.
-
int strnput_escape(char **destp, size_t *sizep, size_t pos, const char *src, int len, StrCategory unescaped, const char *escape)#
Like strnput(), but escapes all characters in categories larger than
unescaped, which should be less thanstrcatOther.Escaped characters are written as
escapefollowed by 2-character hex representation of the character (byte) value.Returns -1 on error.
Quoting/unquoting strings
-
int strquote(char *dest, size_t size, const char *s)#
Double-quote input string
sand write it todest.Embedded double-quotes are escaped with backslash. At most size characters are written to
dest(including terminating NUL).Returns number of characters written to
dest(excluding terminating NUL). If the output is truncated, the number of characters which should have been written is returned.
-
int strnquote(char *dest, size_t size, const char *s, int n, StrquoteFlags flags)#
Like strquote(), but reads at most
nbytes froms.
-
int strunquote(char *dest, size_t size, const char *s, int *consumed, StrquoteFlags flags)#
Strip double-quotes from
sand write the result todest.At most
sizecharacters are written todest(including terminating NUL). The inputsmay optionally starts with a sequence of blanks. It should then be followed by a double quote. The scanning stops at the next unescaped double quote.Returns number of characters written to
dest(excluding terminating NUL). If the output is truncated, the number of characters which should have been written is returned.Returns a negative value on error (-1 if the first non-blank character in
sis not a double quote and -2 if no terminating double quote is found).
-
int strnunquote(char *dest, size_t size, const char *s, int n, int *consumed, StrquoteFlags flags)#
Like strunquote, but if
nis non-negative, at mostnbytes are read froms.This mostly make sense in combination when
flags & strquoteNoEscapeis true.
-
int strnput_unquote(char **destp, size_t *sizep, size_t pos, const char *s, int n, int *consumed, StrquoteFlags flags)#
Like strnunquote(), but reallocates the destination and writes to position
pos.On allocation error, -3 is returned.
Hexadecimal encoding/decoding
-
int strhex_encode(char *hex, size_t hexsize, const unsigned char *data, size_t size)#
Writes binary data to hex-encoded string.
hexdestination string. Will be NUL-terminated.hexsizesize of memory poined to byhex(incl. NUL terminator).datapoints to the first byte of binary data of sizesize.sizenumber of bytes to read fromdata.Returns number of bytes one wants to write to
hex(not incl. NUL terminator), or -1 on error.
-
int strhex_decode(unsigned char *data, size_t size, const char *hex, int hexsize)#
Read binary data from hex-encoded string.
datapointer to buffer to write to. No more thansizebytes are written.sizesize ofdatain bytes.hexhex-encoded string to read from.hexsizenumber of bytes to read fromhex. If negative,hexis assumed to be NUL-terminated and the whole string is read.Returns number of bytes written to
data, assumingsizeis sufficiently large, or -1 on error.
Character categorisation
-
StrCategory strcategory(int c)#
Returns the category of character
c.
-
int strcatspn(const char *s, StrCategory cat)#
Returns the length of initial segment of
swhich concists entirely of bytes in categorycat.
-
int strcatcspn(const char *s, StrCategory cat)#
Returns the length of initial segment of
swhich concists entirely of bytes NOT in categorycat.
-
int strcatjspn(const char *s, StrCategory cat)#
Returns the length of initial segment of
swhich concists entirely of bytes in all categories less or equal tocat.
-
int strcatcjspn(const char *s, StrCategory cat)#
Returns the length of initial segment of
swhich concists entirely of bytes NOT in all categories less or equal tocat.
Allocated string list
A string list is an allocated NULL-terminated array of pointers to allocated strings.
-
char **strlst_insert(char **strlst, size_t *n, const char *s, int i)#
Insert string
sbefore positioniin NULL-terminated array of string pointersstrlst.If
iis negative count from the end of the string, like Python. Anyiout of range correspond to appending.nis the allocated length ofstrlst. If neededstrlstwill be reallocated andnupdated.Returns a pointer to the new string list or NULL on allocation error.
-
char **strlst_append(char **strlst, size_t *n, const char *s)#
Appends string
sto NULL-terminated array of string pointersstrlst.nis the allocated length ofstrlst. If neededstrlstwill be reallocated andnupdated.Returns a pointer to the new string list or NULL on allocation error.
-
size_t strlst_count(char **strlst)#
Return number of elements in string list.
-
void strlst_free(char **strlst)#
Free all memory in string list.
-
const char *strlst_get(char **strlst, int i)#
Returns a pointer to element
ithe string list. Like in Python, negativeicounts from the back.The caller gets a borrowed reference to the string. Do not free it.
Returns NULL if
iis out of range.
-
int strlst_remove(char **strlst, int i)#
Remove element
ifrom the string list. Like in Python, negativeicounts from the back.Returns non-zero if
iis out of range.
-
char *strlst_pop(char **strlst, int i)#
Remove and return element
ifrom the string list. Like in Python, negativeicounts from the back.The caller becomes the owner of the returned string and is responsible to free it.
Returns NULL if
iis out of range.
-
int natoi(const char *s, int n)#
A version of atoi() that reads at most
nbytes.
-
int strchk_semver(const char *v)#
Checks if
vpoints to a valid semantic version 2.0.0 number.Returns -1 if
vis not a valid semantic version number. Otherwise, the length of the version number is returned.
-
int strnchk_semver(const char *v, size_t n)#
Check if the initial part of
vis a valid semantic version 2.0.0 number.Only the first
nbytes ofvare checked.Returns the length of the semantic version number or -1 if
vis not a valid semantic version number.
-
int strcmp_semver(const char *v1, const char *v2)#
Compare strings
v1andv2using semantic versioning 2.0.0 order.Returns -1 if v1 < v2 0 if v1 == v2 1 if v1 > v2
See also: https://semver.org/
-
int strncmp_semver(const char *v1, const char *v2, size_t n)#
Like strcmp_version(), but compares only the first
nbytes ofv1andv2.