aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/mv.h228
1 files changed, 228 insertions, 0 deletions
diff --git a/include/mv.h b/include/mv.h
new file mode 100644
index 0000000..c4fb703
--- /dev/null
+++ b/include/mv.h
@@ -0,0 +1,228 @@
1// mv.h --- Memory View -*- C -*-
2//
3// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7// This file is part of COSMOS Codebase.
8//
9// Commentary:
10//
11// Removing deps:
12// - #define MV_NO_UNISTD
13// - #define MV_NO_STDIO
14//
15// Code:
16
17#ifndef INCLUDED_COSMOS_MV_H
18#define INCLUDED_COSMOS_MV_H
19
20#include "types.h"
21
22struct mv {
23 const u8 *beg;
24 const u8 *end;
25};
26
27#define MV_NULL ((struct mv) {0})
28#define MV( lit) mv_from_mb (MB (lit))
29#define MV0(lit) mv_from_mb (MB0 (lit))
30#define MV_FMT(mv) (int) mv_sz (mv), (mv).beg
31
32#define DBG_ASSERT_MV(mv) \
33 DBG_ASSERT_MSG (!(mv).beg == !(mv).end, "Broken memory view invariant!")
34
35enum mv_bit {
36 MV_BIT_BEG, //!< Go from beg (usually implied).
37 MV_BIT_END, //!< Go from end.
38 MV_BIT_INV, //!< Invert the resulting range.
39 MV_BIT_NOT, //!< Not the delimeter.
40 MV_BIT_INC_BEG, //!< Include delimiter in the beginning.
41 MV_BIT_INC_END, //!< Include delimiter in the end.
42 MV_BIT_REP, //!< Repeat action while possible.
43 MV_BIT_CNT
44};
45
46// masks for flg arguments
47#define MV_BEG (1 << MV_BIT_BEG)
48#define MV_END (1 << MV_BIT_END)
49#define MV_OUT (MV_BEG | MV_END)
50#define MV_INV (1 << MV_BIT_INV)
51#define MV_NOT (1 << MV_BIT_NOT)
52#define MV_INC_BEG (1 << MV_BIT_INC_BEG)
53#define MV_INC_END (1 << MV_BIT_INC_END)
54#define MV_INC (MV_INC_BEG | MV_INC_END)
55#define MV_REP (1 << MV_BIT_REP)
56
57usz mv_sz (struct mv);
58int mv_cmp_fun (const void *, const void *); //!< For `qsort` and alike.
59int mv_trim (struct mv *); //!< Same as `mv_strip_any (mv, MV0 (" \t\n\r"), MV_BEG | MV_END | MV_REP)`.
60struct mv mv_basename (struct mv);
61struct mv mv_dirname (struct mv);
62
63// mv_from*
64struct mv mv_from (const void *beg, const void *end);
65struct mv mv_from_mb (struct mb);
66struct mv mv_from_mb0 (struct mb);
67struct mv mv_from_cstr (const char *cstr);
68
69// mv_copy_to*
70void mv_copy_to (struct mv, void *ptr);
71void mv_copy_to_mb (struct mv, struct mb);
72
73// mv_to_* (2 <= base <= 36)
74u64 mv_to_u64 (struct mv, int base); //!< Returns U64_MAX on invalid input.
75i64 mv_to_i64 (struct mv, int base); //!< Returns I64_MIN on invalid input.
76f64 mv_to_f64 (struct mv, int base); //!< Returns INFINITY on invalid input.
77
78// mv_is_*
79int mv_is_null (struct mv); //!< `mv_eq (mv, MV_NULL)` semantics.
80int mv_is_empty (struct mv); //!< `mv_equal (mv_or (mv, MV0 ("")), MV0 (""))` semantics.
81
82// short circuiting
83static inline struct mv mv_or (struct mv lhs, struct mv rhs) { return mv_is_empty (lhs) ? rhs : lhs; }
84static inline struct mv mv_and (struct mv lhs, struct mv rhs) { return !mv_is_empty (lhs) ? rhs : lhs; }
85
86// comparison
87int mv_eq (struct mv, struct mv); //!< Shallow comparison.
88int mv_equal (struct mv, struct mv); //!< Deep comparison.
89int mv_cmp (struct mv, struct mv); //!< `memcmp` like.
90
91// mv_contains*
92int mv_contains (struct mv, struct mv );
93int mv_contains_u8 (struct mv, u8 );
94int mv_contains_u16 (struct mv, u16);
95int mv_contains_u32 (struct mv, u32);
96int mv_contains_u64 (struct mv, u64);
97int mv_contains_any (struct mv, struct mv );
98
99// mv_affixed* (flg: [MV_BEG | MV_END])
100int mv_affixed (struct mv, struct mv, int flg);
101int mv_affixed_u8 (struct mv, u8, int flg);
102int mv_affixed_u16 (struct mv, u16, int flg);
103int mv_affixed_u32 (struct mv, u32, int flg);
104int mv_affixed_u64 (struct mv, u64, int flg);
105int mv_affixed_any (struct mv, struct mv, int flg);
106
107// mv_affixed2*
108int mv_affixed2 (struct mv, struct mv, struct mv );
109int mv_affixed2_u8 (struct mv, u8, u8 );
110int mv_affixed2_u16 (struct mv, u16, u16);
111int mv_affixed2_u32 (struct mv, u32, u32);
112int mv_affixed2_u64 (struct mv, u64, u64);
113int mv_affixed2_any (struct mv, struct mv, struct mv );
114
115// const void *mv_at (struct mv, usz, int flg); // (flg: [MV_BEG] ^ MV_END)
116// /* */ void mv_shrink (struct mv *, usz, int flg); // (flg: [MV_BEG | MV_END])
117
118// mv_find* (flg: [MV_BEG] ^ MV_END)
119struct mv mv_find (struct mv, struct mv, int flg);
120const u8 *mv_find_u8 (struct mv, u8, int flg);
121const u16 *mv_find_u16 (struct mv, u16, int flg);
122const u32 *mv_find_u32 (struct mv, u32, int flg);
123const u64 *mv_find_u64 (struct mv, u64, int flg);
124const u8 *mv_find_any (struct mv, struct mv, int flg);
125
126// mv_strip* (flg: [MV_BEG | MV_END] | MV_REP)
127int mv_strip (struct mv *, struct mv, int flg);
128int mv_strip_n (struct mv *, usz, int flg);
129int mv_strip_u8 (struct mv *, u8, int flg);
130int mv_strip_u16 (struct mv *, u16, int flg);
131int mv_strip_u32 (struct mv *, u32, int flg);
132int mv_strip_u64 (struct mv *, u64, int flg);
133int mv_strip_any (struct mv *, struct mv, int flg);
134
135// mv_strip_upto* (flg: [MV_BEG | MV_END] | MV_REP)
136int mv_strip_upto (struct mv *, struct mv, int flg);
137int mv_strip_upto_u8 (struct mv *, u8, int flg);
138int mv_strip_upto_u16 (struct mv *, u16, int flg);
139int mv_strip_upto_u32 (struct mv *, u32, int flg);
140int mv_strip_upto_u64 (struct mv *, u64, int flg);
141int mv_strip_upto_any (struct mv *, struct mv, int flg);
142
143// mv_peek* (flg: [MV_BEG] ^ MV_END)
144struct mv mv_peek (struct mv, usz, int flg);
145/* */ u8 mv_peek_u8 (struct mv, int flg);
146/* */ u16 mv_peek_u16 (struct mv, int flg);
147/* */ u32 mv_peek_u32 (struct mv, int flg);
148/* */ u64 mv_peek_u64 (struct mv, int flg);
149struct mv mv_peek_cstr (struct mv); //!< Same as `mv_peek_upto_u8 (mv, '\0', 0)`.
150
151// mv_peek_upto* (flg: [MV_BEG] ^ MV_END | MV_INC | MV_INV)
152struct mv mv_peek_upto (struct mv, struct mv, int flg);
153struct mv mv_peek_upto_u8 (struct mv, u8, int flg);
154struct mv mv_peek_upto_u16 (struct mv, u16, int flg);
155struct mv mv_peek_upto_u32 (struct mv, u32, int flg);
156struct mv mv_peek_upto_u64 (struct mv, u64, int flg);
157struct mv mv_peek_upto_any (struct mv, struct mv, int flg);
158
159// mv_peek_in* (flg: [MV_BEG] ^ MV_END | MV_INC)
160struct mv mv_peek_in (struct mv *, struct mv, struct mv, int flg);
161struct mv mv_peek_in_u8 (struct mv *, u8, u8, int flg);
162struct mv mv_peek_in_u16 (struct mv *, u16, u16, int flg);
163struct mv mv_peek_in_u32 (struct mv *, u32, u32, int flg);
164struct mv mv_peek_in_u64 (struct mv *, u64, u64, int flg);
165struct mv mv_peek_in_any (struct mv *, struct mv, struct mv, int flg);
166
167// mv_chop* (flg: [MV_BEG] ^ MV_END | MV_INC_BEG | MV_INC_END | MV_INV)
168struct mv mv_chop (struct mv *, usz, int flg);
169/* */ u8 mv_chop_u8 (struct mv *, int flg);
170/* */ u16 mv_chop_u16 (struct mv *, int flg);
171/* */ u32 mv_chop_u32 (struct mv *, int flg);
172/* */ u64 mv_chop_u64 (struct mv *, int flg);
173struct mv mv_chop_cstr (struct mv *); //!< Same as `mv_chop_upto_u8 (mv, '\0', 0)`.
174
175// TODO(cmmm): find a better name for this mv_exclude.
176// It's a really powerful building block.
177struct mv mv_exclude (struct mv *, struct mv, int flg);
178
179// mv_chop_upto_* (flg: [MV_BEG] ^ MV_END | MV_INC_BEG | MV_INC_END)
180struct mv mv_chop_upto (struct mv *, struct mv, int flg);
181struct mv mv_chop_upto_u8 (struct mv *, u8, int flg);
182struct mv mv_chop_upto_u16 (struct mv *, u16, int flg);
183struct mv mv_chop_upto_u32 (struct mv *, u32, int flg);
184struct mv mv_chop_upto_u64 (struct mv *, u64, int flg);
185struct mv mv_chop_upto_any (struct mv *, struct mv, int flg);
186
187#ifndef MV_NO_FEAT_GPA_CTX
188
189// MAYBE(cmmm): dup into the scratch arena?
190struct mb mv_dup (struct mv);
191struct mb mv_dup0 (struct mv);
192
193#endif // MV_NO_FEAT_GPA_CTX
194
195#ifndef MV_NO_FEAT_UNISTD
196
197int mv_write (struct mv, int fd);
198
199#endif // MV_NO_FEAT_UNISTD
200
201#ifndef MV_NO_FEAT_STDIO
202#include <stdio.h>
203
204usz mv_fwrite (struct mv, FILE *strm);
205
206#endif // MV_NO_FEAT_STDIO
207
208#ifndef MV_NO_FEAT_ARR
209
210struct mv_arr {
211 struct mv *arr;
212 /* */ usz len;
213};
214
215#define MV_ARR(...) ((struct mv_arr) { ARR (__VA_ARGS__) })
216#define MV_ARR_NULL ((struct mv_arr) { ARR_NULL })
217#define MV_ARR_FROM_DA(da) ((struct mv_arr) { ARR_FROM_DA (da) })
218
219struct mb _mv_arr_join (struct mv_arr, struct mv sep, int zero);
220
221static inline struct mb mv_arr_join (struct mv_arr mvs, struct mv sep) { return _mv_arr_join (mvs, sep, 0); }
222static inline struct mb mv_arr_join0 (struct mv_arr mvs, struct mv sep) { return _mv_arr_join (mvs, sep, 1); }
223
224#endif // MV_NO_FEAT_ARR
225
226#endif // INCLUDED_COSMOS_MV_H
227
228// mv.h ends here