// mv.c --- Memory View -*- C -*- // // Copyright (c) 2026 Martin Michalec // // SPDX-License-Identifier: MIT // // This file is part of COSMOS Codebase. // // Commentary: // // Code: #include "mv.h" #include "dbg.h" // utility usz mv_sz (struct mv mv) { DBG_ASSERT_MV (mv); return mv.end - mv.beg; } int mv_trim (struct mv *mv) { UNUSED (mv); DBG_DIE ("TODO(cmmm): implement"); return 0; } #include // TEMP(cmmm): remove dep void mv_copy_to (struct mv mv, void *ptr) { DBG_ASSERT_MV (mv); DBG_ASSERT_NN (ptr); memcpy (ptr, mv.beg, mv_sz (mv)); } void mv_copy_to_mb (struct mv mv, struct mb mb) { DBG_ASSERT_MV (mv); DBG_ASSERT_NN (mb.ptr); DBG_ASSERT_LE (mv_sz (mv), mb.sz, "%lu", "%lu"); memcpy (mb.ptr, mv.beg, MIN (mv_sz (mv), mb.sz)); } struct mv mv_basename (struct mv mv) { DBG_ASSERT (!mv_is_empty (mv)); mv_strip_u8 (&mv, '/', MV_END | MV_REP); return mv_peek_upto_u8 (mv, '/', MV_END); } struct mv mv_dirname (struct mv mv) { DBG_ASSERT_MV (mv); if (mv_is_empty (mv)) return MV0 ("."); DBG_TRACE ("info", "%.*s", MV_FMT (mv)); mv_strip_u8 (&mv, '/', MV_END | MV_REP); DBG_TRACE ("info", "%.*s", MV_FMT (mv)); mv_chop_upto_u8 (&mv, '/', MV_END | MV_INC_END | MV_INV); DBG_TRACE ("info", "%.*s", MV_FMT (mv)); mv_strip_u8 (&mv, '/', MV_END | MV_REP); DBG_TRACE ("info", "%.*s", MV_FMT (mv)); if (mv_is_empty (mv)) return MV0 ("."); return mv; } int mv_cmp_fun (const void *lhs, const void *rhs) { DBG_ASSERT_NN (lhs); DBG_ASSERT_NN (rhs); return mv_cmp (*(const struct mv *) lhs, *(const struct mv *) rhs); } // mv_from* struct mv mv_from (const void *beg, const void *end) { struct mv mv; mv.beg = beg; mv.end = end; DBG_ASSERT_MV (mv); return mv; } struct mv mv_from_mb (struct mb mb) { return mv_from (mb.ptr, (u8 *) mb.ptr + mb.sz); } struct mv mv_from_mb0 (struct mb mb) { return mv_from (mb.ptr, (u8 *) mb.ptr + mb.sz - 1); } struct mv mv_from_cstr (const char *cstr) { struct mv mv = mv_from (cstr, cstr); if (LIKELY (cstr != NULL)) { while (*mv.end != '\0') mv.end += 1; } return mv; } // mv_to_* u64 mv_to_u64 (struct mv mv, int base) { DBG_ASSERT_MV (mv); DBG_ASSERT_IN (2, base, 36, "%d", "%d", "%d"); DBG_ASSERT_IN_MSG (2, base, 10, "%d", "%d", "%d", "other bases yet unhandled"); u64 val = 0; while (mv.beg < mv.end && '0' <= *mv.beg && *mv.beg <= '0' + base - 1) { u64 digit = *mv.beg - '0'; // overflow protection if (val > (U64_MAX - digit)/base) return U64_MAX; val = val*base + digit; mv.beg += 1; } if (mv.beg != mv.end) return U64_MAX; return val; } i64 mv_to_i64 (struct mv mv, int base) { DBG_DIE ("TODO(cmmm): implement"); return 0; } f64 mv_to_f64 (struct mv mv, int base) { DBG_DIE ("TODO(cmmm): implement"); return 0; } // mv_is_* int mv_is_null (struct mv mv) { DBG_ASSERT_MV (mv); return mv.beg == NULL; } int mv_is_empty (struct mv mv) { DBG_ASSERT_MV (mv); return mv.beg == mv.end; } // comparison int mv_eq (struct mv lhs, struct mv rhs) { DBG_ASSERT_MV (lhs); DBG_ASSERT_MV (rhs); return (lhs.beg == rhs.beg && lhs.end == rhs.end); } int mv_equal (struct mv lhs, struct mv rhs) { DBG_ASSERT_MV (lhs); DBG_ASSERT_MV (rhs); if (mv_sz (lhs) != mv_sz (rhs)) return 0; if (lhs.beg == rhs.beg) return 1; return mv_cmp (lhs, rhs) == 0; } int mv_cmp (struct mv lhs, struct mv rhs) { DBG_ASSERT_MV (lhs); DBG_ASSERT_MV (rhs); return strncmp (lhs.beg, rhs.beg, MIN (mv_sz (lhs), mv_sz (rhs))); } // mv_contains* int mv_contains (struct mv mv, struct mv seq) { UNUSED (mv); UNUSED (seq); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_contains_u8 (struct mv mv, u8 seq) { UNUSED (mv); UNUSED (seq); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_contains_u16 (struct mv mv, u16 seq) { UNUSED (mv); UNUSED (seq); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_contains_u32 (struct mv mv, u32 seq) { UNUSED (mv); UNUSED (seq); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_contains_u64 (struct mv mv, u64 seq) { UNUSED (mv); UNUSED (seq); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_contains_any (struct mv mv, struct mv set) { UNUSED (mv); UNUSED (set); DBG_DIE ("TODO(cmmm): implement"); return 0; } // mv_affixed* (flg: [MV_BEG | MV_END]) int mv_affixed (struct mv mv, struct mv affix, int flg) { DBG_ASSERT_MV (mv); DBG_ASSERT_MV (affix); DBG_ASSERT_EZ_MSG (flg & ~(MV_BEG | MV_END), "%d", "%b*u", MV_BIT_CNT, flg); usz affix_sz = mv_sz (affix); if (flg & MV_BEG) { if (mv_equal (mv_peek (mv, affix_sz, MV_BEG), affix)) return 1; } if (flg & MV_END) { if (mv_equal (mv_peek (mv, affix_sz, MV_END), affix)) return 1; } return 0; } int mv_affixed_u8 (struct mv mv, u8 affix, int flg) { UNUSED (mv); UNUSED (affix); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_affixed_u16 (struct mv mv, u16 affix, int flg) { UNUSED (mv); UNUSED (affix); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_affixed_u32 (struct mv mv, u32 affix, int flg) { UNUSED (mv); UNUSED (affix); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_affixed_u64 (struct mv mv, u64 affix, int flg) { UNUSED (mv); UNUSED (affix); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_affixed_any (struct mv mv, struct mv affix, int flg) { UNUSED (mv); UNUSED (affix); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } // mv_affixed2* int mv_affixed2 (struct mv mv, struct mv prefix, struct mv suffix) { UNUSED (mv); UNUSED (prefix); UNUSED (suffix); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_affixed2_u8 (struct mv mv, u8 prefix, u8 suffix) { UNUSED (mv); UNUSED (prefix); UNUSED (suffix); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_affixed2_u16 (struct mv mv, u16 prefix, u16 suffix) { UNUSED (mv); UNUSED (prefix); UNUSED (suffix); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_affixed2_u32 (struct mv mv, u32 prefix, u32 suffix) { UNUSED (mv); UNUSED (prefix); UNUSED (suffix); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_affixed2_u64 (struct mv mv, u64 prefix, u64 suffix) { UNUSED (mv); UNUSED (prefix); UNUSED (suffix); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_affixed2_any (struct mv mv, struct mv prefix_set, struct mv suffix_set) { UNUSED (mv); UNUSED (prefix_set); UNUSED (suffix_set); DBG_DIE ("TODO(cmmm): implement"); return 0; } // mv_at ([MV_BEG] ^ MV_END) const void * mv_at (struct mv mv, usz idx, int flg) { DBG_ASSERT_MV (mv); DBG_ASSERT_LT (mv_sz (mv), idx, "%lu", "%lu"); if (flg & MV_END) return mv.end - idx - 1; else return mv.beg + idx; } // mv_shrink ([MV_BEG | MV_END]) void mv_shrink (struct mv *mv, usz sz, int flg) { DBG_ASSERT_NN ( mv); DBG_ASSERT_MV (*mv); if (!(flg & (MV_BEG | MV_END))) flg |= MV_BEG | MV_END; if (flg & MV_BEG) { DBG_ASSERT_LT (mv_sz (*mv), sz, "%lu", "%lu"); mv->beg += sz; } if (flg & MV_END) { DBG_ASSERT_LT (mv_sz (*mv), sz, "%lu", "%lu"); mv->end -= sz; } } // mv_find* (flg: [MV_BEG] ^ MV_END) struct mv mv_find (struct mv mv, struct mv ndl, int flg) { UNUSED (mv); UNUSED (ndl); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } const u8 * mv_find_u8 (struct mv mv, u8 ndl, int flg) { DBG_ASSERT_MV (mv); const u8 *ptr; if (flg & MV_END) { DBG_ASSERT_MSG (~flg & MV_BEG, "MV_BEG ^ MV_END!"); for (ptr = mv.end - 1; ptr >= mv.beg; --ptr) if (*ptr == ndl) return ptr; } else { // [MV_BEG] for (ptr = mv.beg; ptr < mv.end; ++ptr) if (*ptr == ndl) return ptr; } return NULL; } const u16 * mv_find_u16 (struct mv mv, u16 ndl, int flg) { DBG_ASSERT_MV (mv); UNUSED (mv); UNUSED (ndl); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return NULL; } const u32 * mv_find_u32 (struct mv mv, u32 ndl, int flg) { UNUSED (mv); UNUSED (ndl); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return NULL; } const u64 * mv_find_u64 (struct mv mv, u64 ndl, int flg) { UNUSED (mv); UNUSED (ndl); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return NULL; } const u8 * mv_find_any (struct mv mv, struct mv ndl_set, int flg) { DBG_ASSERT_MV (mv); const u8 *ptr, *ndl; if (flg & MV_END) { DBG_ASSERT_MSG (~flg & MV_BEG, "MV_BEG ^ MV_END!"); for (ptr = mv.end - 1; ptr >= mv.beg; --ptr) { for (ndl = ndl_set.beg; ndl < ndl_set.end; ++ndl) if (*ptr == *ndl) return ptr; } } else /* [MV_BEG] */ { for (ptr = mv.beg; ptr < mv.end; ++ptr) { for (ndl = ndl_set.beg; ndl < ndl_set.end; ++ndl) if (*ptr == *ndl) return ptr; } } return NULL; } // mv_strip* (flg: [MV_BEG | MV_END] | MV_REP) int mv_strip (struct mv *mv, struct mv seq, int flg) { UNUSED (mv); UNUSED (seq); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_strip_n (struct mv *mv, usz n, int flg) { UNUSED (mv); UNUSED (n); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_strip_u8 (struct mv *mv, u8 val, int flg) { DBG_ASSERT_NN ( mv); DBG_ASSERT_MV (*mv); if ((flg & (MV_BEG | MV_END)) == 0) flg |= MV_BEG | MV_END; int stripped = 0; if (flg & MV_BEG) { if (mv->beg[+0] == val) mv->beg += 1; else goto try_end; stripped = 1; if (flg & MV_REP) while (mv->beg[+0] == val) mv->beg += 1; } try_end: if (flg & MV_END) { if (mv->end[-1] == val) mv->end += 1; else return stripped; stripped = 1; if (flg & MV_REP) while (mv->end[-1] == val) mv->end += 1; } return stripped; } int mv_strip_u16 (struct mv *mv, u16 val, int flg) { UNUSED (mv); UNUSED (val); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_strip_u32 (struct mv *mv, u32 val, int flg) { UNUSED (mv); UNUSED (val); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_strip_u64 (struct mv *mv, u64 val, int flg) { UNUSED (mv); UNUSED (val); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_strip_any (struct mv *mv, struct mv set, int flg) { UNUSED (mv); UNUSED (set); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } // mv_strip_upto* (flg: [MV_BEG | MV_END] | MV_REP) int mv_strip_upto (struct mv *mv, struct mv del, int flg) { UNUSED (mv); UNUSED (del); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_strip_upto_u8 (struct mv *mv, u8 del, int flg) { UNUSED (mv); UNUSED (del); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_strip_upto_u16 (struct mv *mv, u16 del, int flg) { UNUSED (mv); UNUSED (del); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_strip_upto_u32 (struct mv *mv, u32 del, int flg) { UNUSED (mv); UNUSED (del); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_strip_upto_u64 (struct mv *mv, u64 del, int flg) { UNUSED (mv); UNUSED (del); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } int mv_strip_upto_any (struct mv *mv, struct mv del_set, int flg) { UNUSED (mv); UNUSED (del_set); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } // mv_peek* (flg: [MV_BEG] ^ MV_END) struct mv mv_peek (struct mv mv, usz n, int flg) { DBG_ASSERT_MV (mv); n = MIN (n, mv_sz (mv)); if (flg & MV_END) return mv_from (mv.end - n, mv.end); else return mv_from (mv.beg, mv.beg + n); } u8 mv_peek_u8 (struct mv mv, int flg) { DBG_ASSERT (!mv_is_empty (mv)); if (flg & MV_END) return mv.end[-1]; return *mv.beg; } u16 mv_peek_u16 (struct mv mv, int flg) { UNUSED (mv); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } u32 mv_peek_u32 (struct mv mv, int flg) { UNUSED (mv); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } u64 mv_peek_u64 (struct mv mv, int flg) { UNUSED (mv); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } struct mv mv_peek_cstr (struct mv mv) { return mv_peek_upto_u8 (mv, '\0', 0); } // mv_peek_upto* (flg: [MV_BEG] ^ MV_END | MV_INC) struct mv mv_peek_upto (struct mv mv, struct mv del, int flg) { UNUSED (mv); UNUSED (del); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } struct mv mv_peek_upto_u8 (struct mv mv, u8 del, int flg) { DBG_ASSERT_MV (mv); const u8 *hit = mv_find_u8 (mv, del, flg); if (!hit) return mv; if (flg & MV_END) { if (~flg & MV_INC_END) hit += 1; if ( flg & MV_INV) return mv_from (mv.beg, hit); else return mv_from (hit, mv.end); } else { if ( flg & MV_INC_BEG) hit += 1; if ( flg & MV_INV) return mv_from (hit, mv.end); else return mv_from (mv.beg, hit); } } struct mv mv_peek_upto_u16 (struct mv mv, u16 del, int flg) { UNUSED (mv); UNUSED (del); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } struct mv mv_peek_upto_u32 (struct mv mv, u32 del, int flg) { UNUSED (mv); UNUSED (del); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } struct mv mv_peek_upto_u64 (struct mv mv, u64 del, int flg) { UNUSED (mv); UNUSED (del); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } struct mv mv_peek_upto_any (struct mv mv, struct mv del, int flg) { UNUSED (mv); UNUSED (del); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } // mv_peek_in* (flg: [MV_BEG] ^ MV_END | MV_INC) struct mv mv_peek_in (struct mv *mv, struct mv lhs, struct mv rhs, int flg) { UNUSED (mv); UNUSED (lhs); UNUSED (rhs); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } struct mv mv_peek_in_u8 (struct mv *mv, u8 lhs, u8 rhs, int flg) { UNUSED (mv); UNUSED (lhs); UNUSED (rhs); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } struct mv mv_peek_in_u16 (struct mv *mv, u16 lhs, u16 rhs, int flg) { UNUSED (mv); UNUSED (lhs); UNUSED (rhs); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } struct mv mv_peek_in_u32 (struct mv *mv, u32 lhs, u32 rhs, int flg) { UNUSED (mv); UNUSED (lhs); UNUSED (rhs); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } struct mv mv_peek_in_u64 (struct mv *mv, u64 lhs, u64 rhs, int flg) { UNUSED (mv); UNUSED (lhs); UNUSED (rhs); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } struct mv mv_peek_in_any (struct mv *mv, struct mv lhs, struct mv rhs, int flg) { UNUSED (mv); UNUSED (lhs); UNUSED (rhs); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } // mv_chop* (flg: [MV_BEG] ^ MV_END | MV_INC) struct mv mv_chop (struct mv *mv, usz n, int flg) { DBG_ASSERT_NN ( mv); DBG_ASSERT_MV (*mv); DBG_ASSERT_LE (n, mv_sz (*mv), "%lu", "%lu"); DBG_ASSERT_MSG (0 == (flg & ~(MV_BEG | MV_END | MV_INV)), "%0*b", MV_BIT_CNT, flg); if (flg & MV_END) return mv_exclude (mv, mv_from (mv->end - n, mv->end), flg | MV_INC_END); else return mv_exclude (mv, mv_from (mv->beg, mv->beg + n), flg | MV_INC_BEG); } u8 mv_chop_u8 (struct mv *mv, int flg) { DBG_ASSERT_NN ( mv); DBG_ASSERT_MV (*mv); DBG_ASSERT_LE (1, mv_sz (*mv), "%lu", "%lu"); DBG_ASSERT (flg & ~(MV_BEG | MV_END)); if (flg & MV_END) return *--mv->end; else return * mv->beg++; } u16 mv_chop_u16 (struct mv *mv, int flg) { UNUSED (mv); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } u32 mv_chop_u32 (struct mv *mv, int flg) { UNUSED (mv); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } u64 mv_chop_u64 (struct mv *mv, int flg) { UNUSED (mv); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return 0; } struct mv mv_chop_cstr (struct mv *mv) { struct mv cstr = mv_peek_cstr (*mv); mv->beg = cstr.end + 1; return cstr; } // mv_chop_upto* (flg: [MV_BEG] ^ MV_END | MV_INC_BEG | MV_INC_END | MV_INV) struct mv mv_exclude (struct mv *mv, struct mv exc, int flg) { DBG_ASSERT_NN ( mv); DBG_ASSERT_MV (*mv); DBG_ASSERT_MV (exc); struct mv beg, end; if (mv_is_null (exc)) { beg = *mv; end = mv_from (mv->end, mv->end); } else { DBG_ASSERT_IN (mv->beg, exc.beg, mv->end, "%p", "%p", "%p"); DBG_ASSERT_IN (mv->beg, exc.end, mv->end, "%p", "%p", "%p"); beg = (flg & MV_INC_BEG) ? mv_from (mv->beg, exc.end) : mv_from (mv->beg, exc.beg); end = (flg & MV_INC_END) ? mv_from (exc.beg, mv->end) : mv_from (exc.end, mv->end); } if ((flg & MV_END) != (flg & MV_INV)) { *mv = beg; return end; } else { *mv = end; return beg; } } struct mv mv_chop_upto (struct mv *mv, struct mv del, int flg) { UNUSED (mv); UNUSED (del); UNUSED (flg); DBG_DIE ("TODO(cmmm): implement"); return MV_NULL; } struct mv mv_chop_upto_u8 (struct mv *mv, u8 del, int flg) { DBG_ASSERT_NN (mv); const u8 *hit = mv_find_u8 (*mv, del, flg); if (!hit) return mv_exclude (mv, MV_NULL, flg); return mv_exclude (mv, mv_from (hit, hit + sizeof (*hit)), flg); } struct mv mv_chop_upto_u16 (struct mv *mv, u16 del, int flg) { DBG_ASSERT_NN (mv); const u16 *hit = mv_find_u16 (*mv, del, flg); if (!hit) return mv_exclude (mv, MV_NULL, flg); return mv_exclude (mv, mv_from (hit, hit + sizeof (*hit)), flg); } struct mv mv_chop_upto_u32 (struct mv *mv, u32 del, int flg) { DBG_ASSERT_NN (mv); const u32 *hit = mv_find_u32 (*mv, del, flg); if (!hit) return mv_exclude (mv, MV_NULL, flg); return mv_exclude (mv, mv_from (hit, hit + sizeof (*hit)), flg); } struct mv mv_chop_upto_u64 (struct mv *mv, u64 del, int flg) { DBG_ASSERT_NN (mv); const u64 *hit = mv_find_u64 (*mv, del, flg); if (!hit) return mv_exclude (mv, MV_NULL, flg); return mv_exclude (mv, mv_from (hit, hit + sizeof (*hit)), flg); } struct mv mv_chop_upto_any (struct mv *mv, struct mv del_set, int flg) { DBG_ASSERT_NN (mv); const u8 *hit = mv_find_any (*mv, del_set, flg); if (!hit) return mv_exclude (mv, MV_NULL, flg); return mv_exclude (mv, mv_from (hit, hit + sizeof (*hit)), flg); } #ifndef MV_NO_FEAT_GPA #include "gpa.h" struct mb mv_dup (struct mv mv) { DBG_ASSERT_NN (mv.beg); DBG_ASSERT_LT (mv.beg, mv.end, "%p", "%p"); usz sz = mv_sz (mv); struct mb mb = { CTX_GRAB (sz, 1), sz }; mv_copy_to_mb (mv, mb); return mb; } struct mb mv_dup0 (struct mv mv) { DBG_ASSERT_NN (mv.beg); DBG_ASSERT_LT (mv.beg, mv.end, "%p", "%p"); usz sz = mv_sz (mv); u8 *ptr = CTX_GRAB (sz, 1 + 1); mv_copy_to (mv, ptr); ptr[sz] = '\0'; return MB_FROM (ptr, sz + 1); } #endif // MV_NO_FEAT_GPA #ifndef MV_NO_FEAT_UNISTD #include int mv_write (struct mv mv, int fd) { return write (fd, mv.beg, mv_sz (mv)); } #endif // MV_NO_FEAT_UNISTD #ifndef MV_NO_FEAT_STDIO #include usz mv_fwrite (struct mv mv, FILE *strm) { return fwrite (mv.beg, 1, mv_sz (mv), strm); } #endif // MV_NO_FEAT_STDIO #ifndef MV_NO_FEAT_ARR struct mb _mv_arr_join (struct mv_arr mvs, struct mv sep, int zero) { // for (usz i = 0; i < mvs.len; ++i) // DBG_TRACE ("info", "i: %lu, sz: %lu, mv: %.*s", i, mv_sz (mvs.arr[i]), MV_FMT (mvs.arr[i])); usz sz = (mvs.len - 1)*mv_sz (sep); for (usz i = 0; i < mvs.len; ++i) sz += mv_sz (mvs.arr[i]); struct mb mb = { CTX_GRAB (1, sz + zero), sz + zero }; u8 *ptr = mb.ptr; memcpy (ptr, mvs.arr[0].beg, mv_sz (mvs.arr[0])); ptr += mv_sz (mvs.arr[0]); for (usz i = 1; i < mvs.len; ++i) { memcpy (ptr, sep.beg, mv_sz (sep)); ptr += mv_sz (sep); memcpy (ptr, mvs.arr[i].beg, mv_sz (mvs.arr[i])); ptr += mv_sz (mvs.arr[i]); } if (zero) *ptr = '\0'; return mb; } #endif // MV_NO_FEAT_ARR // mv.c ends here