diff options
| -rw-r--r-- | include/mv.h | 228 | ||||
| -rw-r--r-- | src/mv.c | 1100 |
2 files changed, 1328 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 | ||
diff --git a/src/mv.c b/src/mv.c new file mode 100644 index 0000000..7b2d6a7 --- /dev/null +++ b/src/mv.c | |||
| @@ -0,0 +1,1100 @@ | |||
| 1 | // mv.c --- 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 | // Code: | ||
| 12 | |||
| 13 | #include "mv.h" | ||
| 14 | #include "dbg.h" | ||
| 15 | |||
| 16 | // utility | ||
| 17 | |||
| 18 | usz | ||
| 19 | mv_sz (struct mv mv) | ||
| 20 | { | ||
| 21 | DBG_ASSERT_MV (mv); | ||
| 22 | return mv.end - mv.beg; | ||
| 23 | } | ||
| 24 | |||
| 25 | int | ||
| 26 | mv_trim (struct mv *mv) | ||
| 27 | { | ||
| 28 | UNUSED (mv); | ||
| 29 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 30 | return 0; | ||
| 31 | } | ||
| 32 | |||
| 33 | #include <string.h> // TEMP(cmmm): remove dep | ||
| 34 | |||
| 35 | void | ||
| 36 | mv_copy_to (struct mv mv, void *ptr) | ||
| 37 | { | ||
| 38 | DBG_ASSERT_MV (mv); | ||
| 39 | DBG_ASSERT_NN (ptr); | ||
| 40 | memcpy (ptr, mv.beg, mv_sz (mv)); | ||
| 41 | } | ||
| 42 | |||
| 43 | void | ||
| 44 | mv_copy_to_mb (struct mv mv, struct mb mb) | ||
| 45 | { | ||
| 46 | DBG_ASSERT_MV (mv); | ||
| 47 | DBG_ASSERT_NN (mb.ptr); | ||
| 48 | DBG_ASSERT_LE (mv_sz (mv), mb.sz, "%lu", "%lu"); | ||
| 49 | memcpy (mb.ptr, mv.beg, MIN (mv_sz (mv), mb.sz)); | ||
| 50 | } | ||
| 51 | |||
| 52 | struct mv | ||
| 53 | mv_basename (struct mv mv) | ||
| 54 | { | ||
| 55 | DBG_ASSERT (!mv_is_empty (mv)); | ||
| 56 | mv_strip_u8 (&mv, '/', MV_END | MV_REP); | ||
| 57 | return mv_peek_upto_u8 (mv, '/', MV_END); | ||
| 58 | } | ||
| 59 | |||
| 60 | struct mv | ||
| 61 | mv_dirname (struct mv mv) | ||
| 62 | { | ||
| 63 | DBG_ASSERT_MV (mv); | ||
| 64 | if (mv_is_empty (mv)) | ||
| 65 | return MV0 ("."); | ||
| 66 | |||
| 67 | DBG_TRACE ("info", "%.*s", MV_FMT (mv)); | ||
| 68 | mv_strip_u8 (&mv, '/', MV_END | MV_REP); | ||
| 69 | DBG_TRACE ("info", "%.*s", MV_FMT (mv)); | ||
| 70 | mv_chop_upto_u8 (&mv, '/', MV_END | MV_INC_END | MV_INV); | ||
| 71 | DBG_TRACE ("info", "%.*s", MV_FMT (mv)); | ||
| 72 | mv_strip_u8 (&mv, '/', MV_END | MV_REP); | ||
| 73 | DBG_TRACE ("info", "%.*s", MV_FMT (mv)); | ||
| 74 | |||
| 75 | if (mv_is_empty (mv)) | ||
| 76 | return MV0 ("."); | ||
| 77 | return mv; | ||
| 78 | } | ||
| 79 | |||
| 80 | int | ||
| 81 | mv_cmp_fun (const void *lhs, const void *rhs) | ||
| 82 | { | ||
| 83 | DBG_ASSERT_NN (lhs); | ||
| 84 | DBG_ASSERT_NN (rhs); | ||
| 85 | return mv_cmp (*(const struct mv *) lhs, | ||
| 86 | *(const struct mv *) rhs); | ||
| 87 | } | ||
| 88 | |||
| 89 | // mv_from* | ||
| 90 | |||
| 91 | struct mv | ||
| 92 | mv_from (const void *beg, const void *end) | ||
| 93 | { | ||
| 94 | struct mv mv; | ||
| 95 | mv.beg = beg; | ||
| 96 | mv.end = end; | ||
| 97 | DBG_ASSERT_MV (mv); | ||
| 98 | return mv; | ||
| 99 | } | ||
| 100 | |||
| 101 | struct mv | ||
| 102 | mv_from_mb (struct mb mb) | ||
| 103 | { | ||
| 104 | return mv_from (mb.ptr, (u8 *) mb.ptr + mb.sz); | ||
| 105 | } | ||
| 106 | |||
| 107 | struct mv | ||
| 108 | mv_from_mb0 (struct mb mb) | ||
| 109 | { | ||
| 110 | return mv_from (mb.ptr, (u8 *) mb.ptr + mb.sz - 1); | ||
| 111 | } | ||
| 112 | |||
| 113 | struct mv | ||
| 114 | mv_from_cstr (const char *cstr) | ||
| 115 | { | ||
| 116 | struct mv mv = mv_from (cstr, cstr); | ||
| 117 | if (LIKELY (cstr != NULL)) { | ||
| 118 | while (*mv.end != '\0') | ||
| 119 | mv.end += 1; | ||
| 120 | } return mv; | ||
| 121 | } | ||
| 122 | |||
| 123 | // mv_to_* | ||
| 124 | |||
| 125 | u64 | ||
| 126 | mv_to_u64 (struct mv mv, int base) | ||
| 127 | { | ||
| 128 | DBG_ASSERT_MV (mv); | ||
| 129 | DBG_ASSERT_IN (2, base, 36, "%d", "%d", "%d"); | ||
| 130 | DBG_ASSERT_IN_MSG (2, base, 10, "%d", "%d", "%d", "other bases yet unhandled"); | ||
| 131 | |||
| 132 | u64 val = 0; | ||
| 133 | while (mv.beg < mv.end && '0' <= *mv.beg && *mv.beg <= '0' + base - 1) { | ||
| 134 | u64 digit = *mv.beg - '0'; | ||
| 135 | |||
| 136 | // overflow protection | ||
| 137 | if (val > (U64_MAX - digit)/base) | ||
| 138 | return U64_MAX; | ||
| 139 | |||
| 140 | val = val*base + digit; | ||
| 141 | mv.beg += 1; | ||
| 142 | } | ||
| 143 | |||
| 144 | if (mv.beg != mv.end) | ||
| 145 | return U64_MAX; | ||
| 146 | |||
| 147 | return val; | ||
| 148 | } | ||
| 149 | |||
| 150 | i64 | ||
| 151 | mv_to_i64 (struct mv mv, int base) | ||
| 152 | { | ||
| 153 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 154 | return 0; | ||
| 155 | } | ||
| 156 | |||
| 157 | f64 | ||
| 158 | mv_to_f64 (struct mv mv, int base) | ||
| 159 | { | ||
| 160 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 161 | return 0; | ||
| 162 | } | ||
| 163 | |||
| 164 | // mv_is_* | ||
| 165 | |||
| 166 | int | ||
| 167 | mv_is_null (struct mv mv) | ||
| 168 | { | ||
| 169 | DBG_ASSERT_MV (mv); | ||
| 170 | return mv.beg == NULL; | ||
| 171 | } | ||
| 172 | |||
| 173 | int | ||
| 174 | mv_is_empty (struct mv mv) | ||
| 175 | { | ||
| 176 | DBG_ASSERT_MV (mv); | ||
| 177 | return mv.beg == mv.end; | ||
| 178 | } | ||
| 179 | |||
| 180 | // comparison | ||
| 181 | |||
| 182 | int | ||
| 183 | mv_eq (struct mv lhs, struct mv rhs) | ||
| 184 | { | ||
| 185 | DBG_ASSERT_MV (lhs); | ||
| 186 | DBG_ASSERT_MV (rhs); | ||
| 187 | return (lhs.beg == rhs.beg && | ||
| 188 | lhs.end == rhs.end); | ||
| 189 | } | ||
| 190 | |||
| 191 | int | ||
| 192 | mv_equal (struct mv lhs, struct mv rhs) | ||
| 193 | { | ||
| 194 | DBG_ASSERT_MV (lhs); | ||
| 195 | DBG_ASSERT_MV (rhs); | ||
| 196 | |||
| 197 | if (mv_sz (lhs) != mv_sz (rhs)) | ||
| 198 | return 0; | ||
| 199 | |||
| 200 | if (lhs.beg == rhs.beg) | ||
| 201 | return 1; | ||
| 202 | |||
| 203 | return mv_cmp (lhs, rhs) == 0; | ||
| 204 | } | ||
| 205 | |||
| 206 | int | ||
| 207 | mv_cmp (struct mv lhs, struct mv rhs) | ||
| 208 | { | ||
| 209 | DBG_ASSERT_MV (lhs); | ||
| 210 | DBG_ASSERT_MV (rhs); | ||
| 211 | return strncmp (lhs.beg, rhs.beg, MIN (mv_sz (lhs), mv_sz (rhs))); | ||
| 212 | } | ||
| 213 | |||
| 214 | // mv_contains* | ||
| 215 | |||
| 216 | int | ||
| 217 | mv_contains (struct mv mv, struct mv seq) | ||
| 218 | { | ||
| 219 | UNUSED (mv); | ||
| 220 | UNUSED (seq); | ||
| 221 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 222 | return 0; | ||
| 223 | } | ||
| 224 | |||
| 225 | int | ||
| 226 | mv_contains_u8 (struct mv mv, u8 seq) | ||
| 227 | { | ||
| 228 | UNUSED (mv); | ||
| 229 | UNUSED (seq); | ||
| 230 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 231 | return 0; | ||
| 232 | } | ||
| 233 | |||
| 234 | int | ||
| 235 | mv_contains_u16 (struct mv mv, u16 seq) | ||
| 236 | { | ||
| 237 | UNUSED (mv); | ||
| 238 | UNUSED (seq); | ||
| 239 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 240 | return 0; | ||
| 241 | } | ||
| 242 | |||
| 243 | int | ||
| 244 | mv_contains_u32 (struct mv mv, u32 seq) | ||
| 245 | { | ||
| 246 | UNUSED (mv); | ||
| 247 | UNUSED (seq); | ||
| 248 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 249 | return 0; | ||
| 250 | } | ||
| 251 | |||
| 252 | int | ||
| 253 | mv_contains_u64 (struct mv mv, u64 seq) | ||
| 254 | { | ||
| 255 | UNUSED (mv); | ||
| 256 | UNUSED (seq); | ||
| 257 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 258 | return 0; | ||
| 259 | } | ||
| 260 | |||
| 261 | int | ||
| 262 | mv_contains_any (struct mv mv, struct mv set) | ||
| 263 | { | ||
| 264 | UNUSED (mv); | ||
| 265 | UNUSED (set); | ||
| 266 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 267 | return 0; | ||
| 268 | } | ||
| 269 | |||
| 270 | // mv_affixed* (flg: [MV_BEG | MV_END]) | ||
| 271 | |||
| 272 | int | ||
| 273 | mv_affixed (struct mv mv, struct mv affix, int flg) | ||
| 274 | { | ||
| 275 | DBG_ASSERT_MV (mv); | ||
| 276 | DBG_ASSERT_MV (affix); | ||
| 277 | DBG_ASSERT_EZ_MSG (flg & ~(MV_BEG | MV_END), "%d", "%b*u", MV_BIT_CNT, flg); | ||
| 278 | |||
| 279 | usz affix_sz = mv_sz (affix); | ||
| 280 | if (flg & MV_BEG) { | ||
| 281 | if (mv_equal (mv_peek (mv, affix_sz, MV_BEG), affix)) | ||
| 282 | return 1; | ||
| 283 | } | ||
| 284 | |||
| 285 | if (flg & MV_END) { | ||
| 286 | if (mv_equal (mv_peek (mv, affix_sz, MV_END), affix)) | ||
| 287 | return 1; | ||
| 288 | } | ||
| 289 | |||
| 290 | return 0; | ||
| 291 | } | ||
| 292 | |||
| 293 | int | ||
| 294 | mv_affixed_u8 (struct mv mv, u8 affix, int flg) | ||
| 295 | { | ||
| 296 | UNUSED (mv); | ||
| 297 | UNUSED (affix); | ||
| 298 | UNUSED (flg); | ||
| 299 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 300 | return 0; | ||
| 301 | } | ||
| 302 | |||
| 303 | int | ||
| 304 | mv_affixed_u16 (struct mv mv, u16 affix, int flg) | ||
| 305 | { | ||
| 306 | UNUSED (mv); | ||
| 307 | UNUSED (affix); | ||
| 308 | UNUSED (flg); | ||
| 309 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 310 | return 0; | ||
| 311 | } | ||
| 312 | |||
| 313 | int | ||
| 314 | mv_affixed_u32 (struct mv mv, u32 affix, int flg) | ||
| 315 | { | ||
| 316 | UNUSED (mv); | ||
| 317 | UNUSED (affix); | ||
| 318 | UNUSED (flg); | ||
| 319 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 320 | return 0; | ||
| 321 | } | ||
| 322 | |||
| 323 | int | ||
| 324 | mv_affixed_u64 (struct mv mv, u64 affix, int flg) | ||
| 325 | { | ||
| 326 | UNUSED (mv); | ||
| 327 | UNUSED (affix); | ||
| 328 | UNUSED (flg); | ||
| 329 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 330 | return 0; | ||
| 331 | } | ||
| 332 | |||
| 333 | int | ||
| 334 | mv_affixed_any (struct mv mv, struct mv affix, int flg) | ||
| 335 | { | ||
| 336 | UNUSED (mv); | ||
| 337 | UNUSED (affix); | ||
| 338 | UNUSED (flg); | ||
| 339 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 340 | return 0; | ||
| 341 | } | ||
| 342 | |||
| 343 | // mv_affixed2* | ||
| 344 | |||
| 345 | int | ||
| 346 | mv_affixed2 (struct mv mv, struct mv prefix, struct mv suffix) | ||
| 347 | { | ||
| 348 | UNUSED (mv); | ||
| 349 | UNUSED (prefix); | ||
| 350 | UNUSED (suffix); | ||
| 351 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 352 | return 0; | ||
| 353 | } | ||
| 354 | |||
| 355 | int | ||
| 356 | mv_affixed2_u8 (struct mv mv, u8 prefix, u8 suffix) | ||
| 357 | { | ||
| 358 | UNUSED (mv); | ||
| 359 | UNUSED (prefix); | ||
| 360 | UNUSED (suffix); | ||
| 361 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 362 | return 0; | ||
| 363 | } | ||
| 364 | |||
| 365 | int | ||
| 366 | mv_affixed2_u16 (struct mv mv, u16 prefix, u16 suffix) | ||
| 367 | { | ||
| 368 | UNUSED (mv); | ||
| 369 | UNUSED (prefix); | ||
| 370 | UNUSED (suffix); | ||
| 371 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 372 | return 0; | ||
| 373 | } | ||
| 374 | |||
| 375 | int | ||
| 376 | mv_affixed2_u32 (struct mv mv, u32 prefix, u32 suffix) | ||
| 377 | { | ||
| 378 | UNUSED (mv); | ||
| 379 | UNUSED (prefix); | ||
| 380 | UNUSED (suffix); | ||
| 381 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 382 | return 0; | ||
| 383 | } | ||
| 384 | |||
| 385 | int | ||
| 386 | mv_affixed2_u64 (struct mv mv, u64 prefix, u64 suffix) | ||
| 387 | { | ||
| 388 | UNUSED (mv); | ||
| 389 | UNUSED (prefix); | ||
| 390 | UNUSED (suffix); | ||
| 391 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 392 | return 0; | ||
| 393 | } | ||
| 394 | |||
| 395 | int | ||
| 396 | mv_affixed2_any (struct mv mv, struct mv prefix_set, struct mv suffix_set) | ||
| 397 | { | ||
| 398 | UNUSED (mv); | ||
| 399 | UNUSED (prefix_set); | ||
| 400 | UNUSED (suffix_set); | ||
| 401 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 402 | return 0; | ||
| 403 | } | ||
| 404 | |||
| 405 | // mv_at ([MV_BEG] ^ MV_END) | ||
| 406 | |||
| 407 | const void * | ||
| 408 | mv_at (struct mv mv, usz idx, int flg) | ||
| 409 | { | ||
| 410 | DBG_ASSERT_MV (mv); | ||
| 411 | DBG_ASSERT_LT (mv_sz (mv), idx, "%lu", "%lu"); | ||
| 412 | |||
| 413 | if (flg & MV_END) return mv.end - idx - 1; | ||
| 414 | else return mv.beg + idx; | ||
| 415 | } | ||
| 416 | |||
| 417 | // mv_shrink ([MV_BEG | MV_END]) | ||
| 418 | |||
| 419 | void | ||
| 420 | mv_shrink (struct mv *mv, usz sz, int flg) | ||
| 421 | { | ||
| 422 | DBG_ASSERT_NN ( mv); | ||
| 423 | DBG_ASSERT_MV (*mv); | ||
| 424 | |||
| 425 | if (!(flg & (MV_BEG | MV_END))) | ||
| 426 | flg |= MV_BEG | MV_END; | ||
| 427 | |||
| 428 | if (flg & MV_BEG) { | ||
| 429 | DBG_ASSERT_LT (mv_sz (*mv), sz, "%lu", "%lu"); | ||
| 430 | mv->beg += sz; | ||
| 431 | } | ||
| 432 | |||
| 433 | if (flg & MV_END) { | ||
| 434 | DBG_ASSERT_LT (mv_sz (*mv), sz, "%lu", "%lu"); | ||
| 435 | mv->end -= sz; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | // mv_find* (flg: [MV_BEG] ^ MV_END) | ||
| 440 | |||
| 441 | struct mv | ||
| 442 | mv_find (struct mv mv, struct mv ndl, int flg) | ||
| 443 | { | ||
| 444 | UNUSED (mv); | ||
| 445 | UNUSED (ndl); | ||
| 446 | UNUSED (flg); | ||
| 447 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 448 | return MV_NULL; | ||
| 449 | } | ||
| 450 | |||
| 451 | const u8 * | ||
| 452 | mv_find_u8 (struct mv mv, u8 ndl, int flg) | ||
| 453 | { | ||
| 454 | DBG_ASSERT_MV (mv); | ||
| 455 | |||
| 456 | const u8 *ptr; | ||
| 457 | if (flg & MV_END) { | ||
| 458 | DBG_ASSERT_MSG (~flg & MV_BEG, "MV_BEG ^ MV_END!"); | ||
| 459 | for (ptr = mv.end - 1; ptr >= mv.beg; --ptr) | ||
| 460 | if (*ptr == ndl) return ptr; | ||
| 461 | } else { // [MV_BEG] | ||
| 462 | for (ptr = mv.beg; ptr < mv.end; ++ptr) | ||
| 463 | if (*ptr == ndl) return ptr; | ||
| 464 | } | ||
| 465 | |||
| 466 | return NULL; | ||
| 467 | } | ||
| 468 | |||
| 469 | const u16 * | ||
| 470 | mv_find_u16 (struct mv mv, u16 ndl, int flg) | ||
| 471 | { | ||
| 472 | DBG_ASSERT_MV (mv); | ||
| 473 | UNUSED (mv); | ||
| 474 | UNUSED (ndl); | ||
| 475 | UNUSED (flg); | ||
| 476 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 477 | return NULL; | ||
| 478 | } | ||
| 479 | |||
| 480 | const u32 * | ||
| 481 | mv_find_u32 (struct mv mv, u32 ndl, int flg) | ||
| 482 | { | ||
| 483 | UNUSED (mv); | ||
| 484 | UNUSED (ndl); | ||
| 485 | UNUSED (flg); | ||
| 486 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 487 | return NULL; | ||
| 488 | } | ||
| 489 | |||
| 490 | const u64 * | ||
| 491 | mv_find_u64 (struct mv mv, u64 ndl, int flg) | ||
| 492 | { | ||
| 493 | UNUSED (mv); | ||
| 494 | UNUSED (ndl); | ||
| 495 | UNUSED (flg); | ||
| 496 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 497 | return NULL; | ||
| 498 | } | ||
| 499 | |||
| 500 | const u8 * | ||
| 501 | mv_find_any (struct mv mv, struct mv ndl_set, int flg) | ||
| 502 | { | ||
| 503 | DBG_ASSERT_MV (mv); | ||
| 504 | |||
| 505 | const u8 *ptr, *ndl; | ||
| 506 | if (flg & MV_END) { | ||
| 507 | DBG_ASSERT_MSG (~flg & MV_BEG, "MV_BEG ^ MV_END!"); | ||
| 508 | for (ptr = mv.end - 1; ptr >= mv.beg; --ptr) { | ||
| 509 | for (ndl = ndl_set.beg; ndl < ndl_set.end; ++ndl) | ||
| 510 | if (*ptr == *ndl) return ptr; | ||
| 511 | } | ||
| 512 | } else /* [MV_BEG] */ { | ||
| 513 | for (ptr = mv.beg; ptr < mv.end; ++ptr) { | ||
| 514 | for (ndl = ndl_set.beg; ndl < ndl_set.end; ++ndl) | ||
| 515 | if (*ptr == *ndl) return ptr; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | |||
| 519 | return NULL; | ||
| 520 | } | ||
| 521 | |||
| 522 | // mv_strip* (flg: [MV_BEG | MV_END] | MV_REP) | ||
| 523 | |||
| 524 | int | ||
| 525 | mv_strip (struct mv *mv, struct mv seq, int flg) | ||
| 526 | { | ||
| 527 | UNUSED (mv); | ||
| 528 | UNUSED (seq); | ||
| 529 | UNUSED (flg); | ||
| 530 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 531 | return 0; | ||
| 532 | } | ||
| 533 | |||
| 534 | int | ||
| 535 | mv_strip_n (struct mv *mv, usz n, int flg) | ||
| 536 | { | ||
| 537 | UNUSED (mv); | ||
| 538 | UNUSED (n); | ||
| 539 | UNUSED (flg); | ||
| 540 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 541 | return 0; | ||
| 542 | } | ||
| 543 | |||
| 544 | int | ||
| 545 | mv_strip_u8 (struct mv *mv, u8 val, int flg) | ||
| 546 | { | ||
| 547 | DBG_ASSERT_NN ( mv); | ||
| 548 | DBG_ASSERT_MV (*mv); | ||
| 549 | |||
| 550 | if ((flg & (MV_BEG | MV_END)) == 0) | ||
| 551 | flg |= MV_BEG | MV_END; | ||
| 552 | |||
| 553 | int stripped = 0; | ||
| 554 | if (flg & MV_BEG) { | ||
| 555 | if (mv->beg[+0] == val) mv->beg += 1; | ||
| 556 | else goto try_end; | ||
| 557 | stripped = 1; | ||
| 558 | if (flg & MV_REP) | ||
| 559 | while (mv->beg[+0] == val) mv->beg += 1; | ||
| 560 | } | ||
| 561 | |||
| 562 | try_end: | ||
| 563 | if (flg & MV_END) { | ||
| 564 | if (mv->end[-1] == val) mv->end += 1; | ||
| 565 | else return stripped; | ||
| 566 | stripped = 1; | ||
| 567 | if (flg & MV_REP) | ||
| 568 | while (mv->end[-1] == val) mv->end += 1; | ||
| 569 | } | ||
| 570 | |||
| 571 | return stripped; | ||
| 572 | } | ||
| 573 | |||
| 574 | int | ||
| 575 | mv_strip_u16 (struct mv *mv, u16 val, int flg) | ||
| 576 | { | ||
| 577 | UNUSED (mv); | ||
| 578 | UNUSED (val); | ||
| 579 | UNUSED (flg); | ||
| 580 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 581 | return 0; | ||
| 582 | } | ||
| 583 | |||
| 584 | int | ||
| 585 | mv_strip_u32 (struct mv *mv, u32 val, int flg) | ||
| 586 | { | ||
| 587 | UNUSED (mv); | ||
| 588 | UNUSED (val); | ||
| 589 | UNUSED (flg); | ||
| 590 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 591 | return 0; | ||
| 592 | } | ||
| 593 | |||
| 594 | int | ||
| 595 | mv_strip_u64 (struct mv *mv, u64 val, int flg) | ||
| 596 | { | ||
| 597 | UNUSED (mv); | ||
| 598 | UNUSED (val); | ||
| 599 | UNUSED (flg); | ||
| 600 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 601 | return 0; | ||
| 602 | } | ||
| 603 | |||
| 604 | int | ||
| 605 | mv_strip_any (struct mv *mv, struct mv set, int flg) | ||
| 606 | { | ||
| 607 | UNUSED (mv); | ||
| 608 | UNUSED (set); | ||
| 609 | UNUSED (flg); | ||
| 610 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 611 | return 0; | ||
| 612 | } | ||
| 613 | |||
| 614 | // mv_strip_upto* (flg: [MV_BEG | MV_END] | MV_REP) | ||
| 615 | |||
| 616 | int | ||
| 617 | mv_strip_upto (struct mv *mv, struct mv del, int flg) | ||
| 618 | { | ||
| 619 | UNUSED (mv); | ||
| 620 | UNUSED (del); | ||
| 621 | UNUSED (flg); | ||
| 622 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 623 | return 0; | ||
| 624 | } | ||
| 625 | |||
| 626 | int | ||
| 627 | mv_strip_upto_u8 (struct mv *mv, u8 del, int flg) | ||
| 628 | { | ||
| 629 | UNUSED (mv); | ||
| 630 | UNUSED (del); | ||
| 631 | UNUSED (flg); | ||
| 632 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 633 | return 0; | ||
| 634 | } | ||
| 635 | |||
| 636 | int | ||
| 637 | mv_strip_upto_u16 (struct mv *mv, u16 del, int flg) | ||
| 638 | { | ||
| 639 | UNUSED (mv); | ||
| 640 | UNUSED (del); | ||
| 641 | UNUSED (flg); | ||
| 642 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 643 | return 0; | ||
| 644 | } | ||
| 645 | |||
| 646 | int | ||
| 647 | mv_strip_upto_u32 (struct mv *mv, u32 del, int flg) | ||
| 648 | { | ||
| 649 | UNUSED (mv); | ||
| 650 | UNUSED (del); | ||
| 651 | UNUSED (flg); | ||
| 652 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 653 | return 0; | ||
| 654 | } | ||
| 655 | |||
| 656 | int | ||
| 657 | mv_strip_upto_u64 (struct mv *mv, u64 del, int flg) | ||
| 658 | { | ||
| 659 | UNUSED (mv); | ||
| 660 | UNUSED (del); | ||
| 661 | UNUSED (flg); | ||
| 662 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 663 | return 0; | ||
| 664 | } | ||
| 665 | |||
| 666 | int | ||
| 667 | mv_strip_upto_any (struct mv *mv, struct mv del_set, int flg) | ||
| 668 | { | ||
| 669 | UNUSED (mv); | ||
| 670 | UNUSED (del_set); | ||
| 671 | UNUSED (flg); | ||
| 672 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 673 | return 0; | ||
| 674 | } | ||
| 675 | |||
| 676 | // mv_peek* (flg: [MV_BEG] ^ MV_END) | ||
| 677 | |||
| 678 | struct mv | ||
| 679 | mv_peek (struct mv mv, usz n, int flg) | ||
| 680 | { | ||
| 681 | DBG_ASSERT_MV (mv); | ||
| 682 | |||
| 683 | n = MIN (n, mv_sz (mv)); | ||
| 684 | if (flg & MV_END) return mv_from (mv.end - n, mv.end); | ||
| 685 | else return mv_from (mv.beg, mv.beg + n); | ||
| 686 | } | ||
| 687 | |||
| 688 | u8 | ||
| 689 | mv_peek_u8 (struct mv mv, int flg) | ||
| 690 | { | ||
| 691 | DBG_ASSERT (!mv_is_empty (mv)); | ||
| 692 | |||
| 693 | if (flg & MV_END) | ||
| 694 | return mv.end[-1]; | ||
| 695 | return *mv.beg; | ||
| 696 | } | ||
| 697 | |||
| 698 | u16 | ||
| 699 | mv_peek_u16 (struct mv mv, int flg) | ||
| 700 | { | ||
| 701 | UNUSED (mv); | ||
| 702 | UNUSED (flg); | ||
| 703 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 704 | return 0; | ||
| 705 | } | ||
| 706 | |||
| 707 | u32 | ||
| 708 | mv_peek_u32 (struct mv mv, int flg) | ||
| 709 | { | ||
| 710 | UNUSED (mv); | ||
| 711 | UNUSED (flg); | ||
| 712 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 713 | return 0; | ||
| 714 | } | ||
| 715 | |||
| 716 | u64 | ||
| 717 | mv_peek_u64 (struct mv mv, int flg) | ||
| 718 | { | ||
| 719 | UNUSED (mv); | ||
| 720 | UNUSED (flg); | ||
| 721 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 722 | return 0; | ||
| 723 | } | ||
| 724 | |||
| 725 | struct mv | ||
| 726 | mv_peek_cstr (struct mv mv) | ||
| 727 | { | ||
| 728 | return mv_peek_upto_u8 (mv, '\0', 0); | ||
| 729 | } | ||
| 730 | |||
| 731 | // mv_peek_upto* (flg: [MV_BEG] ^ MV_END | MV_INC) | ||
| 732 | |||
| 733 | struct mv | ||
| 734 | mv_peek_upto (struct mv mv, struct mv del, int flg) | ||
| 735 | { | ||
| 736 | UNUSED (mv); | ||
| 737 | UNUSED (del); | ||
| 738 | UNUSED (flg); | ||
| 739 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 740 | return MV_NULL; | ||
| 741 | } | ||
| 742 | |||
| 743 | struct mv | ||
| 744 | mv_peek_upto_u8 (struct mv mv, u8 del, int flg) | ||
| 745 | { | ||
| 746 | DBG_ASSERT_MV (mv); | ||
| 747 | |||
| 748 | const u8 *hit = mv_find_u8 (mv, del, flg); | ||
| 749 | if (!hit) return mv; | ||
| 750 | |||
| 751 | if (flg & MV_END) { | ||
| 752 | if (~flg & MV_INC_END) hit += 1; | ||
| 753 | if ( flg & MV_INV) return mv_from (mv.beg, hit); | ||
| 754 | else return mv_from (hit, mv.end); | ||
| 755 | } else { | ||
| 756 | if ( flg & MV_INC_BEG) hit += 1; | ||
| 757 | if ( flg & MV_INV) return mv_from (hit, mv.end); | ||
| 758 | else return mv_from (mv.beg, hit); | ||
| 759 | } | ||
| 760 | } | ||
| 761 | |||
| 762 | struct mv | ||
| 763 | mv_peek_upto_u16 (struct mv mv, u16 del, int flg) | ||
| 764 | { | ||
| 765 | UNUSED (mv); | ||
| 766 | UNUSED (del); | ||
| 767 | UNUSED (flg); | ||
| 768 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 769 | return MV_NULL; | ||
| 770 | } | ||
| 771 | |||
| 772 | struct mv | ||
| 773 | mv_peek_upto_u32 (struct mv mv, u32 del, int flg) | ||
| 774 | { | ||
| 775 | UNUSED (mv); | ||
| 776 | UNUSED (del); | ||
| 777 | UNUSED (flg); | ||
| 778 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 779 | return MV_NULL; | ||
| 780 | } | ||
| 781 | |||
| 782 | struct mv | ||
| 783 | mv_peek_upto_u64 (struct mv mv, u64 del, int flg) | ||
| 784 | { | ||
| 785 | UNUSED (mv); | ||
| 786 | UNUSED (del); | ||
| 787 | UNUSED (flg); | ||
| 788 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 789 | return MV_NULL; | ||
| 790 | } | ||
| 791 | |||
| 792 | struct mv | ||
| 793 | mv_peek_upto_any (struct mv mv, struct mv del, int flg) | ||
| 794 | { | ||
| 795 | UNUSED (mv); | ||
| 796 | UNUSED (del); | ||
| 797 | UNUSED (flg); | ||
| 798 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 799 | return MV_NULL; | ||
| 800 | } | ||
| 801 | |||
| 802 | // mv_peek_in* (flg: [MV_BEG] ^ MV_END | MV_INC) | ||
| 803 | |||
| 804 | struct mv | ||
| 805 | mv_peek_in (struct mv *mv, struct mv lhs, struct mv rhs, int flg) | ||
| 806 | { | ||
| 807 | UNUSED (mv); | ||
| 808 | UNUSED (lhs); | ||
| 809 | UNUSED (rhs); | ||
| 810 | UNUSED (flg); | ||
| 811 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 812 | return MV_NULL; | ||
| 813 | } | ||
| 814 | |||
| 815 | struct mv | ||
| 816 | mv_peek_in_u8 (struct mv *mv, u8 lhs, u8 rhs, int flg) | ||
| 817 | { | ||
| 818 | UNUSED (mv); | ||
| 819 | UNUSED (lhs); | ||
| 820 | UNUSED (rhs); | ||
| 821 | UNUSED (flg); | ||
| 822 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 823 | return MV_NULL; | ||
| 824 | } | ||
| 825 | |||
| 826 | struct mv | ||
| 827 | mv_peek_in_u16 (struct mv *mv, u16 lhs, u16 rhs, int flg) | ||
| 828 | { | ||
| 829 | UNUSED (mv); | ||
| 830 | UNUSED (lhs); | ||
| 831 | UNUSED (rhs); | ||
| 832 | UNUSED (flg); | ||
| 833 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 834 | return MV_NULL; | ||
| 835 | } | ||
| 836 | |||
| 837 | struct mv | ||
| 838 | mv_peek_in_u32 (struct mv *mv, u32 lhs, u32 rhs, int flg) | ||
| 839 | { | ||
| 840 | UNUSED (mv); | ||
| 841 | UNUSED (lhs); | ||
| 842 | UNUSED (rhs); | ||
| 843 | UNUSED (flg); | ||
| 844 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 845 | return MV_NULL; | ||
| 846 | } | ||
| 847 | |||
| 848 | struct mv | ||
| 849 | mv_peek_in_u64 (struct mv *mv, u64 lhs, u64 rhs, int flg) | ||
| 850 | { | ||
| 851 | UNUSED (mv); | ||
| 852 | UNUSED (lhs); | ||
| 853 | UNUSED (rhs); | ||
| 854 | UNUSED (flg); | ||
| 855 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 856 | return MV_NULL; | ||
| 857 | } | ||
| 858 | |||
| 859 | struct mv | ||
| 860 | mv_peek_in_any (struct mv *mv, struct mv lhs, struct mv rhs, int flg) | ||
| 861 | { | ||
| 862 | UNUSED (mv); | ||
| 863 | UNUSED (lhs); | ||
| 864 | UNUSED (rhs); | ||
| 865 | UNUSED (flg); | ||
| 866 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 867 | return MV_NULL; | ||
| 868 | } | ||
| 869 | |||
| 870 | // mv_chop* (flg: [MV_BEG] ^ MV_END | MV_INC) | ||
| 871 | |||
| 872 | struct mv | ||
| 873 | mv_chop (struct mv *mv, usz n, int flg) | ||
| 874 | { | ||
| 875 | DBG_ASSERT_NN ( mv); | ||
| 876 | DBG_ASSERT_MV (*mv); | ||
| 877 | DBG_ASSERT_LE (n, mv_sz (*mv), "%lu", "%lu"); | ||
| 878 | DBG_ASSERT_MSG (0 == (flg & ~(MV_BEG | MV_END | MV_INV)), "%0*b", MV_BIT_CNT, flg); | ||
| 879 | if (flg & MV_END) return mv_exclude (mv, mv_from (mv->end - n, mv->end), flg | MV_INC_END); | ||
| 880 | else return mv_exclude (mv, mv_from (mv->beg, mv->beg + n), flg | MV_INC_BEG); | ||
| 881 | } | ||
| 882 | |||
| 883 | u8 | ||
| 884 | mv_chop_u8 (struct mv *mv, int flg) | ||
| 885 | { | ||
| 886 | DBG_ASSERT_NN ( mv); | ||
| 887 | DBG_ASSERT_MV (*mv); | ||
| 888 | DBG_ASSERT_LE (1, mv_sz (*mv), "%lu", "%lu"); | ||
| 889 | DBG_ASSERT (flg & ~(MV_BEG | MV_END)); | ||
| 890 | if (flg & MV_END) return *--mv->end; | ||
| 891 | else return * mv->beg++; | ||
| 892 | } | ||
| 893 | |||
| 894 | u16 | ||
| 895 | mv_chop_u16 (struct mv *mv, int flg) | ||
| 896 | { | ||
| 897 | UNUSED (mv); | ||
| 898 | UNUSED (flg); | ||
| 899 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 900 | return 0; | ||
| 901 | } | ||
| 902 | |||
| 903 | u32 | ||
| 904 | mv_chop_u32 (struct mv *mv, int flg) | ||
| 905 | { | ||
| 906 | UNUSED (mv); | ||
| 907 | UNUSED (flg); | ||
| 908 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 909 | return 0; | ||
| 910 | } | ||
| 911 | |||
| 912 | u64 | ||
| 913 | mv_chop_u64 (struct mv *mv, int flg) | ||
| 914 | { | ||
| 915 | UNUSED (mv); | ||
| 916 | UNUSED (flg); | ||
| 917 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 918 | return 0; | ||
| 919 | } | ||
| 920 | |||
| 921 | struct mv | ||
| 922 | mv_chop_cstr (struct mv *mv) | ||
| 923 | { | ||
| 924 | struct mv cstr = mv_peek_cstr (*mv); | ||
| 925 | mv->beg = cstr.end + 1; | ||
| 926 | return cstr; | ||
| 927 | } | ||
| 928 | |||
| 929 | // mv_chop_upto* (flg: [MV_BEG] ^ MV_END | MV_INC_BEG | MV_INC_END | MV_INV) | ||
| 930 | |||
| 931 | struct mv | ||
| 932 | mv_exclude (struct mv *mv, struct mv exc, int flg) | ||
| 933 | { | ||
| 934 | DBG_ASSERT_NN ( mv); | ||
| 935 | DBG_ASSERT_MV (*mv); | ||
| 936 | DBG_ASSERT_MV (exc); | ||
| 937 | |||
| 938 | struct mv beg, end; | ||
| 939 | if (mv_is_null (exc)) { | ||
| 940 | beg = *mv; | ||
| 941 | end = mv_from (mv->end, mv->end); | ||
| 942 | } else { | ||
| 943 | DBG_ASSERT_IN (mv->beg, exc.beg, mv->end, "%p", "%p", "%p"); | ||
| 944 | DBG_ASSERT_IN (mv->beg, exc.end, mv->end, "%p", "%p", "%p"); | ||
| 945 | |||
| 946 | beg = (flg & MV_INC_BEG) | ||
| 947 | ? mv_from (mv->beg, exc.end) | ||
| 948 | : mv_from (mv->beg, exc.beg); | ||
| 949 | end = (flg & MV_INC_END) | ||
| 950 | ? mv_from (exc.beg, mv->end) | ||
| 951 | : mv_from (exc.end, mv->end); | ||
| 952 | } | ||
| 953 | |||
| 954 | if ((flg & MV_END) != (flg & MV_INV)) { *mv = beg; return end; } | ||
| 955 | else { *mv = end; return beg; } | ||
| 956 | } | ||
| 957 | |||
| 958 | struct mv | ||
| 959 | mv_chop_upto (struct mv *mv, struct mv del, int flg) | ||
| 960 | { | ||
| 961 | UNUSED (mv); | ||
| 962 | UNUSED (del); | ||
| 963 | UNUSED (flg); | ||
| 964 | DBG_DIE ("TODO(cmmm): implement"); | ||
| 965 | return MV_NULL; | ||
| 966 | } | ||
| 967 | |||
| 968 | struct mv | ||
| 969 | mv_chop_upto_u8 (struct mv *mv, u8 del, int flg) | ||
| 970 | { | ||
| 971 | DBG_ASSERT_NN (mv); | ||
| 972 | const u8 *hit = mv_find_u8 (*mv, del, flg); | ||
| 973 | if (!hit) return mv_exclude (mv, MV_NULL, flg); | ||
| 974 | return mv_exclude (mv, mv_from (hit, hit + sizeof (*hit)), flg); | ||
| 975 | } | ||
| 976 | |||
| 977 | struct mv | ||
| 978 | mv_chop_upto_u16 (struct mv *mv, u16 del, int flg) | ||
| 979 | { | ||
| 980 | DBG_ASSERT_NN (mv); | ||
| 981 | const u16 *hit = mv_find_u16 (*mv, del, flg); | ||
| 982 | if (!hit) return mv_exclude (mv, MV_NULL, flg); | ||
| 983 | return mv_exclude (mv, mv_from (hit, hit + sizeof (*hit)), flg); | ||
| 984 | } | ||
| 985 | |||
| 986 | struct mv | ||
| 987 | mv_chop_upto_u32 (struct mv *mv, u32 del, int flg) | ||
| 988 | { | ||
| 989 | DBG_ASSERT_NN (mv); | ||
| 990 | const u32 *hit = mv_find_u32 (*mv, del, flg); | ||
| 991 | if (!hit) return mv_exclude (mv, MV_NULL, flg); | ||
| 992 | return mv_exclude (mv, mv_from (hit, hit + sizeof (*hit)), flg); | ||
| 993 | } | ||
| 994 | |||
| 995 | struct mv | ||
| 996 | mv_chop_upto_u64 (struct mv *mv, u64 del, int flg) | ||
| 997 | { | ||
| 998 | DBG_ASSERT_NN (mv); | ||
| 999 | const u64 *hit = mv_find_u64 (*mv, del, flg); | ||
| 1000 | if (!hit) return mv_exclude (mv, MV_NULL, flg); | ||
| 1001 | return mv_exclude (mv, mv_from (hit, hit + sizeof (*hit)), flg); | ||
| 1002 | } | ||
| 1003 | |||
| 1004 | struct mv | ||
| 1005 | mv_chop_upto_any (struct mv *mv, struct mv del_set, int flg) | ||
| 1006 | { | ||
| 1007 | DBG_ASSERT_NN (mv); | ||
| 1008 | const u8 *hit = mv_find_any (*mv, del_set, flg); | ||
| 1009 | if (!hit) return mv_exclude (mv, MV_NULL, flg); | ||
| 1010 | return mv_exclude (mv, mv_from (hit, hit + sizeof (*hit)), flg); | ||
| 1011 | } | ||
| 1012 | |||
| 1013 | #ifndef MV_NO_FEAT_GPA | ||
| 1014 | #include "gpa.h" | ||
| 1015 | |||
| 1016 | struct mb | ||
| 1017 | mv_dup (struct mv mv) | ||
| 1018 | { | ||
| 1019 | DBG_ASSERT_NN (mv.beg); | ||
| 1020 | DBG_ASSERT_LT (mv.beg, mv.end, "%p", "%p"); | ||
| 1021 | |||
| 1022 | usz sz = mv_sz (mv); | ||
| 1023 | struct mb mb = { CTX_GRAB (sz, 1), sz }; | ||
| 1024 | mv_copy_to_mb (mv, mb); | ||
| 1025 | |||
| 1026 | return mb; | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | struct mb | ||
| 1030 | mv_dup0 (struct mv mv) | ||
| 1031 | { | ||
| 1032 | DBG_ASSERT_NN (mv.beg); | ||
| 1033 | DBG_ASSERT_LT (mv.beg, mv.end, "%p", "%p"); | ||
| 1034 | |||
| 1035 | usz sz = mv_sz (mv); | ||
| 1036 | u8 *ptr = CTX_GRAB (sz, 1 + 1); | ||
| 1037 | mv_copy_to (mv, ptr); | ||
| 1038 | ptr[sz] = '\0'; | ||
| 1039 | |||
| 1040 | return MB_FROM (ptr, sz + 1); | ||
| 1041 | } | ||
| 1042 | |||
| 1043 | #endif // MV_NO_FEAT_GPA | ||
| 1044 | |||
| 1045 | #ifndef MV_NO_FEAT_UNISTD | ||
| 1046 | |||
| 1047 | #include <unistd.h> | ||
| 1048 | int | ||
| 1049 | mv_write (struct mv mv, int fd) | ||
| 1050 | { | ||
| 1051 | return write (fd, mv.beg, mv_sz (mv)); | ||
| 1052 | } | ||
| 1053 | |||
| 1054 | #endif // MV_NO_FEAT_UNISTD | ||
| 1055 | |||
| 1056 | #ifndef MV_NO_FEAT_STDIO | ||
| 1057 | #include <stdio.h> | ||
| 1058 | |||
| 1059 | usz | ||
| 1060 | mv_fwrite (struct mv mv, FILE *strm) | ||
| 1061 | { | ||
| 1062 | return fwrite (mv.beg, 1, mv_sz (mv), strm); | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | #endif // MV_NO_FEAT_STDIO | ||
| 1066 | |||
| 1067 | #ifndef MV_NO_FEAT_ARR | ||
| 1068 | |||
| 1069 | struct mb | ||
| 1070 | _mv_arr_join (struct mv_arr mvs, struct mv sep, int zero) | ||
| 1071 | { | ||
| 1072 | // for (usz i = 0; i < mvs.len; ++i) | ||
| 1073 | // DBG_TRACE ("info", "i: %lu, sz: %lu, mv: %.*s", i, mv_sz (mvs.arr[i]), MV_FMT (mvs.arr[i])); | ||
| 1074 | |||
| 1075 | usz sz = (mvs.len - 1)*mv_sz (sep); | ||
| 1076 | for (usz i = 0; i < mvs.len; ++i) | ||
| 1077 | sz += mv_sz (mvs.arr[i]); | ||
| 1078 | |||
| 1079 | struct mb mb = { CTX_GRAB (1, sz + zero), sz + zero }; | ||
| 1080 | |||
| 1081 | u8 *ptr = mb.ptr; | ||
| 1082 | memcpy (ptr, mvs.arr[0].beg, mv_sz (mvs.arr[0])); | ||
| 1083 | ptr += mv_sz (mvs.arr[0]); | ||
| 1084 | for (usz i = 1; i < mvs.len; ++i) { | ||
| 1085 | memcpy (ptr, sep.beg, mv_sz (sep)); | ||
| 1086 | ptr += mv_sz (sep); | ||
| 1087 | memcpy (ptr, mvs.arr[i].beg, mv_sz (mvs.arr[i])); | ||
| 1088 | ptr += mv_sz (mvs.arr[i]); | ||
| 1089 | } | ||
| 1090 | |||
| 1091 | if (zero) | ||
| 1092 | *ptr = '\0'; | ||
| 1093 | |||
| 1094 | return mb; | ||
| 1095 | |||
| 1096 | } | ||
| 1097 | |||
| 1098 | #endif // MV_NO_FEAT_ARR | ||
| 1099 | |||
| 1100 | // mv.c ends here | ||
