You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by el...@apache.org on 2007/01/01 22:41:43 UTC
svn commit: r491640 -
/labs/dungeon/src/main/java/org/apache/dungeon/util/StringTools.java
Author: elecharny
Date: Mon Jan 1 13:41:43 2007
New Revision: 491640
URL: http://svn.apache.org/viewvc?view=rev&rev=491640
Log:
Fixed a lot of potential NPE and a *big* bug when dealing with chars above 0x7FFF
Modified:
labs/dungeon/src/main/java/org/apache/dungeon/util/StringTools.java
Modified: labs/dungeon/src/main/java/org/apache/dungeon/util/StringTools.java
URL: http://svn.apache.org/viewvc/labs/dungeon/src/main/java/org/apache/dungeon/util/StringTools.java?view=diff&rev=491640&r1=491639&r2=491640
==============================================================================
--- labs/dungeon/src/main/java/org/apache/dungeon/util/StringTools.java (original)
+++ labs/dungeon/src/main/java/org/apache/dungeon/util/StringTools.java Mon Jan 1 13:41:43 2007
@@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;
import java.io.FileFilter;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.ArrayList;
@@ -81,7 +82,7 @@
private static final int UTF8_SIX_BYTES = 0x00FC;
/** <alpha> ::= [0x41-0x5A] | [0x61-0x7A] */
- public static final boolean[] ALPHA =
+ private static final boolean[] ALPHA =
{
false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false,
@@ -102,7 +103,7 @@
};
/** <alpha> ::= [0x41-0x5A] | [0x61-0x7A] | '_'*/
- public static final boolean[] IDENT =
+ private static final boolean[] IDENT =
{
false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false,
@@ -123,7 +124,7 @@
};
/** <alpha> | <digit> | '-' */
- public static final boolean[] CHAR =
+ private static final boolean[] CHAR =
{
false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false,
@@ -144,7 +145,7 @@
};
/** '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' */
- public static final boolean[] DIGIT =
+ private static final boolean[] DIGIT =
{
false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false,
@@ -185,7 +186,7 @@
false, false, false, false, false, false, false, false };
/** <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66] */
- public static final byte[] HEX_VALUE =
+ private static final byte[] HEX_VALUE =
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00 -> 0F
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10 -> 1F
@@ -197,7 +198,7 @@
};
/** lowerCase = 'a' .. 'z', '0'..'9', '-' */
- public static final char[] LOWER_CASE =
+ private static final char[] LOWER_CASE =
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
@@ -733,7 +734,6 @@
*/
public static final List getPaths( String paths, FileFilter filter )
{
- final int max = paths.length() - 1;
int start = 0;
int stop = -1;
String path = null;
@@ -745,6 +745,8 @@
return list;
}
+ final int max = paths.length() - 1;
+
// Loop spliting string using OS path separator: terminate
// when the start index is at the end of the paths string.
while ( start < max )
@@ -1182,13 +1184,13 @@
{
byte[] bytes = new byte[countNbBytesPerChar( car )];
- if ( car <= 0x7F )
+ if ( ( car | 0x7F ) == 0x7F )
{
// Single byte char
bytes[0] = ( byte ) car;
return bytes;
}
- else if ( car <= 0x7FF )
+ else if ( ( car | 0x7F) == 0x7FF )
{
// two bytes char
bytes[0] = ( byte ) ( 0x00C0 + ( ( car & 0x07C0 ) >> 6 ) );
@@ -1331,7 +1333,7 @@
/**
* Check if a text is present at the current position in another string.
*
- * @param string1
+ * @param string
* The string which contains the data
* @param index
* Current position in the string
@@ -1339,12 +1341,17 @@
* The text we want to check
* @return <code>true</code> if the string contains the text.
*/
- public static final boolean areEquals( String string1, int index, String text )
+ public static final boolean areEquals( String string, int index, String text )
{
- int length1 = string1.length();
+ if ( ( string == null ) || ( text == null ) )
+ {
+ return false;
+ }
+
+ int length1 = string.length();
int length2 = text.length();
- if ( ( string1 == null ) || ( length1 == 0 ) || ( length1 <= index ) || ( index < 0 )
+ if ( ( length1 == 0 ) || ( length1 <= index ) || ( index < 0 )
|| ( text == null ) || ( length2 == 0 )
|| ( length2 > ( length1 + index ) ) )
{
@@ -1352,7 +1359,7 @@
}
else
{
- return string1.substring( index ).startsWith( text );
+ return string.substring( index ).startsWith( text );
}
}
@@ -1522,9 +1529,14 @@
*/
public static final boolean isCharASCII( String string, int index, char car )
{
+ if ( string == null )
+ {
+ return false;
+ }
+
int length = string.length();
- if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+ if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
{
return false;
}
@@ -1545,9 +1557,14 @@
*/
public static final char charAt( String string, int index )
{
+ if ( string == null )
+ {
+ return '\0';
+ }
+
int length = string.length();
- if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+ if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
{
return '\0';
}
@@ -1578,7 +1595,7 @@
{
byte c = byteArray[index];
- if ( ( c > 127 ) || ( HEX[c] == false ) )
+ if ( ( ( c | 0x7F ) != 0x7F ) || ( HEX[c] == false ) )
{
return false;
}
@@ -1610,7 +1627,7 @@
{
char c = chars[index];
- if ( ( c > 127 ) || ( HEX[c] == false ) )
+ if ( ( ( c | 0x7F ) != 0x7F ) || ( HEX[c] == false ) )
{
return false;
}
@@ -1633,9 +1650,14 @@
*/
public static final boolean isHex( String string, int index )
{
+ if ( string == null )
+ {
+ return false;
+ }
+
int length = string.length();
- if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+ if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
{
return false;
}
@@ -1643,7 +1665,7 @@
{
char c = string.charAt( index );
- if ( ( c > 127 ) || ( HEX[c] == false ) )
+ if ( ( ( c | 0x7F ) != 0x7F ) || ( HEX[c] == false ) )
{
return false;
}
@@ -1671,7 +1693,7 @@
}
else
{
- return ( ( ( byteArray[0] > 127 ) || !DIGIT[byteArray[0]] ) ? false : true );
+ return ( ( ( ( byteArray[0] | 0x7F ) != 0x7F ) || !DIGIT[byteArray[0]] ) ? false : true );
}
}
@@ -1710,7 +1732,7 @@
{
byte c = byteArray[index++];
- if ( ( c > 127 ) || ( ALPHA[c] == false ) )
+ if ( ( ( c | 0x7F ) != 0x7F ) || ( ALPHA[c] == false ) )
{
return false;
}
@@ -1743,7 +1765,7 @@
{
char c = chars[index++];
- if ( ( c > 127 ) || ( ALPHA[c] == false ) )
+ if ( ( ( c | 0x7F ) != 0x7F ) || ( ALPHA[c] == false ) )
{
return false;
}
@@ -1776,7 +1798,7 @@
{
char c = chars[index++];
- if ( ( c > 127 ) || ( IDENT[c] == false ) )
+ if ( ( ( c | 0x7F ) != 0x7F ) || ( IDENT[c] == false ) )
{
return false;
}
@@ -1800,9 +1822,14 @@
*/
public static final boolean isAlphaASCII( String string, int index )
{
+ if ( string == null )
+ {
+ return false;
+ }
+
int length = string.length();
- if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+ if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
{
return false;
}
@@ -1810,7 +1837,7 @@
{
char c = string.charAt( index++ );
- if ( ( c > 127 ) || ( ALPHA[c] == false ) )
+ if ( ( ( c | 0x7F ) != 0x7F ) || ( ALPHA[c] == false ) )
{
return false;
}
@@ -1840,7 +1867,7 @@
}
else
{
- return ( ( ( byteArray[index] > 127 ) || !DIGIT[byteArray[index]] ) ? false : true );
+ return ( ( ( ( byteArray[index] | 0x7F ) != 0x7F ) || !DIGIT[byteArray[index]] ) ? false : true );
}
}
@@ -1863,7 +1890,7 @@
}
else
{
- return ( ( ( chars[index] > 127 ) || !DIGIT[chars[index]] ) ? false : true );
+ return ( ( ( ( chars[index] | 0x7F ) != 0x7F ) || !DIGIT[chars[index]] ) ? false : true );
}
}
@@ -1879,16 +1906,21 @@
*/
public static final boolean isDigit( String string, int index )
{
+ if ( string == null )
+ {
+ return false;
+ }
+
int length = string.length();
- if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+ if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
{
return false;
}
else
{
char c = string.charAt( index );
- return ( ( ( c > 127 ) || !DIGIT[c] ) ? false : true );
+ return ( ( ( ( c | 0x7F ) != 0x7F ) || !DIGIT[c] ) ? false : true );
}
}
@@ -1908,7 +1940,7 @@
}
else
{
- return ( ( ( chars[0] > 127 ) || !DIGIT[chars[0]] ) ? false : true );
+ return ( ( ( ( chars[0] | 0x7F ) != 0x7F ) || !DIGIT[chars[0]] ) ? false : true );
}
}
@@ -1933,7 +1965,7 @@
{
byte c = byteArray[index++];
- if ( ( c > 127 ) || ( CHAR[c] == false ) )
+ if ( ( ( c | 0x7F ) != 0x7F ) || ( CHAR[c] == false ) )
{
return false;
}
@@ -1965,7 +1997,7 @@
{
char c = chars[index++];
- if ( ( c > 127 ) || ( CHAR[c] == false ) )
+ if ( ( ( c | 0x7F ) != 0x7F ) || ( CHAR[c] == false ) )
{
return false;
}
@@ -1988,9 +2020,14 @@
*/
public static final boolean isAlphaDigitMinus( String string, int index )
{
+ if ( string == null )
+ {
+ return false;
+ }
+
int length = string.length();
- if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+ if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
{
return false;
}
@@ -1998,7 +2035,7 @@
{
char c = string.charAt( index++ );
- if ( ( c > 127 ) || ( CHAR[c] == false ) )
+ if ( ( ( c | 0x7F ) != 0x7F ) || ( CHAR[c] == false ) )
{
return false;
}
@@ -2804,7 +2841,7 @@
*/
public static final String getDefaultCharsetName()
{
- if (null == defaultCharset)
+ if ( null == defaultCharset )
{
try
{
@@ -2812,7 +2849,19 @@
Method method = Charset.class.getMethod( "defaultCharset", new Class[0] );
defaultCharset = ((Charset) method.invoke( null, new Object[0]) ).name();
}
- catch (Exception e)
+ catch ( NoSuchMethodException nsme )
+ {
+ defaultCharset = new OutputStreamWriter( new ByteArrayOutputStream() ).getEncoding();
+ }
+ catch ( InvocationTargetException ite )
+ {
+ defaultCharset = new OutputStreamWriter( new ByteArrayOutputStream() ).getEncoding();
+ }
+ catch ( IllegalAccessException iea )
+ {
+ defaultCharset = new OutputStreamWriter( new ByteArrayOutputStream() ).getEncoding();
+ }
+ catch ( RuntimeException e )
{
// fall back to old method
defaultCharset = new OutputStreamWriter( new ByteArrayOutputStream() ).getEncoding();
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org