xref: /curl/docs/internals/STRPARSE.md (revision d5c738c6)
1<!--
2Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3
4SPDX-License-Identifier: curl
5-->
6
7# String parsing with `strparse`
8
9The functions take input via a pointer to a pointer, which allows the
10functions to advance the pointer on success which then by extension allows
11"chaining" of functions like this example that gets a word, a space and then a
12second word:
13
14~~~c
15if(Curl_str_word(&line, &word1, MAX) ||
16   Curl_str_singlespace(&line) ||
17   Curl_str_word(&line, &word2, MAX))
18  fprintf(stderr, "ERROR\n");
19~~~
20
21## Strings
22
23The functions that return string information does so by populating a
24`struct Curl_str`:
25
26~~~c
27struct Curl_str {
28  char *str;
29  size_t len;
30};
31~~~
32
33## `Curl_str_word`
34
35~~~c
36int Curl_str_word(char **linep, struct Curl_str *out, const size_t max);
37~~~
38
39Get a sequence of bytes until the first space or the end of the string. Return
40non-zero on error. There is no way to include a space in the word, no sort of
41escaping. The word must be at least one byte, otherwise it is considered an
42error.
43
44`max` is the longest accepted word, or it returns error.
45
46On a successful return, `linep` is updated to point to the byte immediately
47following the parsed word.
48
49## `Curl_str_until`
50
51~~~c
52int Curl_str_until(char **linep, struct Curl_str *out, const size_t max,
53                   char delim);
54~~~
55
56Like `Curl_str_word` but instead of parsing to space, it parses to a given
57custom delimiter non-zero byte `delim`.
58
59`max` is the longest accepted word, or it returns error.
60
61The parsed word must be at least one byte, otherwise it is considered an
62error.
63
64## `Curl_str_quotedword`
65
66~~~c
67int Curl_str_quotedword(char **linep, struct Curl_str *out, const size_t max);
68~~~
69
70Get a "quoted" word. This means everything that is provided within a leading
71and an ending double character. No escaping possible.
72
73`max` is the longest accepted word, or it returns error.
74
75The parsed word must be at least one byte, otherwise it is considered an
76error.
77
78## `Curl_str_single`
79
80~~~c
81int Curl_str_single(char **linep, char byte);
82~~~
83
84Advance over a single character provided in `byte`. Return non-zero on error.
85
86## `Curl_str_singlespace`
87
88~~~c
89int Curl_str_singlespace(char **linep);
90~~~
91
92Advance over a single ASCII space. Return non-zero on error.
93
94## `Curl_str_number`
95
96~~~c
97int Curl_str_number(char **linep, size_t *nump, size_t max);
98~~~
99
100Get an unsigned decimal number. Leading zeroes are just swallowed. Return
101non-zero on error.
102
103## `Curl_str_newline`
104
105~~~c
106int Curl_str_newline(char **linep);
107~~~
108
109Check for a single CR or LF. Return non-zero on error */
110