• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012, Google Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 package org.jf.util;
33 
34 import java.io.IOException;
35 import java.io.Writer;
36 
37 public class StringUtils {
writeEscapedChar(Writer writer, char c)38     public static void writeEscapedChar(Writer writer, char c) throws IOException {
39         if ((c >= ' ') && (c < 0x7f)) {
40             if ((c == '\'') || (c == '\"') || (c == '\\')) {
41                 writer.write('\\');
42             }
43             writer.write(c);
44             return;
45         } else if (c <= 0x7f) {
46             switch (c) {
47                 case '\n': writer.write("\\n"); return;
48                 case '\r': writer.write("\\r"); return;
49                 case '\t': writer.write("\\t"); return;
50             }
51         }
52 
53         writer.write("\\u");
54         writer.write(Character.forDigit(c >> 12, 16));
55         writer.write(Character.forDigit((c >> 8) & 0x0f, 16));
56         writer.write(Character.forDigit((c >> 4) & 0x0f, 16));
57         writer.write(Character.forDigit(c & 0x0f, 16));
58     }
59 
writeEscapedString(Writer writer, String value)60     public static void writeEscapedString(Writer writer, String value) throws IOException {
61         for (int i = 0; i < value.length(); i++) {
62             char c = value.charAt(i);
63 
64             if ((c >= ' ') && (c < 0x7f)) {
65                 if ((c == '\'') || (c == '\"') || (c == '\\')) {
66                     writer.write('\\');
67                 }
68                 writer.write(c);
69                 continue;
70             } else if (c <= 0x7f) {
71                 switch (c) {
72                     case '\n': writer.write("\\n"); continue;
73                     case '\r': writer.write("\\r"); continue;
74                     case '\t': writer.write("\\t"); continue;
75                 }
76             }
77 
78             writer.write("\\u");
79             writer.write(Character.forDigit(c >> 12, 16));
80             writer.write(Character.forDigit((c >> 8) & 0x0f, 16));
81             writer.write(Character.forDigit((c >> 4) & 0x0f, 16));
82             writer.write(Character.forDigit(c & 0x0f, 16));
83         }
84     }
85 
escapeString(String value)86     public static String escapeString(String value) {
87         int len = value.length();
88         StringBuilder sb = new StringBuilder(len * 3 / 2);
89 
90         for (int i = 0; i < len; i++) {
91             char c = value.charAt(i);
92 
93             if ((c >= ' ') && (c < 0x7f)) {
94                 if ((c == '\'') || (c == '\"') || (c == '\\')) {
95                     sb.append('\\');
96                 }
97                 sb.append(c);
98                 continue;
99             } else if (c <= 0x7f) {
100                 switch (c) {
101                     case '\n': sb.append("\\n"); continue;
102                     case '\r': sb.append("\\r"); continue;
103                     case '\t': sb.append("\\t"); continue;
104                 }
105             }
106 
107             sb.append("\\u");
108             sb.append(Character.forDigit(c >> 12, 16));
109             sb.append(Character.forDigit((c >> 8) & 0x0f, 16));
110             sb.append(Character.forDigit((c >> 4) & 0x0f, 16));
111             sb.append(Character.forDigit(c & 0x0f, 16));
112         }
113 
114         return sb.toString();
115     }
116 }
117