Skip to content
ZstrFindSubstring

ZstrFindSubstring

Description

Find first occurrence of needle in haystack.

Success

Returns pointer to first occurrence or NULL if not found.

Failure

Aborts via LOG_FATAL when either pointer is NULL.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    Zstr ZstrFindSubstring(Zstr haystack, Zstr needle) {
        if (!needle) {
            LOG_FATAL("Invalid arguments");
        bool result = false;
    
        if (ZstrFindSubstring(StrBegin(&bv_str), pattern) != NULL) {
            result = true;
        }
    Zstr str_find_zstr(const Str *s, Zstr key) {
        ValidateStr(s);
        return ZstrFindSubstring(StrBegin(s), key);
    }
        //   - includes a Content-Length: 11 (length of "<h1>hi</h1>")
        //   - ends with the body
        bool ok = StrLen(&wire) > 0 && ZstrFindSubstring(StrBegin(&wire), "HTTP/1.1 200 OK\r\n") == StrBegin(&wire) &&
                  ZstrFindSubstring(StrBegin(&wire), "Content-Type: text/html\r\n") != NULL &&
                  ZstrFindSubstring(StrBegin(&wire), "Content-Length: 11\r\n") != NULL &&
        //   - ends with the body
        bool ok = StrLen(&wire) > 0 && ZstrFindSubstring(StrBegin(&wire), "HTTP/1.1 200 OK\r\n") == StrBegin(&wire) &&
                  ZstrFindSubstring(StrBegin(&wire), "Content-Type: text/html\r\n") != NULL &&
                  ZstrFindSubstring(StrBegin(&wire), "Content-Length: 11\r\n") != NULL &&
                  ZstrFindSubstring(StrBegin(&wire), "\r\n\r\n<h1>hi</h1>") != NULL;
        bool ok = StrLen(&wire) > 0 && ZstrFindSubstring(StrBegin(&wire), "HTTP/1.1 200 OK\r\n") == StrBegin(&wire) &&
                  ZstrFindSubstring(StrBegin(&wire), "Content-Type: text/html\r\n") != NULL &&
                  ZstrFindSubstring(StrBegin(&wire), "Content-Length: 11\r\n") != NULL &&
                  ZstrFindSubstring(StrBegin(&wire), "\r\n\r\n<h1>hi</h1>") != NULL;
                  ZstrFindSubstring(StrBegin(&wire), "Content-Type: text/html\r\n") != NULL &&
                  ZstrFindSubstring(StrBegin(&wire), "Content-Length: 11\r\n") != NULL &&
                  ZstrFindSubstring(StrBegin(&wire), "\r\n\r\n<h1>hi</h1>") != NULL;
    
        StrDeinit(&wire);
        // (Apple's C mangling prepends `_`; substring search matches both
        // "bt_capture_with_helper" and "_bt_capture_with_helper".)
        bool ok = StrLen(&rendered) > 0 && ZstrFindSubstring(StrBegin(&rendered), "bt_capture_with_helper") != NULL &&
                  ZstrFindSubstring(StrBegin(&rendered), "bt_capture_outer") != NULL;
        // "bt_capture_with_helper" and "_bt_capture_with_helper".)
        bool ok = StrLen(&rendered) > 0 && ZstrFindSubstring(StrBegin(&rendered), "bt_capture_with_helper") != NULL &&
                  ZstrFindSubstring(StrBegin(&rendered), "bt_capture_outer") != NULL;
    
    #if FEATURE_SYS_SYMRESOLVE
        // the Linux backend emits source filenames today (via DwarfLines);
        // macOS / Windows only emit names + offsets.
        ok = ok && ZstrFindSubstring(StrBegin(&rendered), "Backtrace.c") != NULL;
    #endif
    
        ok = ok && StrLen(&rendered) > 0;
        ok = ok && ZstrFindSubstring(StrBegin(&rendered), "bt_vec_capture_with_helper") != NULL;
        ok = ok && ZstrFindSubstring(StrBegin(&rendered), "bt_vec_capture_outer") != NULL;
        ok = ok && StrLen(&rendered) > 0;
        ok = ok && ZstrFindSubstring(StrBegin(&rendered), "bt_vec_capture_with_helper") != NULL;
        ok = ok && ZstrFindSubstring(StrBegin(&rendered), "bt_vec_capture_outer") != NULL;
    
        StrDeinit(&rendered);
    
        // Should also contain the helper name through the shared resolver.
        bool ok = StrLen(&out) > 0 && ZstrFindSubstring(StrBegin(&out), "bt_capture_with_helper") != NULL;
    
        StrDeinit(&out);
        bool ok = n >= 2;
        ok      = ok && StrLen(&rendered) > 0;
        ok      = ok && ZstrFindSubstring(StrBegin(&rendered), "cfi_capture_inner") != NULL;
        ok      = ok && ZstrFindSubstring(StrBegin(&rendered), "cfi_capture_outer") != NULL;
        ok      = ok && StrLen(&rendered) > 0;
        ok      = ok && ZstrFindSubstring(StrBegin(&rendered), "cfi_capture_inner") != NULL;
        ok      = ok && ZstrFindSubstring(StrBegin(&rendered), "cfi_capture_outer") != NULL;
    
        StrDeinit(&rendered);
        ResolvedSymbol r;
        bool           ok = SymbolResolverResolve(&res, (void *)&symres_marker_helper, &r);
        ok                = ok && r.symbol_name != NULL && ZstrFindSubstring(r.symbol_name, "symres_marker_helper") != NULL;
    
        SymbolResolverDeinit(&res);
        const MachoSymbol *sym = MachoResolveAddress(&m, vaddr);
        bool               ok  = sym != NULL && sym->name != NULL &&
                  ZstrFindSubstring(sym->name, "test_macho_resolves_running_binary_symbol") != NULL;
    
        MachoDeinit(&m);
    
        bool ok = StrLen(&out) > 0;
        ok      = ok && (ZstrFindSubstring(StrBegin(&out), "leak:") != NULL);
        ok      = ok && (ZstrFindSubstring(StrBegin(&out), "24 bytes") != NULL);
        ok      = ok && (ZstrFindSubstring(StrBegin(&out), "40 bytes") != NULL);
        bool ok = StrLen(&out) > 0;
        ok      = ok && (ZstrFindSubstring(StrBegin(&out), "leak:") != NULL);
        ok      = ok && (ZstrFindSubstring(StrBegin(&out), "24 bytes") != NULL);
        ok      = ok && (ZstrFindSubstring(StrBegin(&out), "40 bytes") != NULL);
        ok      = ok && (ZstrFindSubstring(StrBegin(&out), "leak:") != NULL);
        ok      = ok && (ZstrFindSubstring(StrBegin(&out), "24 bytes") != NULL);
        ok      = ok && (ZstrFindSubstring(StrBegin(&out), "40 bytes") != NULL);
    
        StrDeinit(&out);
        if (built) {
            const DwarfLineEntry *e = DwarfLinesResolve(&lines, file_relative);
            if (e && e->file && ZstrFindSubstring(e->file, "Dwarf.c") != NULL && e->line > 0) {
                ok = true;
            }
        if (built && VecLen(&fns.entries) > 0) {
            const DwarfFunction *f = DwarfFunctionsResolve(&fns, file_relative);
            ok = f != NULL && f->name != NULL && ZstrFindSubstring(f->name, "dwarf_marker_helper") != NULL;
            DwarfFunctionsDeinit(&fns);
        }
        if (built && VecLen(&fns.entries) > 0) {
            const DwarfFunction *f = DwarfFunctionsResolve(&fns, file_relative);
            ok                     = f && f->name && ZstrFindSubstring(f->name, expect_name) != NULL;
            DwarfFunctionsDeinit(&fns);
        }
Last updated on