ao_string_tokenize(3)

🌸 NAME 🌸

Bestie, meet ao_string_tokenize from the Programmer's Manual β€” the ao_string_tokenize(3) page, specifically. It does exactly what it says on the tin: ao_string_tokenize β€” tokenize an input string. ✨


✨ SYNOPSIS ✨

#include <your-opts.h>
cc [...] -o outfile infile.c -lopts [...]

token_list_t * ao_string_tokenize(char const * string);

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so this function takes one input string and turns it into a list of strings. The list of strings is derived by separating the input based on white space separation. But here's the plot twist: if the input contains either single or double quote characters, then the text after that character up to a matching quote will become the string in the list. It's giving respect the quotes energy. πŸ’…

The returned pointer should be deallocated with free(3C) when you're done using the data. The data are placed in a single block of allocated memory. Do not deallocate individual token/strings. Literally one free(3C) for the whole situation, bestie. 🫢

The structure pointed to will contain at least these two fields:

  • tkn_ct β€” The number of tokens found in the input string.
  • tok_list β€” An array of tkn_ct + 1 pointers to substring tokens, with the last pointer set to NULL.

There are two types of quoted strings: single quoted (') and double quoted ("). Singly quoted strings are fairly raw in that escape characters (\) are simply another character, except when preceding the following characters:

\  double backslashes reduce to one
'   incorporates the single quote into the string
0fP  suppresses both the backslash and newline character

Double quote strings are formed according to the rules of string constants in ANSI-C programs. 🌈

string β€” the string to be tokenized.

πŸ”₯ RETURN VALUE πŸ”₯

Pointer to a structure that lists each token. Yep, that's the whole vibe. πŸ‘‘

πŸ’€ ERRORS πŸ’€

NULL is returned and errno will be set to indicate the problem:

  • EINVAL β€” There was an unterminated quoted string.
  • ENOENT β€” The input string was empty.
  • ENOMEM β€” There is not enough memory. @end itemize
Yes, that stray @end itemize is literally in the original ERRORS section, so it stays. No cap. 😭

πŸŽ€ EXAMPLES πŸŽ€

#include <stdlib.h>
int ix;
token_list_t * ptl = ao_string_tokenize(some_string)
for (ix = 0; ix < ptl->tkn_ct; ix++)
do_something_with_tkn(ptl->tkn_list[ix]);
free(ptl);

Note that everything is freed with the one call to free(3C). Obsessed with a single cleanup call, honestly. 🧚

🌟 SEE ALSO 🌟

The info documentation for the -lopts library.

Source/version: the ao_string_tokenize(3) page in the Programmer's Manual; dated 2018-08-26. Mwah πŸ’‹βœ¨ β€” Periodt.