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
|
/**
* \file strutil.h
* A collection of string utilities.
*/
#ifndef STRUTIL_H
#define STRUTIL_H
#include "dynstr.h"
#include <string.h>
/**
* Allocate a string initialized with the len first bytes of src.
* Similar to strndup but it doesn't check for '\0' in src.
* Returns NULL in case of allocation errors.
* @param src string to copy len bytes from, may not be NULL
* @param len bytes to copy from src
* @return newly allocate null-terminated string
*/
MALLOC NONULL char* strdup_len(const char* src, size_t len);
/**
* Unescape (remove backslashes from) a null-terminated string inline.
* Does not have any special handling for sequences like \\n, \\t or \\0.
* @param str null-terminated string to remove backslashes from, may not be NULL
*/
NONULL void unescape(char* str);
/**
* Escape all listed characters if they appear in string with backslash.
* Also escapes backslash.
* Returns NULL in case of allocation errors.
* @param str null-terminated string to escape characters in if needed,
* may not be NULL
* @param chars null-terminated string containing all characters that need to
* be escaped, may not be NULL
* @return newly allocated null-terminated copy of string with
* characters escaped
*/
MALLOC NONULL char* escape(const char* str, const char* chars);
/**
* Escape all listed characters if they appear in the dynamic string with
* backslash inline. Also escapes backslash.
* @param str dynamic string to escape characters in if needed,
* may not be NULL
* @param chars null-terminated string containing all characters that need to
* be escaped, may not be NULL
* @return false in case of allocation errors
*/
NONULL bool dynstr_escape(dynstr_t* str, const char* chars);
/**
* Split string by delimiter.
* Returned list will always contain at least one item and will be
* null-terminated.
* Returns NULL in case of allocation errors.
* @param start start of string to split by delimiter
* @param end end of string to split by delimiter
* @param delim delimiter to use
* @return null-terminated list of strings or NULL
*/
NONULL char** split_len(const char* start, const char* end, char delim);
/**
* Split string by delimiter.
* Returned list will always contain at least one item and will be
* null-terminated.
* Returns NULL in case of allocation errors.
* @param str string to split by delimiter
* @param delim delimiter to use
* @return null-terminated list of strings or NULL
*/
NONULL static inline char** split(const char* str, char delim)
{
assert(str);
return split_len(str, str + strlen(str), delim);
}
#endif /* STRUTIL_H */
|