Header And Logo

PostgreSQL
| The world's most advanced open source database.

utilities.c

Go to the documentation of this file.
00001 #include "header.h"
00002 
00003 #define unless(C) if(!(C))
00004 
00005 #define CREATE_SIZE 1
00006 
00007 extern symbol * create_s(void) {
00008     symbol * p;
00009     void * mem = malloc(HEAD + (CREATE_SIZE + 1) * sizeof(symbol));
00010     if (mem == NULL) return NULL;
00011     p = (symbol *) (HEAD + (char *) mem);
00012     CAPACITY(p) = CREATE_SIZE;
00013     SET_SIZE(p, CREATE_SIZE);
00014     return p;
00015 }
00016 
00017 extern void lose_s(symbol * p) {
00018     if (p == NULL) return;
00019     free((char *) p - HEAD);
00020 }
00021 
00022 /*
00023    new_p = skip_utf8(p, c, lb, l, n); skips n characters forwards from p + c
00024    if n +ve, or n characters backwards from p + c - 1 if n -ve. new_p is the new
00025    position, or 0 on failure.
00026 
00027    -- used to implement hop and next in the utf8 case.
00028 */
00029 
00030 extern int skip_utf8(const symbol * p, int c, int lb, int l, int n) {
00031     int b;
00032     if (n >= 0) {
00033         for (; n > 0; n--) {
00034             if (c >= l) return -1;
00035             b = p[c++];
00036             if (b >= 0xC0) {   /* 1100 0000 */
00037                 while (c < l) {
00038                     b = p[c];
00039                     if (b >= 0xC0 || b < 0x80) break;
00040                     /* break unless b is 10------ */
00041                     c++;
00042                 }
00043             }
00044         }
00045     } else {
00046         for (; n < 0; n++) {
00047             if (c <= lb) return -1;
00048             b = p[--c];
00049             if (b >= 0x80) {   /* 1000 0000 */
00050                 while (c > lb) {
00051                     b = p[c];
00052                     if (b >= 0xC0) break; /* 1100 0000 */
00053                     c--;
00054                 }
00055             }
00056         }
00057     }
00058     return c;
00059 }
00060 
00061 /* Code for character groupings: utf8 cases */
00062 
00063 static int get_utf8(const symbol * p, int c, int l, int * slot) {
00064     int b0, b1;
00065     if (c >= l) return 0;
00066     b0 = p[c++];
00067     if (b0 < 0xC0 || c == l) {   /* 1100 0000 */
00068         * slot = b0; return 1;
00069     }
00070     b1 = p[c++];
00071     if (b0 < 0xE0 || c == l) {   /* 1110 0000 */
00072         * slot = (b0 & 0x1F) << 6 | (b1 & 0x3F); return 2;
00073     }
00074     * slot = (b0 & 0xF) << 12 | (b1 & 0x3F) << 6 | (p[c] & 0x3F); return 3;
00075 }
00076 
00077 static int get_b_utf8(const symbol * p, int c, int lb, int * slot) {
00078     int b0, b1;
00079     if (c <= lb) return 0;
00080     b0 = p[--c];
00081     if (b0 < 0x80 || c == lb) {   /* 1000 0000 */
00082         * slot = b0; return 1;
00083     }
00084     b1 = p[--c];
00085     if (b1 >= 0xC0 || c == lb) {   /* 1100 0000 */
00086         * slot = (b1 & 0x1F) << 6 | (b0 & 0x3F); return 2;
00087     }
00088     * slot = (p[c] & 0xF) << 12 | (b1 & 0x3F) << 6 | (b0 & 0x3F); return 3;
00089 }
00090 
00091 extern int in_grouping_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
00092     do {
00093     int ch;
00094     int w = get_utf8(z->p, z->c, z->l, & ch);
00095     unless (w) return -1;
00096     if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
00097         return w;
00098     z->c += w;
00099     } while (repeat);
00100     return 0;
00101 }
00102 
00103 extern int in_grouping_b_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
00104     do {
00105     int ch;
00106     int w = get_b_utf8(z->p, z->c, z->lb, & ch);
00107     unless (w) return -1;
00108     if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
00109         return w;
00110     z->c -= w;
00111     } while (repeat);
00112     return 0;
00113 }
00114 
00115 extern int out_grouping_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
00116     do {
00117     int ch;
00118     int w = get_utf8(z->p, z->c, z->l, & ch);
00119     unless (w) return -1;
00120     unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
00121         return w;
00122     z->c += w;
00123     } while (repeat);
00124     return 0;
00125 }
00126 
00127 extern int out_grouping_b_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
00128     do {
00129     int ch;
00130     int w = get_b_utf8(z->p, z->c, z->lb, & ch);
00131     unless (w) return -1;
00132     unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
00133         return w;
00134     z->c -= w;
00135     } while (repeat);
00136     return 0;
00137 }
00138 
00139 /* Code for character groupings: non-utf8 cases */
00140 
00141 extern int in_grouping(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
00142     do {
00143     int ch;
00144     if (z->c >= z->l) return -1;
00145     ch = z->p[z->c];
00146     if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
00147         return 1;
00148     z->c++;
00149     } while (repeat);
00150     return 0;
00151 }
00152 
00153 extern int in_grouping_b(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
00154     do {
00155     int ch;
00156     if (z->c <= z->lb) return -1;
00157     ch = z->p[z->c - 1];
00158     if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
00159         return 1;
00160     z->c--;
00161     } while (repeat);
00162     return 0;
00163 }
00164 
00165 extern int out_grouping(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
00166     do {
00167     int ch;
00168     if (z->c >= z->l) return -1;
00169     ch = z->p[z->c];
00170     unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
00171         return 1;
00172     z->c++;
00173     } while (repeat);
00174     return 0;
00175 }
00176 
00177 extern int out_grouping_b(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
00178     do {
00179     int ch;
00180     if (z->c <= z->lb) return -1;
00181     ch = z->p[z->c - 1];
00182     unless (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
00183         return 1;
00184     z->c--;
00185     } while (repeat);
00186     return 0;
00187 }
00188 
00189 extern int eq_s(struct SN_env * z, int s_size, const symbol * s) {
00190     if (z->l - z->c < s_size || memcmp(z->p + z->c, s, s_size * sizeof(symbol)) != 0) return 0;
00191     z->c += s_size; return 1;
00192 }
00193 
00194 extern int eq_s_b(struct SN_env * z, int s_size, const symbol * s) {
00195     if (z->c - z->lb < s_size || memcmp(z->p + z->c - s_size, s, s_size * sizeof(symbol)) != 0) return 0;
00196     z->c -= s_size; return 1;
00197 }
00198 
00199 extern int eq_v(struct SN_env * z, const symbol * p) {
00200     return eq_s(z, SIZE(p), p);
00201 }
00202 
00203 extern int eq_v_b(struct SN_env * z, const symbol * p) {
00204     return eq_s_b(z, SIZE(p), p);
00205 }
00206 
00207 extern int find_among(struct SN_env * z, const struct among * v, int v_size) {
00208 
00209     int i = 0;
00210     int j = v_size;
00211 
00212     int c = z->c; int l = z->l;
00213     symbol * q = z->p + c;
00214 
00215     const struct among * w;
00216 
00217     int common_i = 0;
00218     int common_j = 0;
00219 
00220     int first_key_inspected = 0;
00221 
00222     while(1) {
00223         int k = i + ((j - i) >> 1);
00224         int diff = 0;
00225         int common = common_i < common_j ? common_i : common_j; /* smaller */
00226         w = v + k;
00227         {
00228             int i2; for (i2 = common; i2 < w->s_size; i2++) {
00229                 if (c + common == l) { diff = -1; break; }
00230                 diff = q[common] - w->s[i2];
00231                 if (diff != 0) break;
00232                 common++;
00233             }
00234         }
00235         if (diff < 0) { j = k; common_j = common; }
00236                  else { i = k; common_i = common; }
00237         if (j - i <= 1) {
00238             if (i > 0) break; /* v->s has been inspected */
00239             if (j == i) break; /* only one item in v */
00240 
00241             /* - but now we need to go round once more to get
00242                v->s inspected. This looks messy, but is actually
00243                the optimal approach.  */
00244 
00245             if (first_key_inspected) break;
00246             first_key_inspected = 1;
00247         }
00248     }
00249     while(1) {
00250         w = v + i;
00251         if (common_i >= w->s_size) {
00252             z->c = c + w->s_size;
00253             if (w->function == 0) return w->result;
00254             {
00255                 int res = w->function(z);
00256                 z->c = c + w->s_size;
00257                 if (res) return w->result;
00258             }
00259         }
00260         i = w->substring_i;
00261         if (i < 0) return 0;
00262     }
00263 }
00264 
00265 /* find_among_b is for backwards processing. Same comments apply */
00266 
00267 extern int find_among_b(struct SN_env * z, const struct among * v, int v_size) {
00268 
00269     int i = 0;
00270     int j = v_size;
00271 
00272     int c = z->c; int lb = z->lb;
00273     symbol * q = z->p + c - 1;
00274 
00275     const struct among * w;
00276 
00277     int common_i = 0;
00278     int common_j = 0;
00279 
00280     int first_key_inspected = 0;
00281 
00282     while(1) {
00283         int k = i + ((j - i) >> 1);
00284         int diff = 0;
00285         int common = common_i < common_j ? common_i : common_j;
00286         w = v + k;
00287         {
00288             int i2; for (i2 = w->s_size - 1 - common; i2 >= 0; i2--) {
00289                 if (c - common == lb) { diff = -1; break; }
00290                 diff = q[- common] - w->s[i2];
00291                 if (diff != 0) break;
00292                 common++;
00293             }
00294         }
00295         if (diff < 0) { j = k; common_j = common; }
00296                  else { i = k; common_i = common; }
00297         if (j - i <= 1) {
00298             if (i > 0) break;
00299             if (j == i) break;
00300             if (first_key_inspected) break;
00301             first_key_inspected = 1;
00302         }
00303     }
00304     while(1) {
00305         w = v + i;
00306         if (common_i >= w->s_size) {
00307             z->c = c - w->s_size;
00308             if (w->function == 0) return w->result;
00309             {
00310                 int res = w->function(z);
00311                 z->c = c - w->s_size;
00312                 if (res) return w->result;
00313             }
00314         }
00315         i = w->substring_i;
00316         if (i < 0) return 0;
00317     }
00318 }
00319 
00320 
00321 /* Increase the size of the buffer pointed to by p to at least n symbols.
00322  * If insufficient memory, returns NULL and frees the old buffer.
00323  */
00324 static symbol * increase_size(symbol * p, int n) {
00325     symbol * q;
00326     int new_size = n + 20;
00327     void * mem = realloc((char *) p - HEAD,
00328                          HEAD + (new_size + 1) * sizeof(symbol));
00329     if (mem == NULL) {
00330         lose_s(p);
00331         return NULL;
00332     }
00333     q = (symbol *) (HEAD + (char *)mem);
00334     CAPACITY(q) = new_size;
00335     return q;
00336 }
00337 
00338 /* to replace symbols between c_bra and c_ket in z->p by the
00339    s_size symbols at s.
00340    Returns 0 on success, -1 on error.
00341    Also, frees z->p (and sets it to NULL) on error.
00342 */
00343 extern int replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s, int * adjptr)
00344 {
00345     int adjustment;
00346     int len;
00347     if (z->p == NULL) {
00348         z->p = create_s();
00349         if (z->p == NULL) return -1;
00350     }
00351     adjustment = s_size - (c_ket - c_bra);
00352     len = SIZE(z->p);
00353     if (adjustment != 0) {
00354         if (adjustment + len > CAPACITY(z->p)) {
00355             z->p = increase_size(z->p, adjustment + len);
00356             if (z->p == NULL) return -1;
00357         }
00358         memmove(z->p + c_ket + adjustment,
00359                 z->p + c_ket,
00360                 (len - c_ket) * sizeof(symbol));
00361         SET_SIZE(z->p, adjustment + len);
00362         z->l += adjustment;
00363         if (z->c >= c_ket)
00364             z->c += adjustment;
00365         else
00366             if (z->c > c_bra)
00367                 z->c = c_bra;
00368     }
00369     unless (s_size == 0) memmove(z->p + c_bra, s, s_size * sizeof(symbol));
00370     if (adjptr != NULL)
00371         *adjptr = adjustment;
00372     return 0;
00373 }
00374 
00375 static int slice_check(struct SN_env * z) {
00376 
00377     if (z->bra < 0 ||
00378         z->bra > z->ket ||
00379         z->ket > z->l ||
00380         z->p == NULL ||
00381         z->l > SIZE(z->p)) /* this line could be removed */
00382     {
00383 #if 0
00384         fprintf(stderr, "faulty slice operation:\n");
00385         debug(z, -1, 0);
00386 #endif
00387         return -1;
00388     }
00389     return 0;
00390 }
00391 
00392 extern int slice_from_s(struct SN_env * z, int s_size, const symbol * s) {
00393     if (slice_check(z)) return -1;
00394     return replace_s(z, z->bra, z->ket, s_size, s, NULL);
00395 }
00396 
00397 extern int slice_from_v(struct SN_env * z, const symbol * p) {
00398     return slice_from_s(z, SIZE(p), p);
00399 }
00400 
00401 extern int slice_del(struct SN_env * z) {
00402     return slice_from_s(z, 0, 0);
00403 }
00404 
00405 extern int insert_s(struct SN_env * z, int bra, int ket, int s_size, const symbol * s) {
00406     int adjustment;
00407     if (replace_s(z, bra, ket, s_size, s, &adjustment))
00408         return -1;
00409     if (bra <= z->bra) z->bra += adjustment;
00410     if (bra <= z->ket) z->ket += adjustment;
00411     return 0;
00412 }
00413 
00414 extern int insert_v(struct SN_env * z, int bra, int ket, const symbol * p) {
00415     int adjustment;
00416     if (replace_s(z, bra, ket, SIZE(p), p, &adjustment))
00417         return -1;
00418     if (bra <= z->bra) z->bra += adjustment;
00419     if (bra <= z->ket) z->ket += adjustment;
00420     return 0;
00421 }
00422 
00423 extern symbol * slice_to(struct SN_env * z, symbol * p) {
00424     if (slice_check(z)) {
00425         lose_s(p);
00426         return NULL;
00427     }
00428     {
00429         int len = z->ket - z->bra;
00430         if (CAPACITY(p) < len) {
00431             p = increase_size(p, len);
00432             if (p == NULL)
00433                 return NULL;
00434         }
00435         memmove(p, z->p + z->bra, len * sizeof(symbol));
00436         SET_SIZE(p, len);
00437     }
00438     return p;
00439 }
00440 
00441 extern symbol * assign_to(struct SN_env * z, symbol * p) {
00442     int len = z->l;
00443     if (CAPACITY(p) < len) {
00444         p = increase_size(p, len);
00445         if (p == NULL)
00446             return NULL;
00447     }
00448     memmove(p, z->p, len * sizeof(symbol));
00449     SET_SIZE(p, len);
00450     return p;
00451 }
00452 
00453 #if 0
00454 extern void debug(struct SN_env * z, int number, int line_count) {
00455     int i;
00456     int limit = SIZE(z->p);
00457     /*if (number >= 0) printf("%3d (line %4d): '", number, line_count);*/
00458     if (number >= 0) printf("%3d (line %4d): [%d]'", number, line_count,limit);
00459     for (i = 0; i <= limit; i++) {
00460         if (z->lb == i) printf("{");
00461         if (z->bra == i) printf("[");
00462         if (z->c == i) printf("|");
00463         if (z->ket == i) printf("]");
00464         if (z->l == i) printf("}");
00465         if (i < limit)
00466         {   int ch = z->p[i];
00467             if (ch == 0) ch = '#';
00468             printf("%c", ch);
00469         }
00470     }
00471     printf("'\n");
00472 }
00473 #endif