Loading...
Searching...
No Matches
str_util.h
1/*
2 * Copyright (c) 2014-2018 Cesanta Software Limited
3 * All rights reserved
4 *
5 * Licensed under the Apache License, Version 2.0 (the ""License"");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an ""AS IS"" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef CS_COMMON_STR_UTIL_H_
19#define CS_COMMON_STR_UTIL_H_
20
21#include <stdarg.h>
22#include <stdlib.h>
23
24#include "mg_str.h"
25#include "platform.h"
26
27#ifndef CS_ENABLE_STRDUP
28#define CS_ENABLE_STRDUP 0
29#endif
30
31#ifndef CS_ENABLE_TO64
32#define CS_ENABLE_TO64 0
33#endif
34
35/*
36 * Expands to a string representation of its argument: e.g.
37 * `CS_STRINGIFY_LIT(5) expands to "5"`
38 */
39#if !defined(_MSC_VER) || _MSC_VER >= 1900
40#define CS_STRINGIFY_LIT(...) #__VA_ARGS__
41#else
42#define CS_STRINGIFY_LIT(x) #x
43#endif
44
45/*
46 * Expands to a string representation of its argument, which is allowed
47 * to be a macro: e.g.
48 *
49 * #define FOO 123
50 * CS_STRINGIFY_MACRO(FOO)
51 *
52 * expands to 123.
53 */
54#define CS_STRINGIFY_MACRO(x) CS_STRINGIFY_LIT(x)
55
56#ifdef __cplusplus
57extern "C" {
58#endif
59
60/*
61 * Equivalent of standard `strnlen()`.
62 */
63size_t c_strnlen(const char* s, size_t maxlen);
64
65/*
66 * Equivalent of standard `snprintf()`.
67 */
68int c_snprintf(char* buf, size_t buf_size, const char* format, ...) PRINTF_LIKE(3, 4);
69
70/*
71 * Equivalent of standard `vsnprintf()`.
72 */
73int c_vsnprintf(char* buf, size_t buf_size, const char* format, va_list ap);
74
75/*
76 * Find the first occurrence of find in s, where the search is limited to the
77 * first slen characters of s.
78 */
79const char* c_strnstr(const char* s, const char* find, size_t slen);
80
81/*
82 * Stringify binary data. Output buffer size must be 2 * size_of_input + 1
83 * because each byte of input takes 2 bytes in string representation
84 * plus 1 byte for the terminating \0 character.
85 */
86void cs_to_hex(char* to, const unsigned char* p, size_t len);
87
88/*
89 * Convert stringified binary data back to binary.
90 * Does the reverse of `cs_to_hex()`.
91 */
92void cs_from_hex(char* to, const char* p, size_t len);
93
94#if CS_ENABLE_STRDUP
95/*
96 * Equivalent of standard `strdup()`, defined if only `CS_ENABLE_STRDUP` is 1.
97 */
98char* strdup(const char* src);
99#endif
100
101#if CS_ENABLE_TO64
102#include <stdint.h>
103/*
104 * Simple string -> int64 conversion routine.
105 */
106int64_t cs_to64(const char* s);
107#endif
108
109/*
110 * Cross-platform version of `strncasecmp()`.
111 */
112int mg_ncasecmp(const char* s1, const char* s2, size_t len);
113
114/*
115 * Cross-platform version of `strcasecmp()`.
116 */
117int mg_casecmp(const char* s1, const char* s2);
118
119/*
120 * Prints message to the buffer. If the buffer is large enough to hold the
121 * message, it returns buffer. If buffer is to small, it allocates a large
122 * enough buffer on heap and returns allocated buffer.
123 * This is a supposed use case:
124 *
125 * ```c
126 * char buf[5], *p = buf;
127 * mg_avprintf(&p, sizeof(buf), "%s", "hi there");
128 * use_p_somehow(p);
129 * if (p != buf) {
130 * free(p);
131 * }
132 * ```
133 *
134 * The purpose of this is to avoid malloc-ing if generated strings are small.
135 */
136int mg_asprintf(char** buf, size_t size, const char* fmt, ...) PRINTF_LIKE(3, 4);
137
138/* Same as mg_asprintf, but takes varargs list. */
139int mg_avprintf(char** buf, size_t size, const char* fmt, va_list ap);
140
141/*
142 * A helper function for traversing a comma separated list of values.
143 * It returns a list pointer shifted to the next value or NULL if the end
144 * of the list found.
145 * The value is stored in a val vector. If the value has a form "x=y", then
146 * eq_val vector is initialised to point to the "y" part, and val vector length
147 * is adjusted to point only to "x".
148 * If the list is just a comma separated list of entries, like "aa,bb,cc" then
149 * `eq_val` will contain zero-length string.
150 *
151 * The purpose of this function is to parse comma separated string without
152 * any copying/memory allocation.
153 */
154const char* mg_next_comma_list_entry(const char* list, struct mg_str* val, struct mg_str* eq_val);
155
156/*
157 * Like `mg_next_comma_list_entry()`, but takes `list` as `struct mg_str`.
158 * NB: Test return value's .p, not .len. On last itreation that yields result
159 * .len will be 0 but .p will not. When finished, .p will be NULL.
160 */
161struct mg_str
162 mg_next_comma_list_entry_n(struct mg_str list, struct mg_str* val, struct mg_str* eq_val);
163
164/*
165 * Matches 0-terminated string (mg_match_prefix) or string with given length
166 * mg_match_prefix_n against a glob pattern. Glob syntax:
167 * ```
168 * - * matches zero or more characters until a slash character /
169 * - ** matches zero or more characters
170 * - ? Matches exactly one character which is not a slash /
171 * - | or , divides alternative patterns
172 * - any other character matches itself
173 * ```
174 * Match is case-insensitive. Return number of bytes matched.
175 * Examples:
176 * ```
177 * mg_match_prefix("a*f", len, "abcdefgh") == 6
178 * mg_match_prefix("a*f", len, "abcdexgh") == 0
179 * mg_match_prefix("a*f|de*,xy", len, "defgh") == 5
180 * mg_match_prefix("?*", len, "abc") == 3
181 * mg_match_prefix("?*", len, "") == 0
182 * ```
183 */
184size_t mg_match_prefix(const char* pattern, int pattern_len, const char* str);
185
186/*
187 * Like `mg_match_prefix()`, but takes `pattern` and `str` as `struct mg_str`.
188 */
189size_t mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str);
190
191#ifdef __cplusplus
192}
193#endif
194
195#endif /* CS_COMMON_STR_UTIL_H_ */
Definition mg_str.h:28