Loading...
Searching...
No Matches
mg_str.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_MG_STR_H_
19#define CS_COMMON_MG_STR_H_
20
21#include <stddef.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/* Describes chunk of memory */
28struct mg_str {
29 const char *p; /* Memory chunk pointer */
30 size_t len; /* Memory chunk length */
31};
32
33/*
34 * Helper function for creating mg_str struct from plain C string.
35 * `NULL` is allowed and becomes `{NULL, 0}`.
36 */
37struct mg_str mg_mk_str(const char *s);
38
39/*
40 * Like `mg_mk_str`, but takes string length explicitly.
41 */
42struct mg_str mg_mk_str_n(const char *s, size_t len);
43
44/* Macro for initializing mg_str. */
45#define MG_MK_STR(str_literal) \
46 { str_literal, sizeof(str_literal) - 1 }
47#define MG_MK_STR_N(str_literal, len) \
48 { str_literal, len }
49#define MG_NULL_STR \
50 { NULL, 0 }
51
52/*
53 * Cross-platform version of `strcmp()` where where first string is
54 * specified by `struct mg_str`.
55 */
56int mg_vcmp(const struct mg_str *str2, const char *str1);
57
58/*
59 * Cross-platform version of `strncasecmp()` where first string is
60 * specified by `struct mg_str`.
61 */
62int mg_vcasecmp(const struct mg_str *str2, const char *str1);
63
64/* Creates a copy of s (heap-allocated). */
65struct mg_str mg_strdup(const struct mg_str s);
66
67/*
68 * Creates a copy of s (heap-allocated).
69 * Resulting string is NUL-terminated (but NUL is not included in len).
70 */
71struct mg_str mg_strdup_nul(const struct mg_str s);
72
73/*
74 * Locates character in a string.
75 */
76const char *mg_strchr(const struct mg_str s, int c);
77
78/*
79 * Compare two `mg_str`s; return value is the same as `strcmp`.
80 */
81int mg_strcmp(const struct mg_str str1, const struct mg_str str2);
82
83/*
84 * Like `mg_strcmp`, but compares at most `n` characters.
85 */
86int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n);
87
88/*
89 * Compare two `mg_str`s ignoreing case; return value is the same as `strcmp`.
90 */
91int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2);
92
93/*
94 * Free the string (assuming it was heap allocated).
95 */
96void mg_strfree(struct mg_str *s);
97
98/*
99 * Finds the first occurrence of a substring `needle` in the `haystack`.
100 */
101const char *mg_strstr(const struct mg_str haystack, const struct mg_str needle);
102
103/* Strip whitespace at the start and the end of s */
104struct mg_str mg_strstrip(struct mg_str s);
105
106/* Returns 1 if s starts with the given prefix. */
107int mg_str_starts_with(struct mg_str s, struct mg_str prefix);
108
109#ifdef __cplusplus
110}
111#endif
112
113#endif /* CS_COMMON_MG_STR_H_ */
Definition mg_str.h:28