--- /dev/null
+//\r
+// This software is now distributed according to\r
+// the Lesser Gnu Public License. Please see\r
+// http://www.gnu.org/copyleft/lesser.txt for\r
+// the details.\r
+// -- Happy Computing!\r
+//\r
+package com.stevesoft.pat;\r
+import java.util.Hashtable;\r
+\r
+/** A special optimization of multi that is used when the\r
+ * common subpattern ".*" is encountered.\r
+ */\r
+class DotMulti extends PatternSub {\r
+ patInt fewestMatches,mostMatches;\r
+ public patInt minChars() {\r
+ return fewestMatches;\r
+ }\r
+ public patInt maxChars() {\r
+ return mostMatches;\r
+ }\r
+ public boolean matchFewest = false;\r
+\r
+ StringLike src=null;\r
+ int srclength=0;\r
+ boolean dotDoesntMatchCR=true;\r
+ DotMulti(patInt a,patInt b) {\r
+ fewestMatches = a;\r
+ mostMatches = b;\r
+ }\r
+ public String toString() {\r
+ return ".{"\r
+ +fewestMatches+","+mostMatches+"}"+\r
+ (matchFewest ? "?" : "")+"(?# <= dot multi)"+\r
+ nextString();\r
+ }\r
+ final int submatchInternal(int pos,Pthings pt) {\r
+ if(pos < srclength) {\r
+ if(dotDoesntMatchCR) {\r
+ if(src.charAt(pos) != '\n')\r
+ return 1+pos;\r
+ } else return 1+pos;\r
+ }\r
+ return -1;\r
+ }\r
+ final static int step = 1;\r
+ static int idcount = 1;\r
+ public int matchInternal(int pos,Pthings pt) {\r
+ int m=-1;\r
+ int i=pos;\r
+ src = pt.src;\r
+ srclength = src.length();\r
+ dotDoesntMatchCR = pt.dotDoesntMatchCR;\r
+ if(matchFewest) {\r
+ int nMatches = 0;\r
+ while(fewestMatches.intValue() > nMatches) {\r
+ i=submatchInternal(i,pt);\r
+ if(i<0) return -1;\r
+ nMatches++;\r
+ }\r
+ if(i<0) return -1;\r
+ int ii = nextMatch(i,pt);\r
+ if(ii >= 0) return ii;\r
+ if(!mostMatches.finite()) {\r
+ while(i >= 0) {\r
+ i = submatchInternal(i,pt);\r
+ if(i < 0) return -1;\r
+ ii = nextMatch(i,pt);\r
+ if(ii >= 0) return ii;\r
+ }\r
+ } else {\r
+ while(i > 0) {\r
+ i = submatchInternal(i,pt);\r
+ if(i < 0) return -1;\r
+ nMatches++;\r
+ if(nMatches > mostMatches.intValue())\r
+ return -1;\r
+ ii = nextMatch(i,pt);\r
+ if(ii >= 0) return ii;\r
+ }\r
+ }\r
+ return -1;\r
+ }\r
+ int nMatches = 0;\r
+ while(fewestMatches.intValue() > nMatches) {\r
+ i=submatchInternal(i,pt);\r
+ if(i >= 0)\r
+ nMatches++;\r
+ else\r
+ return -1;\r
+ }\r
+ m=i;\r
+ if(mostMatches.finite()) {\r
+ while(nMatches < mostMatches.intValue()) {\r
+ i = submatchInternal(i,pt);\r
+ if(i>=0) {\r
+ m=i;\r
+ nMatches++;\r
+ } else break;\r
+ }\r
+ } else {\r
+ while(true) {\r
+ i = submatchInternal(i,pt);\r
+ if(i>=0) {\r
+ m=i;\r
+ nMatches++;\r
+ } else break;\r
+ }\r
+ }\r
+ while(m >= pos) {\r
+ int r=nextMatch(m,pt);\r
+ if(r >= 0) return r;\r
+ m -= step;\r
+ nMatches--;\r
+ if(nMatches < fewestMatches.intValue())\r
+ return -1;\r
+ }\r
+ return -1;\r
+ }\r
+ Pattern clone1(Hashtable h) {\r
+ DotMulti dm = new DotMulti(fewestMatches,mostMatches);\r
+ dm.matchFewest = matchFewest;\r
+ return dm;\r
+ }\r
+}\r