Coverage Report

Created: 2026-11-28 17:05

/home/runner/work/panackelty/panackelty/src/vm/builtins_text.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtins_internal.h"
2
#include "utf8.h"
3
#include "value.h"
4
5
#include <stdlib.h>
6
#include <string.h>
7
8
/* Text and byte operations preserve code-point semantics and runtime bounds checks. */
9
10
Value *builtins_text_call(VM *vm, const char *name, Value **a)
11
60.4M
{
12
60.4M
    if (!strcmp(name, "len")) {
  Branch (12:9): [True: 14.6M, False: 15.5M]
  Branch (12:9): [True: 14.6M, False: 15.5M]
13
29.3M
        if (a[0]->kind == V_MAP || a[0]->kind == V_SET) {
  Branch (13:13): [True: 3, False: 14.6M]
  Branch (13:36): [True: 3, False: 14.6M]
  Branch (13:13): [True: 3, False: 14.6M]
  Branch (13:36): [True: 3, False: 14.6M]
14
12
            size_t slots = a[0]->as.sequence.count;
15
12
            return value_size(a[0]->kind == V_MAP ? slots / 2 : slots);
  Branch (15:31): [True: 3, False: 3]
  Branch (15:31): [True: 3, False: 3]
16
12
        }
17
29.3M
        REQUIRE(a[0]->kind == V_STR || a[0]->kind == V_BYTES || a[0]->kind == V_ARRAY,
18
29.3M
                "VM trap: len requires Str, Bytes, Array, Map, or Set");
19
29.3M
        size_t n = a[0]->kind == V_STR     ? a[0]->as.bytes.characters
  Branch (19:20): [True: 8.87M, False: 5.78M]
  Branch (19:20): [True: 8.87M, False: 5.78M]
20
29.3M
                   : a[0]->kind == V_BYTES ? a[0]->as.bytes.length
  Branch (20:22): [True: 0, False: 5.78M]
  Branch (20:22): [True: 0, False: 5.78M]
21
11.5M
                                           : a[0]->as.sequence.count;
22
29.3M
        return value_size(n);
23
29.3M
    }
24
25
31.0M
    if (!strcmp(name, "concat")) {
  Branch (25:9): [True: 3.68M, False: 11.8M]
  Branch (25:9): [True: 3.68M, False: 11.8M]
26
7.37M
        REQUIRE(a[0]->kind == V_ARRAY && a[1]->kind == V_ARRAY, "VM trap: concat requires arrays");
27
7.37M
        return value_array_concat(a[0], a[1]);
28
7.37M
    }
29
30
23.7M
    if (!strcmp(name, "slice")) {
  Branch (30:9): [True: 510k, False: 11.3M]
  Branch (30:9): [True: 510k, False: 11.3M]
31
1.02M
        REQUIRE(a[0]->kind == V_STR && a[1]->kind == V_NAT && a[2]->kind == V_NAT,
32
1.02M
                "VM trap: slice requires Str and Nat bounds");
33
1.02M
        size_t start, end, bs, be;
34
1.02M
        if (!value_index(a[1], &start) || !value_index(a[2], &end) || start > end) {
  Branch (34:13): [True: 0, False: 510k]
  Branch (34:43): [True: 0, False: 510k]
  Branch (34:71): [True: 1, False: 510k]
  Branch (34:13): [True: 0, False: 510k]
  Branch (34:43): [True: 0, False: 510k]
  Branch (34:71): [True: 1, False: 510k]
35
2
            vm->error = "VM trap: invalid string slice";
36
2
            vm->fault_kind = VM_FAULT_REQUEST;
37
2
            return NULL;
38
2
        }
39
1.02M
        size_t count = a[0]->as.bytes.characters;
40
1.02M
        if (end > count) {
  Branch (40:13): [True: 3, False: 510k]
  Branch (40:13): [True: 3, False: 510k]
41
6
            vm->error = "VM trap: invalid string slice";
42
6
            vm->fault_kind = VM_FAULT_REQUEST;
43
6
            return NULL;
44
6
        }
45
1.02M
        if (start == count) {
  Branch (45:13): [True: 3.81k, False: 506k]
  Branch (45:13): [True: 3.81k, False: 506k]
46
7.63k
            bs = a[0]->as.bytes.length;
47
1.01M
        } else if (!utf8_offset(a[0], start, &bs, &be)) {
  Branch (47:20): [True: 0, False: 506k]
  Branch (47:20): [True: 0, False: 506k]
48
0
            return NULL;
49
0
        }
50
1.02M
        if (end == count) {
  Branch (50:13): [True: 182k, False: 327k]
  Branch (50:13): [True: 182k, False: 327k]
51
364k
            be = a[0]->as.bytes.length;
52
655k
        } else {
53
655k
            size_t ignored;
54
655k
            if (!utf8_offset(a[0], end, &be, &ignored)) {
  Branch (54:17): [True: 0, False: 327k]
  Branch (54:17): [True: 0, False: 327k]
55
0
                return NULL;
56
0
            }
57
655k
        }
58
1.02M
        return value_data(V_STR, a[0]->as.bytes.data + bs, be - bs);
59
1.02M
    }
60
61
22.6M
    if (!strcmp(name, "starts_with") || !strcmp(name, "starts_with_at")) {
  Branch (61:9): [True: 35.3k, False: 11.3M]
  Branch (61:41): [True: 5.34M, False: 5.96M]
  Branch (61:9): [True: 35.3k, False: 11.3M]
  Branch (61:41): [True: 5.34M, False: 5.96M]
62
10.7M
        REQUIRE(a[0]->kind == V_STR && a[1]->kind == V_STR, "VM trap: starts_with requires Str");
63
10.7M
        if (!strcmp(name, "starts_with_at")) {
  Branch (63:13): [True: 5.34M, False: 35.3k]
  Branch (63:13): [True: 5.34M, False: 35.3k]
64
10.6M
            REQUIRE(a[2]->kind == V_NAT, "VM trap: starts_with_at requires Nat offset");
65
10.6M
        }
66
10.7M
        size_t offset = 0, bs = 0, be;
67
10.7M
        if (!strcmp(name, "starts_with_at")) {
  Branch (67:13): [True: 5.34M, False: 35.3k]
  Branch (67:13): [True: 5.34M, False: 35.3k]
68
10.6M
            if (!value_index(a[2], &offset)) {
  Branch (68:17): [True: 1, False: 5.34M]
  Branch (68:17): [True: 1, False: 5.34M]
69
2
                return value_bool(false);
70
2
            }
71
10.6M
            if (offset == a[0]->as.bytes.characters) {
  Branch (71:17): [True: 469k, False: 4.87M]
  Branch (71:17): [True: 469k, False: 4.87M]
72
938k
                bs = a[0]->as.bytes.length;
73
9.74M
            } else if (!utf8_offset(a[0], offset, &bs, &be)) {
  Branch (73:24): [True: 2, False: 4.87M]
  Branch (73:24): [True: 2, False: 4.87M]
74
4
                return value_bool(false);
75
4
            }
76
10.6M
        }
77
10.7M
        bool result = a[1]->as.bytes.length <= a[0]->as.bytes.length - bs &&
  Branch (77:23): [True: 4.80M, False: 568k]
  Branch (77:23): [True: 4.80M, False: 568k]
78
10.7M
                      !memcmp(a[0]->as.bytes.data + bs, a[1]->as.bytes.data, a[1]->as.bytes.length);
  Branch (78:23): [True: 49.6k, False: 4.75M]
  Branch (78:23): [True: 49.6k, False: 4.75M]
79
10.7M
        return value_bool(result);
80
10.7M
    }
81
82
11.9M
    if (!strcmp(name, "reverse")) {
  Branch (82:9): [True: 7, False: 5.96M]
  Branch (82:9): [True: 7, False: 5.96M]
83
14
        REQUIRE(a[0]->kind == V_STR, "VM trap: reverse requires Str");
84
14
        size_t n = a[0]->as.bytes.length, source = 0, destination = n;
85
14
        uint8_t *data = malloc(n ? n : 1);
  Branch (85:32): [True: 7, False: 0]
  Branch (85:32): [True: 7, False: 0]
86
14
        if (!data) {
  Branch (86:13): [True: 0, False: 7]
  Branch (86:13): [True: 0, False: 7]
87
0
            return NULL;
88
0
        }
89
64
        while (source < n) {
  Branch (89:16): [True: 25, False: 7]
  Branch (89:16): [True: 25, False: 7]
90
50
            uint8_t c = a[0]->as.bytes.data[source];
91
50
            size_t width = c < 0x80 ? 1 : (c & 0xe0) == 0xc0 ? 2 : (c & 0xf0) == 0xe0 ? 3 : 4;
  Branch (91:28): [True: 13, False: 12]
  Branch (91:43): [True: 6, False: 6]
  Branch (91:68): [True: 1, False: 5]
  Branch (91:28): [True: 13, False: 12]
  Branch (91:43): [True: 6, False: 6]
  Branch (91:68): [True: 1, False: 5]
92
50
            destination -= width;
93
50
            memcpy(data + destination, a[0]->as.bytes.data + source, width);
94
50
            source += width;
95
50
        }
96
14
        Value *v = value_data(V_STR, data, n);
97
14
        free(data);
98
14
        return v;
99
14
    }
100
101
11.9M
    if (!strcmp(name, "is_digit") || !strcmp(name, "is_letter") || !strcmp(name, "is_whitespace")) {
  Branch (101:9): [True: 826k, False: 5.14M]
  Branch (101:38): [True: 2.27M, False: 2.86M]
  Branch (101:68): [True: 1.12M, False: 1.74M]
  Branch (101:9): [True: 826k, False: 5.14M]
  Branch (101:38): [True: 2.27M, False: 2.86M]
  Branch (101:68): [True: 1.12M, False: 1.74M]
102
8.45M
        REQUIRE(a[0]->kind == V_STR, "VM trap: character class requires Str");
103
8.45M
        uint8_t c = a[0]->as.bytes.length == 1 ? a[0]->as.bytes.data[0] : 0;
  Branch (103:21): [True: 4.22M, False: 1]
  Branch (103:21): [True: 4.22M, False: 1]
104
8.45M
        bool result = !strcmp(name, "is_digit") ? c >= '0' && c <= '9'
  Branch (104:23): [True: 826k, False: 3.39M]
  Branch (104:51): [True: 449k, False: 377k]
  Branch (104:63): [True: 42.6k, False: 407k]
  Branch (104:23): [True: 826k, False: 3.39M]
  Branch (104:51): [True: 449k, False: 377k]
  Branch (104:63): [True: 42.6k, False: 407k]
105
8.45M
                      : !strcmp(name, "is_letter")
  Branch (105:25): [True: 2.27M, False: 1.12M]
  Branch (105:25): [True: 2.27M, False: 1.12M]
106
6.79M
                          ? (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
  Branch (106:30): [True: 1.83M, False: 434k]
  Branch (106:42): [True: 127k, False: 1.71M]
  Branch (106:56): [True: 1.64M, False: 506k]
  Branch (106:68): [True: 1.58M, False: 57.7k]
  Branch (106:30): [True: 1.83M, False: 434k]
  Branch (106:42): [True: 127k, False: 1.71M]
  Branch (106:56): [True: 1.64M, False: 506k]
  Branch (106:68): [True: 1.58M, False: 57.7k]
107
6.79M
                          : c == ' ' || c == '\t' || c == '\r' || c == '\n';
  Branch (107:29): [True: 556k, False: 568k]
  Branch (107:41): [True: 0, False: 568k]
  Branch (107:54): [True: 0, False: 568k]
  Branch (107:67): [True: 173, False: 568k]
  Branch (107:29): [True: 556k, False: 568k]
  Branch (107:41): [True: 0, False: 568k]
  Branch (107:54): [True: 0, False: 568k]
  Branch (107:67): [True: 173, False: 568k]
108
8.45M
        return value_bool(result);
109
8.45M
    }
110
111
3.48M
    if (!strcmp(name, "byte_append")) {
  Branch (111:9): [True: 795k, False: 945k]
  Branch (111:9): [True: 795k, False: 945k]
112
1.59M
        REQUIRE(a[0]->kind == V_BYTES && a[1]->kind == V_NAT,
113
1.59M
                "VM trap: byte_append requires Bytes and Nat");
114
1.59M
        size_t byte;
115
1.59M
        if (!value_index(a[1], &byte) || byte > 255) {
  Branch (115:13): [True: 0, False: 795k]
  Branch (115:42): [True: 2, False: 795k]
  Branch (115:13): [True: 0, False: 795k]
  Branch (115:42): [True: 2, False: 795k]
116
4
            vm->error = "VM trap: byte value is outside 0..255";
117
4
            return NULL;
118
4
        }
119
1.59M
        uint8_t item = (uint8_t)byte;
120
1.59M
        return value_data_join(V_BYTES, a[0]->as.bytes.data, a[0]->as.bytes.length,
121
1.59M
                               &item, 1);
122
1.59M
    }
123
124
1.89M
    if (!strcmp(name, "bytes_concat")) {
  Branch (124:9): [True: 621k, False: 324k]
  Branch (124:9): [True: 621k, False: 324k]
125
1.24M
        REQUIRE(a[0]->kind == V_BYTES && a[1]->kind == V_BYTES,
126
1.24M
                "VM trap: bytes_concat requires Bytes");
127
1.24M
        return value_data_join(V_BYTES, a[0]->as.bytes.data, a[0]->as.bytes.length,
128
1.24M
                               a[1]->as.bytes.data, a[1]->as.bytes.length);
129
1.24M
    }
130
131
648k
    if (!strcmp(name, "byte_len")) {
  Branch (131:9): [True: 156k, False: 167k]
  Branch (131:9): [True: 156k, False: 167k]
132
313k
        REQUIRE(a[0]->kind == V_BYTES, "VM trap: byte_len requires Bytes");
133
313k
        return value_size(a[0]->as.bytes.length);
134
313k
    }
135
136
334k
    if (!strcmp(name, "byte_get")) {
  Branch (136:9): [True: 5.80k, False: 161k]
  Branch (136:9): [True: 5.80k, False: 161k]
137
11.6k
        REQUIRE(a[0]->kind == V_BYTES && a[1]->kind == V_NAT,
138
11.6k
                "VM trap: byte_get requires Bytes and Nat");
139
11.6k
        size_t i;
140
11.6k
        if (!value_index(a[1], &i) || i >= a[0]->as.bytes.length) {
  Branch (140:13): [True: 0, False: 5.80k]
  Branch (140:39): [True: 0, False: 5.80k]
  Branch (140:13): [True: 0, False: 5.80k]
  Branch (140:39): [True: 0, False: 5.80k]
141
0
            vm->error = "VM trap: byte index is out of bounds";
142
0
            vm->fault_kind = VM_FAULT_REQUEST;
143
0
            return NULL;
144
0
        }
145
11.6k
        return value_size(a[0]->as.bytes.data[i]);
146
11.6k
    }
147
148
323k
    if (!strcmp(name, "utf8_encode")) {
  Branch (148:9): [True: 149k, False: 12.4k]
  Branch (148:9): [True: 149k, False: 12.4k]
149
298k
        REQUIRE(a[0]->kind == V_STR, "VM trap: utf8_encode requires Str");
150
298k
        return value_data(V_BYTES, a[0]->as.bytes.data, a[0]->as.bytes.length);
151
298k
    }
152
153
24.8k
    if (!strcmp(name, "utf8_decode")) {
  Branch (153:9): [True: 722, False: 11.6k]
  Branch (153:9): [True: 722, False: 11.6k]
154
1.44k
        REQUIRE(a[0]->kind == V_BYTES, "VM trap: utf8_decode requires Bytes");
155
1.44k
        if (!utf8(a[0]->as.bytes.data, a[0]->as.bytes.length)) {
  Branch (155:13): [True: 2, False: 719]
  Branch (155:13): [True: 2, False: 719]
156
4
            vm->error = "VM trap: invalid UTF-8";
157
4
            return NULL;
158
4
        }
159
1.43k
        return value_data(V_STR, a[0]->as.bytes.data, a[0]->as.bytes.length);
160
1.44k
    }
161
162
23.3k
    if (!strcmp(name, "nat_from_str")) {
  Branch (162:9): [True: 11.6k, False: 0]
  Branch (162:9): [True: 11.6k, False: 0]
163
23.3k
        REQUIRE(a[0]->kind == V_STR, "VM trap: nat_from_str requires Str");
164
23.3k
        if (!a[0]->as.bytes.length) {
  Branch (164:13): [True: 0, False: 11.6k]
  Branch (164:13): [True: 0, False: 11.6k]
165
0
            vm->error = "VM trap: text is not a Nat";
166
0
            return NULL;
167
0
        }
168
80.4k
        for (size_t i = 0; i < a[0]->as.bytes.length; i++) {
  Branch (168:28): [True: 28.5k, False: 11.6k]
  Branch (168:28): [True: 28.5k, False: 11.6k]
169
57.0k
            if (a[0]->as.bytes.data[i] < '0' || a[0]->as.bytes.data[i] > '9') {
  Branch (169:17): [True: 0, False: 28.5k]
  Branch (169:49): [True: 0, False: 28.5k]
  Branch (169:17): [True: 0, False: 28.5k]
  Branch (169:49): [True: 0, False: 28.5k]
170
0
                vm->error = "VM trap: text is not a Nat";
171
0
                return NULL;
172
0
            }
173
57.0k
        }
174
23.3k
        PnBigInt n;
175
23.3k
        if (!pn_big_from_digits(&n, (char *)a[0]->as.bytes.data, a[0]->as.bytes.length, 1)) {
  Branch (175:13): [True: 0, False: 11.6k]
  Branch (175:13): [True: 0, False: 11.6k]
176
0
            return NULL;
177
0
        }
178
23.3k
        Value *v = value_big(V_NAT, &n);
179
23.3k
        pn_big_free(&n);
180
23.3k
        return v;
181
23.3k
    }
182
0
    vm->error = "VM trap: unknown builtin";
183
0
    return NULL;
184
23.3k
}
builtins_text_call
Line
Count
Source
11
30.2M
{
12
30.2M
    if (!strcmp(name, "len")) {
  Branch (12:9): [True: 14.6M, False: 15.5M]
13
14.6M
        if (a[0]->kind == V_MAP || a[0]->kind == V_SET) {
  Branch (13:13): [True: 3, False: 14.6M]
  Branch (13:36): [True: 3, False: 14.6M]
14
6
            size_t slots = a[0]->as.sequence.count;
15
6
            return value_size(a[0]->kind == V_MAP ? slots / 2 : slots);
  Branch (15:31): [True: 3, False: 3]
16
6
        }
17
14.6M
        REQUIRE(a[0]->kind == V_STR || a[0]->kind == V_BYTES || a[0]->kind == V_ARRAY,
18
14.6M
                "VM trap: len requires Str, Bytes, Array, Map, or Set");
19
14.6M
        size_t n = a[0]->kind == V_STR     ? a[0]->as.bytes.characters
  Branch (19:20): [True: 8.87M, False: 5.78M]
20
14.6M
                   : a[0]->kind == V_BYTES ? a[0]->as.bytes.length
  Branch (20:22): [True: 0, False: 5.78M]
21
5.78M
                                           : a[0]->as.sequence.count;
22
14.6M
        return value_size(n);
23
14.6M
    }
24
25
15.5M
    if (!strcmp(name, "concat")) {
  Branch (25:9): [True: 3.68M, False: 11.8M]
26
3.68M
        REQUIRE(a[0]->kind == V_ARRAY && a[1]->kind == V_ARRAY, "VM trap: concat requires arrays");
27
3.68M
        return value_array_concat(a[0], a[1]);
28
3.68M
    }
29
30
11.8M
    if (!strcmp(name, "slice")) {
  Branch (30:9): [True: 510k, False: 11.3M]
31
510k
        REQUIRE(a[0]->kind == V_STR && a[1]->kind == V_NAT && a[2]->kind == V_NAT,
32
510k
                "VM trap: slice requires Str and Nat bounds");
33
510k
        size_t start, end, bs, be;
34
510k
        if (!value_index(a[1], &start) || !value_index(a[2], &end) || start > end) {
  Branch (34:13): [True: 0, False: 510k]
  Branch (34:43): [True: 0, False: 510k]
  Branch (34:71): [True: 1, False: 510k]
35
1
            vm->error = "VM trap: invalid string slice";
36
1
            vm->fault_kind = VM_FAULT_REQUEST;
37
1
            return NULL;
38
1
        }
39
510k
        size_t count = a[0]->as.bytes.characters;
40
510k
        if (end > count) {
  Branch (40:13): [True: 3, False: 510k]
41
3
            vm->error = "VM trap: invalid string slice";
42
3
            vm->fault_kind = VM_FAULT_REQUEST;
43
3
            return NULL;
44
3
        }
45
510k
        if (start == count) {
  Branch (45:13): [True: 3.81k, False: 506k]
46
3.81k
            bs = a[0]->as.bytes.length;
47
506k
        } else if (!utf8_offset(a[0], start, &bs, &be)) {
  Branch (47:20): [True: 0, False: 506k]
48
0
            return NULL;
49
0
        }
50
510k
        if (end == count) {
  Branch (50:13): [True: 182k, False: 327k]
51
182k
            be = a[0]->as.bytes.length;
52
327k
        } else {
53
327k
            size_t ignored;
54
327k
            if (!utf8_offset(a[0], end, &be, &ignored)) {
  Branch (54:17): [True: 0, False: 327k]
55
0
                return NULL;
56
0
            }
57
327k
        }
58
510k
        return value_data(V_STR, a[0]->as.bytes.data + bs, be - bs);
59
510k
    }
60
61
11.3M
    if (!strcmp(name, "starts_with") || !strcmp(name, "starts_with_at")) {
  Branch (61:9): [True: 35.3k, False: 11.3M]
  Branch (61:41): [True: 5.34M, False: 5.96M]
62
5.37M
        REQUIRE(a[0]->kind == V_STR && a[1]->kind == V_STR, "VM trap: starts_with requires Str");
63
5.37M
        if (!strcmp(name, "starts_with_at")) {
  Branch (63:13): [True: 5.34M, False: 35.3k]
64
5.34M
            REQUIRE(a[2]->kind == V_NAT, "VM trap: starts_with_at requires Nat offset");
65
5.34M
        }
66
5.37M
        size_t offset = 0, bs = 0, be;
67
5.37M
        if (!strcmp(name, "starts_with_at")) {
  Branch (67:13): [True: 5.34M, False: 35.3k]
68
5.34M
            if (!value_index(a[2], &offset)) {
  Branch (68:17): [True: 1, False: 5.34M]
69
1
                return value_bool(false);
70
1
            }
71
5.34M
            if (offset == a[0]->as.bytes.characters) {
  Branch (71:17): [True: 469k, False: 4.87M]
72
469k
                bs = a[0]->as.bytes.length;
73
4.87M
            } else if (!utf8_offset(a[0], offset, &bs, &be)) {
  Branch (73:24): [True: 2, False: 4.87M]
74
2
                return value_bool(false);
75
2
            }
76
5.34M
        }
77
5.37M
        bool result = a[1]->as.bytes.length <= a[0]->as.bytes.length - bs &&
  Branch (77:23): [True: 4.80M, False: 568k]
78
5.37M
                      !memcmp(a[0]->as.bytes.data + bs, a[1]->as.bytes.data, a[1]->as.bytes.length);
  Branch (78:23): [True: 49.6k, False: 4.75M]
79
5.37M
        return value_bool(result);
80
5.37M
    }
81
82
5.96M
    if (!strcmp(name, "reverse")) {
  Branch (82:9): [True: 7, False: 5.96M]
83
7
        REQUIRE(a[0]->kind == V_STR, "VM trap: reverse requires Str");
84
7
        size_t n = a[0]->as.bytes.length, source = 0, destination = n;
85
7
        uint8_t *data = malloc(n ? n : 1);
  Branch (85:32): [True: 7, False: 0]
86
7
        if (!data) {
  Branch (86:13): [True: 0, False: 7]
87
0
            return NULL;
88
0
        }
89
32
        while (source < n) {
  Branch (89:16): [True: 25, False: 7]
90
25
            uint8_t c = a[0]->as.bytes.data[source];
91
25
            size_t width = c < 0x80 ? 1 : (c & 0xe0) == 0xc0 ? 2 : (c & 0xf0) == 0xe0 ? 3 : 4;
  Branch (91:28): [True: 13, False: 12]
  Branch (91:43): [True: 6, False: 6]
  Branch (91:68): [True: 1, False: 5]
92
25
            destination -= width;
93
25
            memcpy(data + destination, a[0]->as.bytes.data + source, width);
94
25
            source += width;
95
25
        }
96
7
        Value *v = value_data(V_STR, data, n);
97
7
        free(data);
98
7
        return v;
99
7
    }
100
101
5.96M
    if (!strcmp(name, "is_digit") || !strcmp(name, "is_letter") || !strcmp(name, "is_whitespace")) {
  Branch (101:9): [True: 826k, False: 5.14M]
  Branch (101:38): [True: 2.27M, False: 2.86M]
  Branch (101:68): [True: 1.12M, False: 1.74M]
102
4.22M
        REQUIRE(a[0]->kind == V_STR, "VM trap: character class requires Str");
103
4.22M
        uint8_t c = a[0]->as.bytes.length == 1 ? a[0]->as.bytes.data[0] : 0;
  Branch (103:21): [True: 4.22M, False: 1]
104
4.22M
        bool result = !strcmp(name, "is_digit") ? c >= '0' && c <= '9'
  Branch (104:23): [True: 826k, False: 3.39M]
  Branch (104:51): [True: 449k, False: 377k]
  Branch (104:63): [True: 42.6k, False: 407k]
105
4.22M
                      : !strcmp(name, "is_letter")
  Branch (105:25): [True: 2.27M, False: 1.12M]
106
3.39M
                          ? (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
  Branch (106:30): [True: 1.83M, False: 434k]
  Branch (106:42): [True: 127k, False: 1.71M]
  Branch (106:56): [True: 1.64M, False: 506k]
  Branch (106:68): [True: 1.58M, False: 57.7k]
107
3.39M
                          : c == ' ' || c == '\t' || c == '\r' || c == '\n';
  Branch (107:29): [True: 556k, False: 568k]
  Branch (107:41): [True: 0, False: 568k]
  Branch (107:54): [True: 0, False: 568k]
  Branch (107:67): [True: 173, False: 568k]
108
4.22M
        return value_bool(result);
109
4.22M
    }
110
111
1.74M
    if (!strcmp(name, "byte_append")) {
  Branch (111:9): [True: 795k, False: 945k]
112
795k
        REQUIRE(a[0]->kind == V_BYTES && a[1]->kind == V_NAT,
113
795k
                "VM trap: byte_append requires Bytes and Nat");
114
795k
        size_t byte;
115
795k
        if (!value_index(a[1], &byte) || byte > 255) {
  Branch (115:13): [True: 0, False: 795k]
  Branch (115:42): [True: 2, False: 795k]
116
2
            vm->error = "VM trap: byte value is outside 0..255";
117
2
            return NULL;
118
2
        }
119
795k
        uint8_t item = (uint8_t)byte;
120
795k
        return value_data_join(V_BYTES, a[0]->as.bytes.data, a[0]->as.bytes.length,
121
795k
                               &item, 1);
122
795k
    }
123
124
945k
    if (!strcmp(name, "bytes_concat")) {
  Branch (124:9): [True: 621k, False: 324k]
125
621k
        REQUIRE(a[0]->kind == V_BYTES && a[1]->kind == V_BYTES,
126
621k
                "VM trap: bytes_concat requires Bytes");
127
621k
        return value_data_join(V_BYTES, a[0]->as.bytes.data, a[0]->as.bytes.length,
128
621k
                               a[1]->as.bytes.data, a[1]->as.bytes.length);
129
621k
    }
130
131
324k
    if (!strcmp(name, "byte_len")) {
  Branch (131:9): [True: 156k, False: 167k]
132
156k
        REQUIRE(a[0]->kind == V_BYTES, "VM trap: byte_len requires Bytes");
133
156k
        return value_size(a[0]->as.bytes.length);
134
156k
    }
135
136
167k
    if (!strcmp(name, "byte_get")) {
  Branch (136:9): [True: 5.80k, False: 161k]
137
5.80k
        REQUIRE(a[0]->kind == V_BYTES && a[1]->kind == V_NAT,
138
5.80k
                "VM trap: byte_get requires Bytes and Nat");
139
5.80k
        size_t i;
140
5.80k
        if (!value_index(a[1], &i) || i >= a[0]->as.bytes.length) {
  Branch (140:13): [True: 0, False: 5.80k]
  Branch (140:39): [True: 0, False: 5.80k]
141
0
            vm->error = "VM trap: byte index is out of bounds";
142
0
            vm->fault_kind = VM_FAULT_REQUEST;
143
0
            return NULL;
144
0
        }
145
5.80k
        return value_size(a[0]->as.bytes.data[i]);
146
5.80k
    }
147
148
161k
    if (!strcmp(name, "utf8_encode")) {
  Branch (148:9): [True: 149k, False: 12.4k]
149
149k
        REQUIRE(a[0]->kind == V_STR, "VM trap: utf8_encode requires Str");
150
149k
        return value_data(V_BYTES, a[0]->as.bytes.data, a[0]->as.bytes.length);
151
149k
    }
152
153
12.4k
    if (!strcmp(name, "utf8_decode")) {
  Branch (153:9): [True: 722, False: 11.6k]
154
722
        REQUIRE(a[0]->kind == V_BYTES, "VM trap: utf8_decode requires Bytes");
155
721
        if (!utf8(a[0]->as.bytes.data, a[0]->as.bytes.length)) {
  Branch (155:13): [True: 2, False: 719]
156
2
            vm->error = "VM trap: invalid UTF-8";
157
2
            return NULL;
158
2
        }
159
719
        return value_data(V_STR, a[0]->as.bytes.data, a[0]->as.bytes.length);
160
721
    }
161
162
11.6k
    if (!strcmp(name, "nat_from_str")) {
  Branch (162:9): [True: 11.6k, False: 0]
163
11.6k
        REQUIRE(a[0]->kind == V_STR, "VM trap: nat_from_str requires Str");
164
11.6k
        if (!a[0]->as.bytes.length) {
  Branch (164:13): [True: 0, False: 11.6k]
165
0
            vm->error = "VM trap: text is not a Nat";
166
0
            return NULL;
167
0
        }
168
40.2k
        for (size_t i = 0; i < a[0]->as.bytes.length; i++) {
  Branch (168:28): [True: 28.5k, False: 11.6k]
169
28.5k
            if (a[0]->as.bytes.data[i] < '0' || a[0]->as.bytes.data[i] > '9') {
  Branch (169:17): [True: 0, False: 28.5k]
  Branch (169:49): [True: 0, False: 28.5k]
170
0
                vm->error = "VM trap: text is not a Nat";
171
0
                return NULL;
172
0
            }
173
28.5k
        }
174
11.6k
        PnBigInt n;
175
11.6k
        if (!pn_big_from_digits(&n, (char *)a[0]->as.bytes.data, a[0]->as.bytes.length, 1)) {
  Branch (175:13): [True: 0, False: 11.6k]
176
0
            return NULL;
177
0
        }
178
11.6k
        Value *v = value_big(V_NAT, &n);
179
11.6k
        pn_big_free(&n);
180
11.6k
        return v;
181
11.6k
    }
182
0
    vm->error = "VM trap: unknown builtin";
183
0
    return NULL;
184
11.6k
}
builtins_text_call
Line
Count
Source
11
30.2M
{
12
30.2M
    if (!strcmp(name, "len")) {
  Branch (12:9): [True: 14.6M, False: 15.5M]
13
14.6M
        if (a[0]->kind == V_MAP || a[0]->kind == V_SET) {
  Branch (13:13): [True: 3, False: 14.6M]
  Branch (13:36): [True: 3, False: 14.6M]
14
6
            size_t slots = a[0]->as.sequence.count;
15
6
            return value_size(a[0]->kind == V_MAP ? slots / 2 : slots);
  Branch (15:31): [True: 3, False: 3]
16
6
        }
17
14.6M
        REQUIRE(a[0]->kind == V_STR || a[0]->kind == V_BYTES || a[0]->kind == V_ARRAY,
18
14.6M
                "VM trap: len requires Str, Bytes, Array, Map, or Set");
19
14.6M
        size_t n = a[0]->kind == V_STR     ? a[0]->as.bytes.characters
  Branch (19:20): [True: 8.87M, False: 5.78M]
20
14.6M
                   : a[0]->kind == V_BYTES ? a[0]->as.bytes.length
  Branch (20:22): [True: 0, False: 5.78M]
21
5.78M
                                           : a[0]->as.sequence.count;
22
14.6M
        return value_size(n);
23
14.6M
    }
24
25
15.5M
    if (!strcmp(name, "concat")) {
  Branch (25:9): [True: 3.68M, False: 11.8M]
26
3.68M
        REQUIRE(a[0]->kind == V_ARRAY && a[1]->kind == V_ARRAY, "VM trap: concat requires arrays");
27
3.68M
        return value_array_concat(a[0], a[1]);
28
3.68M
    }
29
30
11.8M
    if (!strcmp(name, "slice")) {
  Branch (30:9): [True: 510k, False: 11.3M]
31
510k
        REQUIRE(a[0]->kind == V_STR && a[1]->kind == V_NAT && a[2]->kind == V_NAT,
32
510k
                "VM trap: slice requires Str and Nat bounds");
33
510k
        size_t start, end, bs, be;
34
510k
        if (!value_index(a[1], &start) || !value_index(a[2], &end) || start > end) {
  Branch (34:13): [True: 0, False: 510k]
  Branch (34:43): [True: 0, False: 510k]
  Branch (34:71): [True: 1, False: 510k]
35
1
            vm->error = "VM trap: invalid string slice";
36
1
            vm->fault_kind = VM_FAULT_REQUEST;
37
1
            return NULL;
38
1
        }
39
510k
        size_t count = a[0]->as.bytes.characters;
40
510k
        if (end > count) {
  Branch (40:13): [True: 3, False: 510k]
41
3
            vm->error = "VM trap: invalid string slice";
42
3
            vm->fault_kind = VM_FAULT_REQUEST;
43
3
            return NULL;
44
3
        }
45
510k
        if (start == count) {
  Branch (45:13): [True: 3.81k, False: 506k]
46
3.81k
            bs = a[0]->as.bytes.length;
47
506k
        } else if (!utf8_offset(a[0], start, &bs, &be)) {
  Branch (47:20): [True: 0, False: 506k]
48
0
            return NULL;
49
0
        }
50
510k
        if (end == count) {
  Branch (50:13): [True: 182k, False: 327k]
51
182k
            be = a[0]->as.bytes.length;
52
327k
        } else {
53
327k
            size_t ignored;
54
327k
            if (!utf8_offset(a[0], end, &be, &ignored)) {
  Branch (54:17): [True: 0, False: 327k]
55
0
                return NULL;
56
0
            }
57
327k
        }
58
510k
        return value_data(V_STR, a[0]->as.bytes.data + bs, be - bs);
59
510k
    }
60
61
11.3M
    if (!strcmp(name, "starts_with") || !strcmp(name, "starts_with_at")) {
  Branch (61:9): [True: 35.3k, False: 11.3M]
  Branch (61:41): [True: 5.34M, False: 5.96M]
62
5.37M
        REQUIRE(a[0]->kind == V_STR && a[1]->kind == V_STR, "VM trap: starts_with requires Str");
63
5.37M
        if (!strcmp(name, "starts_with_at")) {
  Branch (63:13): [True: 5.34M, False: 35.3k]
64
5.34M
            REQUIRE(a[2]->kind == V_NAT, "VM trap: starts_with_at requires Nat offset");
65
5.34M
        }
66
5.37M
        size_t offset = 0, bs = 0, be;
67
5.37M
        if (!strcmp(name, "starts_with_at")) {
  Branch (67:13): [True: 5.34M, False: 35.3k]
68
5.34M
            if (!value_index(a[2], &offset)) {
  Branch (68:17): [True: 1, False: 5.34M]
69
1
                return value_bool(false);
70
1
            }
71
5.34M
            if (offset == a[0]->as.bytes.characters) {
  Branch (71:17): [True: 469k, False: 4.87M]
72
469k
                bs = a[0]->as.bytes.length;
73
4.87M
            } else if (!utf8_offset(a[0], offset, &bs, &be)) {
  Branch (73:24): [True: 2, False: 4.87M]
74
2
                return value_bool(false);
75
2
            }
76
5.34M
        }
77
5.37M
        bool result = a[1]->as.bytes.length <= a[0]->as.bytes.length - bs &&
  Branch (77:23): [True: 4.80M, False: 568k]
78
5.37M
                      !memcmp(a[0]->as.bytes.data + bs, a[1]->as.bytes.data, a[1]->as.bytes.length);
  Branch (78:23): [True: 49.6k, False: 4.75M]
79
5.37M
        return value_bool(result);
80
5.37M
    }
81
82
5.96M
    if (!strcmp(name, "reverse")) {
  Branch (82:9): [True: 7, False: 5.96M]
83
7
        REQUIRE(a[0]->kind == V_STR, "VM trap: reverse requires Str");
84
7
        size_t n = a[0]->as.bytes.length, source = 0, destination = n;
85
7
        uint8_t *data = malloc(n ? n : 1);
  Branch (85:32): [True: 7, False: 0]
86
7
        if (!data) {
  Branch (86:13): [True: 0, False: 7]
87
0
            return NULL;
88
0
        }
89
32
        while (source < n) {
  Branch (89:16): [True: 25, False: 7]
90
25
            uint8_t c = a[0]->as.bytes.data[source];
91
25
            size_t width = c < 0x80 ? 1 : (c & 0xe0) == 0xc0 ? 2 : (c & 0xf0) == 0xe0 ? 3 : 4;
  Branch (91:28): [True: 13, False: 12]
  Branch (91:43): [True: 6, False: 6]
  Branch (91:68): [True: 1, False: 5]
92
25
            destination -= width;
93
25
            memcpy(data + destination, a[0]->as.bytes.data + source, width);
94
25
            source += width;
95
25
        }
96
7
        Value *v = value_data(V_STR, data, n);
97
7
        free(data);
98
7
        return v;
99
7
    }
100
101
5.96M
    if (!strcmp(name, "is_digit") || !strcmp(name, "is_letter") || !strcmp(name, "is_whitespace")) {
  Branch (101:9): [True: 826k, False: 5.14M]
  Branch (101:38): [True: 2.27M, False: 2.86M]
  Branch (101:68): [True: 1.12M, False: 1.74M]
102
4.22M
        REQUIRE(a[0]->kind == V_STR, "VM trap: character class requires Str");
103
4.22M
        uint8_t c = a[0]->as.bytes.length == 1 ? a[0]->as.bytes.data[0] : 0;
  Branch (103:21): [True: 4.22M, False: 1]
104
4.22M
        bool result = !strcmp(name, "is_digit") ? c >= '0' && c <= '9'
  Branch (104:23): [True: 826k, False: 3.39M]
  Branch (104:51): [True: 449k, False: 377k]
  Branch (104:63): [True: 42.6k, False: 407k]
105
4.22M
                      : !strcmp(name, "is_letter")
  Branch (105:25): [True: 2.27M, False: 1.12M]
106
3.39M
                          ? (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
  Branch (106:30): [True: 1.83M, False: 434k]
  Branch (106:42): [True: 127k, False: 1.71M]
  Branch (106:56): [True: 1.64M, False: 506k]
  Branch (106:68): [True: 1.58M, False: 57.7k]
107
3.39M
                          : c == ' ' || c == '\t' || c == '\r' || c == '\n';
  Branch (107:29): [True: 556k, False: 568k]
  Branch (107:41): [True: 0, False: 568k]
  Branch (107:54): [True: 0, False: 568k]
  Branch (107:67): [True: 173, False: 568k]
108
4.22M
        return value_bool(result);
109
4.22M
    }
110
111
1.74M
    if (!strcmp(name, "byte_append")) {
  Branch (111:9): [True: 795k, False: 945k]
112
795k
        REQUIRE(a[0]->kind == V_BYTES && a[1]->kind == V_NAT,
113
795k
                "VM trap: byte_append requires Bytes and Nat");
114
795k
        size_t byte;
115
795k
        if (!value_index(a[1], &byte) || byte > 255) {
  Branch (115:13): [True: 0, False: 795k]
  Branch (115:42): [True: 2, False: 795k]
116
2
            vm->error = "VM trap: byte value is outside 0..255";
117
2
            return NULL;
118
2
        }
119
795k
        uint8_t item = (uint8_t)byte;
120
795k
        return value_data_join(V_BYTES, a[0]->as.bytes.data, a[0]->as.bytes.length,
121
795k
                               &item, 1);
122
795k
    }
123
124
945k
    if (!strcmp(name, "bytes_concat")) {
  Branch (124:9): [True: 621k, False: 324k]
125
621k
        REQUIRE(a[0]->kind == V_BYTES && a[1]->kind == V_BYTES,
126
621k
                "VM trap: bytes_concat requires Bytes");
127
621k
        return value_data_join(V_BYTES, a[0]->as.bytes.data, a[0]->as.bytes.length,
128
621k
                               a[1]->as.bytes.data, a[1]->as.bytes.length);
129
621k
    }
130
131
324k
    if (!strcmp(name, "byte_len")) {
  Branch (131:9): [True: 156k, False: 167k]
132
156k
        REQUIRE(a[0]->kind == V_BYTES, "VM trap: byte_len requires Bytes");
133
156k
        return value_size(a[0]->as.bytes.length);
134
156k
    }
135
136
167k
    if (!strcmp(name, "byte_get")) {
  Branch (136:9): [True: 5.80k, False: 161k]
137
5.80k
        REQUIRE(a[0]->kind == V_BYTES && a[1]->kind == V_NAT,
138
5.80k
                "VM trap: byte_get requires Bytes and Nat");
139
5.80k
        size_t i;
140
5.80k
        if (!value_index(a[1], &i) || i >= a[0]->as.bytes.length) {
  Branch (140:13): [True: 0, False: 5.80k]
  Branch (140:39): [True: 0, False: 5.80k]
141
0
            vm->error = "VM trap: byte index is out of bounds";
142
0
            vm->fault_kind = VM_FAULT_REQUEST;
143
0
            return NULL;
144
0
        }
145
5.80k
        return value_size(a[0]->as.bytes.data[i]);
146
5.80k
    }
147
148
161k
    if (!strcmp(name, "utf8_encode")) {
  Branch (148:9): [True: 149k, False: 12.4k]
149
149k
        REQUIRE(a[0]->kind == V_STR, "VM trap: utf8_encode requires Str");
150
149k
        return value_data(V_BYTES, a[0]->as.bytes.data, a[0]->as.bytes.length);
151
149k
    }
152
153
12.4k
    if (!strcmp(name, "utf8_decode")) {
  Branch (153:9): [True: 722, False: 11.6k]
154
722
        REQUIRE(a[0]->kind == V_BYTES, "VM trap: utf8_decode requires Bytes");
155
721
        if (!utf8(a[0]->as.bytes.data, a[0]->as.bytes.length)) {
  Branch (155:13): [True: 2, False: 719]
156
2
            vm->error = "VM trap: invalid UTF-8";
157
2
            return NULL;
158
2
        }
159
719
        return value_data(V_STR, a[0]->as.bytes.data, a[0]->as.bytes.length);
160
721
    }
161
162
11.6k
    if (!strcmp(name, "nat_from_str")) {
  Branch (162:9): [True: 11.6k, False: 0]
163
11.6k
        REQUIRE(a[0]->kind == V_STR, "VM trap: nat_from_str requires Str");
164
11.6k
        if (!a[0]->as.bytes.length) {
  Branch (164:13): [True: 0, False: 11.6k]
165
0
            vm->error = "VM trap: text is not a Nat";
166
0
            return NULL;
167
0
        }
168
40.2k
        for (size_t i = 0; i < a[0]->as.bytes.length; i++) {
  Branch (168:28): [True: 28.5k, False: 11.6k]
169
28.5k
            if (a[0]->as.bytes.data[i] < '0' || a[0]->as.bytes.data[i] > '9') {
  Branch (169:17): [True: 0, False: 28.5k]
  Branch (169:49): [True: 0, False: 28.5k]
170
0
                vm->error = "VM trap: text is not a Nat";
171
0
                return NULL;
172
0
            }
173
28.5k
        }
174
11.6k
        PnBigInt n;
175
11.6k
        if (!pn_big_from_digits(&n, (char *)a[0]->as.bytes.data, a[0]->as.bytes.length, 1)) {
  Branch (175:13): [True: 0, False: 11.6k]
176
0
            return NULL;
177
0
        }
178
11.6k
        Value *v = value_big(V_NAT, &n);
179
11.6k
        pn_big_free(&n);
180
11.6k
        return v;
181
11.6k
    }
182
0
    vm->error = "VM trap: unknown builtin";
183
0
    return NULL;
184
11.6k
}