| 1 |
|
| /* -*- c -*- |
| 2 |
|
| * File: pstring.h |
| 3 |
|
| * Author: Igor Vlasenko <vlasenko@imath.kiev.ua> |
| 4 |
|
| * Created: Fri Jul 1 20:11:51 2005 |
| 5 |
|
| * |
| 6 |
|
| * $Id$ |
| 7 |
|
| */ |
| 8 |
|
| |
| 9 |
|
| #ifdef HAVE_CONFIG_H |
| 10 |
|
| #include "config.h" |
| 11 |
|
| #endif |
| 12 |
|
| #include <stdio.h> |
| 13 |
|
| #include <string.h> |
| 14 |
|
| #include "pstring.h" |
| 15 |
|
| #include "tmpllog.h" |
| 16 |
|
| #include "exprval.h" |
| 17 |
|
| #include "pmiscdef.h" /*for snprintf */ |
| 18 |
|
| |
| 19 |
|
| static |
| 20 |
|
| PSTRING |
| 21 |
18 |
| double_to_pstring (double number, char buffer[], size_t bufsize) { |
| 22 |
18 |
| size_t len=0; |
| 23 |
18 |
| size_t tmplen=0; |
| 24 |
|
| PSTRING retval; |
| 25 |
18 |
| snprintf(buffer,bufsize,"%f",number); |
| 26 |
18 |
| len=strlen(buffer); |
| 27 |
18 |
| tmplen=len; |
| 28 |
|
| /* removing trailing 0 as 2.00000... */ |
| 29 |
78 |
▶ | while (buffer[tmplen-1]=='0' && tmplen-->0); |
Branch: 2 branches
| Branch | True | False | Total |
| while (buffer[tmplen-1]=='0' && tmplen-->0); |
60 |
18 |
78 |
| while (buffer[tmplen-1]=='0' && tmplen-->0); |
60 |
0 |
60 |
|
| 30 |
18 |
▶ | if (buffer[tmplen-1]=='.') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (buffer[tmplen-1]=='.') { |
10 |
8 |
18 |
|
| 31 |
10 |
| tmplen--; |
| 32 |
10 |
| len=tmplen; |
| 33 |
|
| } |
| 34 |
18 |
| retval.begin=buffer; |
| 35 |
18 |
| retval.endnext=buffer+len; |
| 36 |
18 |
| return retval; |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
|
| static |
| 40 |
|
| PSTRING |
| 41 |
410 |
| int_to_pstring (EXPR_int64 number, char buffer[], size_t bufsize) { |
| 42 |
410 |
| size_t len=0; |
| 43 |
|
| PSTRING retval; |
| 44 |
410 |
| snprintf(buffer, bufsize,"%" EXPR_PRId64 , number); |
| 45 |
410 |
| len=strlen(buffer); |
| 46 |
410 |
| retval.begin=buffer; |
| 47 |
410 |
| retval.endnext=buffer+len; |
| 48 |
410 |
| return retval; |
| 49 |
|
| } |
| 50 |
|
| |
| 51 |
|
| static |
| 52 |
|
| int |
| 53 |
24 |
| pstring_ge(PSTRING a, PSTRING b) { |
| 54 |
24 |
| const char* in_a=a.begin; |
| 55 |
24 |
| const char* in_b=b.begin; |
| 56 |
24 |
▶ | if (in_b==NULL) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_b==NULL) return 1; |
0 |
24 |
24 |
|
| 57 |
24 |
▶ | if (in_a==NULL) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_a==NULL) return 0; |
0 |
24 |
24 |
|
| 58 |
24 |
▶ | while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
Branch: 3 branches
| Branch | True | False | Total |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
24 |
0 |
24 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
20 |
4 |
24 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
0 |
20 |
20 |
|
| 59 |
24 |
▶ | if ((in_a==a.endnext && in_b==b.endnext) || *(--in_a) >= *(--in_b) ) return 1; else return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if ((in_a==a.endnext && in_b==b.endnext) || *(--in_a) >= *(--in_b) ) return 1; else return 0; |
0 |
24 |
24 |
| if ((in_a==a.endnext && in_b==b.endnext) || *(--in_a) >= *(--in_b) ) return 1; else return 0; |
0 |
0 |
0 |
| if ((in_a==a.endnext && in_b==b.endnext) || *(--in_a) >= *(--in_b) ) return 1; else return 0; |
12 |
12 |
24 |
|
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
|
| static |
| 63 |
|
| int |
| 64 |
24 |
| pstring_le(PSTRING a, PSTRING b) { |
| 65 |
24 |
| const char* in_a=a.begin; |
| 66 |
24 |
| const char* in_b=b.begin; |
| 67 |
24 |
▶ | if (in_a==NULL) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_a==NULL) return 1; |
0 |
24 |
24 |
|
| 68 |
24 |
▶ | if (in_b==NULL) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_b==NULL) return 0; |
0 |
24 |
24 |
|
| 69 |
24 |
▶ | while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
Branch: 3 branches
| Branch | True | False | Total |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
24 |
0 |
24 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
20 |
4 |
24 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
0 |
20 |
20 |
|
| 70 |
24 |
▶ | if ((in_a==a.endnext && in_b==b.endnext) || *(--in_a) <= *(--in_b) ) return 1; else return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if ((in_a==a.endnext && in_b==b.endnext) || *(--in_a) <= *(--in_b) ) return 1; else return 0; |
0 |
24 |
24 |
| if ((in_a==a.endnext && in_b==b.endnext) || *(--in_a) <= *(--in_b) ) return 1; else return 0; |
0 |
0 |
0 |
| if ((in_a==a.endnext && in_b==b.endnext) || *(--in_a) <= *(--in_b) ) return 1; else return 0; |
16 |
8 |
24 |
|
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
|
| static |
| 74 |
|
| int |
| 75 |
20 |
| pstring_ne(PSTRING a, PSTRING b) { |
| 76 |
20 |
| const char* in_a=a.begin; |
| 77 |
20 |
| const char* in_b=b.begin; |
| 78 |
20 |
▶ | if (in_a==NULL || in_b==NULL) return in_a != in_b; |
Branch: 2 branches
| Branch | True | False | Total |
| if (in_a==NULL || in_b==NULL) return in_a != in_b; |
20 |
0 |
20 |
| if (in_a==NULL || in_b==NULL) return in_a != in_b; |
0 |
20 |
20 |
|
| 79 |
24 |
▶ | while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
Branch: 3 branches
| Branch | True | False | Total |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
24 |
0 |
24 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
24 |
0 |
24 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
4 |
20 |
24 |
|
| 80 |
20 |
▶ | if (in_a==a.endnext && in_b==b.endnext && *(--in_a) == *(--in_b)) return 0; else return 1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (in_a==a.endnext && in_b==b.endnext && *(--in_a) == *(--in_b)) return 0; else return 1; |
20 |
0 |
20 |
| if (in_a==a.endnext && in_b==b.endnext && *(--in_a) == *(--in_b)) return 0; else return 1; |
20 |
0 |
20 |
| if (in_a==a.endnext && in_b==b.endnext && *(--in_a) == *(--in_b)) return 0; else return 1; |
0 |
20 |
20 |
|
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| static |
| 84 |
|
| int |
| 85 |
68 |
| pstring_eq(PSTRING a, PSTRING b) { |
| 86 |
68 |
| const char* in_a=a.begin; |
| 87 |
68 |
| const char* in_b=b.begin; |
| 88 |
68 |
▶ | if (in_a==NULL || in_b==NULL) return in_a == in_b; |
Branch: 2 branches
| Branch | True | False | Total |
| if (in_a==NULL || in_b==NULL) return in_a == in_b; |
68 |
0 |
68 |
| if (in_a==NULL || in_b==NULL) return in_a == in_b; |
4 |
64 |
68 |
|
| 89 |
64 |
▶ | if (in_a==a.endnext) return in_b==b.endnext; |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_a==a.endnext) return in_b==b.endnext; |
4 |
60 |
64 |
|
| 90 |
116 |
▶ | while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
Branch: 3 branches
| Branch | True | False | Total |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
100 |
16 |
116 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
100 |
0 |
100 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
56 |
44 |
100 |
|
| 91 |
60 |
▶ | if (in_a==a.endnext && in_b==b.endnext && *(--in_a) == *(--in_b)) return 1; else return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (in_a==a.endnext && in_b==b.endnext && *(--in_a) == *(--in_b)) return 1; else return 0; |
56 |
4 |
60 |
| if (in_a==a.endnext && in_b==b.endnext && *(--in_a) == *(--in_b)) return 1; else return 0; |
52 |
4 |
56 |
| if (in_a==a.endnext && in_b==b.endnext && *(--in_a) == *(--in_b)) return 1; else return 0; |
12 |
40 |
52 |
|
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| static |
| 95 |
|
| int |
| 96 |
24 |
| pstring_gt(PSTRING a, PSTRING b) { |
| 97 |
24 |
| const char* in_a=a.begin; |
| 98 |
24 |
| const char* in_b=b.begin; |
| 99 |
24 |
▶ | if (in_a==NULL) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_a==NULL) return 0; |
0 |
24 |
24 |
|
| 100 |
24 |
▶ | if (in_b==NULL) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_b==NULL) return 1; |
0 |
24 |
24 |
|
| 101 |
24 |
▶ | while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
Branch: 3 branches
| Branch | True | False | Total |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
24 |
0 |
24 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
20 |
4 |
24 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
0 |
20 |
20 |
|
| 102 |
24 |
▶ | if ((in_b==b.endnext && in_a!=a.endnext) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((in_b==b.endnext && in_a!=a.endnext) |
20 |
4 |
24 |
| if ((in_b==b.endnext && in_a!=a.endnext) |
16 |
4 |
20 |
|
| 103 |
24 |
▶ | || (*(--in_a) > *(--in_b)) ) return 1; else return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| || (*(--in_a) > *(--in_b)) ) return 1; else return 0; |
0 |
20 |
20 |
|
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| static |
| 107 |
|
| int |
| 108 |
36 |
| pstring_lt(PSTRING a, PSTRING b) { |
| 109 |
36 |
| const char* in_a=a.begin; |
| 110 |
36 |
| const char* in_b=b.begin; |
| 111 |
36 |
▶ | if (in_b==NULL) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_b==NULL) return 0; |
0 |
36 |
36 |
|
| 112 |
36 |
▶ | if (in_a==NULL) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_a==NULL) return 1; |
0 |
36 |
36 |
|
| 113 |
36 |
▶ | while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
Branch: 3 branches
| Branch | True | False | Total |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
36 |
0 |
36 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
32 |
4 |
36 |
| while (in_a<a.endnext && in_b < b.endnext && *in_a++==*in_b++); |
0 |
32 |
32 |
|
| 114 |
36 |
▶ | if ((in_b!=b.endnext && in_a==a.endnext) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((in_b!=b.endnext && in_a==a.endnext) |
12 |
24 |
36 |
| if ((in_b!=b.endnext && in_a==a.endnext) |
12 |
0 |
12 |
|
| 115 |
36 |
▶ | || *(--in_a) < *(--in_b) ) return 1; else return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| || *(--in_a) < *(--in_b) ) return 1; else return 0; |
32 |
4 |
36 |
|
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
|
| static |
| 119 |
|
| int |
| 120 |
0 |
| re_notlike(struct expr_parser* exprobj, PSTRING a, PSTRING b) { |
| 121 |
0 |
| return ! re_like(exprobj, a,b); |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| #ifdef HAVE_PCRE2 |
| 125 |
|
| /* https://github.com/luvit/pcre2/blob/master/src/pcre2demo.c */ |
| 126 |
|
| #define PCRE2_CODE_UNIT_WIDTH 8 |
| 127 |
|
| #include <pcre2.h> |
| 128 |
|
| static |
| 129 |
|
| int |
| 130 |
|
| re_like(struct expr_parser* exprobj, PSTRING a, PSTRING b) { |
| 131 |
|
| pcre2_code* re; |
| 132 |
|
| int rc, errornumber; |
| 133 |
|
| PCRE2_SIZE erroroffset; |
| 134 |
|
| PCRE2_SPTR subject=(PCRE2_SPTR)a.begin; |
| 135 |
|
| size_t subject_length=(a.endnext-a.begin); |
| 136 |
|
| char* pattern; |
| 137 |
|
| pcre2_match_data *match_data; |
| 138 |
|
| if (subject==NULL) { |
| 139 |
|
| log_expr(exprobj,TMPL_LOG_INFO, "regular expression: applied to undefined value.\n"); |
| 140 |
|
| return 0; |
| 141 |
|
| } |
| 142 |
|
| if (b.begin==NULL || (b.endnext-b.begin)==0) { |
| 143 |
|
| log_expr(exprobj,TMPL_LOG_INFO, "regular expression: the pattern is empty or undefined.\n"); |
| 144 |
|
| return 1; |
| 145 |
|
| } |
| 146 |
|
| pattern=(char*)malloc(b.endnext-b.begin); |
| 147 |
|
| if (pattern==NULL) { |
| 148 |
|
| log_expr(exprobj,TMPL_LOG_ERROR, "regular expression: memory allocation failed.\n"); |
| 149 |
|
| return 0; |
| 150 |
|
| } |
| 151 |
|
| strncpy(pattern, b.begin, (b.endnext-b.begin)); |
| 152 |
|
| *(pattern+(b.endnext-b.begin))=0; |
| 153 |
|
| re = pcre2_compile((PCRE2_SPTR)pattern,PCRE2_ZERO_TERMINATED,0,&errornumber,&erroroffset,NULL); |
| 154 |
|
| free(pattern); |
| 155 |
|
| if (re==NULL) { |
| 156 |
|
| PCRE2_UCHAR buffer[256]; |
| 157 |
|
| pcre2_get_error_message(errornumber, buffer, sizeof(buffer)); |
| 158 |
|
| log_expr(exprobj,TMPL_LOG_ERROR, "regular expression: PCRE2 compilation failed at offset %zd: %s\n", |
| 159 |
|
| erroroffset, buffer); |
| 160 |
|
| return 0; |
| 161 |
|
| } |
| 162 |
|
| match_data = pcre2_match_data_create_from_pattern(re, NULL); |
| 163 |
|
| rc = pcre2_match(re,subject,subject_length,0,0,match_data,NULL); |
| 164 |
|
| /* Matching failed: handle error cases */ |
| 165 |
|
| if(rc<0 && PCRE2_ERROR_NOMATCH != rc) { |
| 166 |
|
| PCRE2_UCHAR buffer[256]; |
| 167 |
|
| pcre2_get_error_message(errornumber, buffer, sizeof(buffer)); |
| 168 |
|
| log_expr(exprobj,TMPL_LOG_ERROR, "regular expression: PCRE2 reported run error %d: %s\n",errornumber, buffer); |
| 169 |
|
| } |
| 170 |
|
| pcre2_match_data_free(match_data); /* Release memory used for the match */ |
| 171 |
|
| pcre2_code_free(re); /* data and the compiled pattern. */ |
| 172 |
|
| return (rc<0)?0:1; |
| 173 |
|
| } |
| 174 |
|
| #else |
| 175 |
|
| #ifdef HAVE_PCRE |
| 176 |
|
| #include <pcre.h> |
| 177 |
|
| static |
| 178 |
|
| int |
| 179 |
|
| re_like(struct expr_parser* exprobj, PSTRING a, PSTRING b) { |
| 180 |
|
| pcre* re; |
| 181 |
|
| int ovector[30]; |
| 182 |
|
| int rc, erroffset; |
| 183 |
|
| const char* error; |
| 184 |
|
| const char* subject=a.begin; |
| 185 |
|
| int subject_length=(int)(a.endnext-a.begin); |
| 186 |
|
| char* pattern; |
| 187 |
|
| if (subject==NULL) { |
| 188 |
|
| log_expr(exprobj,TMPL_LOG_INFO, "regular expression: applied to undefined value.\n"); |
| 189 |
|
| return 0; |
| 190 |
|
| } |
| 191 |
|
| if (b.begin==NULL || (b.endnext-b.begin)==0) { |
| 192 |
|
| log_expr(exprobj,TMPL_LOG_INFO, "regular expression: the pattern is empty or undefined.\n"); |
| 193 |
|
| return 1; |
| 194 |
|
| } |
| 195 |
|
| pattern=(char*)malloc(b.endnext-b.begin); |
| 196 |
|
| if (pattern==NULL) { |
| 197 |
|
| log_expr(exprobj,TMPL_LOG_ERROR, "regular expression: memory allocation failed.\n"); |
| 198 |
|
| return 0; |
| 199 |
|
| } |
| 200 |
|
| strncpy(pattern, b.begin, (b.endnext-b.begin)); |
| 201 |
|
| *(pattern+(b.endnext-b.begin))=0; |
| 202 |
|
| re = pcre_compile(pattern, 0, &error, &erroffset, NULL); /* default character set */ |
| 203 |
|
| free(pattern); |
| 204 |
|
| if (re==NULL) { |
| 205 |
|
| log_expr(exprobj,TMPL_LOG_ERROR, "regular expression: PCRE compilation failed at offset %d: %s\n", |
| 206 |
|
| erroffset, error); |
| 207 |
|
| return 0; |
| 208 |
|
| } |
| 209 |
|
| rc=pcre_exec(re, NULL, subject, subject_length, 0, 0, ovector, 30); |
| 210 |
|
| return (rc<0)?0:1; |
| 211 |
|
| } |
| 212 |
|
| #else |
| 213 |
|
| static |
| 214 |
|
| int |
| 215 |
0 |
| re_like(struct expr_parser* exprobj, PSTRING a, PSTRING b) { |
| 216 |
0 |
| log_expr(exprobj,TMPL_LOG_ERROR,"can't parse the regular expression (sorry, Stanislav Yadykin regexp extension is disabled at compile time) \n"); |
| 217 |
0 |
| return 0; |
| 218 |
|
| } |
| 219 |
|
| #endif |
| 220 |
|
| #endif |