diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/mv.h | 228 |
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 | |||
| 22 | struct 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 | |||
| 35 | enum 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 | |||
| 57 | usz mv_sz (struct mv); | ||
| 58 | int mv_cmp_fun (const void *, const void *); //!< For `qsort` and alike. | ||
| 59 | int mv_trim (struct mv *); //!< Same as `mv_strip_any (mv, MV0 (" \t\n\r"), MV_BEG | MV_END | MV_REP)`. | ||
| 60 | struct mv mv_basename (struct mv); | ||
| 61 | struct mv mv_dirname (struct mv); | ||
| 62 | |||
| 63 | // mv_from* | ||
| 64 | struct mv mv_from (const void *beg, const void *end); | ||
| 65 | struct mv mv_from_mb (struct mb); | ||
| 66 | struct mv mv_from_mb0 (struct mb); | ||
| 67 | struct mv mv_from_cstr (const char *cstr); | ||
| 68 | |||
| 69 | // mv_copy_to* | ||
| 70 | void mv_copy_to (struct mv, void *ptr); | ||
| 71 | void mv_copy_to_mb (struct mv, struct mb); | ||
| 72 | |||
| 73 | // mv_to_* (2 <= base <= 36) | ||
| 74 | u64 mv_to_u64 (struct mv, int base); //!< Returns U64_MAX on invalid input. | ||
| 75 | i64 mv_to_i64 (struct mv, int base); //!< Returns I64_MIN on invalid input. | ||
| 76 | f64 mv_to_f64 (struct mv, int base); //!< Returns INFINITY on invalid input. | ||
| 77 | |||
| 78 | // mv_is_* | ||
| 79 | int mv_is_null (struct mv); //!< `mv_eq (mv, MV_NULL)` semantics. | ||
| 80 | int mv_is_empty (struct mv); //!< `mv_equal (mv_or (mv, MV0 ("")), MV0 (""))` semantics. | ||
| 81 | |||
| 82 | // short circuiting | ||
| 83 | static inline struct mv mv_or (struct mv lhs, struct mv rhs) { return mv_is_empty (lhs) ? rhs : lhs; } | ||
| 84 | static inline struct mv mv_and (struct mv lhs, struct mv rhs) { return !mv_is_empty (lhs) ? rhs : lhs; } | ||
| 85 | |||
| 86 | // comparison | ||
| 87 | int mv_eq (struct mv, struct mv); //!< Shallow comparison. | ||
| 88 | int mv_equal (struct mv, struct mv); //!< Deep comparison. | ||
| 89 | int mv_cmp (struct mv, struct mv); //!< `memcmp` like. | ||
| 90 | |||
| 91 | // mv_contains* | ||
| 92 | int mv_contains (struct mv, struct mv ); | ||
| 93 | int mv_contains_u8 (struct mv, u8 ); | ||
| 94 | int mv_contains_u16 (struct mv, u16); | ||
| 95 | int mv_contains_u32 (struct mv, u32); | ||
| 96 | int mv_contains_u64 (struct mv, u64); | ||
| 97 | int mv_contains_any (struct mv, struct mv ); | ||
| 98 | |||
| 99 | // mv_affixed* (flg: [MV_BEG | MV_END]) | ||
| 100 | int mv_affixed (struct mv, struct mv, int flg); | ||
| 101 | int mv_affixed_u8 (struct mv, u8, int flg); | ||
| 102 | int mv_affixed_u16 (struct mv, u16, int flg); | ||
| 103 | int mv_affixed_u32 (struct mv, u32, int flg); | ||
| 104 | int mv_affixed_u64 (struct mv, u64, int flg); | ||
| 105 | int mv_affixed_any (struct mv, struct mv, int flg); | ||
| 106 | |||
| 107 | // mv_affixed2* | ||
| 108 | int mv_affixed2 (struct mv, struct mv, struct mv ); | ||
| 109 | int mv_affixed2_u8 (struct mv, u8, u8 ); | ||
| 110 | int mv_affixed2_u16 (struct mv, u16, u16); | ||
| 111 | int mv_affixed2_u32 (struct mv, u32, u32); | ||
| 112 | int mv_affixed2_u64 (struct mv, u64, u64); | ||
| 113 | int 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) | ||
| 119 | struct mv mv_find (struct mv, struct mv, int flg); | ||
| 120 | const u8 *mv_find_u8 (struct mv, u8, int flg); | ||
| 121 | const u16 *mv_find_u16 (struct mv, u16, int flg); | ||
| 122 | const u32 *mv_find_u32 (struct mv, u32, int flg); | ||
| 123 | const u64 *mv_find_u64 (struct mv, u64, int flg); | ||
| 124 | const u8 *mv_find_any (struct mv, struct mv, int flg); | ||
| 125 | |||
| 126 | // mv_strip* (flg: [MV_BEG | MV_END] | MV_REP) | ||
| 127 | int mv_strip (struct mv *, struct mv, int flg); | ||
| 128 | int mv_strip_n (struct mv *, usz, int flg); | ||
| 129 | int mv_strip_u8 (struct mv *, u8, int flg); | ||
| 130 | int mv_strip_u16 (struct mv *, u16, int flg); | ||
| 131 | int mv_strip_u32 (struct mv *, u32, int flg); | ||
| 132 | int mv_strip_u64 (struct mv *, u64, int flg); | ||
| 133 | int mv_strip_any (struct mv *, struct mv, int flg); | ||
| 134 | |||
| 135 | // mv_strip_upto* (flg: [MV_BEG | MV_END] | MV_REP) | ||
| 136 | int mv_strip_upto (struct mv *, struct mv, int flg); | ||
| 137 | int mv_strip_upto_u8 (struct mv *, u8, int flg); | ||
| 138 | int mv_strip_upto_u16 (struct mv *, u16, int flg); | ||
| 139 | int mv_strip_upto_u32 (struct mv *, u32, int flg); | ||
| 140 | int mv_strip_upto_u64 (struct mv *, u64, int flg); | ||
| 141 | int mv_strip_upto_any (struct mv *, struct mv, int flg); | ||
| 142 | |||
| 143 | // mv_peek* (flg: [MV_BEG] ^ MV_END) | ||
| 144 | struct 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); | ||
| 149 | struct 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) | ||
| 152 | struct mv mv_peek_upto (struct mv, struct mv, int flg); | ||
| 153 | struct mv mv_peek_upto_u8 (struct mv, u8, int flg); | ||
| 154 | struct mv mv_peek_upto_u16 (struct mv, u16, int flg); | ||
| 155 | struct mv mv_peek_upto_u32 (struct mv, u32, int flg); | ||
| 156 | struct mv mv_peek_upto_u64 (struct mv, u64, int flg); | ||
| 157 | struct mv mv_peek_upto_any (struct mv, struct mv, int flg); | ||
| 158 | |||
| 159 | // mv_peek_in* (flg: [MV_BEG] ^ MV_END | MV_INC) | ||
| 160 | struct mv mv_peek_in (struct mv *, struct mv, struct mv, int flg); | ||
| 161 | struct mv mv_peek_in_u8 (struct mv *, u8, u8, int flg); | ||
| 162 | struct mv mv_peek_in_u16 (struct mv *, u16, u16, int flg); | ||
| 163 | struct mv mv_peek_in_u32 (struct mv *, u32, u32, int flg); | ||
| 164 | struct mv mv_peek_in_u64 (struct mv *, u64, u64, int flg); | ||
| 165 | struct 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) | ||
| 168 | struct 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); | ||
| 173 | struct 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. | ||
| 177 | struct 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) | ||
| 180 | struct mv mv_chop_upto (struct mv *, struct mv, int flg); | ||
| 181 | struct mv mv_chop_upto_u8 (struct mv *, u8, int flg); | ||
| 182 | struct mv mv_chop_upto_u16 (struct mv *, u16, int flg); | ||
| 183 | struct mv mv_chop_upto_u32 (struct mv *, u32, int flg); | ||
| 184 | struct mv mv_chop_upto_u64 (struct mv *, u64, int flg); | ||
| 185 | struct 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? | ||
| 190 | struct mb mv_dup (struct mv); | ||
| 191 | struct mb mv_dup0 (struct mv); | ||
| 192 | |||
| 193 | #endif // MV_NO_FEAT_GPA_CTX | ||
| 194 | |||
| 195 | #ifndef MV_NO_FEAT_UNISTD | ||
| 196 | |||
| 197 | int 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 | |||
| 204 | usz mv_fwrite (struct mv, FILE *strm); | ||
| 205 | |||
| 206 | #endif // MV_NO_FEAT_STDIO | ||
| 207 | |||
| 208 | #ifndef MV_NO_FEAT_ARR | ||
| 209 | |||
| 210 | struct 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 | |||
| 219 | struct mb _mv_arr_join (struct mv_arr, struct mv sep, int zero); | ||
| 220 | |||
| 221 | static inline struct mb mv_arr_join (struct mv_arr mvs, struct mv sep) { return _mv_arr_join (mvs, sep, 0); } | ||
| 222 | static 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 | ||
