JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / com / stevesoft / pat / Skip.js
1 Clazz.declarePackage ("com.stevesoft.pat");\r
2 Clazz.load (null, "com.stevesoft.pat.Skip", ["com.stevesoft.pat.CaseMgr", "$.Or", "$.Skipped", "$.oneChar", "java.lang.StringBuffer"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.src = null;\r
5 this.c = 0;\r
6 this.mask = 0;\r
7 this.$offset = 0;\r
8 this.ign = false;\r
9 this.m1 = false;\r
10 Clazz.instantialize (this, arguments);\r
11 }, com.stevesoft.pat, "Skip");\r
12 c$.mkmask = Clazz.defineMethod (c$, "mkmask", \r
13 function (c) {\r
14 var x = String.fromCharCode (c);\r
15 return ~((com.stevesoft.pat.CaseMgr.toUpperCaseC (x)).charCodeAt (0) | (com.stevesoft.pat.CaseMgr.toLowerCaseC (x)).charCodeAt (0) | (com.stevesoft.pat.CaseMgr.toTitleCaseC (x)).charCodeAt (0));\r
16 }, "~N");\r
17 c$.string = Clazz.defineMethod (c$, "string", \r
18 function (r) {\r
19 return r.skipper == null ? null : r.skipper.src;\r
20 }, "com.stevesoft.pat.Regex");\r
21 c$.offset = Clazz.defineMethod (c$, "offset", \r
22 function (r) {\r
23 return r.skipper == null ? -1 : r.skipper.$offset;\r
24 }, "com.stevesoft.pat.Regex");\r
25 Clazz.makeConstructor (c$, \r
26 function (s, ign, o) {\r
27 this.src = s;\r
28 this.c = s.charCodeAt (0);\r
29 if (ign) {\r
30 this.mask = com.stevesoft.pat.Skip.mkmask (this.c);\r
31 } else {\r
32 this.mask = 0;\r
33 }this.$offset = o;\r
34 this.ign = ign;\r
35 this.m1 = (s.length == 1);\r
36 }, "~S,~B,~N");\r
37 Clazz.defineMethod (c$, "find", \r
38 function (s) {\r
39 return this.find (s, 0, s.length ());\r
40 }, "com.stevesoft.pat.StringLike");\r
41 c$.min = Clazz.defineMethod (c$, "min", \r
42 function (a, b) {\r
43 return a < b ? a : b;\r
44 }, "~N,~N");\r
45 Clazz.defineMethod (c$, "find", \r
46 function (s, start, end) {\r
47 if (start > end) {\r
48 return -1;\r
49 }start += this.$offset;\r
50 var vend = com.stevesoft.pat.Skip.min (s.length () - 1, end + this.$offset);\r
51 if (this.mask != this.c) {\r
52 for (var i = start; i <= vend; i++) {\r
53 if (0 == (s.charCodeAt (i) & this.mask)) {\r
54 if (this.m1 || com.stevesoft.pat.CaseMgr.regionMatchesLike2 (s, this.ign, i, this.src, 0, this.src.length)) {\r
55 return i - this.$offset;\r
56 }}}\r
57 } else {\r
58 for (var i = start; i <= vend; i++) {\r
59 if (this.c == s.charCodeAt (i)) {\r
60 if (this.m1 || com.stevesoft.pat.CaseMgr.regionMatchesLike2 (s, this.ign, i, this.src, 0, this.src.length)) {\r
61 return i - this.$offset;\r
62 }}}\r
63 }return -1;\r
64 }, "com.stevesoft.pat.StringLike,~N,~N");\r
65 c$.findSkipRegex = Clazz.defineMethod (c$, "findSkipRegex", \r
66 function (r) {\r
67 return com.stevesoft.pat.Skip.findSkip (r.thePattern, r.ignoreCase, !r.dontMatchInQuotes);\r
68 }, "com.stevesoft.pat.Regex");\r
69 c$.findSkip = Clazz.defineMethod (c$, "findSkip", \r
70 function (p, ignoreCase, trnc) {\r
71 var sb =  new StringBuffer ();\r
72 var subsk = null;\r
73 var offset = 0;\r
74 var skipc = -1;\r
75 var skipoff = 0;\r
76 for (; p != null; p = p.next) {\r
77 if (Clazz.instanceOf (p, com.stevesoft.pat.oneChar)) {\r
78 skipc = ((p).c).charCodeAt (0);\r
79 skipoff = offset;\r
80 }if (Clazz.instanceOf (p, com.stevesoft.pat.oneChar) && Clazz.instanceOf (p.next, com.stevesoft.pat.oneChar)) {\r
81 var psav = p;\r
82 sb.append ((p).c);\r
83 while (Clazz.instanceOf (p.next, com.stevesoft.pat.oneChar)) {\r
84 sb.append ((p.next).c);\r
85 p = p.next;\r
86 }\r
87 var st = sb.toString ();\r
88 var sk = null;\r
89 if (st.length > 2) {\r
90 sk =  new com.stevesoft.pat.SkipBMH (st, ignoreCase, offset);\r
91 } else {\r
92 sk =  new com.stevesoft.pat.Skip2 (st, ignoreCase, offset);\r
93 }if (trnc && st.length > 2) {\r
94 psav.next =  new com.stevesoft.pat.Skipped (st.substring (1));\r
95 psav.next.next = p.next;\r
96 psav.next.parent = p.parent;\r
97 }return sk;\r
98 } else if (Clazz.instanceOf (p, com.stevesoft.pat.Or) && (p).v.size () == 1 && !(p).leftForm ().equals ("(?!") && null != (subsk = com.stevesoft.pat.Skip.findSkip ((p).v.elementAt (0), ignoreCase, trnc))) {\r
99 subsk.$offset += offset;\r
100 return subsk;\r
101 } else if (p.minChars ().equals (p.maxChars ())) {\r
102 offset += p.minChars ().intValue ();\r
103 } else {\r
104 return skipc < 0 ? null :  new com.stevesoft.pat.Skip ("" + String.fromCharCode (skipc), ignoreCase, skipoff);\r
105 }}\r
106 return null;\r
107 }, "com.stevesoft.pat.Pattern,~B,~B");\r
108 });\r