Show
Ignore:
Timestamp:
12/17/05 09:09:42 (3 years ago)
Author:
pragma
Message:

Updated the bejezus out of this thing.


Currently Supported

  • character classes (including inverse char classes via [...])
  • match one or more (+)
  • match zero or more (*)
  • match zero or one (?)
  • escape sequences
  • whitespace matching (ws chars are treated literally right now)
  • {n} and {n,m} predicates
  • at the start of an expression
  • $ at the end of an expression
  • grouping via ()
  • most standard escape sequences
  • union operator (outside of parens)
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/meta/regexPredicate.d

    r79 r84  
    3030import meta.string; 
    3131 
     32/* TODO: support all of these 
     33exec 
     34 
     35    A regular expression method that executes a search for a match in a string. It returns an array of information. 
     36 
     37test 
     38 
     39    A regular expression method that tests for a match in a string. It returns true or false. 
     40 
     41match 
     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 
     45search 
     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 
     49replace 
     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 
     53split 
     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 
    3258//match routines 
    3359alias char[][] function(char[] str) MatchPredicate; 
    3460alias const noMatch = (char[][]).init ; 
    3561 
     62// unions the results of two matches together 
    3663template matchUnion(alias firstMatch,alias secondMatch,char[] key){ 
    3764    char[][] matchUnion(char[] str){ 
    38         char[][] results; 
    39         results ~= firstMatch(str); 
     65        char[][] results = firstMatch(str); 
    4066        results ~= secondMatch(str); 
    4167        return results; 
     
    4369} 
    4470 
    45  
     71// attempts a single basic match from the start of the string only 
    4672template matchTest(alias testPredicate,char[] key){ 
    4773    char[][] matchTest(char[] str){ 
    4874        char[][] results; 
    4975        int result = testPredicate(str); 
    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 
     86template 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 
     102template 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) 
     118template 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 
     133template 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){ 
    51138            results ~= str[0..result]; 
    52139        } 
     
    170257 
    171258template testAny(){ 
    172     char[] testAny(char[] str){ 
     259    int testAny(char[] str){ 
    173260        if(str.length == 0) return testFail; 
    174261        //TODO: check for newline (some regexps dont' test this) 
     
    187274} 
    188275 
    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 
     276template 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 
     291template testCharInverse(alias testPredicate,char[] key){ 
     292    int testCharInverse(char[] str){ 
     293        if(testPredicate(str) == testFail) return 1; 
     294        return testFail; 
     295    } 
     296