Skip to content

StrInsertMany

Description

Insert MANY characters into str at position idx. R-form (copy); the source bytes are read-only / borrowed. Two arities via MISRA_OVERLOAD:

StrInsertMany(str, zstr, idx) - 3 args; zstr is NUL-terminated; length derived via ZstrLen. StrInsertMany(str, cstr, cstr_len, idx) - 4 args; counted view. The (Zstr, size) pair sits adjacent.

For Str *Str use StrMergeL / StrMergeR.

No L-form: const source bytes (Zstr / Cstr) cannot carry ownership- transfer semantics. For writeable-source range insert (with L-form ownership transfer) use StrInsertRangeL.

Success

Returns true; bytes copied at idx; trailing characters shifted right by the inserted length.

Failure

Returns false on allocation failure. str unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
            if (MemCompare(s->data + i, match, match_len) == 0) {
                StrDeleteRange(s, i, match_len);
                StrInsertMany(s, replacement, replacement_len, i);
                i        += replacement_len;
                replaced += 1;
    // Test StrInsertMany 4-arg (Cstr) form
    bool test_str_insert_cstr(void) {
        WriteFmt("Testing StrInsertMany (Cstr form)\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Insert a string in the middle: (cstr, cstr_len) adjacent, then idx
        StrInsertMany(&s, " World", 6, 2);
    
        // Check that the string was inserted correctly
    // Test StrInsertMany 3-arg Zstr form
    bool test_str_insert_zstr(void) {
        WriteFmt("Testing StrInsertMany (Zstr form)\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
        // Insert a string in the middle
        Zstr w = " World";
        StrInsertMany(&s, w, 2);
    
        // Check that the string was inserted correctly
    // Test StrInsertMany function
    bool test_str_push_cstr(void) {
        WriteFmt("Testing StrInsertMany\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Push a string at position 2
        StrInsertMany(&s, " World", 6, 2);
    
        // Check that the string was inserted correctly
    // Test StrInsertMany function
    bool test_str_push_zstr(void) {
        WriteFmt("Testing StrInsertMany\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Push a string at position 2
        StrInsertMany(&s, " World", 2);
    
        // Check that the string was inserted correctly
Last updated on