LCOV - differential code coverage report
Current view: top level - src/backend/snowball/libstemmer - utilities.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 59.7 % 300 179 33 88 2 41 136 37 31
Current Date: 2026-03-14 14:10:32 -0400 Functions: 54.8 % 31 17 5 9 1 8 8 5 7
Baseline: lcov-20260315-024220-baseline Branches: 46.0 % 250 115 32 1 102 3 14 98 32 12
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 55.4 % 74 41 33 41
(360..) days: 61.1 % 226 138 88 2 136
Function coverage date bins:
(30,360] days: 58.3 % 12 7 5 7
(360..) days: 52.6 % 19 10 9 1 1 8
Branch coverage date bins:
(30,360] days: 30.4 % 46 14 32 14
(360..) days: 49.5 % 204 101 1 102 3 98

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : #include "snowball_runtime.h"
                                  2                 :                : 
                                  3                 :                : #ifdef SNOWBALL_RUNTIME_THROW_EXCEPTIONS
                                  4                 :                : # include <new>
                                  5                 :                : # include <stdexcept>
                                  6                 :                : # define SNOWBALL_RETURN_OK return
                                  7                 :                : # define SNOWBALL_RETURN_OR_THROW(R, E) throw E
                                  8                 :                : # define SNOWBALL_PROPAGATE_ERR(F) F
                                  9                 :                : #else
                                 10                 :                : # define SNOWBALL_RETURN_OK return 0
                                 11                 :                : # define SNOWBALL_RETURN_OR_THROW(R, E) return R
                                 12                 :                : # define SNOWBALL_PROPAGATE_ERR(F) do { \
                                 13                 :                :         int snowball_err = F; \
                                 14                 :                :         if (snowball_err < 0) return snowball_err; \
                                 15                 :                :     } while (0)
                                 16                 :                : #endif
                                 17                 :                : 
                                 18                 :                : #define CREATE_SIZE 1
                                 19                 :                : 
 6781 tgl@sss.pgh.pa.us          20                 :CBC          22 : extern symbol * create_s(void) {
                                 21                 :                :     symbol * p;
                                 22                 :             22 :     void * mem = malloc(HEAD + (CREATE_SIZE + 1) * sizeof(symbol));
   69 tgl@sss.pgh.pa.us          23         [ -  + ]:GNC          22 :     if (mem == NULL)
   69 tgl@sss.pgh.pa.us          24                 :UNC           0 :         SNOWBALL_RETURN_OR_THROW(NULL, std::bad_alloc());
 6781 tgl@sss.pgh.pa.us          25                 :CBC          22 :     p = (symbol *) (HEAD + (char *) mem);
                                 26                 :             22 :     CAPACITY(p) = CREATE_SIZE;
 2729                            27                 :             22 :     SET_SIZE(p, 0);
 6781                            28                 :             22 :     return p;
                                 29                 :                : }
                                 30                 :                : 
 6781 tgl@sss.pgh.pa.us          31                 :UBC           0 : extern void lose_s(symbol * p) {
                                 32         [ #  # ]:              0 :     if (p == NULL) return;
                                 33                 :              0 :     free((char *) p - HEAD);
                                 34                 :                : }
                                 35                 :                : 
                                 36                 :                : /*
                                 37                 :                :    new_p = skip_utf8(p, c, l, n); skips n characters forwards from p + c.
                                 38                 :                :    new_p is the new position, or -1 on failure.
                                 39                 :                : 
                                 40                 :                :    -- used to implement hop and next in the utf8 case.
                                 41                 :                : */
                                 42                 :                : 
 1850 peter@eisentraut.org       43                 :CBC       22299 : extern int skip_utf8(const symbol * p, int c, int limit, int n) {
                                 44                 :                :     int b;
                                 45         [ -  + ]:          22299 :     if (n < 0) return -1;
                                 46         [ +  + ]:          47609 :     for (; n > 0; n--) {
                                 47         [ +  + ]:          28981 :         if (c >= limit) return -1;
                                 48                 :          25310 :         b = p[c++];
                                 49         [ -  + ]:          25310 :         if (b >= 0xC0) {   /* 1100 0000 */
 1850 peter@eisentraut.org       50         [ #  # ]:UBC           0 :             while (c < limit) {
                                 51                 :              0 :                 b = p[c];
                                 52   [ #  #  #  # ]:              0 :                 if (b >= 0xC0 || b < 0x80) break;
                                 53                 :                :                 /* break unless b is 10------ */
                                 54                 :              0 :                 c++;
                                 55                 :                :             }
                                 56                 :                :         }
                                 57                 :                :     }
 1850 peter@eisentraut.org       58                 :CBC       18628 :     return c;
                                 59                 :                : }
                                 60                 :                : 
                                 61                 :                : /*
                                 62                 :                :    new_p = skip_b_utf8(p, c, lb, n); skips n characters backwards from p + c - 1
                                 63                 :                :    new_p is the new position, or -1 on failure.
                                 64                 :                : 
                                 65                 :                :    -- used to implement hop and next in the utf8 case.
                                 66                 :                : */
                                 67                 :                : 
                                 68                 :            140 : extern int skip_b_utf8(const symbol * p, int c, int limit, int n) {
                                 69                 :                :     int b;
                                 70         [ -  + ]:            140 :     if (n < 0) return -1;
                                 71         [ +  + ]:            280 :     for (; n > 0; n--) {
                                 72         [ -  + ]:            140 :         if (c <= limit) return -1;
                                 73                 :            140 :         b = p[--c];
                                 74         [ -  + ]:            140 :         if (b >= 0x80) {   /* 1000 0000 */
 1850 peter@eisentraut.org       75         [ #  # ]:UBC           0 :             while (c > limit) {
                                 76                 :              0 :                 b = p[c];
                                 77         [ #  # ]:              0 :                 if (b >= 0xC0) break; /* 1100 0000 */
                                 78                 :              0 :                 c--;
                                 79                 :                :             }
                                 80                 :                :         }
                                 81                 :                :     }
 6781 tgl@sss.pgh.pa.us          82                 :CBC         140 :     return c;
                                 83                 :                : }
                                 84                 :                : 
                                 85                 :                : /* Code for character groupings: utf8 cases */
                                 86                 :                : 
                                 87                 :          33880 : static int get_utf8(const symbol * p, int c, int l, int * slot) {
                                 88                 :                :     int b0, b1, b2;
                                 89         [ +  + ]:          33880 :     if (c >= l) return 0;
                                 90                 :          28672 :     b0 = p[c++];
                                 91   [ -  +  -  - ]:          28672 :     if (b0 < 0xC0 || c == l) {   /* 1100 0000 */
 2446 peter@eisentraut.org       92                 :          28672 :         *slot = b0;
                                 93                 :          28672 :         return 1;
                                 94                 :                :     }
 2446 peter@eisentraut.org       95                 :UBC           0 :     b1 = p[c++] & 0x3F;
 6781 tgl@sss.pgh.pa.us          96   [ #  #  #  # ]:              0 :     if (b0 < 0xE0 || c == l) {   /* 1110 0000 */
 2446 peter@eisentraut.org       97                 :              0 :         *slot = (b0 & 0x1F) << 6 | b1;
                                 98                 :              0 :         return 2;
                                 99                 :                :     }
                                100                 :              0 :     b2 = p[c++] & 0x3F;
                                101   [ #  #  #  # ]:              0 :     if (b0 < 0xF0 || c == l) {   /* 1111 0000 */
                                102                 :              0 :         *slot = (b0 & 0xF) << 12 | b1 << 6 | b2;
                                103                 :              0 :         return 3;
                                104                 :                :     }
 1850                           105                 :              0 :     *slot = (b0 & 0x7) << 18 | b1 << 12 | b2 << 6 | (p[c] & 0x3F);
 2446                           106                 :              0 :     return 4;
                                107                 :                : }
                                108                 :                : 
 6781 tgl@sss.pgh.pa.us         109                 :CBC        2154 : static int get_b_utf8(const symbol * p, int c, int lb, int * slot) {
                                110                 :                :     int a, b;
                                111         [ +  + ]:           2154 :     if (c <= lb) return 0;
 2446 peter@eisentraut.org      112                 :           2091 :     b = p[--c];
                                113   [ -  +  -  - ]:           2091 :     if (b < 0x80 || c == lb) {   /* 1000 0000 */
                                114                 :           2091 :         *slot = b;
                                115                 :           2091 :         return 1;
                                116                 :                :     }
 2446 peter@eisentraut.org      117                 :UBC           0 :     a = b & 0x3F;
                                118                 :              0 :     b = p[--c];
                                119   [ #  #  #  # ]:              0 :     if (b >= 0xC0 || c == lb) {   /* 1100 0000 */
                                120                 :              0 :         *slot = (b & 0x1F) << 6 | a;
                                121                 :              0 :         return 2;
                                122                 :                :     }
                                123                 :              0 :     a |= (b & 0x3F) << 6;
                                124                 :              0 :     b = p[--c];
                                125   [ #  #  #  # ]:              0 :     if (b >= 0xE0 || c == lb) {   /* 1110 0000 */
                                126                 :              0 :         *slot = (b & 0xF) << 12 | a;
                                127                 :              0 :         return 3;
                                128                 :                :     }
 1850                           129                 :              0 :     *slot = (p[--c] & 0x7) << 18 | (b & 0x3F) << 12 | a;
 2446                           130                 :              0 :     return 4;
                                131                 :                : }
                                132                 :                : 
 6781 tgl@sss.pgh.pa.us         133                 :CBC       22252 : extern int in_grouping_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
                                134                 :                :     do {
                                135                 :                :         int ch;
 2106 peter@eisentraut.org      136                 :          23092 :         int w = get_utf8(z->p, z->c, z->l, & ch);
                                137         [ +  + ]:          35852 :         if (!w) return -1;
                                138   [ +  +  +  +  :          19360 :         if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
                                              +  + ]
                                139                 :          12760 :             return w;
                                140                 :           6600 :         z->c += w;
 6781 tgl@sss.pgh.pa.us         141         [ +  + ]:           6600 :     } while (repeat);
                                142                 :           5760 :     return 0;
                                143                 :                : }
                                144                 :                : 
                                145                 :            433 : extern int in_grouping_b_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
                                146                 :                :     do {
                                147                 :                :         int ch;
 2106 peter@eisentraut.org      148                 :            433 :         int w = get_b_utf8(z->p, z->c, z->lb, & ch);
                                149         [ -  + ]:            655 :         if (!w) return -1;
                                150   [ +  -  +  -  :            433 :         if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
                                              +  + ]
                                151                 :            222 :             return w;
                                152                 :            211 :         z->c -= w;
 6781 tgl@sss.pgh.pa.us         153         [ -  + ]:            211 :     } while (repeat);
                                154                 :            211 :     return 0;
                                155                 :                : }
                                156                 :                : 
                                157                 :           5670 : extern int out_grouping_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
                                158                 :                :     do {
                                159                 :                :         int ch;
 2106 peter@eisentraut.org      160                 :          10788 :         int w = get_utf8(z->p, z->c, z->l, & ch);
                                161         [ +  + ]:          14982 :         if (!w) return -1;
                                162   [ +  -  +  +  :           9312 :         if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
                                              +  + ]
                                163                 :           4194 :             return w;
                                164                 :           5118 :         z->c += w;
 6781 tgl@sss.pgh.pa.us         165         [ +  - ]:           5118 :     } while (repeat);
 6781 tgl@sss.pgh.pa.us         166                 :UBC           0 :     return 0;
                                167                 :                : }
                                168                 :                : 
 6781 tgl@sss.pgh.pa.us         169                 :CBC        1273 : extern int out_grouping_b_U(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
                                170                 :                :     do {
                                171                 :                :         int ch;
 2106 peter@eisentraut.org      172                 :           1721 :         int w = get_b_utf8(z->p, z->c, z->lb, & ch);
                                173         [ +  + ]:           2306 :         if (!w) return -1;
                                174   [ +  -  +  +  :           1658 :         if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
                                              +  + ]
                                175                 :            585 :             return w;
                                176                 :           1073 :         z->c -= w;
 6781 tgl@sss.pgh.pa.us         177         [ +  + ]:           1073 :     } while (repeat);
                                178                 :            625 :     return 0;
                                179                 :                : }
                                180                 :                : 
                                181                 :                : /* Code for character groupings: non-utf8 cases */
                                182                 :                : 
 6781 tgl@sss.pgh.pa.us         183                 :UBC           0 : extern int in_grouping(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
                                184                 :                :     do {
                                185                 :                :         int ch;
 2106 peter@eisentraut.org      186         [ #  # ]:              0 :         if (z->c >= z->l) return -1;
                                187                 :              0 :         ch = z->p[z->c];
                                188   [ #  #  #  #  :              0 :         if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
                                              #  # ]
                                189                 :              0 :             return 1;
                                190                 :              0 :         z->c++;
 6781 tgl@sss.pgh.pa.us         191         [ #  # ]:              0 :     } while (repeat);
                                192                 :              0 :     return 0;
                                193                 :                : }
                                194                 :                : 
                                195                 :              0 : extern int in_grouping_b(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
                                196                 :                :     do {
                                197                 :                :         int ch;
 2106 peter@eisentraut.org      198         [ #  # ]:              0 :         if (z->c <= z->lb) return -1;
                                199                 :              0 :         ch = z->p[z->c - 1];
                                200   [ #  #  #  #  :              0 :         if (ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
                                              #  # ]
                                201                 :              0 :             return 1;
                                202                 :              0 :         z->c--;
 6781 tgl@sss.pgh.pa.us         203         [ #  # ]:              0 :     } while (repeat);
                                204                 :              0 :     return 0;
                                205                 :                : }
                                206                 :                : 
                                207                 :              0 : extern int out_grouping(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
                                208                 :                :     do {
                                209                 :                :         int ch;
 2106 peter@eisentraut.org      210         [ #  # ]:              0 :         if (z->c >= z->l) return -1;
                                211                 :              0 :         ch = z->p[z->c];
                                212   [ #  #  #  #  :              0 :         if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
                                              #  # ]
                                213                 :              0 :             return 1;
                                214                 :              0 :         z->c++;
 6781 tgl@sss.pgh.pa.us         215         [ #  # ]:              0 :     } while (repeat);
                                216                 :              0 :     return 0;
                                217                 :                : }
                                218                 :                : 
                                219                 :              0 : extern int out_grouping_b(struct SN_env * z, const unsigned char * s, int min, int max, int repeat) {
                                220                 :                :     do {
                                221                 :                :         int ch;
 2106 peter@eisentraut.org      222         [ #  # ]:              0 :         if (z->c <= z->lb) return -1;
                                223                 :              0 :         ch = z->p[z->c - 1];
                                224   [ #  #  #  #  :              0 :         if (!(ch > max || (ch -= min) < 0 || (s[ch >> 3] & (0X1 << (ch & 0X7))) == 0))
                                              #  # ]
                                225                 :              0 :             return 1;
                                226                 :              0 :         z->c--;
 6781 tgl@sss.pgh.pa.us         227         [ #  # ]:              0 :     } while (repeat);
                                228                 :              0 :     return 0;
                                229                 :                : }
                                230                 :                : 
                                231                 :              0 : extern int eq_s(struct SN_env * z, int s_size, const symbol * s) {
                                232   [ #  #  #  # ]:              0 :     if (z->l - z->c < s_size || memcmp(z->p + z->c, s, s_size * sizeof(symbol)) != 0) return 0;
                                233                 :              0 :     z->c += s_size; return 1;
                                234                 :                : }
                                235                 :                : 
 6781 tgl@sss.pgh.pa.us         236                 :GBC         131 : extern int eq_s_b(struct SN_env * z, int s_size, const symbol * s) {
                                237   [ +  +  +  - ]:            131 :     if (z->c - z->lb < s_size || memcmp(z->p + z->c - s_size, s, s_size * sizeof(symbol)) != 0) return 0;
 6781 tgl@sss.pgh.pa.us         238                 :UBC           0 :     z->c -= s_size; return 1;
                                239                 :                : }
                                240                 :                : 
                                241                 :              0 : extern int eq_v(struct SN_env * z, const symbol * p) {
                                242                 :              0 :     return eq_s(z, SIZE(p), p);
                                243                 :                : }
                                244                 :                : 
                                245                 :              0 : extern int eq_v_b(struct SN_env * z, const symbol * p) {
                                246                 :              0 :     return eq_s_b(z, SIZE(p), p);
                                247                 :                : }
                                248                 :                : 
   69 tgl@sss.pgh.pa.us         249                 :GNC        2517 : extern int find_among(struct SN_env * z, const struct among * v, int v_size,
                                250                 :                :                       int (*call_among_func)(struct SN_env*)) {
                                251                 :                : 
 6781 tgl@sss.pgh.pa.us         252                 :CBC        2517 :     int i = 0;
                                253                 :           2517 :     int j = v_size;
                                254                 :                : 
                                255                 :           2517 :     int c = z->c; int l = z->l;
 1850 peter@eisentraut.org      256                 :           2517 :     const symbol * q = z->p + c;
                                257                 :                : 
                                258                 :                :     const struct among * w;
                                259                 :                : 
 6781 tgl@sss.pgh.pa.us         260                 :           2517 :     int common_i = 0;
                                261                 :           2517 :     int common_j = 0;
                                262                 :                : 
                                263                 :           2517 :     int first_key_inspected = 0;
                                264                 :                : 
 2106 peter@eisentraut.org      265                 :           7186 :     while (1) {
 6781 tgl@sss.pgh.pa.us         266                 :           9703 :         int k = i + ((j - i) >> 1);
                                267                 :           9703 :         int diff = 0;
                                268                 :           9703 :         int common = common_i < common_j ? common_i : common_j; /* smaller */
                                269                 :           9703 :         w = v + k;
                                270                 :                :         {
                                271         [ +  + ]:          11759 :             int i2; for (i2 = common; i2 < w->s_size; i2++) {
                                272         [ +  + ]:          11741 :                 if (c + common == l) { diff = -1; break; }
                                273                 :          11705 :                 diff = q[common] - w->s[i2];
                                274         [ +  + ]:          11705 :                 if (diff != 0) break;
                                275                 :           2056 :                 common++;
                                276                 :                :             }
                                277                 :                :         }
 2446 peter@eisentraut.org      278         [ +  + ]:           9703 :         if (diff < 0) {
                                279                 :           4756 :             j = k;
                                280                 :           4756 :             common_j = common;
                                281                 :                :         } else {
                                282                 :           4947 :             i = k;
                                283                 :           4947 :             common_i = common;
                                284                 :                :         }
 6781 tgl@sss.pgh.pa.us         285         [ +  + ]:           9703 :         if (j - i <= 1) {
                                286         [ +  + ]:           2820 :             if (i > 0) break; /* v->s has been inspected */
                                287         [ +  + ]:            606 :             if (j == i) break; /* only one item in v */
                                288                 :                : 
                                289                 :                :             /* - but now we need to go round once more to get
                                290                 :                :                v->s inspected. This looks messy, but is actually
                                291                 :                :                the optimal approach.  */
                                292                 :                : 
                                293         [ +  + ]:            519 :             if (first_key_inspected) break;
                                294                 :            303 :             first_key_inspected = 1;
                                295                 :                :         }
                                296                 :                :     }
   69 tgl@sss.pgh.pa.us         297                 :GNC        2517 :     w = v + i;
                                298                 :                :     while (1) {
 6781 tgl@sss.pgh.pa.us         299         [ +  + ]:CBC        2517 :         if (common_i >= w->s_size) {
                                300                 :             18 :             z->c = c + w->s_size;
   69 tgl@sss.pgh.pa.us         301         [ +  - ]:GNC          18 :             if (!w->function) return w->result;
   69 tgl@sss.pgh.pa.us         302                 :UNC           0 :             z->af = w->function;
                                303         [ #  # ]:              0 :             if (call_among_func(z)) {
 6781 tgl@sss.pgh.pa.us         304                 :UBC           0 :                 z->c = c + w->s_size;
   69 tgl@sss.pgh.pa.us         305                 :UNC           0 :                 return w->result;
                                306                 :                :             }
                                307                 :                :         }
   69 tgl@sss.pgh.pa.us         308         [ +  - ]:GNC        2499 :         if (!w->substring_i) return 0;
   69 tgl@sss.pgh.pa.us         309                 :UNC           0 :         w += w->substring_i;
                                310                 :                :     }
                                311                 :                : }
                                312                 :                : 
                                313                 :                : /* find_among_b is for backwards processing. Same comments apply */
                                314                 :                : 
   69 tgl@sss.pgh.pa.us         315                 :GNC        5427 : extern int find_among_b(struct SN_env * z, const struct among * v, int v_size,
                                316                 :                :                         int (*call_among_func)(struct SN_env*)) {
                                317                 :                : 
 6781 tgl@sss.pgh.pa.us         318                 :CBC        5427 :     int i = 0;
                                319                 :           5427 :     int j = v_size;
                                320                 :                : 
                                321                 :           5427 :     int c = z->c; int lb = z->lb;
 1850 peter@eisentraut.org      322                 :           5427 :     const symbol * q = z->p + c - 1;
                                323                 :                : 
                                324                 :                :     const struct among * w;
                                325                 :                : 
 6781 tgl@sss.pgh.pa.us         326                 :           5427 :     int common_i = 0;
                                327                 :           5427 :     int common_j = 0;
                                328                 :                : 
                                329                 :           5427 :     int first_key_inspected = 0;
                                330                 :                : 
 2106 peter@eisentraut.org      331                 :          14842 :     while (1) {
 6781 tgl@sss.pgh.pa.us         332                 :          20269 :         int k = i + ((j - i) >> 1);
                                333                 :          20269 :         int diff = 0;
                                334                 :          20269 :         int common = common_i < common_j ? common_i : common_j;
                                335                 :          20269 :         w = v + k;
                                336                 :                :         {
                                337         [ +  + ]:          31587 :             int i2; for (i2 = w->s_size - 1 - common; i2 >= 0; i2--) {
                                338         [ +  + ]:          30317 :                 if (c - common == lb) { diff = -1; break; }
                                339                 :          30299 :                 diff = q[- common] - w->s[i2];
                                340         [ +  + ]:          30299 :                 if (diff != 0) break;
                                341                 :          11318 :                 common++;
                                342                 :                :             }
                                343                 :                :         }
                                344         [ +  + ]:          20269 :         if (diff < 0) { j = k; common_j = common; }
                                345                 :          10713 :                  else { i = k; common_i = common; }
                                346         [ +  + ]:          20269 :         if (j - i <= 1) {
                                347         [ +  + ]:           6607 :             if (i > 0) break;
                                348         [ +  + ]:           2360 :             if (j == i) break;
                                349         [ +  + ]:           1901 :             if (first_key_inspected) break;
                                350                 :           1180 :             first_key_inspected = 1;
                                351                 :                :         }
                                352                 :                :     }
   69 tgl@sss.pgh.pa.us         353                 :GNC        5427 :     w = v + i;
                                354                 :                :     while (1) {
 6781 tgl@sss.pgh.pa.us         355         [ +  + ]:CBC        7282 :         if (common_i >= w->s_size) {
                                356                 :           1788 :             z->c = c - w->s_size;
   69 tgl@sss.pgh.pa.us         357         [ +  - ]:GNC        1788 :             if (!w->function) return w->result;
   69 tgl@sss.pgh.pa.us         358                 :UNC           0 :             z->af = w->function;
                                359         [ #  # ]:              0 :             if (call_among_func(z)) {
 6781 tgl@sss.pgh.pa.us         360                 :UBC           0 :                 z->c = c - w->s_size;
   69 tgl@sss.pgh.pa.us         361                 :UNC           0 :                 return w->result;
                                362                 :                :             }
                                363                 :                :         }
   69 tgl@sss.pgh.pa.us         364         [ +  + ]:GNC        5494 :         if (!w->substring_i) return 0;
                                365                 :           1855 :         w += w->substring_i;
                                366                 :                :     }
                                367                 :                : }
                                368                 :                : 
                                369                 :                : 
                                370                 :                : /* Increase the size of the buffer pointed to by p to at least n symbols.
                                371                 :                :  * On success, returns 0.  If insufficient memory, returns -1.
                                372                 :                :  */
                                373                 :             22 : static int increase_size(symbol ** p, int n) {
 6781 tgl@sss.pgh.pa.us         374                 :CBC          22 :     int new_size = n + 20;
   69 tgl@sss.pgh.pa.us         375                 :GNC          22 :     void * mem = realloc((char *) *p - HEAD,
                                376                 :                :                          HEAD + (new_size + 1) * sizeof(symbol));
                                377                 :                :     symbol * q;
                                378         [ -  + ]:             22 :     if (mem == NULL) return -1;
 6781 tgl@sss.pgh.pa.us         379                 :CBC          22 :     q = (symbol *) (HEAD + (char *)mem);
                                380                 :             22 :     CAPACITY(q) = new_size;
   69 tgl@sss.pgh.pa.us         381                 :GNC          22 :     *p = q;
                                382                 :             22 :     return 0;
                                383                 :                : }
                                384                 :                : 
                                385                 :                : /* to replace symbols between c_bra and c_ket in z->p by the
                                386                 :                :    s_size symbols at s.
                                387                 :                :    Returns 0 on success, -1 on error.
                                388                 :                : */
                                389                 :           4180 : extern SNOWBALL_ERR replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s)
                                390                 :                : {
                                391                 :           4180 :     int adjustment = s_size - (c_ket - c_bra);
 6781 tgl@sss.pgh.pa.us         392         [ +  + ]:CBC        4180 :     if (adjustment != 0) {
   69 tgl@sss.pgh.pa.us         393                 :GNC        1861 :         int len = SIZE(z->p);
 6781 tgl@sss.pgh.pa.us         394         [ +  + ]:CBC        1861 :         if (adjustment + len > CAPACITY(z->p)) {
   69 tgl@sss.pgh.pa.us         395         [ -  + ]:GNC          22 :             SNOWBALL_PROPAGATE_ERR(increase_size(&z->p, adjustment + len));
                                396                 :                :         }
 6781 tgl@sss.pgh.pa.us         397                 :CBC        1861 :         memmove(z->p + c_ket + adjustment,
                                398                 :           1861 :                 z->p + c_ket,
                                399                 :           1861 :                 (len - c_ket) * sizeof(symbol));
                                400                 :           1861 :         SET_SIZE(z->p, adjustment + len);
                                401                 :           1861 :         z->l += adjustment;
                                402         [ +  + ]:           1861 :         if (z->c >= c_ket)
                                403                 :             58 :             z->c += adjustment;
 2446 peter@eisentraut.org      404         [ -  + ]:           1803 :         else if (z->c > c_bra)
 2446 peter@eisentraut.org      405                 :UBC           0 :             z->c = c_bra;
                                406                 :                :     }
 2729 tgl@sss.pgh.pa.us         407         [ +  - ]:CBC        4180 :     if (s_size) memmove(z->p + c_bra, s, s_size * sizeof(symbol));
   69 tgl@sss.pgh.pa.us         408                 :GNC        4180 :     SNOWBALL_RETURN_OK;
                                409                 :                : }
                                410                 :                : 
                                411                 :                : # define REPLACE_S(Z, B, K, SIZE, S) \
                                412                 :                :     SNOWBALL_PROPAGATE_ERR(replace_s(Z, B, K, SIZE, S))
                                413                 :                : 
                                414                 :           1354 : static SNOWBALL_ERR slice_check(struct SN_env * z) {
                                415                 :                : 
 6781 tgl@sss.pgh.pa.us         416         [ +  - ]:CBC        1354 :     if (z->bra < 0 ||
                                417         [ +  - ]:           1354 :         z->bra > z->ket ||
                                418         [ +  - ]:           1354 :         z->ket > z->l ||
                                419         [ -  + ]:           1354 :         z->l > SIZE(z->p)) /* this line could be removed */
                                420                 :                :     {
                                421                 :                : #if 0
                                422                 :                :         fprintf(stderr, "faulty slice operation:\n");
                                423                 :                :         debug(z, -1, 0);
                                424                 :                : #endif
   69 tgl@sss.pgh.pa.us         425                 :UNC           0 :         SNOWBALL_RETURN_OR_THROW(-1, std::logic_error("Snowball slice invalid"));
                                426                 :                :     }
   69 tgl@sss.pgh.pa.us         427                 :GNC        1354 :     SNOWBALL_RETURN_OK;
                                428                 :                : }
                                429                 :                : 
                                430                 :                : # define SLICE_CHECK(Z) SNOWBALL_PROPAGATE_ERR(slice_check(Z))
                                431                 :                : 
                                432                 :            755 : extern SNOWBALL_ERR slice_from_s(struct SN_env * z, int s_size, const symbol * s) {
                                433         [ -  + ]:            755 :     SLICE_CHECK(z);
                                434         [ -  + ]:            755 :     REPLACE_S(z, z->bra, z->ket, s_size, s);
                                435                 :            755 :     z->ket = z->bra + s_size;
                                436                 :            755 :     SNOWBALL_RETURN_OK;
                                437                 :                : }
                                438                 :                : 
   69 tgl@sss.pgh.pa.us         439                 :UNC           0 : extern SNOWBALL_ERR slice_from_v(struct SN_env * z, const symbol * p) {
 6781 tgl@sss.pgh.pa.us         440                 :UBC           0 :     return slice_from_s(z, SIZE(p), p);
                                441                 :                : }
                                442                 :                : 
   69 tgl@sss.pgh.pa.us         443                 :GNC         599 : extern SNOWBALL_ERR slice_del(struct SN_env * z) {
                                444         [ -  + ]:            599 :     SLICE_CHECK(z);
                                445                 :                :     {
                                446                 :            599 :         int slice_size = z->ket - z->bra;
                                447         [ +  - ]:            599 :         if (slice_size != 0) {
                                448                 :            599 :             int len = SIZE(z->p);
                                449                 :            599 :             memmove(z->p + z->bra,
                                450                 :            599 :                     z->p + z->ket,
                                451                 :            599 :                     (len - z->ket) * sizeof(symbol));
                                452                 :            599 :             SET_SIZE(z->p, len - slice_size);
                                453                 :            599 :             z->l -= slice_size;
                                454         [ -  + ]:            599 :             if (z->c >= z->ket)
   69 tgl@sss.pgh.pa.us         455                 :UNC           0 :                 z->c -= slice_size;
   69 tgl@sss.pgh.pa.us         456         [ -  + ]:GNC         599 :             else if (z->c > z->bra)
   69 tgl@sss.pgh.pa.us         457                 :UNC           0 :                 z->c = z->bra;
                                458                 :                :         }
                                459                 :                :     }
   69 tgl@sss.pgh.pa.us         460                 :GNC         599 :     z->ket = z->bra;
                                461                 :            599 :     SNOWBALL_RETURN_OK;
                                462                 :                : }
                                463                 :                : 
   69 tgl@sss.pgh.pa.us         464                 :UNC           0 : extern SNOWBALL_ERR insert_s(struct SN_env * z, int bra, int ket, int s_size, const symbol * s) {
                                465         [ #  # ]:              0 :     REPLACE_S(z, bra, ket, s_size, s);
                                466         [ #  # ]:              0 :     if (bra <= z->ket) {
                                467                 :              0 :         int adjustment = s_size - (ket - bra);
                                468                 :              0 :         z->ket += adjustment;
                                469         [ #  # ]:              0 :         if (bra <= z->bra) z->bra += adjustment;
                                470                 :                :     }
                                471                 :              0 :     SNOWBALL_RETURN_OK;
                                472                 :                : }
                                473                 :                : 
                                474                 :              0 : extern SNOWBALL_ERR insert_v(struct SN_env * z, int bra, int ket, const symbol * p) {
 2729 tgl@sss.pgh.pa.us         475                 :UBC           0 :     return insert_s(z, bra, ket, SIZE(p), p);
                                476                 :                : }
                                477                 :                : 
   69 tgl@sss.pgh.pa.us         478                 :UNC           0 : extern SNOWBALL_ERR slice_to(struct SN_env * z, symbol ** p) {
                                479         [ #  # ]:              0 :     SLICE_CHECK(z);
                                480                 :                :     {
 6781 tgl@sss.pgh.pa.us         481                 :UBC           0 :         int len = z->ket - z->bra;
   69 tgl@sss.pgh.pa.us         482         [ #  # ]:UNC           0 :         if (CAPACITY(*p) < len) {
                                483         [ #  # ]:              0 :             SNOWBALL_PROPAGATE_ERR(increase_size(p, len));
                                484                 :                :         }
                                485                 :              0 :         memmove(*p, z->p + z->bra, len * sizeof(symbol));
                                486                 :              0 :         SET_SIZE(*p, len);
                                487                 :                :     }
                                488                 :              0 :     SNOWBALL_RETURN_OK;
                                489                 :                : }
                                490                 :                : 
                                491                 :              0 : extern SNOWBALL_ERR assign_to(struct SN_env * z, symbol ** p) {
 6781 tgl@sss.pgh.pa.us         492                 :UBC           0 :     int len = z->l;
   69 tgl@sss.pgh.pa.us         493         [ #  # ]:UNC           0 :     if (CAPACITY(*p) < len) {
                                494         [ #  # ]:              0 :         SNOWBALL_PROPAGATE_ERR(increase_size(p, len));
                                495                 :                :     }
                                496                 :              0 :     memmove(*p, z->p, len * sizeof(symbol));
                                497                 :              0 :     SET_SIZE(*p, len);
                                498                 :              0 :     SNOWBALL_RETURN_OK;
                                499                 :                : }
                                500                 :                : 
 2729 tgl@sss.pgh.pa.us         501                 :UBC           0 : extern int len_utf8(const symbol * p) {
                                502                 :              0 :     int size = SIZE(p);
                                503                 :              0 :     int len = 0;
                                504         [ #  # ]:              0 :     while (size--) {
                                505                 :              0 :         symbol b = *p++;
                                506   [ #  #  #  # ]:              0 :         if (b >= 0xC0 || b < 0x80) ++len;
                                507                 :                :     }
                                508                 :              0 :     return len;
                                509                 :                : }
        

Generated by: LCOV version 2.4-beta