externint skip_utf8(const symbol * p, int c, int limit, int n) { int b; if (n < 0) return -1; for (; n > 0; n--) { if (c >= limit) return -1;
b = p[c++]; if (b >= 0xC0) { /* 1100 0000 */ while (c < limit) {
b = p[c]; if (b >= 0xC0 || b < 0x80) break; /* break unless b is 10------ */
c++;
}
}
} return c;
}
externint skip_b_utf8(const symbol * p, int c, int limit, int n) { int b; if (n < 0) return -1; for (; n > 0; n--) { if (c <= limit) return -1;
b = p[--c]; if (b >= 0x80) { /* 1000 0000 */ while (c > limit) {
b = p[c]; if (b >= 0xC0) break; /* 1100 0000 */
c--;
}
}
} return c;
}
externint find_among(struct SN_env * z, conststruct among * v, int v_size) {
int i = 0; int j = v_size;
int c = z->c; int l = z->l; const symbol * q = z->p + c;
conststruct among * w;
int common_i = 0; int common_j = 0;
int first_key_inspected = 0;
while (1) { int k = i + ((j - i) >> 1); int diff = 0; int common = common_i < common_j ? common_i : common_j; /* smaller */
w = v + k;
{ int i2; for (i2 = common; i2 < w->s_size; i2++) { if (c + common == l) { diff = -1; break; }
diff = q[common] - w->s[i2]; if (diff != 0) break;
common++;
}
} if (diff < 0) {
j = k;
common_j = common;
} else {
i = k;
common_i = common;
} if (j - i <= 1) { if (i > 0) break; /* v->s has been inspected */ if (j == i) break; /* only one item in v */
/* - but now we need to go round once more to get v->sinspected.Thislooksmessy,butisactually
the optimal approach. */
if (first_key_inspected) break;
first_key_inspected = 1;
}
} while (1) {
w = v + i; if (common_i >= w->s_size) {
z->c = c + w->s_size; if (w->function == NULL) return w->result;
{ int res = w->function(z);
z->c = c + w->s_size; if (res) return w->result;
}
}
i = w->substring_i; if (i < 0) return0;
}
}
/* find_among_b is for backwards processing. Same comments apply */
externint find_among_b(struct SN_env * z, conststruct among * v, int v_size) {
int i = 0; int j = v_size;
int c = z->c; int lb = z->lb; const symbol * q = z->p + c - 1;
conststruct among * w;
int common_i = 0; int common_j = 0;
int first_key_inspected = 0;
while (1) { int k = i + ((j - i) >> 1); int diff = 0; int common = common_i < common_j ? common_i : common_j;
w = v + k;
{ int i2; for (i2 = w->s_size - 1 - common; i2 >= 0; i2--) { if (c - common == lb) { diff = -1; break; }
diff = q[- common] - w->s[i2]; if (diff != 0) break;
common++;
}
} if (diff < 0) { j = k; common_j = common; } else { i = k; common_i = common; } if (j - i <= 1) { if (i > 0) break; if (j == i) break; if (first_key_inspected) break;
first_key_inspected = 1;
}
} while (1) {
w = v + i; if (common_i >= w->s_size) {
z->c = c - w->s_size; if (w->function == NULL) return w->result;
{ int res = w->function(z);
z->c = c - w->s_size; if (res) return w->result;
}
}
i = w->substring_i; if (i < 0) return0;
}
}
/* Increase the size of the buffer pointed to by p to at least n symbols. *Ifinsufficientmemory,returnsNULLandfreestheoldbuffer.
*/ static symbol * increase_size(symbol * p, int n) {
symbol * q; int new_size = n + 20; void * mem = realloc((char *) p - HEAD,
HEAD + (new_size + 1) * sizeof(symbol)); if (mem == NULL) {
lose_s(p); return NULL;
}
q = (symbol *) (HEAD + (char *)mem);
CAPACITY(q) = new_size; return q;
}
/* to replace symbols between c_bra and c_ket in z->p by the s_sizesymbolsats. Returns0onsuccess,-1onerror. Also,freesz->p(andsetsittoNULL)onerror.
*/ externint replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s, int * adjptr)
{ int adjustment; int len; if (z->p == NULL) {
z->p = create_s(); if (z->p == NULL) return -1;
}
adjustment = s_size - (c_ket - c_bra);
len = SIZE(z->p); if (adjustment != 0) { if (adjustment + len > CAPACITY(z->p)) {
z->p = increase_size(z->p, adjustment + len); if (z->p == NULL) return -1;
}
memmove(z->p + c_ket + adjustment,
z->p + c_ket,
(len - c_ket) * sizeof(symbol));
SET_SIZE(z->p, adjustment + len);
z->l += adjustment; if (z->c >= c_ket)
z->c += adjustment; elseif (z->c > c_bra)
z->c = c_bra;
} if (s_size) memmove(z->p + c_bra, s, s_size * sizeof(symbol)); if (adjptr != NULL)
*adjptr = adjustment; return0;
}
staticint slice_check(struct SN_env * z) {
if (z->bra < 0 ||
z->bra > z->ket ||
z->ket > z->l ||
z->p == NULL ||
z->l > SIZE(z->p)) /* this line could be removed */
{ #if0
fprintf(stderr, "faulty slice operation:\n");
debug(z, -1, 0); #endif return -1;
} return0;
}
externint slice_from_s(struct SN_env * z, int s_size, const symbol * s) { if (slice_check(z)) return -1; return replace_s(z, z->bra, z->ket, s_size, s, NULL);
}
externint insert_s(struct SN_env * z, int bra, int ket, int s_size, const symbol * s) { int adjustment; if (replace_s(z, bra, ket, s_size, s, &adjustment)) return -1; if (bra <= z->bra) z->bra += adjustment; if (bra <= z->ket) z->ket += adjustment; return0;
}
externint insert_v(struct SN_env * z, int bra, int ket, const symbol * p) { return insert_s(z, bra, ket, SIZE(p), p);
}
extern symbol * slice_to(struct SN_env * z, symbol * p) { if (slice_check(z)) {
lose_s(p); return NULL;
}
{ int len = z->ket - z->bra; if (CAPACITY(p) < len) {
p = increase_size(p, len); if (p == NULL) return NULL;
}
memmove(p, z->p + z->bra, len * sizeof(symbol));
SET_SIZE(p, len);
} return p;
}
extern symbol * assign_to(struct SN_env * z, symbol * p) { int len = z->l; if (CAPACITY(p) < len) {
p = increase_size(p, len); if (p == NULL) return NULL;
}
memmove(p, z->p, len * sizeof(symbol));
SET_SIZE(p, len); return p;
}
externint len_utf8(const symbol * p) { int size = SIZE(p); int len = 0; while (size--) {
symbol b = *p++; if (b >= 0xC0 || b < 0x80) ++len;
} return len;
}
#if0 externvoid debug(struct SN_env * z, int number, int line_count) { int i; int limit = SIZE(z->p); /*if (number >= 0) printf("%3d (line %4d): '", number, line_count);*/ if (number >= 0) printf("%3d (line %4d): [%d]'", number, line_count,limit); for (i = 0; i <= limit; i++) { if (z->lb == i) printf("{"); if (z->bra == i) printf("["); if (z->c == i) printf("|"); if (z->ket == i) printf("]"); if (z->l == i) printf("}"); if (i < limit)
{ int ch = z->p[i]; if (ch == 0) ch = '#';
printf("%c", ch);
}
}
printf("'\n");
} #endif
Messung V0.5 in Prozent
[Verzeichnis aufwärts0.20unsichere VerbindungÜbersetzung europäischer Sprachen durch Browser2026-08-08]