1 package antlr; 2 3 9 10 14 public class ANTLRHashString { 15 private String s; 17 private char[] buf; 18 private int len; 19 private CharScanner lexer; 20 private static final int prime = 151; 21 22 23 public ANTLRHashString(char[] buf, int length, CharScanner lexer) { 24 this.lexer = lexer; 25 setBuffer(buf, length); 26 } 27 28 public ANTLRHashString(CharScanner lexer) { 30 this.lexer = lexer; 31 } 32 33 public ANTLRHashString(String s, CharScanner lexer) { 34 this.lexer = lexer; 35 setString(s); 36 } 37 38 private final char charAt(int index) { 39 return (s != null) ? s.charAt(index) : buf[index]; 40 } 41 42 public boolean equals(Object o) { 44 if (!(o instanceof ANTLRHashString) && !(o instanceof String )) { 45 return false; 46 } 47 48 ANTLRHashString s; 49 if (o instanceof String ) { 50 s = new ANTLRHashString((String )o, lexer); 51 } 52 else { 53 s = (ANTLRHashString)o; 54 } 55 int l = length(); 56 if (s.length() != l) { 57 return false; 58 } 59 if (lexer.getCaseSensitiveLiterals()) { 60 for (int i = 0; i < l; i++) { 61 if (charAt(i) != s.charAt(i)) { 62 return false; 63 } 64 } 65 } 66 else { 67 for (int i = 0; i < l; i++) { 68 if (lexer.toLower(charAt(i)) != lexer.toLower(s.charAt(i))) { 69 return false; 70 } 71 } 72 } 73 return true; 74 } 75 76 public int hashCode() { 77 int hashval = 0; 78 int l = length(); 79 80 if (lexer.getCaseSensitiveLiterals()) { 81 for (int i = 0; i < l; i++) { 82 hashval = hashval * prime + charAt(i); 83 } 84 } 85 else { 86 for (int i = 0; i < l; i++) { 87 hashval = hashval * prime + lexer.toLower(charAt(i)); 88 } 89 } 90 return hashval; 91 } 92 93 private final int length() { 94 return (s != null) ? s.length() : len; 95 } 96 97 public void setBuffer(char[] buf, int length) { 98 this.buf = buf; 99 this.len = length; 100 s = null; 101 } 102 103 public void setString(String s) { 104 this.s = s; 105 buf = null; 106 } 107 } 108 | Popular Tags |