1 package antlr;
2
3 * <b>SOFTWARE RIGHTS</b>
5 * <p>
6 * ANTLR 2.5.0 MageLang Institute, 1998
7 * <p>
8 * We reserve no legal rights to the ANTLR--it is fully in the
9 * public domain. An individual or company may do whatever
10 * they wish with source code distributed with ANTLR or the
11 * code generated by ANTLR, including the incorporation of
12 * ANTLR, or its output, into commerical software.
13 * <p>
14 * We encourage users to develop software with ANTLR. However,
15 * we do ask that credit is given to us for developing
16 * ANTLR. By "credit", we mean that if you use ANTLR or
17 * incorporate any source code into one of your programs
18 * (commercial product, research project, or otherwise) that
19 * you acknowledge this fact somewhere in the documentation,
20 * research report, etc... If you like ANTLR and have
21 * developed a nice tool with the output, please mention that
22 * you developed it using ANTLR. In addition, we ask that the
23 * headers remain intact in our source code. As long as these
24 * guidelines are kept, we expect to continue enhancing this
25 * system and expect to make other tools available as they are
26 * completed.
27 * <p>
28 * The ANTLR gang:
29 * @version ANTLR 2.5.0 MageLang Institute, 1998
30 * @author Terence Parr, <a href=http://www.MageLang.com>MageLang Institute</a>
31 * @author <br>John Lilley, <a href=http://www.Empathy.com>Empathy Software</a>
32 */
33
37 public class ANTLRHashString {
38 private String s;
40 private char[] buf;
41 private int len;
42 private CharScanner lexer;
43 private static final int prime = 151;
44
45
46 public ANTLRHashString(char[] buf, int length, CharScanner lexer) {
47 this.lexer = lexer;
48 setBuffer(buf, length);
49 }
50 public ANTLRHashString(CharScanner lexer) {
52 this.lexer = lexer;
53 }
54 public ANTLRHashString(String s, CharScanner lexer) {
55 this.lexer = lexer;
56 setString(s);
57 }
58 private final char charAt(int index) {
59 return (s!=null) ? s.charAt(index) : buf[index];
60 }
61 public boolean equals(Object o) {
63 if (!(o instanceof ANTLRHashString) && !(o instanceof String)) {
64 return false;
65 }
66
67 ANTLRHashString s;
68 if ( o instanceof String ) {
69 s = new ANTLRHashString((String)o,lexer);
70 }
71 else {
72 s = (ANTLRHashString)o;
73 }
74 int l = length();
75 if (s.length() != l) {
76 return false;
77 }
78 if (lexer.getCaseSensitiveLiterals()) {
79 for (int i = 0; i < l; i++) {
80 if (charAt(i) != s.charAt(i)) {
81 return false;
82 }
83 }
84 } else {
85 for (int i = 0; i < l; i++) {
86 if (lexer.toLower(charAt(i)) != lexer.toLower(s.charAt(i))) {
87 return false;
88 }
89 }
90 }
91 return true;
92 }
93 public int hashCode() {
94 int hashval = 0;
95 int l = length();
96
97 if (lexer.getCaseSensitiveLiterals()) {
98 for (int i = 0; i < l; i++) {
99 hashval = hashval * prime + charAt(i);
100 }
101 } else {
102 for (int i = 0; i < l; i++) {
103 hashval = hashval * prime + lexer.toLower(charAt(i));
104 }
105 }
106 return hashval;
107 }
108 private final int length() {
109 return (s!=null) ? s.length() : len;
110 }
111 public void setBuffer(char[] buf, int length) {
112 this.buf = buf;
113 this.len = length;
114 s = null;
115 }
116 public void setString(String s) {
117 this.s = s;
118 buf = null;
119 }
120 }
121