Skip to content
StrIterFromStr

StrIterFromStr

Description

Construct a forward-walking StrIter from a Str value. Alias- reframe of IterInitFromVec in string vocabulary: Str is a Vec(char), so the borrowed-data semantics and NULL-allocator alignment fallback documented on IterInitFromVec apply unchanged.

Parameters

Name Direction Description
s in Source string (by value – the resulting iter holds a non-owning pointer into s’s buffer).

Success

Returns a struct-literal StrIter covering [StrBegin(&s), StrBegin(&s) + StrLen(&s)) with pos = 0, dir = 1.

Failure

Macro cannot fail; an empty Str yields a remaining- length-0 iterator.

Usage example (Cross-references)

Usage examples (Cross-references)
        }
    
        StrIter si = StrIterFromStr(buf);
        char    c;
        while (StrIterRemainingLength(&si)) {
        u64               kw_len       = sizeof(NS_KEYWORD) - 1;
    
        StrIter si = StrIterFromStr(buf);
        char    c;
        while (StrIterRemainingLength(&si)) {
        // `StrResize`/`StrReserve` keep a NUL sentinel at index `length`, so path
        // slices that alias the buffer are usable as `Zstr`.
        StrIter si = StrIterFromStr(out->raw);
        while (StrIterRemainingLength(&si)) {
            ProcMapEntry e = {0};
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"name\": \"Alice\", \"city\": \"New York\"}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        Str name = StrInit(&alloc);
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"count\": 42, \"score\": 95.5, \"year\": 2024}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        u32 count = 0;
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"enabled\": true, \"visible\": false}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        bool enabled = false;
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        Person person = {0};
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"debug_mode\": false, \"timeout\": 30, \"log_level\": \"INFO\"}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        Config config    = {0};
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"languages\": [\"C\", \"Python\", \"Rust\"]}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        Vec(Str) languages = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        struct {
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        SimpleProduct product = {0};
    
        Str     json = StrInitFromZstr("{\"test\": \"value\"}", &alloc);
        StrIter si   = StrIterFromStr(json);
    
        bool success = true;
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"id\": 12345, \"name\": \"test\", \"active\": true, \"score\": 98.5}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        struct {
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        struct {
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        struct {
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        typedef Vec(AnnSymbol) Symbols;
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        ApiResponse response = {false, StrInit(&alloc), VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc)};
        bool    success = true;
        Str     json = StrInitFromZstr("{\"id\": 12345, \"name\": \"test_func\", \"size\": 1024, \"vaddr\": 4096}", &alloc);
        StrIter si   = StrIterFromStr(json);
    
        FunctionInfo info = {0};
        bool    success = true;
        Str     json    = StrInitFromZstr("{\"id\": 54321, \"name\": \"test_model\"}", &alloc);
        StrIter si      = StrIterFromStr(json);
    
        ModelInfo info = {0};
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        SearchResult result = {0};
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        ApiResponse response = {false, StrInit(&alloc), VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc)};
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        ApiResponse response = {false, StrInit(&alloc), VecInitWithDeepCopy(NULL, AnnSymbolDeinit, &alloc)};
        // Test completely empty object
        Str     json1 = StrInitFromZstr("{}", &alloc);
        StrIter si1   = StrIterFromStr(json1);
    
        struct {
        // Test empty object with whitespace
        Str     json2 = StrInitFromZstr("  {   }  ", &alloc);
        StrIter si2   = StrIterFromStr(json2);
    
        struct {
        // Test completely empty array
        Str     json1 = StrInitFromZstr("{\"items\":[]}", &alloc);
        StrIter si1   = StrIterFromStr(json1);
    
        Vec(i32) items = VecInit(&alloc);
        // Test empty array with whitespace
        Str     json2 = StrInitFromZstr("{\"data\": [  ] }", &alloc);
        StrIter si2   = StrIterFromStr(json2);
    
        Vec(Str) data = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
    
        Str     json = StrInitFromZstr("{\"name\":\"\",\"description\":\"\"}", &alloc);
        StrIter si   = StrIterFromStr(json);
    
        struct {
    
        Str     json = StrInitFromZstr("{\"temp\":-25,\"balance\":-1000.50,\"delta\":-0.001}", &alloc);
        StrIter si   = StrIterFromStr(json);
    
        struct {
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        struct {
    
        Str     json = StrInitFromZstr("{\"int_zero\":0,\"float_zero\":0.0,\"bool_false\":false}", &alloc);
        StrIter si   = StrIterFromStr(json);
    
        struct {
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        struct {
            &alloc
        );
        StrIter si = StrIterFromStr(json);
    
        struct {
        // Test with lots of different whitespace patterns
        Str     json = StrInitFromZstr("  {\n\t\"name\"  :  \"test\"  ,\n  \"value\": 42\t,\"flag\"\n:\ntrue\n}\t", &alloc);
        StrIter si   = StrIterFromStr(json);
    
        struct {
    
        Str     json = StrInitFromZstr("{\"outer\":{},\"list\":[],\"deep\":{\"inner\":{}}}", &alloc);
        StrIter si   = StrIterFromStr(json);
    
        struct {
        Str json =
            StrInitFromZstr("{\"empty_obj\":{},\"filled_obj\":{\"x\":1},\"empty_arr\":[],\"filled_arr\":[1,2]}", &alloc);
        StrIter si = StrIterFromStr(json);
    
        struct {
        // Using smaller values that are safer to work with
        Str json   = StrInitFromZstr("{\"max_int\":2147483647,\"min_int\":-2147483648,\"one\":1,\"minus_one\":-1}", &alloc);
        StrIter si = StrIterFromStr(json);
    
        struct {
        Str json =
            StrInitFromZstr("{\"tiny\":0.000001,\"huge\":999999.999999,\"zero\":0.0,\"negative_tiny\":-0.000001}", &alloc);
        StrIter si = StrIterFromStr(json);
    
        struct {
        {
            Str     j   = StrInitFromZstr("tru3", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = true;
            StrIter out = JReadBool(si, &b);
        {
            Str     j   = StrInitFromZstr("fXlse", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = true;
            StrIter out = JReadBool(si, &b);
        {
            Str     j   = StrInitFromZstr("tr", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = true;
            StrIter out = JReadBool(si, &b);
        {
            Str     j       = StrInitFromZstr("nuXX", &alloc);
            StrIter si      = StrIterFromStr(j);
            bool    is_null = true;
            StrIter out     = JReadNull(si, &is_null);
        {
            Str     j   = StrInitFromZstr("true rest", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = false;
            StrIter out = JReadBool(si, &b);
        {
            Str     j   = StrInitFromZstr("false rest", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = true;
            StrIter out = JReadBool(si, &b);
        {
            Str     j       = StrInitFromZstr("null rest", &alloc);
            StrIter si      = StrIterFromStr(j);
            bool    is_null = false;
            StrIter out     = JReadNull(si, &is_null);
        {
            Str     j   = StrInitFromZstr("true", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = false;
            StrIter out = JReadBool(si, &b);
        {
            Str     j   = StrInitFromZstr("false", &alloc);
            StrIter si  = StrIterFromStr(j);
            bool    b   = true;
            StrIter out = JReadBool(si, &b);
        {
            Str     j       = StrInitFromZstr("null", &alloc);
            StrIter si      = StrIterFromStr(j);
            bool    is_null = false;
            StrIter out     = JReadNull(si, &is_null);
        for (u64 i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
            Str     j   = StrInitFromZstr(cases[i], &alloc);
            StrIter si  = StrIterFromStr(j);
            Str     out = StrInit(&alloc);
            StrIter r   = JReadString(si, &out);
        {
            Str     j   = StrInitFromZstr("\"a\\u00e9b\"", &alloc);
            StrIter si  = StrIterFromStr(j);
            Str     out = StrInit(&alloc);
            StrIter r   = JReadString(si, &out);
        {
            Str     j   = StrInitFromZstr("\"plain\"", &alloc);
            StrIter si  = StrIterFromStr(j);
            Str     out = StrInit(&alloc);
            StrIter r   = JReadString(si, &out);
            &alloc
        );
        StrIter si     = StrIterFromStr(json);
        i64     wanted = 0;
        for (u64 i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
            Str     json = StrInitFromZstr(cases[i], &alloc);
            StrIter si   = StrIterFromStr(json);
            i64     a    = 0;
            i64     b    = 0;
        {
            Str     j   = StrInitFromZstr("-12345", &alloc);
            StrIter si  = StrIterFromStr(j);
            i64     v   = 0;
            StrIter out = JReadInteger(si, &v);
            // Positive control: a sign-flip mutation would make this negative.
            Str     j   = StrInitFromZstr("12345", &alloc);
            StrIter si  = StrIterFromStr(j);
            i64     v   = 0;
            StrIter out = JReadInteger(si, &v);
        {
            Str     j   = StrInitFromZstr("-2.5", &alloc);
            StrIter si  = StrIterFromStr(j);
            f64     v   = 0.0;
            StrIter out = JReadFloat(si, &v);
    
        Str     j   = StrInitFromZstr("7", &alloc);
        StrIter si  = StrIterFromStr(j);
        f64     val = 0.0;
        StrIter out = JReadFloat(si, &val);
    
        Str     j   = StrInitFromZstr("13", &alloc);
        StrIter si  = StrIterFromStr(j);
        f64     val = 0.0;
        StrIter out = JReadFloat(si, &val);
        // value the unconditional clear set it to, which must be false.
        Str     j       = StrInitFromZstr("nuII", &alloc);
        StrIter si      = StrIterFromStr(j);
        bool    is_null = true; // poison: must be overwritten to false
        StrIter out     = JReadNull(si, &is_null);
    
        Str     j       = StrInitFromZstr("null", &alloc);
        StrIter si      = StrIterFromStr(j);
        bool    is_null = false;
        StrIter out     = JReadNull(si, &is_null);
        } parsed = {0, 0.0, false, StrInit(&alloc)};
    
        StrIter si = StrIterFromStr(json);
        JR_OBJ(si, {
            JR_INT_KV(si, "count", parsed.count);
        } parsed = {0};
    
        StrIter si = StrIterFromStr(json);
        JR_OBJ(si, {
            JR_INT_KV(si, "big_int", parsed.big_int);
        } parsed = {false, true, false, true}; // Initialize with opposite values
    
        StrIter si = StrIterFromStr(json);
        JR_OBJ(si, {
            JR_BOOL_KV(si, "flag1", parsed.flag1);
        } parsed = {StrInit(&alloc), StrInit(&alloc), StrInit(&alloc), StrInit(&alloc)};
    
        StrIter si = StrIterFromStr(json);
        JR_OBJ(si, {
            JR_STR_KV(si, "empty", parsed.empty);
        Vec(Str) parsed_strings = VecInitWithDeepCopy(NULL, StrDeinit, &alloc);
    
        StrIter si = StrIterFromStr(json);
        JR_OBJ(si, {
            JR_ARR_KV(si, "numbers", {
        TestPerson parsed_person = {0, StrInit(&alloc), 0, false, 0.0};
    
        StrIter si = StrIterFromStr(json);
        JR_OBJ(si, {
            JR_OBJ_KV(si, "user", {
        parsed.flags             = VecInitT(parsed.flags, &alloc);
    
        StrIter si = StrIterFromStr(json);
        JR_OBJ(si, {
            JR_OBJ_KV(si, "user", {
        bool found_empty_object = false;
    
        StrIter si = StrIterFromStr(json);
        JR_OBJ(si, {
            JR_STR_KV(si, "empty_string", parsed_str);
        } parsed = {0, 0, 999, 999.0, false, true}; // Initialize with different values
    
        StrIter si = StrIterFromStr(json);
        JR_OBJ(si, {
            JR_INT_KV(si, "max_int", parsed.max_int);
        KvConfig         cfg   = KvConfigInit(&alloc);
        Str              text  = StrInitFromZstr(src, &alloc);
        StrIter          input = StrIterFromStr(text);
        Str             *got   = NULL;
        bool             ok    = false;
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("flag = yes\n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        bool             v      = false;
        bool             result = true;
        DefaultAllocator alloc  = DefaultAllocatorInit();
        Str              src    = StrInitFromZstr("xy\nZ", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigSkipLine(input);
        char             c      = 0;
        DefaultAllocator alloc  = DefaultAllocatorInit();
        Str              src    = StrInitFromZstr("a\n\nZ", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigSkipLine(input);
        char             c      = 0;
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("a=1\r\nb=2\r\n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigParse(input, &cfg);
        i64              a      = 0;
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("k = # just a comment\n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        Str             *v      = NULL;
        bool             result = true;
        // case. Pair with a trailing-comment line so both branches matter.
        Str     src    = StrInitFromZstr("a = 1 # c\nb = 2\n", &alloc);
        StrIter input  = StrIterFromStr(src);
        StrIter si     = KvConfigParse(input, &cfg);
        i64     a      = 0;
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("k = \"v\" junk\n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigParse(input, &cfg);
        bool             result = true;
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("   k = v\n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigParse(input, &cfg);
        Str             *v      = NULL;
        // A line with only spaces, then a real pair.
        Str     src    = StrInitFromZstr("   \nk = v\n", &alloc);
        StrIter input  = StrIterFromStr(src);
        StrIter si     = KvConfigParse(input, &cfg);
        Str    *v      = NULL;
        KvConfig         cfg    = KvConfigInit(&alloc);
        Str              src    = StrInitFromZstr("k = v\n   \n", &alloc);
        StrIter          input  = StrIterFromStr(src);
        StrIter          si     = KvConfigParse(input, &cfg);
        bool             result = true;
            &alloc
        );
        StrIter input  = StrIterFromStr(src);
        StrIter si     = KvConfigParse(input, &cfg);
        Str    *host   = KvConfigGetPtr(&cfg, "host");
            &alloc
        );
        StrIter input  = StrIterFromStr(src);
        StrIter si     = KvConfigParse(input, &cfg);
        Str    *path   = KvConfigGetPtr(&cfg, "path");
        KvConfig         cfg         = KvConfigInit(&alloc);
        Str              src         = StrInitFromZstr("host = localhost\n", &alloc);
        StrIter          input       = StrIterFromStr(src);
        Str              host_copy   = StrInit(&alloc);
        Str             *stored_host = NULL;
        bool    disabled = true;
        bool    result   = true;
        StrIter input    = StrIterFromStr(src);
    
        (void)KvConfigParse(input, &cfg);
            &alloc
        );
        StrIter input   = StrIterFromStr(src);
        StrIter si      = KvConfigParse(input, &cfg);
        bool    enabled = false;
            &alloc
        );
        StrIter input  = StrIterFromStr(src);
        bool    result = true;
        bool    v      = false;
            &alloc
        );
        StrIter input  = StrIterFromStr(src);
        bool    result = true;
            &alloc
        );
        StrIter input  = StrIterFromStr(src);
        Str    *a      = NULL;
        Str    *b      = NULL;
            &alloc
        );
        StrIter input  = StrIterFromStr(src);
        StrIter si     = KvConfigParse(input, &cfg);
        Str    *host   = NULL;
        KvConfig cfg   = KvConfigInit(base);
        Str      text  = StrInitFromZstr(src, base);
        StrIter  input = StrIterFromStr(text);
    
        (void)KvConfigParse(input, &cfg);
        // keeps both buffers off any small-string fast path.
        Str     text  = StrInitFromZstr("key =   spaced-out-value-here   \n", adbg);
        StrIter input = StrIterFromStr(text);
    
        (void)KvConfigParse(input, &cfg);
        KvConfig cfg   = KvConfigInit(adbg);
        Str      text  = StrInitFromZstr("a-reasonably-long-config-key = v\n", adbg);
        StrIter  input = StrIterFromStr(text);
    
        (void)KvConfigParse(input, &cfg);
        KvConfig cfg   = KvConfigInit(adbg);
        Str      text  = StrInitFromZstr("k = a-reasonably-long-config-value\n", adbg);
        StrIter  input = StrIterFromStr(text);
    
        (void)KvConfigParse(input, &cfg);
        KvConfig cfg   = KvConfigInit(adbg);
        Str      text  = StrInitFromZstr("present-config-key = value\n", adbg);
        StrIter  input = StrIterFromStr(text);
    
        (void)KvConfigParse(input, &cfg);
Last updated on