Source code: org/esau/ptarmigan/util/HelperMisc.java
1 /* $Header: /cvsroot/ptarmigan/ptarmigan/src/java/org/esau/ptarmigan/util/HelperMisc.java,v 1.3 2002/09/19 03:35:45 reedesau Exp $ */
2
3 package org.esau.ptarmigan.util;
4
5 import java.io.File;
6 import java.io.IOException;
7
8 /**
9 * Some static methods of general use
10 *
11 * @author Reed Esau
12 * @version $Revision: 1.3 $ $Date: 2002/09/19 03:35:45 $
13 */
14 public final class HelperMisc {
15
16 //
17 // tolerant type-convertors
18 //
19
20 /** a safe integer parser */
21 public static int parseInt(String str, int default_val) {
22 try {
23 return Integer.parseInt(str);
24 }
25 catch (NumberFormatException ignore) {
26 }
27 return default_val;
28 }
29
30 //
31 // buffer methods
32 //
33
34 /** is the needle the first thing in the haystack? */
35 public static boolean startsWith(byte[] haystack,
36 int haystack_offset,
37 int haystack_count,
38 byte[] needle) {
39 if (haystack.length < haystack_offset + haystack_count)
40 throw new IllegalArgumentException("bad range: length=" + haystack.length
41 + " offset=" + haystack_offset
42 + " count=" + haystack_count);
43 if (haystack_count < needle.length)
44 return false; // not enough room for needle to exist
45 int i;
46 for (i = 0; i < needle.length; i++)
47 if (needle[i] != haystack[haystack_offset+i])
48 break;
49 return i == needle.length;
50 }
51
52 //
53 // String and buffer cleaners
54 //
55
56 /**
57 * Removes all non-alpha chars
58 *
59 * @return "" if all characters stripped.
60 */
61 public static String cleanAlpha(String str) {
62 StringBuffer buf = new StringBuffer();
63 int len = str.length();
64 for (int i = 0; i < len; i++) {
65 char ch = str.charAt(i);
66 if (Character.isLetter(ch))
67 buf.append(ch);
68 }
69 return buf.toString();
70 }
71
72 /**
73 * Removes all chars that do not fit into the categories (isLetterOrDigit,
74 * isWhitespace, printable ASCII); does not trim().
75 *
76 * TODO: figure out some way to include non-ASCII punctuation characters.
77 *
78 * @return "" if all characters stripped.
79 */
80 public static String clean(String str) {
81 final String whitespace = " \t\n\r"; //excludes VT and other whitespace chars
82 StringBuffer buf = new StringBuffer();
83 int len = str.length();
84 for (int i = 0; i < len; i++) {
85 char ch = str.charAt(i);
86 if (Character.isLetterOrDigit(ch)
87 || whitespace.indexOf(ch) >= 0
88 || (32 <= ch && ch < 127))
89 buf.append(ch);
90 }
91 return buf.toString();
92 }
93
94 //
95 // diagnostics (not in use presently)
96 //
97
98 // public static String dump(String str) {
99 // StringBuffer buf = new StringBuffer();
100 // for (int i = 0; i < str.length(); i++) {
101 // buf.append( Integer.toHexString( str.charAt(i)));
102 // if (i<str.length()-1)
103 // buf.append(',');
104 // }
105 // return buf.toString();
106 // }
107 //
108 // public static String dump(byte[] ar) {
109 // StringBuffer buf = new StringBuffer();
110 // for (int i = 0; i < ar.length; i++) {
111 // buf.append( Integer.toHexString(ar[i]) );
112 // if (i<ar.length-1)
113 // buf.append(',');
114 // }
115 // return buf.toString();
116 // }
117 }
118
119 /*
120 PTARMIGAN MODIFIED BSD LICENSE
121
122 Copyright (c) 2002, Reed Esau (reed.esau@pobox.com) All rights reserved.
123
124 Redistribution and use in source and binary forms, with or without
125 modification, are permitted provided that the following conditions are
126 met:
127
128 Redistributions of source code must retain the above copyright notice,
129 this list of conditions and the following disclaimer.
130
131 Redistributions in binary form must reproduce the above copyright notice,
132 this list of conditions and the following disclaimer in the documentation
133 and/or other materials provided with the distribution.
134
135 Neither the name of the Ptarmigan Project
136 (http://ptarmigan.sourceforge.net) nor the names of its contributors may
137 be used to endorse or promote products derived from this software without
138 specific prior written permission.
139
140 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
141 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
142 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
143 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
144 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
145 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
146 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
147 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
148 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
149 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
150 POSSIBILITY OF SUCH DAMAGE.
151 */