| 1 |
|
| #ifndef ESHU_HL_UTIL_H |
| 2 |
|
| #define ESHU_HL_UTIL_H |
| 3 |
|
| |
| 4 |
|
| #include "eshu.h" |
| 5 |
|
| |
| 6 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 7 |
|
| * HTML-escaping output helpers |
| 8 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 9 |
|
| |
| 10 |
17257 |
| static void eshu_hl_putc_html(eshu_buf_t *b, char c) { |
| 11 |
17257 |
| switch (c) { |
| 12 |
146 |
| case '<': eshu_buf_write(b, "<", 4); break; |
| 13 |
144 |
| case '>': eshu_buf_write(b, ">", 4); break; |
| 14 |
32 |
| case '&': eshu_buf_write(b, "&", 5); break; |
| 15 |
309 |
| case '"': eshu_buf_write(b, """,6); break; |
| 16 |
16626 |
| default: eshu_buf_putc(b, c); break; |
| 17 |
|
| } |
| 18 |
17257 |
| } |
| 19 |
|
| |
| 20 |
4809 |
| static void eshu_hl_write_html(eshu_buf_t *b, const char *s, size_t n) { |
| 21 |
|
| size_t i; |
| 22 |
21948 |
▶ | for (i = 0; i < n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) |
17139 |
4809 |
21948 |
|
| 23 |
17139 |
| eshu_hl_putc_html(b, s[i]); |
| 24 |
4809 |
| } |
| 25 |
|
| |
| 26 |
|
| /* Emit a span around [tok_start, tok_end) — content is HTML-escaped */ |
| 27 |
1984 |
| static void eshu_hl_span(eshu_buf_t *b, const char *cls, |
| 28 |
|
| const char *tok_start, const char *tok_end) { |
| 29 |
1984 |
| eshu_buf_write(b, "<span class=\"", 13); |
| 30 |
1984 |
| eshu_buf_write(b, cls, strlen(cls)); |
| 31 |
1984 |
| eshu_buf_putc(b, '"'); |
| 32 |
1984 |
| eshu_buf_putc(b, '>'); |
| 33 |
1984 |
| eshu_hl_write_html(b, tok_start, (size_t)(tok_end - tok_start)); |
| 34 |
1984 |
| eshu_buf_write(b, "</span>", 7); |
| 35 |
1984 |
| } |
| 36 |
|
| |
| 37 |
|
| /* Flush accumulated plain text from [plain, upto) HTML-escaped */ |
| 38 |
3360 |
| static void eshu_hl_flush(eshu_buf_t *b, const char *plain, const char *upto) { |
| 39 |
3360 |
▶ | if (upto > plain) |
Branch: 1 branch
| Branch | True | False | Total |
| if (upto > plain) |
2193 |
1167 |
3360 |
|
| 40 |
2193 |
| eshu_hl_write_html(b, plain, (size_t)(upto - plain)); |
| 41 |
3360 |
| } |
| 42 |
|
| |
| 43 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 44 |
|
| * Keyword lookup (NULL-terminated sorted word list) |
| 45 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 46 |
|
| |
| 47 |
2207 |
| static int eshu_hl_kw(const char *s, size_t n, const char * const *list) { |
| 48 |
|
| size_t i; |
| 49 |
94858 |
▶ | for (i = 0; list[i]; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; list[i]; i++) { |
93555 |
1303 |
94858 |
|
| 50 |
93555 |
| size_t klen = strlen(list[i]); |
| 51 |
93555 |
▶ | if (klen == n && memcmp(list[i], s, n) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (klen == n && memcmp(list[i], s, n) == 0) |
9151 |
84404 |
93555 |
| if (klen == n && memcmp(list[i], s, n) == 0) |
904 |
8247 |
9151 |
|
| 52 |
904 |
| return 1; |
| 53 |
|
| } |
| 54 |
1303 |
| return 0; |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 58 |
|
| * Generic character helpers |
| 59 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 60 |
|
| |
| 61 |
6697 |
| static int eshu_hl_isalnum_(char c) { |
| 62 |
6697 |
▶ | return isalnum((unsigned char)c) || c == '_'; |
Branch: 2 branches
| Branch | True | False | Total |
| return isalnum((unsigned char)c) || c == '_'; |
1381 |
5316 |
6697 |
| return isalnum((unsigned char)c) || c == '_'; |
15 |
1366 |
1381 |
|
| 63 |
|
| } |
| 64 |
4654 |
| static int eshu_hl_isalpha_(char c) { |
| 65 |
4654 |
▶ | return isalpha((unsigned char)c) || c == '_'; |
Branch: 2 branches
| Branch | True | False | Total |
| return isalpha((unsigned char)c) || c == '_'; |
3162 |
1492 |
4654 |
| return isalpha((unsigned char)c) || c == '_'; |
7 |
3155 |
3162 |
|
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
|
| |
| 69 |
|
| #endif /* ESHU_HL_UTIL_H */ |