summaryrefslogtreecommitdiffstats
path: root/libraries/oscP5/src/netP5/StringUtils.java
blob: 393bc9013552d7e7b71ae72d75f0c7e94f34fcf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/**
 * A network library for processing which supports UDP, TCP and Multicast.
 *
 * (c) 2004-2012
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 * 
 * @author		Andreas Schlegel http://www.sojamo.de
 * @modified	12/23/2012
 * @version		0.9.9
 */

/**
 * StringUtils Contains some basic utility methods for handling Strings.
 *
 * Copyright (C) 2003 Johan K�nng�rd
 * Contains code Copyright (C) 2001,2002 Stephen Ostermiller
 * http://ostermiller.org/utils/StringHelper.java.html
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * The GPL is located at: http://www.gnu.org/licenses/gpl.txt
 *
 * @author Johan K�nng�rd, http://dev.kanngard.net/
 * @version 0.4
 */

package netP5;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 * @invisible
 */
public class StringUtils extends Object {

	/**
	 * Protected because this class does only contain static methods.
	 */
	protected StringUtils() {
	}

	/**
	 * Returns the substring to the right of the specified substring in the
	 * specified String, starting from the left.
	 *
	 * @param source
	 *            the source String to search.
	 * @param searchFor
	 *            the substring to search for in source.
	 * @return the substring that is to the right of searchFor in source.
	 */
	public static String right(String source, String searchFor) {
		int index = source.indexOf(searchFor) + searchFor.length();

		if (index < 0) {
			return "";
		}
		return source.substring(index);
	}

	/**
	 * Returns the substring to the right of the specified substring in the
	 * specified String, starting from the right.
	 *
	 * @param source
	 *            the source String to search.
	 * @param searchFor
	 *            the substring to search for in source.
	 * @return the substring that is to the right of searchFor in source,
	 *         starting from the right.
	 */
	public static String rightBack(String source, String searchFor) {
		int index = source.lastIndexOf(searchFor) + searchFor.length();

		if (index < 0) {
			return "";
		}
		return source.substring(index);
	}

	/**
	 * Returns the substring to the left of the specified substring in the
	 * specified String, starting from the left.
	 *
	 * @param source
	 *            the source String to search.
	 * @param searchFor
	 *            the substring to search for in source.
	 * @return the substring that is to the left of searchFor in source.
	 */
	public static String left(String source, String searchFor) {
		int index = source.indexOf(searchFor);

		if (index <= 0) {
			return "";
		}
		return source.substring(0, index);
	}

	/**
	 * Returns the substring to the left of the specified substring in the
	 * specified String, starting from the right.
	 *
	 * @param source
	 *            the source String to search.
	 * @param searchFor
	 *            the substring to search for in source.
	 * @return the substring that is to the left of searchFor in source,
	 *         starting from the right.
	 */
	public static String leftBack(String source, String searchFor) {
		int index = source.lastIndexOf(searchFor);

		if (index <= 0) {
			return "";
		}
		return source.substring(0, index);
	}

	/**
	 * Returns the substring between two substrings. I.e.
	 * StringUtils.middle("This i a big challenge", "a", "challenge") returns "
	 * big ".
	 *
	 * @param source
	 *            the String to search.
	 * @param start
	 *            the String to the left to search for, from the left.
	 * @param end
	 *            the String to the right to search for, from the right.
	 */
	public static String middle(String source, String start, String end) {
		String one = StringUtils.right(source, start);
		return StringUtils.leftBack(one, end);
	}

	/**
	 * Returns a substring of a String, starting from specified index and with
	 * specified length. I. e. StringUtils.middle("This is a big challenge", 5,
	 * 6) returns " is a "
	 *
	 * @param source
	 *            the String to get a substring from.
	 * @param startIndex
	 *            the index in the source String to get the substring from.
	 * @param length
	 *            the length of the substring to return.
	 */
	public static String middle(String source, int startIndex, int length) {
		return source.substring(startIndex, source.length() - length);
	}

	/**
	 * Replaces substrings in a string.
	 *
	 * @param source
	 *            the source String to replace substrings in.
	 * @param searchFor
	 *            the string to search for.
	 * @param replaceWith
	 *            the string to replace all found searchFor-substrings with.
	 */
	public static String replace(String source, String searchFor,
			String replaceWith) {
		if (source.length() < 1) {
			return "";
		}
		int p = 0;

		while (p < source.length() && (p = source.indexOf(searchFor, p)) >= 0) {
			source = source.substring(0, p) + replaceWith
					+ source.substring(p + searchFor.length(), source.length());
			p += replaceWith.length();
		}
		return source;
	}

	/**
	 * Replaces several substrings in a string.
	 *
	 * @param source
	 *            the source String to replace substrings in.
	 * @param searchFor
	 *            the substrings to search for.
	 * @param replaceWith
	 *            what to replace every searchFor with,
	 */
	public static String replace(String source, String[] searchFor,
			String replaceWith) {
		for (int i = 0; i < searchFor.length; i++) {
			StringUtils.replace(source, searchFor[i], replaceWith);
		}
		return source;
	}

	/**
	 * Splits every String in an array at the specified lengths.
	 *
	 * Example: <code><pre>
	 * String source[] = { &quot;123a123b123c123d&quot;, &quot;Bla1bla2bla3bla4bla5bla6bla7&quot; };
	 * int[] lengths = { 3, 1, 3, 1 };
	 * Vector result = StringUtils.explode(source, lengths);
	 * Object element = null;
	 * String[] rowElements = null;
	 * Enumeration enum = result.elements();
	 * while (enum.hasMoreElements()) {
	 * 	element = enum.nextElement();
	 * 	if (element instanceof String[]) {
	 * 		rowElements = (String[]) element;
	 * 		for (int i = 0; i &lt; rowElements.length; i++) {
	 * 			System.out.println(rowElements[i]);
	 * 		}
	 * 	}
	 * }
	 * </pre></code> The result that will be output: 123 a 123 b
	 *
	 * Bla 1 bla 2
	 *
	 * @return a Vector containing String arrays (the rows).
	 */
	public static Vector explode(String[] source, int[] lengths) {
		Vector v = new Vector();
		for (int i = 0; i < source.length; i++) {
			v.addElement(StringUtils.explode(source[i], lengths));
		}
		return v;
	}

	/**
	 * Splits a string at the specified lengths and returns an array of Strings.
	 *
	 * @param source
	 *            the String to split.
	 * @lengths an array of lengths where to split the String.
	 * @return an array of Strings with the same number of elements as the
	 *         number of elements in the lengths argument. The length of each
	 *         String element is specified by the correspondent lengths array
	 *         element.
	 * @throws IndexOutOfBoundsException
	 *             if any of the length�s are invalid.
	 */
	public static String[] explode(String source, int[] lengths) {
		String[] result = new String[lengths.length];
		int position = 0;
		for (int i = 0; i < lengths.length; i++) {
			if (lengths[i] + position > source.length()) {
				throw new IndexOutOfBoundsException();
			}
			result[i] = source.substring(position, position + lengths[i]);
			position += lengths[i];
		}
		return result;
	}

	/**
	 * Splits a string into an array with a space as delimiter.
	 *
	 * @param source
	 *            the source String to explode.
	 * @return an array of strings that are made out of splitting the string at
	 *         the spaces.
	 */
	public static String[] explode(String source) {
		return StringUtils.explode(source, " ");
	}

	/**
	 * Splits a string into an array with the specified delimiter. Original code
	 * Copyright (C) 2001,2002 Stephen Ostermiller
	 * http://ostermiller.org/utils/StringHelper.java.html
	 *
	 * <p>
	 * This method is meant to be similar to the split function in other
	 * programming languages but it does not use regular expressions. Rather the
	 * String is split on a single String literal. It is equivalent to the
	 *
	 * @Explode function in Lotus Notes / Domino.
	 *          </p>
	 *          <p>
	 *          Unlike java.util.StringTokenizer which accepts multiple
	 *          character tokens as delimiters, the delimiter here is a single
	 *          String literal.
	 *          </p>
	 *          <p>
	 *          Each null token is returned as an empty String. Delimiters are
	 *          never returned as tokens.
	 *          </p>
	 *          <p>
	 *          If there is no delimiter because it is either empty or null, the
	 *          only element in the result is the original String.
	 *          </p>
	 *          <p>
	 *          StringHelper.explode("1-2-3", "-");<br>
	 *          result: {"1", "2", "3"}<br>
	 *          StringHelper.explode("-1--2-", "-");<br>
	 *          result: {"", "1", ,"", "2", ""}<br>
	 *          StringHelper.explode("123", "");<br>
	 *          result: {"123"}<br>
	 *          StringHelper.explode("1-2---3----4", "--");<br>
	 *          result: {"1-2", "-3", "", "4"}<br>
	 *          </p>
	 * @param s
	 *            the String to explode.
	 * @param delimiter
	 *            the delimiter where to split the string.
	 * @return an array of strings that are made out of splitting the string at
	 *         the specified delimiter.
	 * @throws NullPointerException
	 *             if s is null.
	 */
	public static String[] explode(String s, String delimiter) {
		int delimiterLength;
		int stringLength = s.length();

		if (delimiter == null || (delimiterLength = delimiter.length()) == 0) {
			return new String[] { s };
		}
		// a two pass solution is used because a one pass solution would
		// require the possible resizing and copying of memory structures
		// In the worst case it would have to be resized n times with each
		// resize having a O(n) copy leading to an O(n^2) algorithm.
		int count = 0;
		int start = 0;
		int end;

		while ((end = s.indexOf(delimiter, start)) != -1) {
			count++;
			start = end + delimiterLength;
		}
		count++;

		String[] result = new String[count];
		// Scan s again, but this time pick out the tokens
		count = 0;
		start = 0;
		while ((end = s.indexOf(delimiter, start)) != -1) {
			result[count] = s.substring(start, end);
			count++;
			start = end + delimiterLength;
		}
		end = stringLength;
		result[count] = s.substring(start, end);
		return result;
	}

	public static String[] slice(int theNum, String[] theStringArray) {
		if (theNum < theStringArray.length) {
			String[] t = new String[theStringArray.length - theNum];
			for (int i = theNum; i < theStringArray.length; i++) {
				t[i - theNum] = theStringArray[i];
			}
			return t;
		}
		return theStringArray;
	}

	/**
	 * Combines an array to a string, using the specified delimiter.
	 *
	 * @param elements
	 *            the array to combine to a single string.
	 * @param delimiter
	 *            the delimiter to put between the combined elements.
	 * @return the array combined to a string.
	 */
	public static String implode(Object[] elements, String delimiter) {
		StringBuffer buffer = new StringBuffer("");
		for (int i = 0; i < elements.length - 1; i++) {
			buffer.append((String) elements[i] + delimiter);
		}
		buffer.append((String) elements[elements.length - 1]);
		return buffer.toString();
	}

	/**
	 * Combines an array to a string, using a comma and a space as delimiter.
	 *
	 * @param elements
	 *            the array to combine to a single string.
	 * @return the array combined to a string.
	 */
	public static String implode(Object[] elements) {
		return implode(elements, ", ");
	}

	/**
	 * Used by randomString(int) for valid characters.
	 */
	protected static String VALID_RANDOM_CHARACTERS = "abcdefghijkmnopqrstuvwxyz"
			+ "ABCDEFGHJKLMNPQRSTUVWXYZ-_.,;:<>()1234567890%&/=?+";

	/**
	 * Removes all instances of a character in a String.
	 *
	 * @param source
	 *            the String to remove substring in.
	 * @param searchFor
	 *            the character to remove.
	 * @return the replaced String.
	 */
	public static String remove(String source, char searchFor) {
		String s = String.valueOf(searchFor);
		return StringUtils.remove(source, s);
	}

	/**
	 * Removes all instances of a substring in a String.
	 *
	 * @param source
	 *            the String to remove substring in.
	 * @param searchFor
	 *            the substring to remove.
	 * @return the replaced String.
	 */
	public static String remove(String source, String searchFor) {

		return StringUtils.replace(source, searchFor, "");
	}

	/**
	 * Removes all instances of substrings in a String.
	 *
	 * @param source
	 *            the String to remove substrings in.
	 * @param searchFor
	 *            an array of substrings to remove from the source String.
	 * @return the replaced String.
	 */
	public static String remove(String source, String searchFor[]) {
		return StringUtils.replace(source, searchFor, "");
	}

	/**
	 * Removes duplicates of a substring in a String. Case sensitive.
	 *
	 * @param source
	 *            the String to remove duplicates in.
	 * @param searchFor
	 *            the substring that can only occur one at a time, several can
	 *            exist in the source though.
	 */
	public static String removeDuplicates(String source, String searchFor) {
		StringBuffer result = new StringBuffer("");
		Enumeration myEnum = new StringTokenizer(source, searchFor, true);
		String current = "";
		String previous = "";

		while (myEnum.hasMoreElements()) {
			current = (String) myEnum.nextElement();
			if (!current.equals(previous)) {
				result.append(current);
			}
			previous = current;
		}
		return result.toString();
	}

	/**
	 * A utility method to remove duplicate characters from a string. For
	 * example, it would convert "hello" to "helo", and "abcd123abcaaa" to
	 * "abcd123".
	 *
	 * @param source
	 *            the String to remove all duplicate characters in.
	 * @return a String with no duplicate characters.
	 */
	protected String unique(String source) {
		String result = "";

		for (int k = 0; k < source.length(); k++) {
			if (result.indexOf(source.charAt(k)) == -1) {
				result += source.charAt(k);
			}
		}
		return result;
	}

	/**
	 * Prints the stacktrace to a buffer and returns the buffer as a String.
	 *
	 * @param t
	 *            the Throwable you wnat to generate a stacktrace for.
	 * @return the stacktrace of the supplied Throwable.
	 */
	public static String getStackTrace(Throwable t) throws IOException {
		StringWriter sw = new StringWriter();
		t.printStackTrace(new PrintWriter(sw));
		sw.close();
		return sw.toString();
	}

	/**
	 * Checks if a String is empty or null.
	 *
	 * @param s
	 *            the String to test if it is empty or null.
	 * @return true if the String is null or empty ("").
	 */
	public static boolean isEmpty(String s) {
		if (s == null) {
			return true;
		}
		return s.equals("");
	}

	/**
	 * Creates a string of the given width with the given string left justified
	 * (followed by an appropriate number of spaces).
	 *
	 * @param source
	 *            the String to justify
	 * @param length
	 *            the length of the resulting String
	 * @return the source String padded with spaces to fill up the length. If
	 *         the source string is longer than the length argument, the source
	 *         String is returned.
	 */
	public static String leftJustify(String source, int length) {
		if (source.length() >= length) {
			return source;
		}
		return StringUtils.spaces(length - source.length()) + source;
	}

	/**
	 * Creates a string of the given width with the given string right justified
	 * (with an appropriate number of spaces before it).
	 *
	 * @param source
	 *            the String to justify
	 * @param length
	 *            the length of the resulting String
	 * @return the source String padded with spaces to fill up the length. If
	 *         the source string is longer than the length argument, the source
	 *         String is returned.
	 */
	public static String rightJustify(String source, int length) {
		if (source.length() >= length) {
			return source;
		}

		return source + StringUtils.spaces(length - source.length());
	}

	/**
	 * Creates a string of the given width with the given string left justified
	 * (padded by an appropriate number of spaces in front and after it).
	 *
	 * @param source
	 *            the String to justify
	 * @param length
	 *            the length of the resulting String
	 * @return the source String padded with spaces to fill up the length. If
	 *         the source string is longer than the length argument, the source
	 *         String is returned.
	 */
	public static String centerJustify(String source, int length) {
		if (source.length() >= length) {
			return source;
		}
		int leftLength = (length - source.length()) / 2;
		int rightLength = length - (leftLength + source.length());
		return StringUtils.spaces(leftLength) + source
				+ StringUtils.spaces(rightLength);
	}

	/**
	 * Returns a String with the specified number of spaces.
	 *
	 * @param length
	 *            the number of spaces to return.
	 * @return a String consisting of the specified number of spaces.
	 */
	public static String spaces(int length) {
		return duplicate(" ", length);
	}

	/**
	 * Returns a String with the source String copied the specified number of
	 * times.
	 *
	 * @param source
	 *            the source String to copy.
	 * @param length
	 *            the number of copies of source to return.
	 * @return a String consisting of the specified source String copied the
	 *         specified number of times.
	 */
	public static String duplicate(String source, int copies) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < copies; i++) {
			buf.append(source);
		}
		return buf.toString();
	}

	/**
	 * Switches the case of the supplied String. Any lower case characters will
	 * be uppercase and vice versa.
	 *
	 * @param source
	 *            the String to switch case of.
	 * @return the supplied String with switched case.
	 */
	public static String switchCase(String source) {
		char[] sourceArray = source.toCharArray();
		StringBuffer result = new StringBuffer();

		for (int i = 0; i < sourceArray.length; i++) {
			result.append(StringUtils.switchCase(sourceArray[i]));
		}
		return result.toString();
	}

	/**
	 * Switches the case of the supplied character. A lower case character will
	 * be uppercase and vice versa.
	 *
	 * @param source
	 *            the character to switch case of.
	 * @return the supplied character with switched case.
	 */
	public static char switchCase(char source) {
		if (Character.isUpperCase(source)) {
			return Character.toLowerCase(source);
		}
		if (Character.isLowerCase(source)) {
			return Character.toUpperCase(source);
		}
		return source;
	}

	public static int getInt(String theString) {
		int i = 0;
		try {
			i = Integer.valueOf(theString).intValue();
		} catch (Exception iex) {
		}
		return i;
	}

	public static float getFloat(String theString) {
		float i = 0;
		try {
			i = Float.valueOf(theString).floatValue();
		} catch (Exception iex) {
		}
		return i;
	}

	public static String arrayToString(String[] theArray) {
		String myString = "";
		for (int i = 0; i < theArray.length; i++) {
			myString += theArray[i] + ",";
		}
		myString = myString.substring(0, myString.length() - 1);
		return myString;
	}



        public static String arrayToString(String[] theArray, int theStart, int theEnd) {
          String myString = "";
          if (theArray.length > theStart) {
            for (int i = theStart; i < theEnd; i++) {
              myString += theArray[i]+" ";
            }
            myString = myString.substring(0,myString.length()-1);
          }
          return myString;
        }


}