aboutsummaryrefslogtreecommitdiff
path: root/include/mv.h
blob: c4fb703fca2f69deac579d0b36dbf28a629d2e81 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// mv.h --- Memory View                                                -*- C -*-
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
// This file is part of COSMOS Codebase.
//
// Commentary:
//
//   Removing deps:
//    - #define MV_NO_UNISTD
//    - #define MV_NO_STDIO
//
// Code:

#ifndef INCLUDED_COSMOS_MV_H
#define INCLUDED_COSMOS_MV_H

#include "types.h"

struct mv {
  const u8 *beg;
  const u8 *end;
};

#define MV_NULL ((struct mv) {0})
#define MV( lit) mv_from_mb (MB  (lit))
#define MV0(lit) mv_from_mb (MB0 (lit))
#define MV_FMT(mv) (int) mv_sz (mv), (mv).beg

#define DBG_ASSERT_MV(mv)                                                      \
  DBG_ASSERT_MSG (!(mv).beg == !(mv).end, "Broken memory view invariant!")

enum mv_bit {
  MV_BIT_BEG,                //!< Go from beg (usually implied).
  MV_BIT_END,                //!< Go from end.
  MV_BIT_INV,                //!< Invert the resulting range.
  MV_BIT_NOT,                //!< Not the delimeter.
  MV_BIT_INC_BEG,            //!< Include delimiter in the beginning.
  MV_BIT_INC_END,            //!< Include delimiter in the end.
  MV_BIT_REP,                //!< Repeat action while possible.
  MV_BIT_CNT
};

// masks for flg arguments
#define MV_BEG     (1 << MV_BIT_BEG)
#define MV_END     (1 << MV_BIT_END)
#define MV_OUT     (MV_BEG | MV_END)
#define MV_INV     (1 << MV_BIT_INV)
#define MV_NOT     (1 << MV_BIT_NOT)
#define MV_INC_BEG (1 << MV_BIT_INC_BEG)
#define MV_INC_END (1 << MV_BIT_INC_END)
#define MV_INC     (MV_INC_BEG | MV_INC_END)
#define MV_REP     (1 << MV_BIT_REP)

usz mv_sz (struct mv);
int mv_cmp_fun (const void *, const void *); //!< For `qsort` and alike.
int mv_trim (struct mv *); //!< Same as `mv_strip_any (mv, MV0 (" \t\n\r"), MV_BEG | MV_END | MV_REP)`.
struct mv mv_basename (struct mv);
struct mv mv_dirname  (struct mv);

// mv_from*
struct mv mv_from (const void *beg, const void *end);
struct mv mv_from_mb  (struct mb);
struct mv mv_from_mb0 (struct mb);
struct mv mv_from_cstr (const char *cstr);

// mv_copy_to*
void mv_copy_to    (struct mv, void *ptr);
void mv_copy_to_mb (struct mv, struct mb);

// mv_to_* (2 <= base <= 36)
u64 mv_to_u64 (struct mv, int base); //!< Returns U64_MAX on invalid input.
i64 mv_to_i64 (struct mv, int base); //!< Returns I64_MIN on invalid input.
f64 mv_to_f64 (struct mv, int base); //!< Returns INFINITY on invalid input.

// mv_is_*
int mv_is_null  (struct mv); //!< `mv_eq (mv, MV_NULL)` semantics.
int mv_is_empty (struct mv); //!< `mv_equal (mv_or (mv, MV0 ("")), MV0 (""))` semantics.

// short circuiting
static inline struct mv mv_or  (struct mv lhs, struct mv rhs) { return  mv_is_empty (lhs) ? rhs : lhs; }
static inline struct mv mv_and (struct mv lhs, struct mv rhs) { return !mv_is_empty (lhs) ? rhs : lhs; }

// comparison
int mv_eq    (struct mv, struct mv); //!< Shallow comparison.
int mv_equal (struct mv, struct mv); //!< Deep comparison.
int mv_cmp   (struct mv, struct mv); //!< `memcmp` like.

// mv_contains*
int mv_contains     (struct mv, struct mv );
int mv_contains_u8  (struct mv,        u8 );
int mv_contains_u16 (struct mv,        u16);
int mv_contains_u32 (struct mv,        u32);
int mv_contains_u64 (struct mv,        u64);
int mv_contains_any (struct mv, struct mv );

// mv_affixed* (flg: [MV_BEG | MV_END])
int mv_affixed     (struct mv, struct mv,  int flg);
int mv_affixed_u8  (struct mv,        u8,  int flg);
int mv_affixed_u16 (struct mv,        u16, int flg);
int mv_affixed_u32 (struct mv,        u32, int flg);
int mv_affixed_u64 (struct mv,        u64, int flg);
int mv_affixed_any (struct mv, struct mv,  int flg);

// mv_affixed2*
int mv_affixed2     (struct mv, struct mv, struct mv );
int mv_affixed2_u8  (struct mv,        u8,        u8 );
int mv_affixed2_u16 (struct mv,        u16,       u16);
int mv_affixed2_u32 (struct mv,        u32,       u32);
int mv_affixed2_u64 (struct mv,        u64,       u64);
int mv_affixed2_any (struct mv, struct mv, struct mv );

// const void *mv_at     (struct mv,   usz, int flg); // (flg: [MV_BEG] ^ MV_END)
// /* */ void  mv_shrink (struct mv *, usz, int flg); // (flg: [MV_BEG | MV_END])

// mv_find* (flg: [MV_BEG] ^ MV_END)
struct mv   mv_find     (struct mv, struct mv,  int flg);
const  u8  *mv_find_u8  (struct mv,        u8,  int flg);
const  u16 *mv_find_u16 (struct mv,        u16, int flg);
const  u32 *mv_find_u32 (struct mv,        u32, int flg);
const  u64 *mv_find_u64 (struct mv,        u64, int flg);
const  u8  *mv_find_any (struct mv, struct mv,  int flg);

// mv_strip* (flg: [MV_BEG | MV_END] | MV_REP)
int mv_strip     (struct mv *, struct mv,  int flg);
int mv_strip_n   (struct mv *,        usz, int flg);
int mv_strip_u8  (struct mv *,        u8,  int flg);
int mv_strip_u16 (struct mv *,        u16, int flg);
int mv_strip_u32 (struct mv *,        u32, int flg);
int mv_strip_u64 (struct mv *,        u64, int flg);
int mv_strip_any (struct mv *, struct mv,  int flg);

// mv_strip_upto* (flg: [MV_BEG | MV_END] | MV_REP)
int mv_strip_upto     (struct mv *, struct mv,  int flg);
int mv_strip_upto_u8  (struct mv *,        u8,  int flg);
int mv_strip_upto_u16 (struct mv *,        u16, int flg);
int mv_strip_upto_u32 (struct mv *,        u32, int flg);
int mv_strip_upto_u64 (struct mv *,        u64, int flg);
int mv_strip_upto_any (struct mv *, struct mv,  int flg);

// mv_peek* (flg: [MV_BEG] ^ MV_END)
struct mv  mv_peek      (struct mv, usz, int flg);
/*  */ u8  mv_peek_u8   (struct mv,      int flg);
/*  */ u16 mv_peek_u16  (struct mv,      int flg);
/*  */ u32 mv_peek_u32  (struct mv,      int flg);
/*  */ u64 mv_peek_u64  (struct mv,      int flg);
struct mv  mv_peek_cstr (struct mv); //!< Same as `mv_peek_upto_u8 (mv, '\0', 0)`.

// mv_peek_upto* (flg: [MV_BEG] ^ MV_END | MV_INC | MV_INV)
struct mv mv_peek_upto     (struct mv, struct mv,  int flg);
struct mv mv_peek_upto_u8  (struct mv,        u8,  int flg);
struct mv mv_peek_upto_u16 (struct mv,        u16, int flg);
struct mv mv_peek_upto_u32 (struct mv,        u32, int flg);
struct mv mv_peek_upto_u64 (struct mv,        u64, int flg);
struct mv mv_peek_upto_any (struct mv, struct mv,  int flg);

// mv_peek_in* (flg: [MV_BEG] ^ MV_END | MV_INC)
struct mv mv_peek_in     (struct mv *, struct mv, struct mv,  int flg);
struct mv mv_peek_in_u8  (struct mv *,        u8,        u8,  int flg);
struct mv mv_peek_in_u16 (struct mv *,        u16,       u16, int flg);
struct mv mv_peek_in_u32 (struct mv *,        u32,       u32, int flg);
struct mv mv_peek_in_u64 (struct mv *,        u64,       u64, int flg);
struct mv mv_peek_in_any (struct mv *, struct mv, struct mv,  int flg);

// mv_chop* (flg: [MV_BEG] ^ MV_END | MV_INC_BEG | MV_INC_END | MV_INV)
struct mv  mv_chop      (struct mv *, usz, int flg);
/*  */ u8  mv_chop_u8   (struct mv *,      int flg);
/*  */ u16 mv_chop_u16  (struct mv *,      int flg);
/*  */ u32 mv_chop_u32  (struct mv *,      int flg);
/*  */ u64 mv_chop_u64  (struct mv *,      int flg);
struct mv  mv_chop_cstr (struct mv *); //!< Same as `mv_chop_upto_u8 (mv, '\0', 0)`.

// TODO(cmmm): find a better name for this mv_exclude.
//             It's a really powerful building block.
struct mv mv_exclude (struct mv *, struct mv, int flg);

// mv_chop_upto_* (flg: [MV_BEG] ^ MV_END | MV_INC_BEG | MV_INC_END)
struct mv mv_chop_upto     (struct mv *, struct mv,  int flg);
struct mv mv_chop_upto_u8  (struct mv *,        u8,  int flg);
struct mv mv_chop_upto_u16 (struct mv *,        u16, int flg);
struct mv mv_chop_upto_u32 (struct mv *,        u32, int flg);
struct mv mv_chop_upto_u64 (struct mv *,        u64, int flg);
struct mv mv_chop_upto_any (struct mv *, struct mv,  int flg);

#ifndef MV_NO_FEAT_GPA_CTX

// MAYBE(cmmm): dup into the scratch arena?
struct mb mv_dup  (struct mv);
struct mb mv_dup0 (struct mv);

#endif // MV_NO_FEAT_GPA_CTX

#ifndef MV_NO_FEAT_UNISTD

int mv_write (struct mv, int fd);

#endif // MV_NO_FEAT_UNISTD

#ifndef MV_NO_FEAT_STDIO
#include <stdio.h>

usz mv_fwrite (struct mv, FILE *strm);

#endif // MV_NO_FEAT_STDIO

#ifndef MV_NO_FEAT_ARR

struct mv_arr {
  struct mv *arr;
  /*  */ usz len;
};

#define MV_ARR(...) ((struct mv_arr) { ARR (__VA_ARGS__) })
#define MV_ARR_NULL ((struct mv_arr) { ARR_NULL })
#define MV_ARR_FROM_DA(da) ((struct mv_arr) { ARR_FROM_DA (da) })

struct mb _mv_arr_join (struct mv_arr, struct mv sep, int zero);

static inline struct mb mv_arr_join  (struct mv_arr mvs, struct mv sep) { return _mv_arr_join (mvs, sep, 0); }
static inline struct mb mv_arr_join0 (struct mv_arr mvs, struct mv sep) { return _mv_arr_join (mvs, sep, 1); }

#endif // MV_NO_FEAT_ARR

#endif // INCLUDED_COSMOS_MV_H

// mv.h ends here