Skip to content
StrInitFromCstr

StrInitFromCstr

Description

Initialise a Str by value from a byte range [cstr, cstr + len). The 2-arg form uses the enclosing MisraScope allocator; the 3-arg form takes an explicit allocator (typed handle or raw Allocator *).

Success

Returns a usable Str holding the copied bytes.

Failure

Returns an empty Str on allocator OOM. The empty return is indistinguishable from len == 0; use StrTryInitFromCstr when OOM must be detected. LOG_FATAL if cstr is NULL.

Usage example (Cross-references)

Usage examples (Cross-references)
            }
    
            Str temp = StrInitFromCstr(start, (size)(StrIterIndex(&si) - StrIterIndex(&saved)), &scratch);
    
            // Special-value tokens (`inf`, `nan`, ...) carry through the
        size pos   = StrIterIndex(&si) - StrIterIndex(&saved);
    
        Str temp = StrInitFromCstr(start, pos, &scratch);
    
        if (!is_valid_numeric_string(&temp, true)) {
        size pos   = StrIterIndex(&si) - StrIterIndex(&saved);
    
        Str temp = StrInitFromCstr(start, pos, &scratch);
    
        // A bare base prefix ("0x", "0b", "0o") with no following digits is
            Zstr start = StrIterDataAt(&si, StrIterIndex(&saved));                                                         \
            size pos   = StrIterIndex(&si) - StrIterIndex(&saved);                                                         \
            Str  temp  = StrInitFromCstr(start, pos, &scratch);                                                            \
                                                                                                                           \
            if (StrLen(&temp) == 2 && StrBegin(&temp)[0] == '0' &&                                                         \
            }
    
            Str hex_str = StrInitFromCstr(
                StrIterDataAt(&si, StrIterIndex(&hex_saved)),
                StrIterIndex(&si) - StrIterIndex(&hex_saved),
            }
    
            Str oct_str = StrInitFromCstr(
                StrIterDataAt(&si, StrIterIndex(&oct_saved)),
                StrIterIndex(&si) - StrIterIndex(&oct_saved),
        }
    
        Str bin_str = StrInitFromCstr(
            StrIterDataAt(&si, StrIterIndex(&bin_saved)),
            StrIterIndex(&si) - StrIterIndex(&bin_saved),
        }
    
        Str  temp   = StrInitFromCstr(start, StrIterIndex(&si) - StrIterIndex(&saved), IntAllocator(value));
        Int  parsed = IntInit(IntAllocator(value));
        bool ok     = IntTryFromStrRadix(&parsed, StrBegin(&temp), radix);
    
        StrDeinit(&temp);
        temp = StrInitFromCstr(start, token_len, FloatAllocator(value));
        if (!FloatTryFromStr(&parsed, StrBegin(&temp))) {
            StrDeinit(&temp);
            }
    
            Str temp = StrInitFromCstr(start, (size)(StrIterIndex(&si) - StrIterIndex(&saved)), &scratch);
    
            // Special-value tokens (`inf`, `nan`, ...) carry through the
        size pos   = StrIterIndex(&si) - StrIterIndex(&saved);
    
        Str temp = StrInitFromCstr(start, pos, &scratch);
    
        if (!is_valid_numeric_string(&temp, true)) {
                Zstr next = ZstrFindSubstringN(prev, key, keylen);
                if (next) {
                    Str tmp = StrInitFromCstr(prev, next - prev, s->allocator);
                    VecPushBack(&sv, tmp);
                    prev = next + keylen;
                } else {
                    if (ZstrCompareN(prev, key, end - prev)) {
                        Str tmp = StrInitFromCstr(prev, end - prev, s->allocator);
                        VecPushBack(&sv, tmp);
                    }
        // result bound to the same allocator.
        if (s->length == 0) {
            return StrInitFromCstr("", 0, s->allocator);
        }
    
        size new_len = end >= start ? (end - start + 1) : 0;
        return StrInitFromCstr(start, new_len, s->allocator);
    }
        Zstr        exe = _dyld_get_image_name(0);
        if (exe) {
            *exe_path = StrInitFromCstr(exe, ZstrLen(exe), alloc);
            return exe_path;
        }
                }
    
                direntry.name = StrInitFromCstr(findFileData.cFileName, ZstrLen(findFileData.cFileName), alloc);
                VecPushBack(&dc, direntry);
            } while (FindNextFile(hFind, &findFileData) != 0);
                    DirEntry direntry = {0};
                    direntry.type     = dirent_type_to_misra(de->d_type);
                    direntry.name     = StrInitFromCstr(nm, name_len, alloc);
                    VecPushBack(&dc, direntry);
                }
    
                HostsEntry e = {0};
                e.name       = StrInitFromCstr((Zstr)StrIterDataAt(&si, nm_start), nm_len, alloc);
                ascii_lower((u8 *)StrBegin(&e.name), StrLen(&e.name));
                if (got_v4) {
        }
    
        lookup = StrInitFromCstr(key, ZstrLen(key), MapAllocator(cfg));
        value  = kvconfig_get_ptr_str(cfg, &lookup);
        StrDeinit(&lookup);
        }
    
        return StrInitFromCstr(StrBegin(value), StrLen(value), MapAllocator(cfg));
    }
        }
    
        return StrInitFromCstr(StrBegin(value), StrLen(value), MapAllocator(cfg));
    }
        // empty fresh Str.
        Str copy = (StrLen(src) == 0) ? StrInit((Allocator *)alloc) :
                                        StrInitFromCstr(StrBegin(src), StrLen(src), (Allocator *)alloc);
        *(Str *)dst_ptr = copy;
        return true;
    
    static GraphNodeId city_add_intersection(CityGraph *graph, CityIndex *index, const Str *name, DefaultAllocator *alloc) {
        GraphNodeId id = GraphAddNodeR(graph, StrInitFromCstr(StrBegin(name), StrLen(name), alloc));
    
        Str key_copy = StrInitFromCstr(StrBegin(name), StrLen(name), alloc);
        GraphNodeId id = GraphAddNodeR(graph, StrInitFromCstr(StrBegin(name), StrLen(name), alloc));
    
        Str key_copy = StrInitFromCstr(StrBegin(name), StrLen(name), alloc);
        MapInsertR(index, key_copy, id);
        return id;
    // Test StrInitFromCstr function
    bool test_str_init_from_cstr(void) {
        WriteFmt("Testing StrInitFromCstr\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Zstr test_str = "Hello, World!";
        size len      = 5; // Just "Hello"
        Str  s        = StrInitFromCstr(test_str, len, &alloc);
    
        // Validate the string
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str src = StrInitFromCstr("Hello, World!", ZstrLen("Hello, World!"), &alloc);
    
        Str  dup    = StrInitFromStr(&src, &alloc);
Last updated on