-
Notifications
You must be signed in to change notification settings - Fork 21
/
str.h
151 lines (119 loc) · 4.3 KB
/
str.h
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
#pragma once
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
// string type ----------------------------------------------------------------------------
typedef struct
{
const char* ptr;
size_t info;
} str;
// NULL string
#define str_null ((str){ 0 })
// string properties ----------------------------------------------------------------------
// length of the string
static inline
size_t str_len(const str s) { return s.info >> 1; }
// pointer to the string
static inline
const char* str_ptr(const str s) { return s.ptr ? s.ptr : ""; }
// end of string pointer
static inline
const char* str_end(const str s) { return str_ptr(s) + str_len(s); }
// test if the string is empty
static inline
bool str_is_empty(const str s) { return str_len(s) == 0; }
// test if the string is allocated on the heap
static inline
bool str_is_owner(const str s) { return (s.info & 1) != 0; }
// test if the string is a reference
static inline
bool str_is_ref(const str s) { return !str_is_owner(s); }
// string memory control -------------------------------------------------------------------
// free memory allocated for the string
void str_free(const str s);
// string movements -----------------------------------------------------------------------
// free target string, then assign the new value to it
static inline
void str_assign(str* const ps, const str s) { str_free(*ps); *ps = s; }
// move the string, resetting the source to str_null
static inline
str str_move(str* const ps) { const str t = *ps; *ps = str_null; return t; }
// string helpers --------------------------------------------------------------------------
// reset the string to str_null
static inline
void str_clear(str* const ps) { str_assign(ps, str_null); }
// compare two strings lexicographically
int str_cmp(const str s1, const str s2);
// test if two strings match
static inline
bool str_eq(const str s1, const str s2) { return str_cmp(s1, s2) == 0; }
// concatenate strings
void str_cat_range(str* const dest, const str* const src, const size_t n);
// concatenate string arguments
#define str_cat(dest, ...) \
do { \
const str args[] = { __VA_ARGS__ }; \
str_cat_range((dest), args, sizeof(args)/sizeof(args[0])); \
} while(0)
// join strings around the separator
void str_join_range(str* const dest, const str sep, const str* const src, const size_t n);
// join string arguments around the separator
#define str_join(dest, sep, ...) \
do { \
const str args[] = { __VA_ARGS__ }; \
str_join_range((dest), (sep), args, sizeof(args)/sizeof(args[0])); \
} while(0)
// join strings around the separator ignoring empty ones
void str_join_range_ignore_empty(str* const dest, const str sep, const str* const src, const size_t n);
// join string arguments around the separator ignoring empty ones
#define str_join_ignore_empty(dest, sep, ...) \
do { \
const str args[] = { __VA_ARGS__ }; \
str_join_range_ignore_empty((dest), (sep), args, sizeof(args)/sizeof(args[0])); \
} while(0)
// constructors ----------------------------------------------------------------------------
// helper macros
#define _ref_info(n) ((n) << 1)
#define _owner_info(n) (_ref_info(n) | 1)
// string reference from a string literal
#define str_lit(s) ((str){ "" s, _ref_info(sizeof(s) - 1) })
// make a copy of the given string
str str_dup(const str s);
static inline
str _str_ref(const str s) { return (str){ s.ptr, s.info & ~(size_t)1 }; }
// create a reference to the given range of chars
static inline
str str_ref_range(const char* const s, const size_t n)
{
return (s && n > 0) ? ((str){ s, _ref_info(n) }) : str_null;
}
static inline
str _str_ref_form_ptr(const char* const s)
{
return s ? str_ref_range(s, strlen(s)) : str_null;
}
// string reference from anything
#define str_ref(s) _Generic((s), \
str: _str_ref, \
char*: _str_ref_form_ptr \
)(s)
// take ownership of the given range of bytes; totally unsafe, use at your own risk.
static inline
str str_acquire_range(const char* const s, size_t n)
{
// take ownership even if the string is empty, because its memory is still allocated
return s ? ((str){ s, _owner_info(n) }) : str_null;
}
// take ownership of the given string; totally unsafe, use at your own risk
static inline
str str_acquire(const char* const s)
{
return s ? str_acquire_range(s, strlen(s)) : str_null;
}
#ifdef __cplusplus
}
#endif