| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package eu.simuline.octave.util; |
| 17 | |
|
| 18 | |
import java.util.Map; |
| 19 | |
import java.util.HashMap; |
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
public final class StringUtil { |
| 28 | |
|
| 29 | |
private static final Map<Character, String> CHAR2STR; |
| 30 | |
|
| 31 | |
static { |
| 32 | 2 | CHAR2STR = new HashMap<Character, String>(); |
| 33 | 2 | CHAR2STR.put('"', "\\\""); |
| 34 | 2 | CHAR2STR.put('\\', "\\\\"); |
| 35 | 2 | CHAR2STR.put('\n', "\\n"); |
| 36 | 2 | CHAR2STR.put('\r', "\\r"); |
| 37 | 2 | CHAR2STR.put('\f', "\\f"); |
| 38 | 2 | CHAR2STR.put('\b', "\\b"); |
| 39 | 2 | CHAR2STR.put('\t', "\\t"); |
| 40 | 2 | } // static |
| 41 | |
|
| 42 | 0 | private StringUtil() { |
| 43 | 0 | } |
| 44 | |
|
| 45 | |
@SuppressWarnings("checkstyle:magicnumber") |
| 46 | |
private static void appendChar(StringBuffer buf, char c) { |
| 47 | 0 | String str = CHAR2STR.get(c); |
| 48 | 0 | if (str != null) { |
| 49 | 0 | buf.append(str); |
| 50 | 0 | return; |
| 51 | |
} |
| 52 | 0 | if (c < 0x20) { // NOPMD |
| 53 | 0 | buf.append("\\u00"); |
| 54 | 0 | int x = c / 0x10; |
| 55 | 0 | buf.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A')); |
| 56 | 0 | x = c & 0xF; |
| 57 | 0 | buf.append((char) (x < 0xA ? x + '0' : x - 0xA + 'A')); |
| 58 | 0 | } else { |
| 59 | 0 | buf.append(c); |
| 60 | |
} |
| 61 | 0 | } |
| 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 | 0 | if (str == null) { |
| 75 | 0 | return "null"; |
| 76 | |
} |
| 77 | 0 | final int len = str.length(); |
| 78 | 0 | final StringBuffer buf = new StringBuffer(len + 4); |
| 79 | 0 | buf.append('"'); |
| 80 | 0 | for (int i = 0; i < len; i++) { |
| 81 | 0 | appendChar(buf, str.charAt(i)); |
| 82 | |
} // for each characters |
| 83 | 0 | buf.append('"'); |
| 84 | 0 | 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 | 0 | final StringBuffer buf = new StringBuffer(len + 4); |
| 101 | 0 | buf.append('"'); |
| 102 | 0 | for (int i = 0; i < len; i++) { |
| 103 | 0 | appendChar(buf, cbuf[i]); |
| 104 | |
} |
| 105 | 0 | buf.append('"'); |
| 106 | 0 | return buf.toString(); |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public static boolean parseBoolean(String line) { |
| 112 | 228 | line = line.trim(); |
| 113 | 228 | if ("0".equals(line)) { |
| 114 | 144 | return false; |
| 115 | |
} |
| 116 | 84 | if ("1".equals(line)) { |
| 117 | 84 | return true; |
| 118 | |
} |
| 119 | 0 | throw new NumberFormatException("Invalid input, '" + line + "'"); |
| 120 | |
|
| 121 | |
} |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public static String toString(boolean bool) { |
| 128 | 386 | return bool ? "1" : "0"; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public static <T> String toString(T obj) { |
| 136 | 0 | return obj == null ? "null" : obj.toString(); |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
public static double parseDouble(final String string) { |
| 147 | 6716 | if ("Inf".equals(string)) { |
| 148 | 2 | return Double.POSITIVE_INFINITY; |
| 149 | |
} |
| 150 | 6714 | if ("-Inf".equals(string)) { |
| 151 | 2 | return Double.NEGATIVE_INFINITY; |
| 152 | |
} |
| 153 | 6712 | return Double.parseDouble(string); |
| 154 | |
} |
| 155 | |
} |