| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.tree.tiny; |
| 9 | |
| 10 | import net.sf.saxon.tree.util.FastStringBuffer; |
| 11 | |
| 12 | import java.io.Writer; |
| 13 | |
| 14 | /** |
| 15 | * This is an implementation of the CharSequence interface: it implements |
| 16 | * a CharSequence as a view of an array. The implementation relies on the array |
| 17 | * being immutable: as a minimum, the caller is required to ensure that the array |
| 18 | * contents will not change so long as the CharSlice remains in existence. |
| 19 | * <p>This class should be more efficient than String because it avoids copying the |
| 20 | * characters unnecessarily.</p> |
| 21 | * <p>The methods in the class don't check their arguments. Incorrect arguments will |
| 22 | * generally result in exceptions from lower-level classes.</p> |
| 23 | */ |
| 24 | public final class CharSlice implements CharSequence { |
| 25 | |
| 26 | private char[] array; |
| 27 | private int offset; |
| 28 | private int count; |
| 29 | |
| 30 | /** |
| 31 | * Create a CharSlice that maps to the whole of a char[] array |
| 32 | * |
| 33 | * @param array the char[] array |
| 34 | */ |
| 35 | |
| 36 | public CharSlice(char[] array) { |
| 37 | this.array = array; |
| 38 | offset = 0; |
| 39 | count = array.length; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Create a CharSlice that maps to a section of a char[] array |
| 44 | * |
| 45 | * @param array the char[] array |
| 46 | * @param start position of the first character to be included |
| 47 | * @param length number of characters to be included |
| 48 | */ |
| 49 | |
| 50 | public CharSlice(char[] array, int start, int length) { |
| 51 | this.array = array; |
| 52 | offset = start; |
| 53 | count = length; |
| 54 | if (start + length > array.length) { |
| 55 | throw new IndexOutOfBoundsException("start(" + start + |
| 56 | ") + length(" + length + ") > size(" + array.length + ')'); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the length of this character sequence. The length is the number |
| 62 | * of 16-bit Unicode characters in the sequence. |
| 63 | * |
| 64 | * @return the number of characters in this sequence |
| 65 | */ |
| 66 | public int length() { |
| 67 | return count; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set the length of this character sequence, without changing the array and start offset |
| 72 | * to which it is bound |
| 73 | * |
| 74 | * @param length the new length of the CharSlice (which must be less than the existing length, |
| 75 | * though this is not enforced) |
| 76 | */ |
| 77 | public void setLength(int length) { |
| 78 | count = length; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the character at the specified index. An index ranges from zero |
| 83 | * to <tt>length() - 1</tt>. The first character of the sequence is at |
| 84 | * index zero, the next at index one, and so on, as for array |
| 85 | * indexing. |
| 86 | * |
| 87 | * @param index the index of the character to be returned |
| 88 | * @return the specified character |
| 89 | * @throws java.lang.IndexOutOfBoundsException |
| 90 | * if the <tt>index</tt> argument is negative or not less than |
| 91 | * <tt>length()</tt> |
| 92 | */ |
| 93 | public char charAt(int index) { |
| 94 | return array[offset + index]; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns a new character sequence that is a subsequence of this sequence. |
| 99 | * The subsequence starts with the character at the specified index and |
| 100 | * ends with the character at index <tt>end - 1</tt>. The length of the |
| 101 | * returned sequence is <tt>end - start</tt>, so if <tt>start == end</tt> |
| 102 | * then an empty sequence is returned. |
| 103 | * |
| 104 | * @param start the start index, inclusive |
| 105 | * @param end the end index, exclusive |
| 106 | * @return the specified subsequence |
| 107 | * @throws java.lang.IndexOutOfBoundsException |
| 108 | * if <tt>start</tt> or <tt>end</tt> are negative, |
| 109 | * if <tt>end</tt> is greater than <tt>length()</tt>, |
| 110 | * or if <tt>start</tt> is greater than <tt>end</tt> |
| 111 | */ |
| 112 | public CharSequence subSequence(int start, int end) { |
| 113 | return new CharSlice(array, offset + start, end - start); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Convert to a string |
| 118 | */ |
| 119 | |
| 120 | public String toString() { |
| 121 | return new String(array, offset, count); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Compare equality |
| 126 | */ |
| 127 | |
| 128 | public boolean equals(Object other) { |
| 129 | if (other instanceof CharSlice) { |
| 130 | CharSlice cs2 = (CharSlice) other; |
| 131 | if (count != cs2.count) { |
| 132 | return false; |
| 133 | } |
| 134 | int limit = offset + count; |
| 135 | for (int j = offset, k = cs2.offset; j < limit; ) { |
| 136 | if (array[j++] != cs2.array[k++]) { |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | return true; |
| 141 | } else if (other instanceof CharSequence) { |
| 142 | return count == ((CharSequence) other).length() && toString().equals(other.toString()); |
| 143 | } |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Generate a hash code |
| 149 | */ |
| 150 | |
| 151 | public int hashCode() { |
| 152 | // Same algorithm as String#hashCode(), but not cached |
| 153 | int end = offset + count; |
| 154 | int h = 0; |
| 155 | for (int i = offset; i < end; i++) { |
| 156 | h = 31 * h + array[i]; |
| 157 | } |
| 158 | return h; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get the index of a specific character in the sequence. Returns -1 if not found. |
| 163 | * This method mimics {@link String#indexOf} |
| 164 | * |
| 165 | * @param c the character to be found |
| 166 | * @return the position of the first occurrence of that character, or -1 if not found. |
| 167 | */ |
| 168 | |
| 169 | public int indexOf(char c) { |
| 170 | int end = offset + count; |
| 171 | for (int i = offset; i < end; i++) { |
| 172 | if (array[i] == c) { |
| 173 | return i - offset; |
| 174 | } |
| 175 | } |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Returns a new character sequence that is a subsequence of this sequence. |
| 181 | * Unlike subSequence, this is guaranteed to return a String. |
| 182 | * |
| 183 | * @param start position of the first character to be included (relative to the |
| 184 | * start of the CharSlice, not the underlying array) |
| 185 | * @param end position of the first character <b>not</b> to be included (relative |
| 186 | * to the start of the CharSlice) |
| 187 | * @return the substring, as a String object |
| 188 | */ |
| 189 | |
| 190 | public String substring(int start, int end) { |
| 191 | return new String(array, offset + start, end - start); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Append the contents to another array at a given offset. The caller is responsible |
| 196 | * for ensuring that sufficient space is available. |
| 197 | * |
| 198 | * @param destination the array to which the characters will be copied |
| 199 | * @param destOffset the offset in the target array where the copy will start |
| 200 | */ |
| 201 | |
| 202 | public void copyTo(char[] destination, int destOffset) { |
| 203 | System.arraycopy(array, offset, destination, destOffset, count); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Append the contents to another array at a given offset. The caller is responsible |
| 208 | * for ensuring that sufficient space is available. Otherwise this behaves like String.getChars() |
| 209 | * |
| 210 | * @param start offset of first character to be copied |
| 211 | * @param end offset of the first character that is not copied |
| 212 | * @param destination the array to which the characters will be copied |
| 213 | * @param destOffset the offset in the target array where the copy will start |
| 214 | */ |
| 215 | |
| 216 | public void getChars(int start, int end, char[] destination, int destOffset) { |
| 217 | System.arraycopy(array, offset + start, destination, destOffset, end - start); |
| 218 | } |
| 219 | |
| 220 | public char[] toCharArray() { |
| 221 | char[] chars = new char[count]; |
| 222 | System.arraycopy(array, offset, chars, 0, count); |
| 223 | return chars; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Utility method to turn a CharSequence into an array of chars |
| 228 | * @param in the CharSequence |
| 229 | * @return the corresponding array of chars |
| 230 | */ |
| 231 | |
| 232 | public static char[] toCharArray(CharSequence in) { |
| 233 | if (in instanceof String) { |
| 234 | return ((String) in).toCharArray(); |
| 235 | } else if (in instanceof CharSlice) { |
| 236 | return ((CharSlice) in).toCharArray(); |
| 237 | } else if (in instanceof FastStringBuffer) { |
| 238 | return ((FastStringBuffer) in).toCharArray(); |
| 239 | } else { |
| 240 | return in.toString().toCharArray(); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Write the value to a writer |
| 246 | * |
| 247 | * @param writer the writer to be written to |
| 248 | * @throws java.io.IOException if writing fails |
| 249 | */ |
| 250 | |
| 251 | public void write(/*@NotNull*/ Writer writer) throws java.io.IOException { |
| 252 | writer.write(array, offset, count); |
| 253 | } |
| 254 | |
| 255 | } |
| 256 |