Debian Codegraph @

net/sf/saxon/serialize/charcode/UTF8CharacterSet.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 /**
11 * This class defines properties of the UTF-8 character set
12 */
13
14 public final class UTF8CharacterSet implements CharacterSet {
15
16 private static UTF8CharacterSet theInstance = new UTF8CharacterSet();
17
18 /**
19 * Private constructor to force the singular instance to be used
20 */
21
22 private UTF8CharacterSet() {
23 }
24
25 /**
26 * Get the singular instance of this class
27 *
28 * @return the singular instance of this classthe singular instance of this class
29 */
30
31 public static UTF8CharacterSet getInstance() {
32 return theInstance;
33 }
34
35 public boolean inCharset(int c) {
36 return true;
37 }
38
39 /*@NotNull*/
40 public String getCanonicalName() {
41 return "UTF-8";
42 }
43
44 /**
45 * Static method to generate the UTF-8 representation of a Unicode character
46 *
47 * @param in the Unicode character, or the high half of a surrogate pair
48 * @param in2 the low half of a surrogate pair (ignored unless the first argument is in the
49 * range for a surrogate pair)
50 * @param out an array of at least 4 bytes to hold the UTF-8 representation.
51 * @return the number of bytes in the UTF-8 representation
52 */
53
54 public static int getUTF8Encoding(char in, char in2, byte[] out) {
55 // See Tony Graham, "Unicode, a Primer", page 92
56 int i = (int) in;
57 if (i <= 0x7f) {
58 out[0] = (byte) i;
59 return 1;
60 } else if (i <= 0x7ff) {
61 out[0] = (byte) (0xc0 | ((in >> 6) & 0x1f));
62 out[1] = (byte) (0x80 | (in & 0x3f));
63 return 2;
64 } else if (i >= 0xd800 && i <= 0xdbff) {
65 // surrogate pair
66 int j = (int) in2;
67 if (!(j >= 0xdc00 && j <= 0xdfff)) {
68 throw new IllegalArgumentException("Malformed Unicode Surrogate Pair (" + i + ',' + j + ')');
69 }
70 byte xxxxxx = (byte) (j & 0x3f);
71 byte yyyyyy = (byte) (((i & 0x03) << 4) | ((j >> 6) & 0x0f));
72 byte zzzz = (byte) ((i >> 2) & 0x0f);
73 byte uuuuu = (byte) (((i >> 6) & 0x0f) + 1);
74 out[0] = (byte) (0xf0 | ((uuuuu >> 2) & 0x07));
75 out[1] = (byte) (0x80 | ((uuuuu & 0x03) << 4) | zzzz);
76 out[2] = (byte) (0x80 | yyyyyy);
77 out[3] = (byte) (0x80 | xxxxxx);
78 return 4;
79 } else if (i >= 0xdc00 && i <= 0xdfff) {
80 // second half of surrogate pair - ignore it
81 return 0;
82 } else {
83 out[0] = (byte) (0xe0 | ((in >> 12) & 0x0f));
84 out[1] = (byte) (0x80 | ((in >> 6) & 0x3f));
85 out[2] = (byte) (0x80 | (in & 0x3f));
86 return 3;
87 }
88 }
89
90 /**
91 * Decode a UTF8 character
92 *
93 * @param in array of bytes representing a single UTF-8 encoded character
94 * @param used number of bytes in the array that are actually used
95 * @return the Unicode codepoint of this character
96 * @throws IllegalArgumentException if the byte sequence is not a valid UTF-8 representation
97 */
98
99 public static int decodeUTF8(byte[] in, int used) throws IllegalArgumentException {
100 int bottom = 0;
101 for (int i = 1; i < used; i++) {
102 if ((in[i] & 0xc0) != 0x80) {
103 throw new IllegalArgumentException("Byte " + (i + 1) + " in UTF-8 sequence has wrong top bits");
104 }
105 bottom = (bottom << 6) + (in[i] & 0x3f);
106 }
107 if ((in[0] & 0x80) == 0) {
108 // single byte sequence 0xxxxxxx
109 if (used == 1) {
110 return in[0];
111 } else {
112 throw new IllegalArgumentException("UTF8 single byte expected");
113 }
114 } else if ((in[0] & 0xe0) == 0xc0) {
115 // two byte sequence
116 if (used != 2) {
117 throw new IllegalArgumentException("UTF8 sequence of two bytes expected");
118 }
119 return ((in[0] & 0x1f) << 6) + bottom;
120 } else if ((in[0] & 0xf0) == 0xe0) {
121 // three byte sequence
122 if (used != 3) {
123 throw new IllegalArgumentException("UTF8 sequence of three bytes expected");
124 }
125 return ((in[0] & 0x0f) << 12) + bottom;
126 } else if ((in[0] & 0xf8) == 0xf8) {
127 // four-byte sequence 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
128 if (used != 4) {
129 throw new IllegalArgumentException("UTF8 sequence of four bytes expected");
130 }
131 return ((in[0] & 0x07) << 24) + bottom;
132 } else {
133 throw new IllegalArgumentException("UTF8 invalid first byte");
134 }
135 }
136
137
138 }
139