JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / com / stevesoft / pat / Range.java
1 //
2 // This software is now distributed according to
3 // the Lesser Gnu Public License.  Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
5 // the details.
6 //    -- Happy Computing!
7 //
8 package com.stevesoft.pat;
9
10 import java.util.*;
11
12 /** Thrown when one encounters things like [z-a] */
13 class BadRangeArgs extends RegSyntax
14 {
15 };
16
17 /**
18  * Implments a subelement (ranges) of the [] pattern element. For example,
19  * [a-z023] is implemented using a range and tree oneChar classes.
20  * 
21  * @see Bracket
22  * @see oneChar
23  */
24 class Range extends Pattern
25 {
26   char lo, hi, altlo, althi;
27
28   boolean printBrackets = false;
29
30   public String toString()
31   {
32     String s = protect("" + lo, PROTECT_THESE, ESC) + "-"
33             + protect("" + hi, PROTECT_THESE, ESC);
34     if (!printBrackets)
35     {
36       return s;
37     }
38     return "[" + s + "]";
39   }
40
41   Range(char loi, char hii) throws RegSyntax
42   {
43     lo = loi;
44     hi = hii;
45     oneChar o = null;
46     if (lo >= hi)
47     {
48       // throw new BadRangeArgs();
49       RegSyntaxError.endItAll("Badly formed []'s : " + lo + " >= " + hi);
50     }
51     o = new oneChar(lo);
52     altlo = o.altc;
53     o = new oneChar(hi);
54     althi = o.altc;
55   }
56
57   public int matchInternal(int pos, Pthings pt)
58   {
59     if (pos >= pt.src.length())
60     {
61       return -1;
62     }
63     if (Masked(pos, pt))
64     {
65       return -1;
66     }
67     char c = pt.src.charAt(pos);
68     if (lo <= c && c <= hi || (pt.ignoreCase && (altlo <= c && c <= althi)))
69     {
70       return nextMatch(pos + 1, pt);
71     }
72     return -1;
73   }
74
75   public patInt minChars()
76   {
77     return new patInt(1);
78   }
79
80   public patInt maxChars()
81   {
82     return new patInt(1);
83   }
84
85   public Pattern clone1(Hashtable h)
86   {
87     try
88     {
89       Range r = new Range(lo, hi);
90       r.printBrackets = printBrackets;
91       return r;
92     } catch (RegSyntax rs)
93     {
94       return null;
95     }
96   }
97 };