1 package net.sf.saxon.tinytree; 2 3 import java.io.Writer ; 4 import java.io.Serializable ; 5 6 19 public final class CharSlice implements CharSequence , Serializable { 20 21 private char[] array; 22 private int offset; 23 private int count; 24 25 public CharSlice(char[] array) { 26 this.array = array; 27 this.offset = 0; 28 this.count = array.length; 29 } 30 31 public CharSlice(char[] array, int start, int length) { 32 this.array = array; 33 this.offset = start; 34 this.count = length; 35 } 36 37 43 public int length() { 44 return count; 45 } 46 47 51 public void setLength(int length) { 52 count = length; 53 } 54 55 69 public char charAt(int index) { 70 return array[offset+index]; 71 } 72 73 90 public CharSequence subSequence(int start, int end) { 91 return new CharSlice(array, offset+start, end-start); 92 } 93 94 97 98 public String toString() { 99 return new String (array, offset, count); 100 } 101 102 105 106 public boolean equals(Object other) { 107 return toString().equals(other); 108 } 109 110 113 114 public int hashCode() { 115 int end = offset+count; 117 int h = 0; 118 for (int i = offset; i < end; i++) { 119 h = 31 * h + array[i]; 120 } 121 return h; 122 } 123 124 130 131 public int indexOf(char c) { 132 int end = offset+count; 133 for (int i = offset; i < end; i++) { 134 if (array[i] == c) { 135 return i-offset; 136 }; 137 } 138 return -1; 139 } 140 141 145 146 public String substring(int start, int end) { 147 return new String (array, offset+start, end-start); 148 } 149 150 156 157 public void copyTo(char[] destination, int destOffset) { 158 System.arraycopy(array, offset, destination, destOffset, count); 159 } 160 161 164 165 public void write(Writer writer) throws java.io.IOException { 166 writer.write(array, offset, count); 167 } 168 169 } 170 171 | Popular Tags |