One page: its line-local structural tokens, then its join proposals. */
| 1045 | |
| 1046 | /* One page: its line-local structural tokens, then its join proposals. */ |
| 1047 | static void scan_page(CBMExtractCtx *ctx, const char *text, size_t len, uint32_t page, |
| 1048 | const char *page_qn) { |
| 1049 | u32v_t norm = {0}; |
| 1050 | if (!normalise(text, len, &norm)) { |
| 1051 | u32_free(&norm); |
| 1052 | ctx->result->doc_links.failed = true; |
| 1053 | return; |
| 1054 | } |
| 1055 | /* lines, each stripped; flat = lines joined by '\n' */ |
| 1056 | u32v_t flat = {0}; |
| 1057 | int *starts = NULL; |
| 1058 | int nlines = 0; |
| 1059 | int cap = 0; |
| 1060 | int *lens = NULL; |
| 1061 | for (int i = 0; i <= norm.n;) { |
| 1062 | int j = i; |
| 1063 | while (j < norm.n && norm.v[j] != '\n') { |
| 1064 | j++; |
| 1065 | } |
| 1066 | int a = i; |
| 1067 | int b = j; |
| 1068 | while (a < b && pdf_u_space(norm.v[a])) { |
| 1069 | a++; |
| 1070 | } |
| 1071 | while (b > a && pdf_u_space(norm.v[b - 1])) { |
| 1072 | b--; |
| 1073 | } |
| 1074 | if (nlines == cap) { |
| 1075 | cap = cap ? cap * 2 : PDF_LIST_MIN; |
| 1076 | int *g1 = (int *)cbm_realloc(CBM_MEM_CLASS_EXTRACT, starts, (size_t)cap * sizeof(int)); |
| 1077 | if (g1) { |
| 1078 | starts = g1; |
| 1079 | } |
| 1080 | int *g2 = (int *)cbm_realloc(CBM_MEM_CLASS_EXTRACT, lens, (size_t)cap * sizeof(int)); |
| 1081 | if (g2) { |
| 1082 | lens = g2; |
| 1083 | } |
| 1084 | if (!g1 || !g2) { |
| 1085 | flat.fail = true; |
| 1086 | break; |
| 1087 | } |
| 1088 | } |
| 1089 | if (nlines) { |
| 1090 | u32_push(&flat, '\n'); |
| 1091 | } |
| 1092 | starts[nlines] = flat.n; |
| 1093 | lens[nlines] = b - a; |
| 1094 | nlines++; |
| 1095 | for (int k = a; k < b; k++) { |
| 1096 | u32_push(&flat, norm.v[k]); |
| 1097 | } |
| 1098 | if (j >= norm.n) { |
| 1099 | break; |
| 1100 | } |
| 1101 | i = j + 1; |
| 1102 | } |
| 1103 | u32_free(&norm); |
| 1104 | candv_t cv = {0}; |
no test coverage detected