| 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.regex; |
| 9 | |
| 10 | import net.sf.saxon.value.*; |
| 11 | |
| 12 | /** |
| 13 | * A Unicode string which, in general, may contain non-BMP characters (that is, codepoints |
| 14 | * outside the range 0-65535) |
| 15 | */ |
| 16 | |
| 17 | public final class GeneralUnicodeString extends UnicodeString { |
| 18 | |
| 19 | private int[] chars; |
| 20 | private int start; |
| 21 | private int end; |
| 22 | private CharSequence charSequence; |
| 23 | |
| 24 | public GeneralUnicodeString(CharSequence in) { |
| 25 | chars = net.sf.saxon.value.StringValue.expand(in); |
| 26 | start = 0; |
| 27 | end = chars.length; |
| 28 | charSequence = in; |
| 29 | } |
| 30 | |
| 31 | GeneralUnicodeString(int[] chars, int start, int end) { |
| 32 | this.chars = chars; |
| 33 | this.start = start; |
| 34 | this.end = end; |
| 35 | } |
| 36 | |
| 37 | public UnicodeString uSubstring(int beginIndex, int endIndex) { |
| 38 | if (endIndex > chars.length) { |
| 39 | throw new IndexOutOfBoundsException("endIndex=" + endIndex |
| 40 | + "; sequence size=" + chars.length); |
| 41 | } |
| 42 | if (beginIndex < 0 || beginIndex > endIndex) { |
| 43 | throw new IndexOutOfBoundsException("beginIndex=" + beginIndex |
| 44 | + "; endIndex=" + endIndex); |
| 45 | } |
| 46 | return new GeneralUnicodeString(chars, start + beginIndex, start + endIndex); |
| 47 | } |
| 48 | |
| 49 | public int uCharAt(int pos) { |
| 50 | return chars[start + pos]; |
| 51 | } |
| 52 | |
| 53 | public int uIndexOf(int search, int pos) { |
| 54 | for (int i = pos; i < uLength(); i++) { |
| 55 | if (chars[start + i] == search) { |
| 56 | return i; |
| 57 | } |
| 58 | } |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | public int uLength() { |
| 63 | return end - start; |
| 64 | } |
| 65 | |
| 66 | public boolean isEnd(int pos) { |
| 67 | return pos >= (end - start); |
| 68 | } |
| 69 | |
| 70 | public String toString() { |
| 71 | obtainCharSequence(); |
| 72 | charSequence = charSequence.toString(); |
| 73 | return (String)charSequence; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get a CharSequence representing this string. This is a memo function; the result is saved for |
| 78 | * use if needed again. The CharSequence returned is one that has efficient support for operations |
| 79 | * such as charAt(p) where non-BMP characters are represented as surrogate pairs. |
| 80 | * @return a CharSequence representing the same string, with efficient positional access to |
| 81 | * UTF16 codepoints. |
| 82 | */ |
| 83 | |
| 84 | private CharSequence obtainCharSequence() { |
| 85 | if (charSequence == null) { |
| 86 | int[] c = chars; |
| 87 | if (start != 0) { |
| 88 | c = new int[end - start]; |
| 89 | System.arraycopy(chars, start, c, 0, end - start); |
| 90 | } |
| 91 | charSequence = StringValue.contract(c, end - start); |
| 92 | } |
| 93 | return charSequence; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns the length of this character sequence. The length is the number |
| 98 | * of 16-bit <code>char</code>s in the sequence. |
| 99 | * |
| 100 | * @return the number of <code>char</code>s in this sequence |
| 101 | */ |
| 102 | public int length() { |
| 103 | return obtainCharSequence().length(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns the <code>char</code> value at the specified index. An index ranges from zero |
| 108 | * to <tt>length() - 1</tt>. The first <code>char</code> value of the sequence is at |
| 109 | * index zero, the next at index one, and so on, as for array |
| 110 | * indexing. |
| 111 | * <p>If the <code>char</code> value specified by the index is a |
| 112 | * <a href="Character.html#unicode">surrogate</a>, the surrogate |
| 113 | * value is returned.</p> |
| 114 | * |
| 115 | * @param index the index of the <code>char</code> value to be returned |
| 116 | * @return the specified <code>char</code> value |
| 117 | * @throws IndexOutOfBoundsException if the <tt>index</tt> argument is negative or not less than |
| 118 | * <tt>length()</tt> |
| 119 | */ |
| 120 | public char charAt(int index) { |
| 121 | return obtainCharSequence().charAt(index); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns a new <code>CharSequence</code> that is a subsequence of this sequence. |
| 126 | * The subsequence starts with the <code>char</code> value at the specified index and |
| 127 | * ends with the <code>char</code> value at index <tt>end - 1</tt>. The length |
| 128 | * (in <code>char</code>s) of the |
| 129 | * returned sequence is <tt>end - start</tt>, so if <tt>start == end</tt> |
| 130 | * then an empty sequence is returned. |
| 131 | * |
| 132 | * @param start the start index, inclusive |
| 133 | * @param end the end index, exclusive |
| 134 | * @return the specified subsequence |
| 135 | * @throws IndexOutOfBoundsException if <tt>start</tt> or <tt>end</tt> are negative, |
| 136 | * if <tt>end</tt> is greater than <tt>length()</tt>, |
| 137 | * or if <tt>start</tt> is greater than <tt>end</tt> |
| 138 | */ |
| 139 | public CharSequence subSequence(int start, int end) { |
| 140 | return obtainCharSequence().subSequence(start, end); |
| 141 | } |
| 142 | } |