root/StringReplace.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <string.h>
#include "List.h"

struct occurance {
    size_t prefixSize;
    char* prefix;
    char* position;
    char* followUp;
};

char* replaceStr(const char* data, const char* original, const char* replacement) {

    static const size_t occuranceSize = sizeof(struct occurance);
    const size_t replacementSize = strlen(replacement);
    const size_t originalSize = strlen(original);

    List* occurances = List_create();

    size_t newSize = 0;
    char* foundPt = NULL;
    char* pos = (char*)data;
    while (1) {
        foundPt = strstr(pos, original);

        if (foundPt) {
            struct occurance* occ = malloc(occuranceSize);
            occ->prefixSize = (size_t)foundPt - (size_t)pos;
            occ->prefix = pos;
            occ->position = foundPt;
            occ->followUp = foundPt + originalSize;
            List_pushBack(occurances, occ);
            newSize += occ->prefixSize + replacementSize;
            pos = foundPt + originalSize;
        } else {
            break;
        }
    }

    if (!occurances->first) {
        List_destroyWithContent(occurances, free);
        return NULL;
    }

    newSize += strlen(pos) +1;

    char* newString = malloc(newSize);
    char* insertPos = newString;
    for (ListNode* it = occurances->first; it; it = it->next) {
        struct occurance* occ = it->data;
        memcpy(insertPos, occ->prefix, occ->prefixSize);
        insertPos += occ->prefixSize;
        memcpy(insertPos, replacement, replacementSize);
        insertPos += replacementSize;
        if (!it->next) {
            strcpy(insertPos, occ->followUp);
        }
    }

    List_destroyWithContent(occurances, free);
    return newString;
}