| 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 | /** |
| 11 | * An implementation of UnicodeString optimized for strings that contain |
| 12 | * no characters outside the Latin-1 range (i.e. no characters whose codepoints exceed 255). |
| 13 | */ |
| 14 | public final class LatinString extends UnicodeString { |
| 15 | |
| 16 | private byte[] chars; |
| 17 | |
| 18 | public final static LatinString SINGLE_SPACE = new LatinString(new byte[]{(byte) 0x20}); |
| 19 | |
| 20 | /** |
| 21 | * Create a LatinString |
| 22 | * @param src The string value. The caller must ensure that this contains no characters > 255 |
| 23 | */ |
| 24 | public LatinString(CharSequence src) { |
| 25 | int len = src.length(); |
| 26 | chars = new byte[len]; |
| 27 | for (int i=0; i<len; i++) { |
| 28 | chars[i] = (byte) (src.charAt(i) & 0xff); |
| 29 | } |
| 30 | |
| 31 | } |
| 32 | |
| 33 | private LatinString(byte[] chars) { |
| 34 | this.chars = chars; |
| 35 | } |
| 36 | |
| 37 | public LatinString uSubstring(int beginIndex, int endIndex) { |
| 38 | byte[] s = new byte[endIndex - beginIndex]; |
| 39 | System.arraycopy(chars, beginIndex, s, 0, endIndex - beginIndex); |
| 40 | return new LatinString(s); |
| 41 | } |
| 42 | |
| 43 | public int uCharAt(int pos) { |
| 44 | return chars[pos] & 0xff; |
| 45 | } |
| 46 | |
| 47 | public int uIndexOf(int search, int pos) { |
| 48 | if (search > 255) { |
| 49 | return -1; |
| 50 | } else { |
| 51 | for (int i = pos; i < chars.length; i++) { |
| 52 | if ((chars[i] & 0xff) == search) { |
| 53 | return i; |
| 54 | } |
| 55 | } |
| 56 | return -1; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public int uLength() { |
| 61 | return chars.length; |
| 62 | } |
| 63 | |
| 64 | public boolean isEnd(int pos) { |
| 65 | return pos >= chars.length; |
| 66 | } |
| 67 | |
| 68 | public String toString() { |
| 69 | char[] expanded = new char[chars.length]; |
| 70 | for (int i=0; i<chars.length; i++) { |
| 71 | expanded[i] = (char)(chars[i] & 0xff); |
| 72 | } |
| 73 | return new String(expanded); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the length of this character sequence. The length is the number |
| 78 | * of 16-bit <code>char</code>s in the sequence. |
| 79 | * |
| 80 | * @return the number of <code>char</code>s in this sequence |
| 81 | */ |
| 82 | public int length() { |
| 83 | return chars.length; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns the <code>char</code> value at the specified index. An index ranges from zero |
| 88 | * to <tt>length() - 1</tt>. The first <code>char</code> value of the sequence is at |
| 89 | * index zero, the next at index one, and so on, as for array |
| 90 | * indexing. |
| 91 | * <p>If the <code>char</code> value specified by the index is a |
| 92 | * <a href="Character.html#unicode">surrogate</a>, the surrogate |
| 93 | * value is returned.</p> |
| 94 | * |
| 95 | * @param index the index of the <code>char</code> value to be returned |
| 96 | * @return the specified <code>char</code> value |
| 97 | * @throws IndexOutOfBoundsException if the <tt>index</tt> argument is negative or not less than |
| 98 | * <tt>length()</tt> |
| 99 | */ |
| 100 | public char charAt(int index) { |
| 101 | return (char)(chars[index] & 0xff); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns a new <code>CharSequence</code> that is a subsequence of this sequence. |
| 106 | * The subsequence starts with the <code>char</code> value at the specified index and |
| 107 | * ends with the <code>char</code> value at index <tt>end - 1</tt>. The length |
| 108 | * (in <code>char</code>s) of the |
| 109 | * returned sequence is <tt>end - start</tt>, so if <tt>start == end</tt> |
| 110 | * then an empty sequence is returned. |
| 111 | * |
| 112 | * @param start the start index, inclusive |
| 113 | * @param end the end index, exclusive |
| 114 | * @return the specified subsequence |
| 115 | * @throws IndexOutOfBoundsException if <tt>start</tt> or <tt>end</tt> are negative, |
| 116 | * if <tt>end</tt> is greater than <tt>length()</tt>, |
| 117 | * or if <tt>start</tt> is greater than <tt>end</tt> |
| 118 | */ |
| 119 | public CharSequence subSequence(int start, int end) { |
| 120 | return uSubstring(start, end); |
| 121 | } |
| 122 | |
| 123 | } |