Debian Codegraph @

net/sf/saxon/serialize/charcode/UTF16CharacterSet.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.serialize.charcode;
9
10 import java.util.function.IntPredicate;
11
12 /**
13 * A class to hold some static constants and methods associated with processing UTF16 and surrogate pairs
14 */
15
16 public class UTF16CharacterSet implements CharacterSet {
17
18 private static UTF16CharacterSet theInstance = new UTF16CharacterSet();
19
20 /**
21 * Private constructor to force the singular instance to be used
22 */
23
24 private UTF16CharacterSet() {
25 }
26
27 /**
28 * Get the singular instance of this class
29 *
30 * @return the singular instance of this classthe singular instance of this class
31 */
32
33 public static UTF16CharacterSet getInstance() {
34 return theInstance;
35 }
36
37 public boolean inCharset(int c) {
38 return true;
39 }
40
41 public String getCanonicalName() {
42 return "UTF-16";
43 }
44
45
46 public static final int NONBMP_MIN = 0x10000;
47 public static final int NONBMP_MAX = 0x10FFFF;
48
49 public static final char SURROGATE1_MIN = 0xD800;
50 public static final char SURROGATE1_MAX = 0xDBFF;
51 public static final char SURROGATE2_MIN = 0xDC00;
52 public static final char SURROGATE2_MAX = 0xDFFF;
53
54 /**
55 * Return the non-BMP character corresponding to a given surrogate pair
56 * surrogates.
57 *
58 * @param high The high surrogate.
59 * @param low The low surrogate.
60 * @return the Unicode codepoint represented by the surrogate pair
61 */
62 public static int combinePair(char high, char low) {
63 return (high - SURROGATE1_MIN) * 0x400 + (low - SURROGATE2_MIN) + NONBMP_MIN;
64 }
65
66 /**
67 * Return the high surrogate of a non-BMP character
68 *
69 * @param ch The Unicode codepoint of the non-BMP character to be divided.
70 * @return the first character in the surrogate pair
71 */
72
73 public static char highSurrogate(int ch) {
74 return (char) (((ch - NONBMP_MIN) >> 10) + SURROGATE1_MIN);
75 }
76
77 /**
78 * Return the low surrogate of a non-BMP character
79 *
80 * @param ch The Unicode codepoint of the non-BMP character to be divided.
81 * @return the second character in the surrogate pair
82 */
83
84 public static char lowSurrogate(int ch) {
85 return (char) (((ch - NONBMP_MIN) & 0x3FF) + SURROGATE2_MIN);
86 }
87
88 /**
89 * Test whether a given character is a surrogate (high or low)
90 *
91 * @param c the character to test
92 * @return true if the character is the high or low half of a surrogate pair
93 */
94
95 public static boolean isSurrogate(int c) {
96 return (c & 0xF800) == 0xD800;
97 }
98
99 /**
100 * Test whether the given character is a high surrogate
101 *
102 * @param ch The character to test.
103 * @return true if the character is the first character in a surrogate pair
104 */
105 public static boolean isHighSurrogate(int ch) {
106 return (SURROGATE1_MIN <= ch && ch <= SURROGATE1_MAX);
107 }
108
109 /**
110 * Test whether the given character is a low surrogate
111 *
112 * @param ch The character to test.
113 * @return true if the character is the second character in a surrogate pair
114 */
115
116 public static boolean isLowSurrogate(int ch) {
117 return (SURROGATE2_MIN <= ch && ch <= SURROGATE2_MAX);
118 }
119
120 /**
121 * Test whether a CharSequence contains any surrogates (i.e. any non-BMP characters
122 *
123 * @param s the string to be tested
124 */
125
126 public static boolean containsSurrogates(/*@NotNull*/ CharSequence s) {
127 for (int i = 0; i < s.length(); i++) {
128 if (isSurrogate(s.charAt(i))) {
129 return true;
130 }
131 }
132 return false;
133 }
134
135 /**
136 * Test whether all the characters in a CharSequence are valid XML characters
137 *
138 * @param chars the character sequence to be tested
139 * @param predicate the predicate that all characters must satisfy
140 * @return the codepoint of the first invalid character in the character sequence (according to the supplied predicate);
141 * or -1 if all characters in the character sequence are valid
142 */
143
144 public static int firstInvalidChar(CharSequence chars, IntPredicate predicate) {
145 for (int c = 0; c < chars.length(); c++) {
146 int ch32 = chars.charAt(c);
147 if (UTF16CharacterSet.isHighSurrogate(ch32)) {
148 char low = chars.charAt(c++);
149 ch32 = UTF16CharacterSet.combinePair((char) ch32, low);
150 }
151 if (!predicate.test(ch32)) {
152 return ch32;
153 }
154 }
155 return -1;
156 }
157
158 public static void main(String[] args) {
159 System.err.println(Integer.toHexString(highSurrogate(0xeffff)));
160 System.err.println(Integer.toHexString(lowSurrogate(0xeffff)));
161 }
162 }
163