Debian Codegraph @

net/sf/saxon/regex/BMPString.java

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