Debian Codegraph @

src/com/google/debugging/sourcemap/Util.java

raw symbols SCIP
1 /*
2 * Copyright 2011 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.google.debugging.sourcemap;
17
18 import java.io.IOException;
19 import java.nio.charset.CharsetEncoder;
20
21 /**
22 * @author [email protected] (John Lenz)
23 */
24 class Util {
25
26 private static final char[] HEX_CHARS
27 = { '0', '1', '2', '3', '4', '5', '6', '7',
28 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
29
30 /**
31 * Escapes the given string to a double quoted (") JavaScript/JSON string
32 */
33 static String escapeString(String s) {
34 return escapeString(s, '"', "\\\"", "\'", "\\\\", null);
35 }
36
37 /** Helper to escape JavaScript string as well as regular expression */
38 static String escapeString(String s, char quote,
39 String doublequoteEscape,
40 String singlequoteEscape,
41 String backslashEscape,
42 CharsetEncoder outputCharsetEncoder) {
43 StringBuilder sb = new StringBuilder(s.length() + 2);
44 sb.append(quote);
45 for (int i = 0; i < s.length(); i++) {
46 char c = s.charAt(i);
47 switch (c) {
48 case '\n': sb.append("\\n"); break;
49 case '\r': sb.append("\\r"); break;
50 case '\t': sb.append("\\t"); break;
51 case '\\': sb.append(backslashEscape); break;
52 case '\"': sb.append(doublequoteEscape); break;
53 case '\'': sb.append(singlequoteEscape); break;
54 case '>': // Break --> into --\> or ]]> into ]]\>
55 if (i >= 2 &&
56 ((s.charAt(i - 1) == '-' && s.charAt(i - 2) == '-') ||
57 (s.charAt(i - 1) == ']' && s.charAt(i - 2) == ']'))) {
58 sb.append("\\>");
59 } else {
60 sb.append(c);
61 }
62 break;
63 case '<':
64 // Break </script into <\/script
65 final String END_SCRIPT = "/script";
66
67 // Break <!-- into <\!--
68 final String START_COMMENT = "!--";
69
70 if (s.regionMatches(true, i + 1, END_SCRIPT, 0,
71 END_SCRIPT.length())) {
72 sb.append("<\\");
73 } else if (s.regionMatches(false, i + 1, START_COMMENT, 0,
74 START_COMMENT.length())) {
75 sb.append("<\\");
76 } else {
77 sb.append(c);
78 }
79 break;
80 default:
81 // If we're given an outputCharsetEncoder, then check if the
82 // character can be represented in this character set.
83 if (outputCharsetEncoder != null) {
84 if (outputCharsetEncoder.canEncode(c)) {
85 sb.append(c);
86 } else {
87 // Unicode-escape the character.
88 appendCharAsHex(sb, c);
89 }
90 } else {
91 // No charsetEncoder provided - pass straight Latin characters
92 // through, and escape the rest. Doing the explicit character
93 // check is measurably faster than using the CharsetEncoder.
94 if (c > 0x1f && c <= 0x7f) {
95 sb.append(c);
96 } else {
97 // Other characters can be misinterpreted by some JS parsers,
98 // or perhaps mangled by proxies along the way,
99 // so we play it safe and Unicode escape them.
100 appendCharAsHex(sb, c);
101 }
102 }
103 }
104 }
105 sb.append(quote);
106 return sb.toString();
107 }
108
109 /**
110 * @see #appendHexJavaScriptRepresentation(Appendable, int)
111 */
112 @SuppressWarnings("cast")
113 private static void appendCharAsHex(
114 StringBuilder sb, char c) {
115 try {
116 appendHexJavaScriptRepresentation(sb, (int)c);
117 } catch (IOException ex) {
118 // StringBuilder does not throw IOException.
119 throw new RuntimeException(ex);
120 }
121 }
122
123 /**
124 * Returns a JavaScript representation of the character in a hex escaped
125 * format.
126 * @param out The buffer to which the hex representation should be appended.
127 * @param codePoint The code point to append.
128 */
129 private static void appendHexJavaScriptRepresentation(
130 Appendable out, int codePoint)
131 throws IOException {
132 if (Character.isSupplementaryCodePoint(codePoint)) {
133 // Handle supplementary Unicode values which are not representable in
134 // JavaScript. We deal with these by escaping them as two 4B sequences
135 // so that they will round-trip properly when sent from Java to JavaScript
136 // and back.
137 char[] surrogates = Character.toChars(codePoint);
138 appendHexJavaScriptRepresentation(out, surrogates[0]);
139 appendHexJavaScriptRepresentation(out, surrogates[1]);
140 return;
141 }
142 out.append("\\u")
143 .append(HEX_CHARS[(codePoint >>> 12) & 0xf])
144 .append(HEX_CHARS[(codePoint >>> 8) & 0xf])
145 .append(HEX_CHARS[(codePoint >>> 4) & 0xf])
146 .append(HEX_CHARS[codePoint & 0xf]);
147 }
148
149 }