1 3 package jodd.util; 4 5 13 public class Wildcard { 14 15 22 public static boolean match(String string, String pattern) { 23 return match(string, pattern, 0, 0); 24 } 25 26 29 private static boolean match(String string, String pattern, int stringStartNdx, int patternStartNdx) { 30 int pNdx = patternStartNdx; 31 int sNdx = stringStartNdx; 32 int pLen = pattern.length(); 33 int sLen = string.length(); 34 boolean nextIsNotWildcard = false; 35 36 while (true) { 37 38 if ((sNdx >= sLen) == true) { while ((pNdx < pLen) && (pattern.charAt(pNdx) == '*')) { 41 pNdx++; 42 } 43 return pNdx >= pLen; 44 } 45 if (pNdx >= pLen) { return false; 47 } 48 char p = pattern.charAt(pNdx); 50 if (nextIsNotWildcard == false) { 52 53 if (p == '\\') { 54 pNdx++; 55 nextIsNotWildcard = true; 56 continue; 57 } 58 if (p == '?') { 59 sNdx++; pNdx++; 60 continue; 61 } 62 if (p == '*') { 63 char pnext = 0; if (pNdx + 1 < pLen) { 65 pnext = pattern.charAt(pNdx + 1); 66 } 67 if (pnext == '*') { pNdx++; 69 continue; 70 } 71 int i; 72 pNdx++; 73 74 for (i = string.length(); i >= sNdx; i--) { 77 if (match(string, pattern, i, pNdx) == true) { 78 return true; 79 } 80 } 81 return false; 82 } 83 } else { 84 nextIsNotWildcard = false; 85 } 86 87 if (p != string.charAt(sNdx)) { 89 return false; 90 } 91 92 sNdx++; pNdx++; 94 } 95 } 96 97 98 100 105 public static int matchOne(String src, String [] patterns) { 106 for (int i = 0; i < patterns.length; i++) { 107 if (match(src, patterns[i]) == true) { 108 return i; 109 } 110 } 111 return -1; 112 } 113 114 } 115 | Popular Tags |