aboutsummaryrefslogtreecommitdiff
path: root/src/cordhelpers.c
blob: 353a52d93d0e43feeae03dead4a9e9f1c3ad49dd (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
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
62
63
64
65
66
// Some helper functions for the GC Cord library

#include <gc/cord.h>
#include <stdint.h>

#include "cordhelpers.h"
#include "stdlib/print.h"
#include "stdlib/util.h"

public CORD CORD_quoted(CORD str)
{
    CORD quoted = "\"";
    CORD_pos i;
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
    CORD_FOR(i, str) {
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
        char c = CORD_pos_fetch(i);
        switch (c) {
        case '\a': quoted = CORD_cat(quoted, "\\a"); break;
        case '\b': quoted = CORD_cat(quoted, "\\b"); break;
        case '\x1b': quoted = CORD_cat(quoted, "\\e"); break;
        case '\f': quoted = CORD_cat(quoted, "\\f"); break;
        case '\n': quoted = CORD_cat(quoted, "\\n"); break;
        case '\r': quoted = CORD_cat(quoted, "\\r"); break;
        case '\t': quoted = CORD_cat(quoted, "\\t"); break;
        case '\v': quoted = CORD_cat(quoted, "\\v"); break;
        case '"': quoted = CORD_cat(quoted, "\\\""); break;
        case '\\': quoted = CORD_cat(quoted, "\\\\"); break;
        case '\x00' ... '\x06': case '\x0E' ... '\x1A':
        case '\x1C' ... '\x1F': case '\x7F' ... '\x7F':
            quoted = CORD_all(quoted, "\\x", String(hex((uint64_t)c, .no_prefix=true, .uppercase=true, .digits=2)));
            break;
        default: quoted = CORD_cat_char(quoted, c); break;
        }
    }
    quoted = CORD_cat_char(quoted, '"');
    return quoted;
}

public CORD CORD_replace(CORD c, CORD to_replace, CORD replacement)
{
    size_t len = CORD_len(c);
    size_t replaced_len = CORD_len(to_replace);
    size_t pos = 0;
    CORD ret = CORD_EMPTY;
    while (pos < len) {
        size_t found = CORD_str(c, pos, to_replace);
        if (found == CORD_NOT_FOUND) {
            if (pos < len)
                ret = CORD_cat(ret, CORD_substr(c, pos, len));
            return ret;
        }
        if (found > pos)
            ret = CORD_cat(ret, CORD_substr(c, pos, found-pos));
        ret = CORD_cat(ret, replacement);
        pos = found + replaced_len;
    }
    return ret;
}

// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0