💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Vgmi › files › 8f14e977588ffaf2d02630251cd62… captured on 2023-12-28 at 15:44:02. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
0 /*
1 * ISC License
2 * Copyright (c) 2023 RMF <rawmonk@firemail.cc>
3 */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <ctype.h>
8 #define PAGE_INTERNAL
9 #include "page.h"
10 #include "utf8.h"
11
12 static int case_insensitive(int a, int b) {
13 return tolower(a) == tolower(b);
14 }
15
16 void page_search(struct page *page, const char *search) {
17 size_t y, x, length;
18 uint32_t field[1024] = {0}, occurrences;
19 while (page->results) {
20 struct page_search *next = page->results->next;
21 free(page->results);
22 page->results = next;
23 }
24 for (length = 0; ; length++) {
25 search += utf8_char_to_unicode(&field[length], search);
26 if (!field[length]) break;
27 }
28 if (!length) return;
29 occurrences = 0;
30 for (y = 0; y < page->length; y++) {
31 size_t i = 0;
32 for (x = 0; x < page->lines[y].length; x++) {
33 struct page_cell *cell = &page->lines[y].cells[x];
34 if (case_insensitive(cell->codepoint, field[i])) i++;
35 else i = 0;
36 cell->selected = 0;
37 if (i == length) {
38 occurrences++;
39 for (; i > 0; i--) {
40 cell->selected = occurrences;
41 cell--;
42 }
43 }
44 }
45 }
46 page->occurrences = occurrences;
47 }
48
49 int page_selection_line(struct page page) {
50 size_t y, x;
51 unsigned int selected = page.selected;
52 if (selected < 1 && selected >= page.occurrences) return 0;
53 for (y = 0; y < page.length; y++) {
54 for (x = 0; x < page.lines[y].length; x++) {
55 if (page.lines[y].cells[x].selected == selected)
56 return y;
57 }
58 }
59 return 0;
60 }
61