1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
<!--
Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
SPDX-License-Identifier: curl
-->
# String parsing with `strparse`
The functions take input via a pointer to a pointer, which allows the
functions to advance the pointer on success which then by extension allows
"chaining" of functions like this example that gets a word, a space and then a
second word:
~~~c
if(curlx_str_word(&line, &word1, MAX) ||
curlx_str_singlespace(&line) ||
curlx_str_word(&line, &word2, MAX))
fprintf(stderr, "ERROR\n");
~~~
The input pointer **must** point to a null-terminated buffer area or these
functions risk continuing "off the edge".
## Strings
The functions that return string information does so by populating a
`struct Curl_str`:
~~~c
struct Curl_str {
char *str;
size_t len;
};
~~~
Access the struct fields with `curlx_str()` for the pointer and `curlx_strlen()`
for the length rather than using the struct fields directly.
## `curlx_str_init`
~~~c
void curlx_str_init(struct Curl_str *out)
~~~
This initiates a string struct. The parser functions that store info in
strings always init the string themselves, so this stand-alone use is often
not necessary.
## `curlx_str_assign`
~~~c
void curlx_str_assign(struct Curl_str *out, const char *str, size_t len)
~~~
Set a pointer and associated length in the string struct.
## `curlx_str_word`
~~~c
int curlx_str_word(char **linep, struct Curl_str *out, const size_t max);
~~~
Get a sequence of bytes until the first space or the end of the string. Return
non-zero on error. There is no way to include a space in the word, no sort of
escaping. The word must be at least one byte, otherwise it is considered an
error.
`max` is the longest accepted word, or it returns error.
On a successful return, `linep` is updated to point to the byte immediately
following the parsed word.
## `curlx_str_until`
~~~c
int curlx_str_until(char **linep, struct Curl_str *out, const size_t max,
char delim);
~~~
Like `curlx_str_word` but instead of parsing to space, it parses to a given
custom delimiter non-zero byte `delim`.
`max` is the longest accepted word, or it returns error.
The parsed word must be at least one byte, otherwise it is considered an
error.
## `curlx_str_untilnl`
~~~c
int curlx_str_untilnl(char **linep, struct Curl_str *out, const size_t max);
~~~
Like `curlx_str_untilnl` but instead parses until it finds a "newline byte".
That means either a CR (ASCII 13) or an LF (ASCII 10) octet.
`max` is the longest accepted word, or it returns error.
The parsed word must be at least one byte, otherwise it is considered an
error.
## `curlx_str_cspn`
~~~c
int curlx_str_cspn(const char **linep, struct Curl_str *out, const char *cspn);
~~~
Get a sequence of characters until one of the bytes in the `cspn` string
matches. Similar to the `strcspn` function.
## `curlx_str_quotedword`
~~~c
int curlx_str_quotedword(char **linep, struct Curl_str *out, const size_t max);
~~~
Get a "quoted" word. This means everything that is provided within a leading
and an ending double quote character. No escaping possible.
`max` is the longest accepted word, or it returns error.
The parsed word must be at least one byte, otherwise it is considered an
error.
## `curlx_str_single`
~~~c
int curlx_str_single(char **linep, char byte);
~~~
Advance over a single character provided in `byte`. Return non-zero on error.
## `curlx_str_singlespace`
~~~c
int curlx_str_singlespace(char **linep);
~~~
Advance over a single ASCII space. Return non-zero on error.
## `curlx_str_passblanks`
~~~c
void curlx_str_passblanks(char **linep);
~~~
Advance over all spaces and tabs.
## `curlx_str_trimblanks`
~~~c
void curlx_str_trimblanks(struct Curl_str *out);
~~~
Trim off blanks (spaces and tabs) from the start and the end of the given
string.
## `curlx_str_number`
~~~c
int curlx_str_number(char **linep, curl_size_t *nump, size_t max);
~~~
Get an unsigned decimal number not larger than `max`. Leading zeroes are
swallowed. Return non-zero on error. Returns error if there was not a single
digit.
## `curlx_str_numblanks`
~~~c
int curlx_str_numblanks(char **linep, curl_size_t *nump);
~~~
Get an unsigned 63-bit decimal number. Leading blanks and zeroes are skipped.
Returns non-zero on error. Returns error if there was not a single digit.
## `curlx_str_hex`
~~~c
int curlx_str_hex(char **linep, curl_size_t *nump, size_t max);
~~~
Get an unsigned hexadecimal number not larger than `max`. Leading zeroes are
swallowed. Return non-zero on error. Returns error if there was not a single
digit. Does *not* handled `0x` prefix.
## `curlx_str_octal`
~~~c
int curlx_str_octal(char **linep, curl_size_t *nump, size_t max);
~~~
Get an unsigned octal number not larger than `max`. Leading zeroes are
swallowed. Return non-zero on error. Returns error if there was not a single
digit.
## `curlx_str_newline`
~~~c
int curlx_str_newline(char **linep);
~~~
Check for a single CR or LF. Return non-zero on error */
## `curlx_str_casecompare`
~~~c
int curlx_str_casecompare(struct Curl_str *str, const char *check);
~~~
Returns true if the provided string in the `str` argument matches the `check`
string case insensitively.
## `curlx_str_cmp`
~~~c
int curlx_str_cmp(struct Curl_str *str, const char *check);
~~~
Returns true if the provided string in the `str` argument matches the `check`
string case sensitively. This is *not* the same return code as `strcmp`.
## `curlx_str_nudge`
~~~c
int curlx_str_nudge(struct Curl_str *str, size_t num);
~~~
Removes `num` bytes from the beginning (left) of the string kept in `str`. If
`num` is larger than the string, it instead returns an error.
|