aboutsummaryrefslogtreecommitdiff
path: root/src/base/string/replace.c
blob: 127daed00c4ed97a57f85acc24591f9698d9f112 (plain)
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
#include "internal.h"

// replace will replace all occurences of the given bytes 'from' to bytes 'to'
// edits are done in place and modify the string.
// NOTE:  as of now strings from and to must be the same size.
void
str·replace(string s, const byte* from, const byte* to)
{
    vlong fromL = strlen(from);
    vlong toL   = strlen(to);
    if (toL != fromL) { panicf("different sized replacement string not supported"); }

    vlong l = str·len(s);
    vlong i = l;
    vlong j = l;

    for (i = 0; i < l; i++) {
        for (j = 0; j < toL; j++) {
            if (s[i] == from[j]) {
                s[i] = to[j];
                break;
            }
        }
    }
}