-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathStringUtilities.java
More file actions
203 lines (178 loc) · 6.38 KB
/
StringUtilities.java
File metadata and controls
203 lines (178 loc) · 6.38 KB
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
/**
* OWASP Enterprise Security API (ESAPI)
*
* This file is part of the Open Web Application Security Project (OWASP)
* Enterprise Security API (ESAPI) project. For details, please see
* <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
*
* Copyright (c) 2007 - The OWASP Foundation
*
* The ESAPI is published by OWASP under the BSD license. You should read and accept the
* LICENSE before you use, modify, and/or redistribute this software.
*
* @author Jeff Williams <a href="http://www.aspectsecurity.com">Aspect Security</a>
* @created 2007
*/
package org.owasp.esapi;
import java.util.Arrays;
import java.util.regex.Pattern;
/**
* String utilities used in various filters.
*
* @author Jeff Williams (jeff.williams .at. aspectsecurity.com) <a
* href="http://www.aspectsecurity.com">Aspect Security</a>
* @since June 1, 2007
*/
public class StringUtilities {
private static final Pattern p = Pattern.compile( "\\s");
public static String replaceLinearWhiteSpace( String input ) {
return p.matcher(input).replaceAll( " " );
}
/**
* Removes all unprintable characters from a string
* and replaces with a space.
* @param input
* @return the stripped value
*/
public static String stripControls( String input ) {
if ( input == null ) {
return null;
}
StringBuilder sb = new StringBuilder();
for ( int i=0; i<input.length(); i++ ) {
char c = input.charAt( i );
if ( c > 0x20 && c < 0x7f ) {
sb.append( c );
} else {
sb.append( ' ' );
}
}
return sb.toString();
}
/**
* Union multiple character arrays.
*
* @param list the char[]s to union
* @return the union of the char[]s
*/
public static char[] union(char[]... list) {
StringBuilder sb = new StringBuilder();
for (char[] characters : list) {
for ( char c : characters ) {
if ( !contains( sb, c ) )
sb.append( c );
}
}
char[] toReturn = new char[sb.length()];
sb.getChars(0, sb.length(), toReturn, 0);
Arrays.sort(toReturn);
return toReturn;
}
/**
* Returns true if the character is contained in the provided StringBuilder.
* @param input The input
* @param c The character to check for to see if {@code input} contains.
* @return True if the specified character is contained; false otherwise.
*/
public static boolean contains(StringBuilder input, char c) {
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == c)
return true;
}
return false;
}
/**
* Returns {@code replace} if {@code test} is null, "null" (case-insensitive), or blank, otherwise {@code test}
*
* @param test The value to test
* @param replace The replacement value
* @return The correct value
*/
public static String replaceNull( String test, String replace ) {
return test == null || "null".equalsIgnoreCase( test.trim() ) || "".equals( test.trim() ) ? replace : test;
}
/**
* Calculate the Edit Distance between 2 Strings as a measure of similarity.
*
* For example, if the strings GUMBO and GAMBOL are passed in, the edit distance
* is 2, since GUMBO transforms into GAMBOL by replacing the 'U' with an 'A' and
* adding an 'L'.
*
* Original Implementation of this algorithm by Michael Gilleland, adapted by
* Chas Emerick for the Apache-Commons project
* http://www.merriampark.com/ldjava.htm
*
* @param s The source string
* @param t The target String
* @return The edit distance between the 2 strings
*/
public static int getLevenshteinDistance (String s, String t) {
if (s == null || t == null) {
throw new IllegalArgumentException("Strings must not be null");
}
int n = s.length(); // length of s
int m = t.length(); // length of t
if (n == 0) {
return m;
} else if (m == 0) {
return n;
}
int p[] = new int[n+1]; //'previous' cost array, horizontally
int d[] = new int[n+1]; // cost array, horizontally
int _d[]; //placeholder to assist in swapping p and d
// indexes into strings s and t
int i; // iterates through s
int j; // iterates through t
char t_j; // jth character of t
int cost; // cost
for (i = 0; i<=n; i++) {
p[i] = i;
}
for (j = 1; j<=m; j++) {
t_j = t.charAt(j-1);
d[0] = j;
for (i=1; i<=n; i++) {
cost = s.charAt(i-1)==t_j ? 0 : 1;
// minimum of cell to the left+1, to the top+1, diagonally left and up +cost
d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+cost);
}
// copy current distance counts to 'previous row' distance counts
_d = p;
p = d;
d = _d;
}
// our last action in the above loop was to switch d and p, so p now
// actually has the most recent cost counts
return p[n];
}
/**
* Check to ensure that a {@code String} is not null or empty (after optional
* trimming of leading and trailing whitespace). Usually used with
* assertions, as in
* <pre>
* assert StringUtils.notNullOrEmpty(cipherXform, true) :
* "Cipher transformation may not be null or empty!";
* </pre>
* or an equivalent runtime check that throws an {@code IllegalArgumentException}.
*
* @param str The {@code String} to be checked.
* @param trim If {@code true}, the string is first trimmed before checking
* to see if it is empty, otherwise it is not.
* @return True if the string is null or empty (after possible
* trimming); otherwise false.
* @since 2.0
*/
public static boolean notNullOrEmpty(String str, boolean trim) {
if ( trim ) {
return !( str == null || str.trim().equals("") );
} else {
return !( str == null || str.equals("") );
}
}
/**
* Returns true if String is empty ("") or null.
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
}