Skip to content

VecForeachPtr

Description

Walk each element of v forward, binding var to a pointer to the element. Use when the body mutates elements in place. Convenience wrapper around VecForeachPtrIdx. See VecForeachPtrIdx for the full SUCCESS/FAILURE contract.

Usage example (Cross-references)

Usage examples (Cross-references)
    
    static ArgSpec *find_long(ArgParse *self, Zstr long_name) {
        VecForeachPtr(&self->specs, sp) {
            if (sp->role == ARG_ROLE_POSITIONAL)
                continue;
    
    static ArgSpec *find_short(ArgParse *self, Zstr short_name) {
        VecForeachPtr(&self->specs, sp) {
            if (sp->role == ARG_ROLE_POSITIONAL)
                continue;
    
        bool any_option = false;
        VecForeachPtr(&self->specs, sp) {
            if (sp->role == ARG_ROLE_OPTIONAL || sp->role == ARG_ROLE_FLAG || sp->role == ARG_ROLE_COUNT) {
                any_option = true;
            StrPushBackMany(&usage, " [OPTIONS]");
    
        VecForeachPtr(&self->specs, sp) {
            if (sp->role != ARG_ROLE_REQUIRED)
                continue;
            StrPushBackR(&usage, '>');
        }
        VecForeachPtr(&self->specs, sp) {
            if (sp->role != ARG_ROLE_POSITIONAL)
                continue;
        // and Run without seeing the synthetic entry first.
        bool already_has_help = false;
        VecForeachPtr(&self->specs, sp) {
            if (zstr_eq(sp->long_name, "--help")) {
                already_has_help = true;
        // accurate even on the first overflow token.
        u64 n_positionals = 0;
        VecForeachPtr(&self->specs, sp) {
            if (sp->role == ARG_ROLE_POSITIONAL)
                ++n_positionals;
            u64      seen = 0;
            ArgSpec *pos  = NULL;
            VecForeachPtr(&self->specs, sp) {
                if (sp->role != ARG_ROLE_POSITIONAL)
                    continue;
    
        // Validate required / positional were all set.
        VecForeachPtr(&self->specs, sp) {
            if ((sp->role == ARG_ROLE_REQUIRED || sp->role == ARG_ROLE_POSITIONAL) && !sp->seen) {
                if (sp->role == ARG_ROLE_POSITIONAL) {
    void ValidateStrs(const Strs *vs) {
        ValidateVec(vs);
        VecForeachPtr(vs, sp) {
            ValidateStr(sp);
        }
        // call into the validator.
        if (VecBegin(&self->hosts)) {
            VecForeachPtr(&self->hosts, e) {
                StrDeinit(&e->name);
            }
    
        bool found = false;
        VecForeachPtr(&resp.answers, rec) {
            if (rec->type == qtype) {
                SocketAddr a = qtype == DNS_TYPE_A ? sockaddr_v4(rec->ipv4, port) : sockaddr_v6(rec->ipv6, port);
    
            // 1. /etc/hosts fast path.
            VecForeachPtr(&self->hosts, e) {
                if (StrLen(&e->name) > 0 && ZstrCompare(StrBegin(&e->name), nq) == 0) {
                    SocketAddr a = e->is_ipv6 ? sockaddr_v6(e->ip, port) : sockaddr_v4(e->ip, port);
                DnsType qtype = QUERY_TYPES[i];
                // Iterate nameservers; each gets up to `retries` attempts.
                VecForeachPtr(&self->nameservers, ns) {
                    for (u32 attempt = 0; attempt < self->retries + 1; ++attempt) {
                        if (try_one_query(self, ns, nq, qtype, port, out)) {
            LOG_FATAL("invalid arguments");
        }
        VecForeachPtr(headers, header) {
            if (0 == ZstrCompare(StrBegin(&header->key), key)) {
                return header;
        );
    
        VecForeachPtr(&response->headers, header) {
            StrAppendFmt(&out, "{}: {}\r\n", header->key, header->value);
        }
            return;
        }
        VecForeachPtr(list, r) {
            DnsRecordDeinit(r);
        }
            }
    
            VecForeachPtr(&addrs, a) {
                Str s = SocketAddrFormat(a, alloc);
                // Strip the trailing ":0" since we resolve with port=0; keep
                if (VecLen(vec) > 0) {
                    size_t total_len = 0;
                    VecForeachPtr(vec, str_ptr) {
                        total_len += ZstrLen(*str_ptr);
                    }
                if (VecLen(vec) > 0) {
                    size_t total_len = 0;
                    VecForeachPtr(vec, str_ptr) {
                        total_len += StrLen(str_ptr);
                    }
                if (VecLen(vec) > 0) {
                    int sum = 0;
                    VecForeachPtr(vec, item_ptr) {
                        sum += *item_ptr;
                    }
        bool found_v4 = false;
        if (got) {
            VecForeachPtr(&out, ad) {
                if (ad->family == SOCKET_FAMILY_INET) {
                    found_v4 = true;
        bool ok = got && VecLen(&out) > 0;
        if (ok) {
            VecForeachPtr(&out, ad) {
                Str s = SocketAddrFormat(ad, a);
                u64 L = StrLen(&s);
    // Test VecForeachPtr macro
    bool test_vec_foreach_ptr(void) {
        WriteFmt("Testing VecForeachPtr\n");
    
        // Create a vector of integers
    
        // Use VecForeachPtr to modify the values in the vector
        VecForeachPtr(&vec, item_ptr) {
            *item_ptr *= 2;
        }
        // Use VecForeachPtr to calculate sum
        int sum = 0;
        VecForeachPtr(&vec, item_ptr) {
            sum += *item_ptr;
        }
    /// TAGS: Str, Foreach, Iterate
    ///
    #define StrForeachPtr(str, chrptr) VecForeachPtr((str), (chrptr))
    
    ///
Last updated on