VecForeach

Table of Contents

VecForeach

Description

Iterate over each element var of the given vector v. This is a convenience macro that iterates forward using an internally managed index. The variable var is declared and defined by this macro.

Parameters

NameDirectionDescription
vin,outVector to iterate over.
varinName of the variable to be used which will contain the value of the current element during iteration. The type of var will be the data type of the vector elements (obtained via VEC_DATATYPE(v)).

Success

The body is executed for each element of the vector v from the beginning to the end.

Failure

If the vector v is NULL or its length is zero, the loop body will not be executed. Any failures within the VecForeachIdx macro (like invalid index access) will result in a fatal log message and program termination.

Usage example (Cross-references)

    (void)___is_first___;                                                                                          \
    StrPushBack(&(j), '[');                                                                                        \
    VecForeach(&(arr), item, {                                                                                     \
    if (___is_first___) {                                                                                      \
    ___is_first___ = false;                                                                                \
    /// body        : The block of code to be executed for each character of the Str.
    ///
    #define StrForeach(str, chr, body) VecForeach((str), (chr), {body})
    
    ///
    LOG_FATAL(                                                                                             \
    "Vector range overflow: End index %zu exceeds vector length %zu. "                                 \
    "If you intended to iterate over all items, use VecForeach instead.",                              \
    _e,                                                                                                \
    (v)->length                                                                                        \
    LOG_FATAL(                                                                                             \
    "Vector range overflow: End index {} exceeds vector length {}. "                                   \
    "If you intended to iterate over all items, use VecForeach instead.",                              \
    _e,                                                                                                \
    (v)->length                                                                                        \
    
    // Use VecForeach for iterating over entries
    VecForeach(&entries, e, {
    if (last_value == e.value - 1) {
    StrWriteFmt(&code, "    {},\n", e.name.data);
    
    // Use VecForeach for iterating over entries
    VecForeach(&entries, e, {
    const char* compareTemplate = "    if(ZstrCompareN(\"{}\", zstr, {}) == 0) {{return {};}}\n";
    // Store the length in a variable to avoid taking address of rvalue
    
    // Use VecForeach for iterating over entries
    VecForeach(&entries, e, {
    const char* caseTemplate = "        case {} : {{return \"{}\";}}\n";
    StrWriteFmt(&code, caseTemplate, e.name.data, e.str.data);
    StrDeinit(&code);
    
    VecForeach(&entries, e, {
    StrDeinit(&e.name);
    StrDeinit(&e.str);
    
    // recursively explore directories and get filenames
    VecForeach(&dir_paths, dir_name, {
    // keep track of current path we're exploring
    Str current_path = StrInit();
    SysDirContents dir_contents = SysGetDirContents(dir_name.data);
    Scope(&dir_contents, VecDeinit, {
    VecForeach(&dir_contents, dir_entry, {
    // if it's a directory then store it for exploration later on
    if (dir_entry.type == SYS_DIR_ENTRY_TYPE_DIRECTORY) {
    
    // go over each file and generate corresponding markdown
    VecForeach(&file_paths, file_path, {
    Str file_contents = StrInit();
    Scope(&file_contents, StrDeinit, {
    JW_OBJ(json, {
    JW_OBJ_KV(json, "functions", {
    VecForeach(&symbols, symbol, {
    Str source_key = StrInit();
    StrWriteFmt(&source_key, "{}", symbol.source_function_id);
    StrDeinit(&result->binary_name);
    StrDeinit(&result->sha256);
    VecForeach(&result->tags, tag, { StrDeinit(&tag); });
    VecDeinit(&result->tags);
    StrDeinit(&result->created_at);
    // Test VecForeach macro
    bool test_vec_foreach(void) {
    printf("Testing VecForeach\n");
    
    // Create a vector of integers
    // Use VecForeach to sum the values
    int sum = 0;
    VecForeach(&vec, item, { sum += item; });
    
    // Check the sum
    
    // Use VecForeach to double each value
    VecForeach(&vec, item, { item *= 2; });
    
    // Check that the values in the vector are unchanged (foreach uses value, not reference)
    // Deadend test: Make idx go out of bounds during VecForeach by modifying vector during iteration
    bool test_vec_foreach_out_of_bounds_access(void) {
    printf("Testing VecForeach where modification causes out of bounds access (should crash)\n");
    
    typedef Vec(int) IntVec;
    // VecForeach doesn't use an explicit index but we can still cause issues
    int iteration_count = 0;
    VecForeach(&vec, val, {
    printf("Iteration %d (vec.length=%zu): %d\n", iteration_count, vec.length, val);

Share :