jpayne@68
|
1 package shared;
|
jpayne@68
|
2
|
jpayne@68
|
3 import structures.ByteBuilder;
|
jpayne@68
|
4 import structures.LongList;
|
jpayne@68
|
5
|
jpayne@68
|
6 public class Parse {
|
jpayne@68
|
7
|
jpayne@68
|
8
|
jpayne@68
|
9 public static int parseIntKMG(String b){
|
jpayne@68
|
10 long x=parseKMG(b);
|
jpayne@68
|
11 assert(x<=Integer.MAX_VALUE && x>Integer.MIN_VALUE) : "Value "+x+" is out of range for integers: "+b;
|
jpayne@68
|
12 return (int)x;
|
jpayne@68
|
13 }
|
jpayne@68
|
14
|
jpayne@68
|
15 public static long parseKMG(String b){
|
jpayne@68
|
16 if(b==null){return 0;}
|
jpayne@68
|
17 assert(b.length()>0);
|
jpayne@68
|
18 final char c=Tools.toLowerCase(b.charAt(b.length()-1));
|
jpayne@68
|
19 final boolean dot=b.indexOf('.')>=0;
|
jpayne@68
|
20 if(!dot && !Tools.isLetter(c)){return Long.parseLong(b);}
|
jpayne@68
|
21 // if(!Tools.isLetter(c) && !dot){return Long.parseLong(b);}
|
jpayne@68
|
22
|
jpayne@68
|
23 if(b.equalsIgnoreCase("big") || b.equalsIgnoreCase("inf") || b.equalsIgnoreCase("infinity") || b.equalsIgnoreCase("max") || b.equalsIgnoreCase("huge")){
|
jpayne@68
|
24 return Long.MAX_VALUE;
|
jpayne@68
|
25 }
|
jpayne@68
|
26
|
jpayne@68
|
27 long mult=1;
|
jpayne@68
|
28 if(Tools.isLetter(c)){
|
jpayne@68
|
29 if(c=='k'){mult=1000;}
|
jpayne@68
|
30 else if(c=='m'){mult=1000000;}
|
jpayne@68
|
31 else if(c=='g' || c=='b'){mult=1000000000;}
|
jpayne@68
|
32 else if(c=='t'){mult=1000000000000L;}
|
jpayne@68
|
33 else if(c=='p' || c=='q'){mult=1000000000000000L;}
|
jpayne@68
|
34 else if(c=='e'){mult=1000000000000000000L;}
|
jpayne@68
|
35 // else if(c=='z'){mult=1000000000000000000000L;}//Out of range
|
jpayne@68
|
36 else if(c=='c' || c=='h'){mult=100;}
|
jpayne@68
|
37 else if(c=='d'){mult=10;}
|
jpayne@68
|
38 else{throw new RuntimeException(b);}
|
jpayne@68
|
39 b=b.substring(0, b.length()-1);
|
jpayne@68
|
40 }
|
jpayne@68
|
41
|
jpayne@68
|
42 //Calculate product, check for overflow, and return
|
jpayne@68
|
43 if(!dot){
|
jpayne@68
|
44 long m=Long.parseLong(b);
|
jpayne@68
|
45 long p=m*mult;
|
jpayne@68
|
46 assert(p>=m) : p+", "+m+", "+b;
|
jpayne@68
|
47 return p;
|
jpayne@68
|
48 }else{
|
jpayne@68
|
49 double m=Double.parseDouble(b);
|
jpayne@68
|
50 long p=(long)(m*mult);
|
jpayne@68
|
51 assert(p>=m) : p+", "+m+", "+b;
|
jpayne@68
|
52 return p;
|
jpayne@68
|
53 }
|
jpayne@68
|
54 }
|
jpayne@68
|
55
|
jpayne@68
|
56 public static long parseKMGBinary(String b){
|
jpayne@68
|
57 if(b==null){return 0;}
|
jpayne@68
|
58 char c=Tools.toLowerCase(b.charAt(b.length()-1));
|
jpayne@68
|
59 boolean dot=b.indexOf('.')>=0;
|
jpayne@68
|
60 if(!Tools.isLetter(c) && !dot){return Long.parseLong(b);}
|
jpayne@68
|
61
|
jpayne@68
|
62 long mult=1;
|
jpayne@68
|
63 if(Tools.isLetter(c)){
|
jpayne@68
|
64 if(c=='k'){mult=1024;}
|
jpayne@68
|
65 else if(c=='m'){mult=1024*1024;}
|
jpayne@68
|
66 else if(c=='g' || c=='b'){mult=1024*1024*1024;}
|
jpayne@68
|
67 else if(c=='t'){mult=1024L*1024L*1024L*1024L;}
|
jpayne@68
|
68 else{throw new RuntimeException(b);}
|
jpayne@68
|
69 b=b.substring(0, b.length()-1);
|
jpayne@68
|
70 }
|
jpayne@68
|
71
|
jpayne@68
|
72 if(!dot){return Long.parseLong(b)*mult;}
|
jpayne@68
|
73
|
jpayne@68
|
74 return (long)(Double.parseDouble(b)*mult);
|
jpayne@68
|
75 }
|
jpayne@68
|
76
|
jpayne@68
|
77 public static boolean isNumber(String s){
|
jpayne@68
|
78 if(s==null || s.length()==0){return false;}
|
jpayne@68
|
79 char c=s.charAt(0);
|
jpayne@68
|
80 return Tools.isDigit(c) || c=='.' || c=='-';
|
jpayne@68
|
81 }
|
jpayne@68
|
82
|
jpayne@68
|
83 /**
|
jpayne@68
|
84 * Parse this argument. More liberal than Boolean.parseBoolean.
|
jpayne@68
|
85 * Null, t, true, or 1 all yield true.
|
jpayne@68
|
86 * Everything else, including the String "null", is false.
|
jpayne@68
|
87 * @param s Argument to parse
|
jpayne@68
|
88 * @return boolean form
|
jpayne@68
|
89 */
|
jpayne@68
|
90 public static boolean parseBoolean(String s){
|
jpayne@68
|
91 if(s==null || s.length()<1){return true;}
|
jpayne@68
|
92 if(s.length()==1){
|
jpayne@68
|
93 char c=Tools.toLowerCase(s.charAt(0));
|
jpayne@68
|
94 return c=='t' || c=='1';
|
jpayne@68
|
95 }
|
jpayne@68
|
96 if(s.equalsIgnoreCase("null") || s.equalsIgnoreCase("none")){return false;}
|
jpayne@68
|
97 return Boolean.parseBoolean(s);
|
jpayne@68
|
98 }
|
jpayne@68
|
99
|
jpayne@68
|
100 public static boolean parseYesNo(String s){
|
jpayne@68
|
101 if(s==null || s.length()<1){return true;}
|
jpayne@68
|
102 if(s.length()==1){
|
jpayne@68
|
103 char c=Tools.toLowerCase(s.charAt(0));
|
jpayne@68
|
104 if(c=='y'){return true;}
|
jpayne@68
|
105 if(c=='n'){return false;}
|
jpayne@68
|
106 throw new RuntimeException(s);
|
jpayne@68
|
107 }
|
jpayne@68
|
108
|
jpayne@68
|
109 if(s.equalsIgnoreCase("yes")){return true;}
|
jpayne@68
|
110 if(s.equalsIgnoreCase("no")){return false;}
|
jpayne@68
|
111 if(s.equalsIgnoreCase("unknown")){return false;} //Special case for IMG database
|
jpayne@68
|
112
|
jpayne@68
|
113 throw new RuntimeException(s);
|
jpayne@68
|
114 }
|
jpayne@68
|
115
|
jpayne@68
|
116 public static int[] parseIntArray(String s, String regex){
|
jpayne@68
|
117 if(s==null){return null;}
|
jpayne@68
|
118 String[] split=s.split(regex);
|
jpayne@68
|
119 int[] array=new int[split.length];
|
jpayne@68
|
120 for(int i=0; i<split.length; i++){
|
jpayne@68
|
121 array[i]=Integer.parseInt(split[i]);
|
jpayne@68
|
122 }
|
jpayne@68
|
123 return array;
|
jpayne@68
|
124 }
|
jpayne@68
|
125
|
jpayne@68
|
126 public static byte[] parseByteArray(String s, String regex){
|
jpayne@68
|
127 if(s==null){return null;}
|
jpayne@68
|
128 String[] split=s.split(regex);
|
jpayne@68
|
129 byte[] array=new byte[split.length];
|
jpayne@68
|
130 for(int i=0; i<split.length; i++){
|
jpayne@68
|
131 array[i]=Byte.parseByte(split[i]);
|
jpayne@68
|
132 }
|
jpayne@68
|
133 return array;
|
jpayne@68
|
134 }
|
jpayne@68
|
135
|
jpayne@68
|
136 public static int parseIntHexDecOctBin(final String s){
|
jpayne@68
|
137 if(s==null || s.length()<1){return 0;}
|
jpayne@68
|
138 int radix=10;
|
jpayne@68
|
139 if(s.length()>1 && s.charAt(1)=='0'){
|
jpayne@68
|
140 final char c=s.charAt(1);
|
jpayne@68
|
141 if(c=='x' || c=='X'){radix=16;}
|
jpayne@68
|
142 else if(c=='b' || c=='B'){radix=2;}
|
jpayne@68
|
143 else if(c=='o' || c=='O'){radix=8;}
|
jpayne@68
|
144 }
|
jpayne@68
|
145 return Integer.parseInt(s, radix);
|
jpayne@68
|
146 }
|
jpayne@68
|
147
|
jpayne@68
|
148 /**
|
jpayne@68
|
149 * @param array Text
|
jpayne@68
|
150 * @param a Index of first digit
|
jpayne@68
|
151 * @param b Index after last digit (e.g., array.length)
|
jpayne@68
|
152 * @return Parsed number
|
jpayne@68
|
153 */
|
jpayne@68
|
154 public static float parseFloat(byte[] array, int a, int b){
|
jpayne@68
|
155 return (float)parseDouble(array, a, b);
|
jpayne@68
|
156 }
|
jpayne@68
|
157
|
jpayne@68
|
158 /**
|
jpayne@68
|
159 * @param array Text
|
jpayne@68
|
160 * @param a Index of first digit
|
jpayne@68
|
161 * @param b Index after last digit (e.g., array.length)
|
jpayne@68
|
162 * @return Parsed number
|
jpayne@68
|
163 */
|
jpayne@68
|
164 public static double parseDoubleSlow(byte[] array, int a, int b){
|
jpayne@68
|
165 String s=new String(array, a, b-a);
|
jpayne@68
|
166 return Double.parseDouble(s);
|
jpayne@68
|
167 }
|
jpayne@68
|
168
|
jpayne@68
|
169 public static double parseDouble(final byte[] array, final int start){
|
jpayne@68
|
170 return parseDouble(array, start, array.length);
|
jpayne@68
|
171 }
|
jpayne@68
|
172
|
jpayne@68
|
173 /**
|
jpayne@68
|
174 * @param array Text
|
jpayne@68
|
175 * @param a0 Index of first digit
|
jpayne@68
|
176 * @param b Index after last digit (e.g., array.length)
|
jpayne@68
|
177 * @return Parsed number
|
jpayne@68
|
178 */
|
jpayne@68
|
179 public static double parseDouble(final byte[] array, final int a0, final int b){
|
jpayne@68
|
180 if(Tools.FORCE_JAVA_PARSE_DOUBLE){
|
jpayne@68
|
181 return Double.parseDouble(new String(array, a0, b-a0));
|
jpayne@68
|
182 }
|
jpayne@68
|
183 int a=a0;
|
jpayne@68
|
184 assert(b>a);
|
jpayne@68
|
185 long upper=0;
|
jpayne@68
|
186 final byte z='0';
|
jpayne@68
|
187 long mult=1;
|
jpayne@68
|
188 if(array[a]=='-'){mult=-1; a++;}
|
jpayne@68
|
189
|
jpayne@68
|
190 for(; a<b; a++){
|
jpayne@68
|
191 final byte c=array[a];
|
jpayne@68
|
192 if(c=='.'){break;}
|
jpayne@68
|
193 final int x=(c-z);
|
jpayne@68
|
194 assert(x<10 && x>=0) : x+" = "+(char)c+"\narray="+new String(array)+", start="+a+", stop="+b;
|
jpayne@68
|
195 upper=(upper*10)+x;
|
jpayne@68
|
196 }
|
jpayne@68
|
197
|
jpayne@68
|
198 long lower=0;
|
jpayne@68
|
199 int places=0;
|
jpayne@68
|
200 for(a++; a<b; a++){
|
jpayne@68
|
201 final byte c=array[a];
|
jpayne@68
|
202 final int x=(c-z);
|
jpayne@68
|
203 assert(x<10 && x>=0) : x+" = "+(char)c+"\narray="+new String(array)+", start="+a+", stop="+b+
|
jpayne@68
|
204 "\nThis function does not support exponents; if the input has an exponent, add the flag 'forceJavaParseDouble'.";
|
jpayne@68
|
205 lower=(lower*10)+x;
|
jpayne@68
|
206 places++;
|
jpayne@68
|
207 }
|
jpayne@68
|
208
|
jpayne@68
|
209 double d=mult*(upper+lower*ByteBuilder.decimalInvMult[places]);
|
jpayne@68
|
210 // assert(d==parseDoubleSlow(array, a0, b)) : d+", "+parseDoubleSlow(array, a0, b);
|
jpayne@68
|
211 return d;
|
jpayne@68
|
212 }
|
jpayne@68
|
213
|
jpayne@68
|
214 public static int parseInt(byte[] array, int start){
|
jpayne@68
|
215 return parseInt(array, start, array.length);
|
jpayne@68
|
216 }
|
jpayne@68
|
217
|
jpayne@68
|
218 // /**
|
jpayne@68
|
219 // * @param array Text
|
jpayne@68
|
220 // * @param a Index of first digit
|
jpayne@68
|
221 // * @param b Index after last digit (e.g., array.length)
|
jpayne@68
|
222 // * @return Parsed number
|
jpayne@68
|
223 // */
|
jpayne@68
|
224 // public static int parseInt(byte[] array, int a, int b){
|
jpayne@68
|
225 // assert(b>a);
|
jpayne@68
|
226 // int r=0;
|
jpayne@68
|
227 // final byte z='0';
|
jpayne@68
|
228 // int mult=1;
|
jpayne@68
|
229 // if(array[a]=='-'){mult=-1; a++;}
|
jpayne@68
|
230 // for(; a<b; a++){
|
jpayne@68
|
231 // int x=(array[a]-z);
|
jpayne@68
|
232 // assert(x<10 && x>=0) : x+" = "+(char)array[a]+"\narray="+new String(array)+", start="+a+", stop="+b;
|
jpayne@68
|
233 // r=(r*10)+x;
|
jpayne@68
|
234 // }
|
jpayne@68
|
235 // return r*mult;
|
jpayne@68
|
236 // }
|
jpayne@68
|
237
|
jpayne@68
|
238 /**
|
jpayne@68
|
239 * Returns the int representation of a number represented in ASCII text, from position a to b.
|
jpayne@68
|
240 * This function is much faster than creating a substring and calling Integer.parseInt()
|
jpayne@68
|
241 * Throws Assertions rather than Exceptions for invalid input.
|
jpayne@68
|
242 * This function does NOT detect overflows, e.g., values over 2^31-1 (Integer.MAX_VALUE).
|
jpayne@68
|
243 * This function has no side-effects.
|
jpayne@68
|
244 * @param array byte array containing the text to parse.
|
jpayne@68
|
245 * @param a Index of the first digit of the number.
|
jpayne@68
|
246 * @param b Index after the last digit (e.g., array.length).
|
jpayne@68
|
247 * @return int representation of the parsed number.
|
jpayne@68
|
248 * @throws Assertions rather than Exceptions for invalid input.
|
jpayne@68
|
249 *
|
jpayne@68
|
250 * @TODO Correctly represent Integer.MIN_VALUE
|
jpayne@68
|
251 * @TODO Detect overflow.
|
jpayne@68
|
252 */
|
jpayne@68
|
253 public static int parseInt(byte[] array, int a, int b){
|
jpayne@68
|
254 assert(b>a) : "The start position of the text to parse must come before the stop position: "+
|
jpayne@68
|
255 a+","+b+","+new String(array);
|
jpayne@68
|
256 int r=0; //Initialize the return value to 0.
|
jpayne@68
|
257
|
jpayne@68
|
258 //z holds the ASCII code for 0, which is subtracted from other ASCII codes
|
jpayne@68
|
259 //to yield the int value of a character. For example, '7'-'0'=7,
|
jpayne@68
|
260 //because ASCII '7'=55, while ASCII '0'=48, and 55-48=7.
|
jpayne@68
|
261 final byte z='0';
|
jpayne@68
|
262
|
jpayne@68
|
263 //mult is 1 for positive numbers, or -1 for negative numbers.
|
jpayne@68
|
264 //It will be multiplied by the unsigned result to yield the final signed result.
|
jpayne@68
|
265 int mult=1;
|
jpayne@68
|
266
|
jpayne@68
|
267 //If the term starts with a minus sign, set the multiplier to -1 and increment the position.
|
jpayne@68
|
268 if(array[a]=='-'){mult=-1; a++;}
|
jpayne@68
|
269
|
jpayne@68
|
270 //Iterate through every position, incrementing a, up to b (exclusive).
|
jpayne@68
|
271 for(; a<b; a++){
|
jpayne@68
|
272 //x is the numeric value of the character at position a.
|
jpayne@68
|
273 //In other words, if array[a]='7',
|
jpayne@68
|
274 //x would be 7, not the ASCII code for '7' (which is 55).
|
jpayne@68
|
275 int x=(array[a]-z);
|
jpayne@68
|
276
|
jpayne@68
|
277 //Assert that x is in the range of 0-9; otherwise, the character was not a digit.
|
jpayne@68
|
278 //The ASCII code will be printed here because in some cases the character could be
|
jpayne@68
|
279 //a control character (like carriage return or vertical tab or bell) which is unprintable.
|
jpayne@68
|
280 //But if possible the character will be printed to, as well as the position,
|
jpayne@68
|
281 //and the entire String from which the number is to be parsed.
|
jpayne@68
|
282 assert(x<10 && x>=0) : "Non-digit character with ASCII code "+(int)array[a]+" was encountered.\n"
|
jpayne@68
|
283 +"x="+x+"; char="+(char)array[a]+"\narray="+new String(array)+", start="+a+", stop="+b;
|
jpayne@68
|
284
|
jpayne@68
|
285 //Multiply the old value by 10, then add the new 1's digit.
|
jpayne@68
|
286 //This is because the text is assumed to be base-10,
|
jpayne@68
|
287 //so each subsequent character will represent 1/10th the significance of the previous character.
|
jpayne@68
|
288 r=(r*10)+x;
|
jpayne@68
|
289 }
|
jpayne@68
|
290
|
jpayne@68
|
291 //Change the unsigned value into a signed result, and return it.
|
jpayne@68
|
292 return r*mult;
|
jpayne@68
|
293 }
|
jpayne@68
|
294
|
jpayne@68
|
295 /**
|
jpayne@68
|
296 * @param array Text
|
jpayne@68
|
297 * @param a Index of first digit
|
jpayne@68
|
298 * @param b Index after last digit (e.g., array.length)
|
jpayne@68
|
299 * @return Parsed number
|
jpayne@68
|
300 */
|
jpayne@68
|
301 public static int parseInt(String array, int a, int b){
|
jpayne@68
|
302 // assert(false) : Character.toString(array.charAt(a));
|
jpayne@68
|
303 assert(b>a);
|
jpayne@68
|
304 int r=0;
|
jpayne@68
|
305 final byte z='0';
|
jpayne@68
|
306 int mult=1;
|
jpayne@68
|
307 if(array.charAt(a)=='-'){mult=-1; a++;}
|
jpayne@68
|
308 for(; a<b; a++){
|
jpayne@68
|
309 int x=(array.charAt(a)-z);
|
jpayne@68
|
310 assert(x<10 && x>=0) : x+" = "+array.charAt(a)+"\narray="+new String(array)+", start="+a+", stop="+b;
|
jpayne@68
|
311 r=(r*10)+x;
|
jpayne@68
|
312 }
|
jpayne@68
|
313 return r*mult;
|
jpayne@68
|
314 }
|
jpayne@68
|
315
|
jpayne@68
|
316 public static long parseLong(byte[] array){return parseLong(array, 0, array.length);}
|
jpayne@68
|
317
|
jpayne@68
|
318 public static long parseLong(byte[] array, int start){return parseLong(array, start, array.length);}
|
jpayne@68
|
319
|
jpayne@68
|
320 /**
|
jpayne@68
|
321 * @param array Text
|
jpayne@68
|
322 * @param a Index of first digit
|
jpayne@68
|
323 * @param b Index after last digit (e.g., array.length)
|
jpayne@68
|
324 * @return Parsed number
|
jpayne@68
|
325 */
|
jpayne@68
|
326 public static long parseLong(byte[] array, int a, int b){
|
jpayne@68
|
327 assert(b>a);
|
jpayne@68
|
328 long r=0;
|
jpayne@68
|
329 final byte z='0';
|
jpayne@68
|
330 long mult=1;
|
jpayne@68
|
331 if(array[a]=='-'){mult=-1; a++;}
|
jpayne@68
|
332 for(; a<b; a++){
|
jpayne@68
|
333 int x=(array[a]-z);
|
jpayne@68
|
334 assert(x<10 && x>=0) : x+" = "+(char)array[a]+"\narray="+new String(array)+", start="+a+", stop="+b;
|
jpayne@68
|
335 r=(r*10)+x;
|
jpayne@68
|
336 }
|
jpayne@68
|
337 return r*mult;
|
jpayne@68
|
338 }
|
jpayne@68
|
339
|
jpayne@68
|
340 /**
|
jpayne@68
|
341 * @param array Text
|
jpayne@68
|
342 * @param a Index of first digit
|
jpayne@68
|
343 * @param b Index after last digit (e.g., array.length)
|
jpayne@68
|
344 * @return Parsed number
|
jpayne@68
|
345 */
|
jpayne@68
|
346 public static long parseLong(String array, int a, int b){
|
jpayne@68
|
347 assert(b>a);
|
jpayne@68
|
348 long r=0;
|
jpayne@68
|
349 final byte z='0';
|
jpayne@68
|
350 long mult=1;
|
jpayne@68
|
351 if(array.charAt(a)=='-'){mult=-1; a++;}
|
jpayne@68
|
352 for(; a<b; a++){
|
jpayne@68
|
353 int x=(array.charAt(a)-z);
|
jpayne@68
|
354 assert(x<10 && x>=0) : x+" = "+array.charAt(a)+"\narray="+new String(array)+", start="+a+", stop="+b;
|
jpayne@68
|
355 r=(r*10)+x;
|
jpayne@68
|
356 }
|
jpayne@68
|
357 return r*mult;
|
jpayne@68
|
358 }
|
jpayne@68
|
359
|
jpayne@68
|
360
|
jpayne@68
|
361 //Note: clen is optional, but allows poorly-formatted input like trailing whitespace
|
jpayne@68
|
362 //Without clen ",,," would become {0,0,0,0}
|
jpayne@68
|
363 public static long[] parseLongArray(String sub) {
|
jpayne@68
|
364 if(sub==null || sub.length()<1){return null;}
|
jpayne@68
|
365 long current=0;
|
jpayne@68
|
366 // int clen=0;
|
jpayne@68
|
367 LongList list=new LongList(min(8, 1+sub.length()/2));
|
jpayne@68
|
368 for(int i=0, len=sub.length(); i<len; i++){
|
jpayne@68
|
369 // System.err.println();
|
jpayne@68
|
370 int c=sub.charAt(i)-'0';
|
jpayne@68
|
371 if(c<0 || c>9){
|
jpayne@68
|
372 // System.err.println('A');
|
jpayne@68
|
373 //assert(clen>0);
|
jpayne@68
|
374 list.add(current);
|
jpayne@68
|
375 current=0;
|
jpayne@68
|
376 // clen=0;
|
jpayne@68
|
377 }else{
|
jpayne@68
|
378 // System.err.println('B');
|
jpayne@68
|
379 current=(current*10)+c;
|
jpayne@68
|
380 // clen++;
|
jpayne@68
|
381 }
|
jpayne@68
|
382 // System.err.println("i="+i+", c="+c+", current="+current+", list="+list);
|
jpayne@68
|
383 }
|
jpayne@68
|
384 // if(clen>0){
|
jpayne@68
|
385 list.add(current);
|
jpayne@68
|
386 // }
|
jpayne@68
|
387 // assert(false) : "\n'"+sub+"'\n"+Arrays.toString(list.toArray());
|
jpayne@68
|
388 return list.toArray();
|
jpayne@68
|
389 }
|
jpayne@68
|
390
|
jpayne@68
|
391 public static int parseZmw(String id){
|
jpayne@68
|
392 //Example: m54283_190403_183820/4194374/919_2614
|
jpayne@68
|
393 //Run ID is m54283_190403_183820
|
jpayne@68
|
394 //zmw ID is 4194374.
|
jpayne@68
|
395 //Read start/stop coordinates are 919_2614
|
jpayne@68
|
396 int under=id.indexOf('_');
|
jpayne@68
|
397 int slash=id.indexOf('/');
|
jpayne@68
|
398 if(under<0 || slash<0){return -1;}
|
jpayne@68
|
399 String[] split=id.split("/");
|
jpayne@68
|
400 String z=split[1];
|
jpayne@68
|
401 return Integer.parseInt(z);
|
jpayne@68
|
402 }
|
jpayne@68
|
403
|
jpayne@68
|
404 public static char parseSymbolToCharacter(String b){
|
jpayne@68
|
405 b=parseSymbol(b);
|
jpayne@68
|
406 while(b.length()>1 && b.charAt(0)=='\\'){
|
jpayne@68
|
407 b=b.substring(1);
|
jpayne@68
|
408 }
|
jpayne@68
|
409 return b.charAt(0);
|
jpayne@68
|
410 }
|
jpayne@68
|
411
|
jpayne@68
|
412 public static String parseSymbol(String b){
|
jpayne@68
|
413 if(b==null || b.length()<2){return b;}
|
jpayne@68
|
414
|
jpayne@68
|
415 //Convenience characters
|
jpayne@68
|
416 if(b.equalsIgnoreCase("space")){
|
jpayne@68
|
417 return " ";
|
jpayne@68
|
418 }else if(b.equalsIgnoreCase("tab")){
|
jpayne@68
|
419 return "\t";
|
jpayne@68
|
420 }else if(b.equalsIgnoreCase("whitespace")){
|
jpayne@68
|
421 return "\\s+";
|
jpayne@68
|
422 }else if(b.equalsIgnoreCase("pound")){
|
jpayne@68
|
423 return "#";
|
jpayne@68
|
424 }else if(b.equalsIgnoreCase("greaterthan")){
|
jpayne@68
|
425 return ">";
|
jpayne@68
|
426 }else if(b.equalsIgnoreCase("lessthan")){
|
jpayne@68
|
427 return "<";
|
jpayne@68
|
428 }else if(b.equalsIgnoreCase("equals")){
|
jpayne@68
|
429 return "=";
|
jpayne@68
|
430 }else if(b.equalsIgnoreCase("colon")){
|
jpayne@68
|
431 return ":";
|
jpayne@68
|
432 }else if(b.equalsIgnoreCase("semicolon")){
|
jpayne@68
|
433 return ";";
|
jpayne@68
|
434 }else if(b.equalsIgnoreCase("bang")){
|
jpayne@68
|
435 return "!";
|
jpayne@68
|
436 }else if(b.equalsIgnoreCase("and") || b.equalsIgnoreCase("ampersand")){
|
jpayne@68
|
437 return "&";
|
jpayne@68
|
438 }else if(b.equalsIgnoreCase("quote") || b.equalsIgnoreCase("doublequote")){
|
jpayne@68
|
439 return "\"";
|
jpayne@68
|
440 }else if(b.equalsIgnoreCase("singlequote") || b.equalsIgnoreCase("apostrophe")){
|
jpayne@68
|
441 return "'";
|
jpayne@68
|
442 }
|
jpayne@68
|
443
|
jpayne@68
|
444 //Java meta characters
|
jpayne@68
|
445 if(b.equalsIgnoreCase("backslash")){
|
jpayne@68
|
446 return "\\\\";
|
jpayne@68
|
447 }else if(b.equalsIgnoreCase("hat") || b.equalsIgnoreCase("caret")){
|
jpayne@68
|
448 return "\\^";
|
jpayne@68
|
449 }else if(b.equalsIgnoreCase("dollar")){
|
jpayne@68
|
450 return "\\$";
|
jpayne@68
|
451 }else if(b.equalsIgnoreCase("dot")){
|
jpayne@68
|
452 return "\\.";
|
jpayne@68
|
453 }else if(b.equalsIgnoreCase("pipe") || b.equalsIgnoreCase("or")){
|
jpayne@68
|
454 return "\\|";
|
jpayne@68
|
455 }else if(b.equalsIgnoreCase("questionmark")){
|
jpayne@68
|
456 return "\\?";
|
jpayne@68
|
457 }else if(b.equalsIgnoreCase("star") || b.equalsIgnoreCase("asterisk")){
|
jpayne@68
|
458 return "\\*";
|
jpayne@68
|
459 }else if(b.equalsIgnoreCase("plus")){
|
jpayne@68
|
460 return "\\+";
|
jpayne@68
|
461 }else if(b.equalsIgnoreCase("openparen")){
|
jpayne@68
|
462 return "\\(";
|
jpayne@68
|
463 }else if(b.equalsIgnoreCase("closeparen")){
|
jpayne@68
|
464 return "\\)";
|
jpayne@68
|
465 }else if(b.equalsIgnoreCase("opensquare")){
|
jpayne@68
|
466 return "\\[";
|
jpayne@68
|
467 }else if(b.equalsIgnoreCase("opencurly")){
|
jpayne@68
|
468 return "\\{";
|
jpayne@68
|
469 }
|
jpayne@68
|
470
|
jpayne@68
|
471 //No matches, return the literal
|
jpayne@68
|
472 return b;
|
jpayne@68
|
473 }
|
jpayne@68
|
474
|
jpayne@68
|
475 public static byte[] parseRemap(String b){
|
jpayne@68
|
476 final byte[] remap;
|
jpayne@68
|
477 if(b==null || ("f".equalsIgnoreCase(b) || "false".equalsIgnoreCase(b))){
|
jpayne@68
|
478 remap=null;
|
jpayne@68
|
479 }else{
|
jpayne@68
|
480 assert((b.length()&1)==0) : "Length of remap argument must be even. No whitespace is allowed.";
|
jpayne@68
|
481
|
jpayne@68
|
482 remap=new byte[128];
|
jpayne@68
|
483 for(int j=0; j<remap.length; j++){remap[j]=(byte)j;}
|
jpayne@68
|
484 for(int j=0; j<b.length(); j+=2){
|
jpayne@68
|
485 char x=b.charAt(j), y=b.charAt(j+1);
|
jpayne@68
|
486 remap[x]=(byte)y;
|
jpayne@68
|
487 }
|
jpayne@68
|
488 }
|
jpayne@68
|
489 return remap;
|
jpayne@68
|
490 }
|
jpayne@68
|
491
|
jpayne@68
|
492 public static final int min(int x, int y){return x<y ? x : y;}
|
jpayne@68
|
493 public static final int max(int x, int y){return x>y ? x : y;}
|
jpayne@68
|
494
|
jpayne@68
|
495 }
|