Gwion coverage report


Directory: src/
File: src/emit/escape.c
Date: 2023-01-30 18:32:28
Exec Total Coverage
Lines: 51 51 100.0%
Functions: 4 4 100.0%
Branches: 32 44 72.7%

Line Branch Exec Source
1 #include "gwion_util.h"
2 #include "gwion_ast.h"
3 #include "gwion_env.h"
4 #include "vm.h"
5 #include "instr.h"
6 #include "emit.h"
7 #include "escape.h"
8
9 707 char *escape_table(MemPool p) {
10 707 char *escape = (char *)mp_calloc2(p, 256);
11 707 escape['0'] = '0';
12 707 escape['\''] = '\'';
13 707 escape['"'] = '"';
14 707 escape['\\'] = '\\';
15 707 escape['a'] = (char)7; // audible bell
16 707 escape['b'] = (char)8; // back space
17 707 escape['f'] = (char)12; // form feed
18 707 escape['n'] = (char)10; // new line
19 707 escape['r'] = (char)13; // carriage return
20 707 escape['t'] = (char)9; // horizontal tab
21 707 escape['v'] = (char)11; // vertical tab
22 707 return escape;
23 }
24
25 21 static int get_escape(const Emitter emit, const char c, const loc_t pos) {
26
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 if (emit->info->escape[(int)c]) return emit->info->escape[(int)c];
27 1 env_err(emit->env, pos, _("unrecognized escape sequence '\\%c'"), c);
28 1 return GW_ERROR;
29 }
30
31 211 m_bool escape_str(const Emitter emit, const m_str base, const loc_t pos) {
32 211 unsigned char *str_lit = (unsigned char *)base;
33 211 m_str str = base;
34
2/2
✓ Branch 0 taken 2318 times.
✓ Branch 1 taken 209 times.
2527 while (*str_lit) {
35
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2301 times.
2318 if (*str_lit == '\\') {
36 17 ++str_lit;
37 17 const unsigned char c = *(str_lit);
38 17 const unsigned char c2 = *(str_lit + 1);
39
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 11 times.
17 if (c >= '0' && c <= '7') {
40
4/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 3 times.
5 if (c == '0' && (c2 < '0' || c2 > '7'))
41 2 *str++ = '\0';
42 else {
43 3 const unsigned char c3 = *(str_lit + 2);
44
5/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
3 if (c2 >= '0' && c2 <= '7' && c3 >= '0' && c3 <= '7') {
45 2 *str++ = (char)((c - '0') * 64 + (c2 - '0') * 8 + (c3 - '0'));
46 2 str_lit += 2;
47 } else {
48 1 env_err(emit->env, pos,
49 _("malformed octal escape sequence '\\%c%c%c'"), c, c2, c3);
50 1 return GW_ERROR;
51 }
52 }
53
5/8
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 10 times.
12 } else if (c == 'x' || c == 'X' || c == 'u' || c == 'U') {
54 2 ++str_lit;
55 2 const unsigned char c1 = *(str_lit);
56 2 const unsigned char c3 = *(str_lit + 1);
57
5/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
2 if (c1 >= '0' && c1 <= 'F' && c3 >= '0' && c3 <= 'F') {
58 1 *str++ = (char)((c1 - '0') * 16 + (c3 - '0'));
59 1 ++str_lit;
60 } else {
61 1 env_err(emit->env, pos, _("malformed hex escape sequence '\\%c%c'"),
62 c1, c3);
63 1 return GW_ERROR;
64 }
65 } else
66
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 CHECK_BB((*str++ = (char)get_escape(emit, (char)c, pos)));
67 } else
68 2301 *str++ = (char)*str_lit;
69 2316 ++str_lit;
70 }
71 209 *str = '\0';
72 209 return GW_OK;
73 }
74
75 22 ANN char str2char(const Emitter emit, const m_str c, const loc_t pos) {
76
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
22 return c[0] != '\\' ? c[0] : get_escape(emit, c[1], pos);
77 }
78