MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / page_text

Function page_text

internal/cbm/pdf/pdf_text.c:228–337  ·  view source on GitHub ↗

The prototype's PageText.text(): [ \t ]+ -> " ", " ?\n ?" -> "\n", * RTL runs reversed, spaces stripped at both ends; then the NUL markers go. */

Source from the content-addressed store, hash-verified

226/* The prototype's PageText.text(): [ \t ]+ -> " ", " ?\n ?" -> "\n",
227 * RTL runs reversed, spaces stripped at both ends; then the NUL markers go. */
228static char *page_text(pdf_doc_t *d, pdf_out_t *o, CBMArena *dst, size_t *out_len) {
229 const char *s = o->p ? o->p : "";
230 size_t n = o->n;
231 char *a = (char *)cbm_alloc(CBM_MEM_CLASS_EXTRACT, n + 1);
232 char *b = (char *)cbm_alloc(CBM_MEM_CLASS_EXTRACT, n + 1);
233 if (!a || !b) {
234 cbm_free(CBM_MEM_CLASS_EXTRACT, a);
235 cbm_free(CBM_MEM_CLASS_EXTRACT, b);
236 d->nomem = true;
237 return NULL;
238 }
239 /* 1: runs of space, tab, no-break space */
240 size_t k = 0;
241 for (size_t i = 0; i < n;) {
242 bool sp = s[i] == ' ' || s[i] == '\t' ||
243 (i + 1 < n && (unsigned char)s[i] == 0xC2 && (unsigned char)s[i + 1] == 0xA0);
244 if (!sp) {
245 a[k++] = s[i++];
246 continue;
247 }
248 while (i < n &&
249 (s[i] == ' ' || s[i] == '\t' ||
250 (i + 1 < n && (unsigned char)s[i] == 0xC2 && (unsigned char)s[i + 1] == 0xA0))) {
251 i += s[i] == ' ' || s[i] == '\t' ? 1 : 2;
252 }
253 a[k++] = ' ';
254 }
255 /* 2: one space either side of a newline */
256 size_t m = 0;
257 for (size_t i = 0; i < k;) {
258 if (a[i] == ' ' && i + 1 < k && a[i + 1] == '\n') {
259 b[m++] = '\n';
260 i += 2;
261 if (i < k && a[i] == ' ') {
262 i++;
263 }
264 } else if (a[i] == '\n') {
265 b[m++] = '\n';
266 i++;
267 if (i < k && a[i] == ' ') {
268 i++;
269 }
270 } else {
271 b[m++] = a[i++];
272 }
273 }
274 /* 3: RTL runs ([RTL]+( [RTL]+)*) reversed by code point */
275 k = 0;
276 for (size_t i = 0; i < m;) {
277 size_t l;
278 uint32_t cp = u8_at(b, m, i, &l);
279 if (!is_rtl(cp)) {
280 memcpy(a + k, b + i, l);
281 k += l;
282 i += l;
283 continue;
284 }
285 size_t j = i;

Callers 1

page_extractFunction · 0.85

Calls 5

cbm_allocFunction · 0.85
cbm_freeFunction · 0.85
u8_atFunction · 0.85
is_rtlFunction · 0.85
cbm_arena_allocFunction · 0.50

Tested by

no test coverage detected