diff options
Diffstat (limited to 'src/ops/string.cc')
| -rw-r--r-- | src/ops/string.cc | 274 |
1 files changed, 274 insertions, 0 deletions
diff --git a/src/ops/string.cc b/src/ops/string.cc new file mode 100644 index 0000000..76b7e72 --- /dev/null +++ b/src/ops/string.cc | |||
| @@ -0,0 +1,274 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <ops/operator.h> | ||
| 21 | #include <ops/nnstyle.h> | ||
| 22 | #include <mt/mset.h> | ||
| 23 | |||
| 24 | OP2_IMPL(intersect) { mset a(L),b(R),c; c.Intersection(a,b); out<<c; } | ||
| 25 | OP2_IMPL(union) { mset a(L),b(R),c; c.Union(a,b); out<<c; } | ||
| 26 | OP2_IMPL(difference) { mset a(L),b(R),c; c.Difference(a,b); out<<c; } | ||
| 27 | OP2_IMPL(concats) { | ||
| 28 | if(L.nempty()&&R.nempty()) out<<L<<" "<<R; | ||
| 29 | else if(L.nempty()) out<<L; | ||
| 30 | else out<<R; | ||
| 31 | } | ||
| 32 | |||
| 33 | OP1_IMPL(lcase) {mstring s=a;s.tolower();out<<s;} | ||
| 34 | OP1_IMPL(ucase) {mstring s=a;s.toupper();out<<s;} | ||
| 35 | OP1_IMPL(nncase) {out<<nncase(a);} | ||
| 36 | OP1_IMPL(skipws) {out<<a.right_first_not_of(cset_ws);} | ||
| 37 | |||
| 38 | OP2_IMPL(eqnn) {out<<(nncomp(L,R)==0);} | ||
| 39 | OP2_IMPL(nenn) {out<<(nncomp(L,R)!=0);} | ||
| 40 | |||
| 41 | OP1_IMPL(first) {out<<First(a);} | ||
| 42 | OP1_IMPL(last) {out<<Last(a);} | ||
| 43 | OP1_IMPL(rest) {out<<Rest(a);} | ||
| 44 | |||
| 45 | OP1_IMPL(count) { | ||
| 46 | sref first,rest=a; int n=0; | ||
| 47 | while(Split(first,rest)) n++; | ||
| 48 | out<<n; | ||
| 49 | } | ||
| 50 | |||
| 51 | OP2_IMPL(nth) { | ||
| 52 | sref first,rest=L; int n=atoi(R); | ||
| 53 | while(n-->0&&Split(first,rest)); | ||
| 54 | out<<first; | ||
| 55 | } | ||
| 56 | |||
| 57 | OP2_IMPL(which) { | ||
| 58 | sref first,rest=L; int n=0; | ||
| 59 | while(Split(first,rest)) { n++; if(first==R) { out<<n; return; } } | ||
| 60 | out<<0; | ||
| 61 | } | ||
| 62 | |||
| 63 | OP2_IMPL(cnt) {out<<(L.find(R)>=0);} | ||
| 64 | OP2_IMPL(cntn) {out<<(L.find(R,cset_lcase)>=0);} | ||
| 65 | |||
| 66 | OP2_IMPL(concat) {out<<L<<R;} | ||
| 67 | |||
| 68 | OP2_IMPL(leftand) {out<<L.leftand_first(R);} | ||
| 69 | OP2_IMPL(left) {out<<L.left_first(R);} | ||
| 70 | OP2_IMPL(right) {out<<L.right_first(R);} | ||
| 71 | OP2_IMPL(past) {out<<L.past_first(R);} | ||
| 72 | |||
| 73 | OP2_IMPL(rleftand) {out<<L.leftand_last(R);} | ||
| 74 | OP2_IMPL(rleft) {out<<L.left_last(R);} | ||
| 75 | OP2_IMPL(rright) {out<<L.right_last(R);} | ||
| 76 | OP2_IMPL(rpast) {out<<L.past_last(R);} | ||
| 77 | |||
| 78 | void opAddString(opmap& M) | ||
| 79 | { | ||
| 80 | OP_ADD(32,lcase, | ||
| 81 | "Lower case\n" | ||
| 82 | "Syntax: lcase (string)\n" | ||
| 83 | "Returns: (string)\n" | ||
| 84 | ); | ||
| 85 | OP_ADD(32,ucase, | ||
| 86 | "Upper case\n" | ||
| 87 | "Syntax: ucase (string)\n" | ||
| 88 | "Returns: (string)\n" | ||
| 89 | ); | ||
| 90 | OP_ADD(32,nncase, | ||
| 91 | "NN-style case\n" | ||
| 92 | "Syntax: nncase (string)\n" | ||
| 93 | "Returns: (string)\n" | ||
| 94 | "\n" | ||
| 95 | "NN-style converts names to <lastname> <initials>\n" | ||
| 96 | ); | ||
| 97 | OP_ADD(32,skipws, | ||
| 98 | "Skip whitespace\n" | ||
| 99 | "Syntax: skipws (string)\n" | ||
| 100 | "Returns: (string)\n" | ||
| 101 | "\n" | ||
| 102 | "Skips trailing whitespace\n" | ||
| 103 | ); | ||
| 104 | |||
| 105 | OP_ADD(32,first, | ||
| 106 | "First element in list\n" | ||
| 107 | "Syntax: first (list)\n" | ||
| 108 | "Returns: (string)\n" | ||
| 109 | "\n" | ||
| 110 | "Elements are separated by whitespace\n" | ||
| 111 | ); | ||
| 112 | OP_ADD(32,last, | ||
| 113 | "Last element in list\n" | ||
| 114 | "Syntax: last (list)\n" | ||
| 115 | "Returns: (string)\n" | ||
| 116 | "\n" | ||
| 117 | "Elements are separated by whitespace\n" | ||
| 118 | ); | ||
| 119 | OP_ADD(32,rest, | ||
| 120 | "Rest of elements in list\n" | ||
| 121 | "Syntax: rest (list)\n" | ||
| 122 | "Returns: (list)\n" | ||
| 123 | "\n" | ||
| 124 | "Elements are separated by whitespace\n" | ||
| 125 | ); | ||
| 126 | OP_ADD(32,count, | ||
| 127 | "Count elements in list\n" | ||
| 128 | "Syntax: count (list)\n" | ||
| 129 | "Returns: (int)\n" | ||
| 130 | "\n" | ||
| 131 | "Elements are separated by whitespace\n" | ||
| 132 | ); | ||
| 133 | |||
| 134 | OP_ADD(30,nth, | ||
| 135 | "Nth element in list\n" | ||
| 136 | "Syntax: (list) nth (int)\n" | ||
| 137 | "Returns: (string)\n" | ||
| 138 | "\n" | ||
| 139 | "First element is denoted 1\n" | ||
| 140 | "Elements out of range returns empty\n" | ||
| 141 | "Elements are separated by whitespace\n" | ||
| 142 | ); | ||
| 143 | OP_ADD(30,which, | ||
| 144 | "Which element in list\n" | ||
| 145 | "Syntax: (list) a which (string) b\n" | ||
| 146 | "Returns: (int)\n" | ||
| 147 | "\n" | ||
| 148 | "Which element in a is b (0 if not found)\n" | ||
| 149 | "First element is denoted 1\n" | ||
| 150 | "Elements are separated by whitespace\n" | ||
| 151 | ); | ||
| 152 | |||
| 153 | OP_ADD(28,left, | ||
| 154 | "Left of substring\n" | ||
| 155 | "Syntax: (string) a left (string) b\n" | ||
| 156 | "Returns: (string)\n" | ||
| 157 | "\n" | ||
| 158 | "Everything in a to the left of first b, of a if b not found in a\n" | ||
| 159 | ); | ||
| 160 | OP_ADD(28,leftand, | ||
| 161 | "Left of substring inclusive\n" | ||
| 162 | "Syntax: (string) a leftand (string) b\n" | ||
| 163 | "Returns: (string)\n" | ||
| 164 | "\n" | ||
| 165 | "Everything in a to the left of first b inclusive, of a if b not found in a\n" | ||
| 166 | ); | ||
| 167 | OP_ADD(28,right, | ||
| 168 | "Right of substring inclusive\n" | ||
| 169 | "Syntax: (string) a right (string) b\n" | ||
| 170 | "Returns: (string)\n" | ||
| 171 | "\n" | ||
| 172 | "Everything in a to the right of first b inclusive, of empty if b not found in a\n" | ||
| 173 | ); | ||
| 174 | OP_ADD(28,past, | ||
| 175 | "Right of substring\n" | ||
| 176 | "Syntax: (string) a past (string) b\n" | ||
| 177 | "Returns: (string)\n" | ||
| 178 | "\n" | ||
| 179 | "Everything in a to the right of first b, of empty if b not found in a\n" | ||
| 180 | ); | ||
| 181 | |||
| 182 | OP_ADD(28,rleft, | ||
| 183 | "Left of substring (reversed)\n" | ||
| 184 | "Syntax: (string) a rleft (string) b\n" | ||
| 185 | "Returns: (string)\n" | ||
| 186 | "\n" | ||
| 187 | "Everything in a to the left of last b, of a if b not found in a\n" | ||
| 188 | ); | ||
| 189 | OP_ADD(28,rleftand, | ||
| 190 | "Left of substring inclusive, (reversed)\n" | ||
| 191 | "Syntax: (string) a rleftand (string) b\n" | ||
| 192 | "Returns: (string)\n" | ||
| 193 | "\n" | ||
| 194 | "Everything in a to the left of last b inclusive, of a if b not found in a\n" | ||
| 195 | ); | ||
| 196 | OP_ADD(28,rright, | ||
| 197 | "Right of substring inclusive (reversed)\n" | ||
| 198 | "Syntax: (string) a rright (string) b\n" | ||
| 199 | "Returns: (string)\n" | ||
| 200 | "\n" | ||
| 201 | "Everything in a to the right of last b inclusive, of empty if b not found in a\n" | ||
| 202 | ); | ||
| 203 | OP_ADD(28,rpast, | ||
| 204 | "Right of substring (reversed)\n" | ||
| 205 | "Syntax: (string) a rpast (string) b\n" | ||
| 206 | "Returns: (string)\n" | ||
| 207 | "\n" | ||
| 208 | "Everything in a to the right of last b, of empty if b not found in a\n" | ||
| 209 | ); | ||
| 210 | |||
| 211 | OP_ADDN(24,concat,++, | ||
| 212 | "String concatenation\n" | ||
| 213 | "Syntax: (string) ++ (string)\n" | ||
| 214 | "Returns: (string)\n" | ||
| 215 | ); | ||
| 216 | OP_ADDN(24,concats,+++, | ||
| 217 | "List concatenation\n" | ||
| 218 | "Syntax: (list) +++ (list)\n" | ||
| 219 | "Returns: (list)\n" | ||
| 220 | "\n" | ||
| 221 | "Lists are concatenated with space\n" | ||
| 222 | ); | ||
| 223 | |||
| 224 | OP_ADD(18,cnt, | ||
| 225 | "Contains substring\n" | ||
| 226 | "Syntax: (string) a cnt (string) b\n" | ||
| 227 | "Returns: (int)\n" | ||
| 228 | "\n" | ||
| 229 | "Nonzero if b found in a\n" | ||
| 230 | ); | ||
| 231 | OP_ADD(18,cntn, | ||
| 232 | "Contains substring, case insensitive\n" | ||
| 233 | "Syntax: (string) a cnt (string) b\n" | ||
| 234 | "Returns: (int)\n" | ||
| 235 | "\n" | ||
| 236 | "Nonzero if b found in a\n" | ||
| 237 | ); | ||
| 238 | OP_ADD(18,eqnn, | ||
| 239 | "NN-style equal\n" | ||
| 240 | "Syntax: (string) eqnn (string)\n" | ||
| 241 | "Returns: (int)\n" | ||
| 242 | "\n" | ||
| 243 | "NN-style converts names to <lastname> <initials>\n" | ||
| 244 | ); | ||
| 245 | OP_ADD(18,nenn, | ||
| 246 | "NN-style not equal\n" | ||
| 247 | "Syntax: (string) eqnn (string)\n" | ||
| 248 | "Returns: (int)\n" | ||
| 249 | "\n" | ||
| 250 | "NN-style converts names to <lastname> <initials>\n" | ||
| 251 | ); | ||
| 252 | |||
| 253 | OP_ADDN(10,intersect,&&&, | ||
| 254 | "List intersection\n" | ||
| 255 | "Syntax: (list) a &&& (list) b\n" | ||
| 256 | "Returns: (list)\n" | ||
| 257 | "\n" | ||
| 258 | "Finds the set intersection of a and b\n" | ||
| 259 | ); | ||
| 260 | OP_ADDN(9,difference,---, | ||
| 261 | "List difference\n" | ||
| 262 | "Syntax: (list) a &&& (list) b\n" | ||
| 263 | "Returns: (list)\n" | ||
| 264 | "\n" | ||
| 265 | "Finds the set difference of a and b\n" | ||
| 266 | ); | ||
| 267 | OP_ADDN(9,union,|||, | ||
| 268 | "List union\n" | ||
| 269 | "Syntax: (list) a &&& (list) b\n" | ||
| 270 | "Returns: (list)\n" | ||
| 271 | "\n" | ||
| 272 | "Finds the set union of a and b\n" | ||
| 273 | ); | ||
| 274 | } | ||
