From d9dd01af7a1aad5cba1ac937e7abfd19309fdeb8 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Fri, 6 Nov 2026 05:46:04 +0300 Subject: feat(mv): add memory view --- include/mv.h | 228 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 include/mv.h (limited to 'include') 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 @@ +// mv.h --- Memory View -*- C -*- +// +// Copyright (c) 2026 Martin Michalec +// +// 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 + +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 -- cgit v1.3.1