| | 32 | /* TODO: support all of these |
|---|
| | 33 | exec |
|---|
| | 34 | |
|---|
| | 35 | A regular expression method that executes a search for a match in a string. It returns an array of information. |
|---|
| | 36 | |
|---|
| | 37 | test |
|---|
| | 38 | |
|---|
| | 39 | A regular expression method that tests for a match in a string. It returns true or false. |
|---|
| | 40 | |
|---|
| | 41 | match |
|---|
| | 42 | |
|---|
| | 43 | A String method that executes a search for a match in a string. It returns an array of information or null on a mismatch. |
|---|
| | 44 | |
|---|
| | 45 | search |
|---|
| | 46 | |
|---|
| | 47 | A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails. |
|---|
| | 48 | |
|---|
| | 49 | replace |
|---|
| | 50 | |
|---|
| | 51 | A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring. |
|---|
| | 52 | |
|---|
| | 53 | split |
|---|
| | 54 | |
|---|
| | 55 | A String method that uses a regular expression or a fixed string to break a string into an array of substrings. |
|---|
| | 56 | */ |
|---|
| | 57 | |
|---|
| 50 | | if(result != testFail){ |
|---|
| | 76 | if(result != testFail && result > 0){ |
|---|
| | 77 | results ~= str[0..result]; |
|---|
| | 78 | } |
|---|
| | 79 | return results; |
|---|
| | 80 | } |
|---|
| | 81 | } |
|---|
| | 82 | |
|---|
| | 83 | /* |
|---|
| | 84 | // aggressive test- tests every possible substring for matches |
|---|
| | 85 | //NOTE: you probably should never use this |
|---|
| | 86 | template matchAggressive(alias testPredicate,char[] key){ |
|---|
| | 87 | char[][] matchAggressive(char[] str){ |
|---|
| | 88 | char[][] results; |
|---|
| | 89 | for(uint start=0; start<str.length; start++){ |
|---|
| | 90 | for(uint end=str.length; end>start; end--){ |
|---|
| | 91 | int result = testPredicate(str[start..end]); |
|---|
| | 92 | if(result != testFail && result == end-start){ |
|---|
| | 93 | results ~= str[start..result]; |
|---|
| | 94 | } |
|---|
| | 95 | } |
|---|
| | 96 | } |
|---|
| | 97 | return results; |
|---|
| | 98 | } |
|---|
| | 99 | }*/ |
|---|
| | 100 | |
|---|
| | 101 | // tests all substrings that start at the start of string |
|---|
| | 102 | template matchTestFromStart(alias testPredicate,char[] key,bit aggressive=false){ |
|---|
| | 103 | char[][] matchTestFromStart(char[] str){ |
|---|
| | 104 | for(uint end=str.length; end>0; end--){ |
|---|
| | 105 | char[][] results; |
|---|
| | 106 | int result = testPredicate(str[0..end]); |
|---|
| | 107 | if(result != testFail && result > 0){ |
|---|
| | 108 | results ~= str[0..result]; |
|---|
| | 109 | return results; |
|---|
| | 110 | } |
|---|
| | 111 | } |
|---|
| | 112 | return results; |
|---|
| | 113 | } |
|---|
| | 114 | } |
|---|
| | 115 | |
|---|
| | 116 | // tests all substrings that terminate at the string's end |
|---|
| | 117 | //TODO: refactor by reversing the string (should make for a faster match) |
|---|
| | 118 | template matchTestFromEnd(alias testPredicate,char[] key,bit aggressive=false){ |
|---|
| | 119 | char[][] matchTestFromEnd(char[] str){ |
|---|
| | 120 | char[][] results; |
|---|
| | 121 | for(uint start=0; start<str.length; start++){ |
|---|
| | 122 | int result = testPredicate(str[start..$]); |
|---|
| | 123 | if(result != testFail && result == str.length-start){ |
|---|
| | 124 | results ~= str[start..$]; |
|---|
| | 125 | static if(!aggressive) return results; |
|---|
| | 126 | } |
|---|
| | 127 | } |
|---|
| | 128 | return results; |
|---|
| | 129 | } |
|---|
| | 130 | } |
|---|
| | 131 | |
|---|
| | 132 | // test must completely cover the entire string |
|---|
| | 133 | template matchTestPerfect(alias testPredicate,char[] key){ |
|---|
| | 134 | char[][] matchTestFromStart(char[] str){ |
|---|
| | 135 | char[][] results; |
|---|
| | 136 | int result = testPredicate(str); |
|---|
| | 137 | if(result != testFail && result == str.length){ |
|---|
| 189 | | template testTimes(uint min,uint max,alias testPredicate,char[] key){ |
|---|
| 190 | | int testTimes(char[] str){ |
|---|
| 191 | | if(str.length == 0) return testFail; |
|---|
| 192 | | int result = 0; |
|---|
| 193 | | uint i; |
|---|
| 194 | | for(i=0; i<max; i++){ |
|---|
| 195 | | int nextResult = testPredicate(str[result..$]); |
|---|
| 196 | | if(nextResult == testFail){ |
|---|
| 197 | | if(i < min) return testFail; |
|---|
| 198 | | break; |
|---|
| 199 | | } |
|---|
| 200 | | result += nextResult; |
|---|
| 201 | | } |
|---|
| 202 | | return result; |
|---|
| 203 | | } |
|---|
| 204 | | } |
|---|
| | 276 | template testWordChar(){ |
|---|
| | 277 | int testWordChar(char[] str){ |
|---|
| | 278 | if(str.length == 0) return testFail; |
|---|
| | 279 | if( |
|---|
| | 280 | (str[0] >= 'a' && str[0] <= 'z') || |
|---|
| | 281 | (str[0] >= 'A' && str[0] <= 'Z') || |
|---|
| | 282 | (str[0] >= '0' && str[0] <= '9') || |
|---|
| | 283 | str[0] == '_' |
|---|
| | 284 | ){ |
|---|
| | 285 | return 1; |
|---|
| | 286 | } |
|---|
| | 287 | return testFail; |
|---|
| | 288 | } |
|---|
| | 289 | } |
|---|
| | 290 | |
|---|
| | 291 | template testCharInverse(alias testPredicate,char[] key){ |
|---|
| | 292 | int testCharInverse(char[] str){ |
|---|
| | 293 | if(testPredicate(str) == testFail) return 1; |
|---|
| | 294 | return testFail; |
|---|
| | 295 | } |
|---|
| | 296 | } |
|---|