JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / com / stevesoft / pat / FastMulti.js
1 Clazz.declarePackage ("com.stevesoft.pat");\r
2 Clazz.load (["com.stevesoft.pat.PatternSub"], "com.stevesoft.pat.FastMulti", ["com.stevesoft.pat.RegSyntaxError", "$.patInt"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.fewestMatches = null;\r
5 this.mostMatches = null;\r
6 this.matchFewest = false;\r
7 this.step = -1;\r
8 Clazz.instantialize (this, arguments);\r
9 }, com.stevesoft.pat, "FastMulti", com.stevesoft.pat.PatternSub);\r
10 Clazz.overrideMethod (c$, "minChars", \r
11 function () {\r
12 return this.sub.countMinChars ().mul (this.fewestMatches);\r
13 });\r
14 Clazz.overrideMethod (c$, "maxChars", \r
15 function () {\r
16 return this.sub.countMaxChars ().mul (this.mostMatches);\r
17 });\r
18 Clazz.makeConstructor (c$, \r
19 function (a, b, p) {\r
20 Clazz.superConstructor (this, com.stevesoft.pat.FastMulti, []);\r
21 if (p == null) {\r
22 com.stevesoft.pat.RegSyntaxError.endItAll ("Null length pattern followed by *, +, or other Multi.");\r
23 }this.fewestMatches = a;\r
24 this.mostMatches = b;\r
25 this.sub = p;\r
26 this.step = p.countMinChars ().intValue ();\r
27 this.sub.setParent (null);\r
28 }, "com.stevesoft.pat.patInt,com.stevesoft.pat.patInt,com.stevesoft.pat.Pattern");\r
29 Clazz.defineMethod (c$, "toString", \r
30 function () {\r
31 return this.sub.toString () + "{" + this.fewestMatches + "," + this.mostMatches + "}" + (this.matchFewest ? "?" : "") + "(?# <= fast multi)" + this.nextString ();\r
32 });\r
33 Clazz.defineMethod (c$, "matchInternal", \r
34 function (pos, pt) {\r
35 var m = -1;\r
36 var i = pos;\r
37 var endstr = pt.src.length () - this.step;\r
38 var matches =  new com.stevesoft.pat.patInt (0);\r
39 if (this.matchFewest) {\r
40 if (this.fewestMatches.lessEq (matches)) {\r
41 var ii = this.nextMatch (i, pt);\r
42 if (ii >= 0) {\r
43 return ii;\r
44 }}while (i >= 0 && i <= endstr) {\r
45 i = this.sub.matchInternal (i, pt);\r
46 if (i >= 0) {\r
47 matches.inc ();\r
48 if (this.fewestMatches.lessEq (matches)) {\r
49 var ii = this.nextMatch (i, pt);\r
50 if (ii >= 0) {\r
51 return ii;\r
52 }}if (matches.equals (this.mostMatches)) {\r
53 return -1;\r
54 }}}\r
55 return -1;\r
56 }var nMatches = 0;\r
57 while (this.fewestMatches.intValue () > nMatches) {\r
58 i = this.sub.matchInternal (i, pt);\r
59 if (i >= 0) {\r
60 nMatches++;\r
61 } else {\r
62 return -1;\r
63 }}\r
64 m = i;\r
65 if (this.mostMatches.finite ()) {\r
66 while (nMatches < this.mostMatches.intValue ()) {\r
67 i = this.sub.matchInternal (i, pt);\r
68 if (i >= 0) {\r
69 m = i;\r
70 nMatches++;\r
71 } else {\r
72 break;\r
73 }}\r
74 } else {\r
75 while (true) {\r
76 i = this.sub.matchInternal (i, pt);\r
77 if (i >= 0) {\r
78 m = i;\r
79 nMatches++;\r
80 } else {\r
81 break;\r
82 }}\r
83 }while (m >= pos) {\r
84 var r = this.nextMatch (m, pt);\r
85 if (r >= 0) {\r
86 return r;\r
87 }m -= this.step;\r
88 nMatches--;\r
89 if (nMatches < this.fewestMatches.intValue ()) {\r
90 return -1;\r
91 }}\r
92 return -1;\r
93 }, "~N,com.stevesoft.pat.Pthings");\r
94 Clazz.overrideMethod (c$, "clone1", \r
95 function (h) {\r
96 try {\r
97 var fm =  new com.stevesoft.pat.FastMulti (this.fewestMatches, this.mostMatches, this.sub.clone (h));\r
98 fm.matchFewest = this.matchFewest;\r
99 return fm;\r
100 } catch (rs) {\r
101 if (Clazz.exceptionOf (rs, com.stevesoft.pat.RegSyntax)) {\r
102 return null;\r
103 } else {\r
104 throw rs;\r
105 }\r
106 }\r
107 }, "java.util.Hashtable");\r
108 });\r