1 /*
2 * Copyright 2007, 2008 Ange Optimization ApS
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 eu.simuline.octave.util;
17
18 import java.util.Map;
19 import java.util.HashMap;
20
21 /**
22 * Class for holding static utility functions for string handling: quoting
23 * and for parsing numbers.
24 *
25 * @author Kim Hansen
26 */
27 public final class StringUtil {
28
29 private static final Map<Character, String> CHAR2STR;
30
31 static {
32 CHAR2STR = new HashMap<Character, String>();
33 CHAR2STR.put('"', "\\\"");
34 CHAR2STR.put('\\', "\\\\");
35 CHAR2STR.put('\n', "\\n");
36 CHAR2STR.put('\r', "\\r");
37 CHAR2STR.put('\f', "\\f");
38 CHAR2STR.put('\b', "\\b");
39 CHAR2STR.put('\t', "\\t");
40 } // static
41
42 private StringUtil() {
43 }
44
45 @SuppressWarnings("checkstyle:magicnumber")
46 private static void appendChar(StringBuffer buf, char c) {
47 String str = CHAR2STR.get(c);
48 if (str != null) {
49 buf.append(str);
50 return;
51 }
52 if (c < 0x20) { // NOPMD
53 buf.append("\\u00");
54 int x = c / 0x10;
55 buf.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
56 x = c & 0xF;
57 buf.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A'));
58 } else {
59 buf.append(c);
60 }
61 }
62
63 /**
64 * Quotes string as Java Language string literal.
65 * Returns the string "<code>null</code>" (with length 4)
66 * if <code>str</code> is <code>null</code>.
67 *
68 * Code taken from http://freemarker.sourceforge.net/
69 *
70 * @param str
71 * @return the string encoded and quoted
72 */
73 public static String jQuote(final String str) {
74 if (str == null) {
75 return "null";
76 }
77 final int len = str.length();
78 final StringBuffer buf = new StringBuffer(len + 4);
79 buf.append('"');
80 for (int i = 0; i < len; i++) {
81 appendChar(buf, str.charAt(i));
82 } // for each characters
83 buf.append('"');
84 return buf.toString();
85 }
86
87 /**
88 * Quotes the first <code>len</code> characters of <code>cbuf</code>
89 * as Java Language string literal.
90 * Returns string <code>null</code> if <code>s</code> is <code>null</code>.
91 *
92 * @param cbuf
93 * the buffer
94 * @param len
95 * How much of the buffer to quote
96 * @return the string encoded and quoted:
97 * Starts and ends with <code>"</code>
98 */
99 public static String jQuote(final char[] cbuf, final int len) {
100 final StringBuffer buf = new StringBuffer(len + 4);
101 buf.append('"');
102 for (int i = 0; i < len; i++) {
103 appendChar(buf, cbuf[i]);
104 } // for each characters
105 buf.append('"');
106 return buf.toString();
107 }
108
109 // does not fit: whereas parseBoolean throws OctaveParseExeption,
110 // parseDouble does not.
111 public static boolean parseBoolean(String line) {
112 line = line.trim();
113 if ("0".equals(line)) {
114 return false;
115 }
116 if ("1".equals(line)) {
117 return true;
118 }
119 throw new NumberFormatException("Invalid input, '" + line + "'");
120 //throw new OctaveParseException("Invalid input, '" + line + "'");
121 }
122
123 /**
124 * Returns the string representation of booleans in octave:
125 * 1 for true and 0 for false.
126 */
127 public static String toString(boolean bool) {
128 return bool ? "1" : "0";
129 }
130
131 /**
132 * Returns the string representation of the object <code>obj</code>
133 * which is defined also for null returning just "null".
134 */
135 public static <T> String toString(T obj) {
136 return obj == null ? "null" : obj.toString();
137 }
138
139 /**
140 * This is almost the same as Double.parseDouble(),
141 * but it handles a few more versions of infinity.
142 *
143 * @param string
144 * @return The parsed Double
145 */
146 public static double parseDouble(final String string) {
147 if ("Inf".equals(string)) {
148 return Double.POSITIVE_INFINITY;
149 }
150 if ("-Inf".equals(string)) {
151 return Double.NEGATIVE_INFINITY;
152 }
153 return Double.parseDouble(string);
154 }
155 }