// StringParseUtil.java

package ed.util;

public final class StringParseUtil {
    

    public static boolean parseBoolean( String s , boolean d ){

        if ( s == null )
            return d;
        
        s = s.trim();
        if ( s.length() == 0 )
            return d;
        
        char c = s.charAt( 0 );

        if ( c == 't' || c == 'T' ||
             c == 'y' || c == 'Y' )
            return true;

        if ( c == 'f' || c == 'F' ||
             c == 'n' || c == 'N' )
            return false;
        
        return d;
    }

    public static int parseInt( String s , int def ){
        return parseInt( s , def , null , true );
    }

    public static Number parseIntRadix( String s , int radix ){
        if ( s == null )
            return Double.NaN;
        
        s = s.trim();
        if ( s.length() == 0 )
            return Double.NaN;

        int firstDigit = -1;
        int i = 0;
        if ( s.charAt( 0 ) == '-' ) 
            i = 1;
        for ( ; i<s.length(); i++ ){
            char c = s.charAt ( i );
            if ( '0' <= c && c <= '9' && c < '0' + radix ){
                // OK; c is 0..n-1
            }
            else if ( 'a' <= c && c <= 'z' && c < 'a' + radix - 10 ){
                // OK; if radix is 11, c can be 'a', etc.
            }
            else if ( 'A' <= c && c <= 'Z' && c < 'A' + radix - 10 ){
                // OK; same as above, with caps.
            }
            else {
                break;
            }
        }
        
        try {
            return Integer.parseInt( s.substring( 0, i ) , radix );
        }
        catch (Exception e) {
            return Double.NaN;
        }
    }

    public static int parseInt( String s , int def , final int[] lastIdx , final boolean allowNegative ){
        final boolean useLastIdx = lastIdx != null && lastIdx.length > 0;
        if ( useLastIdx )
            lastIdx[0] = -1;
        
        if ( s == null )
            return def;
        
        s = s.trim();
        if ( s.length() == 0 )
            return def;

        int firstDigit = -1;
        for ( int i=0; i<s.length(); i++ ){
            if ( Character.isDigit( s.charAt( i ) ) ){
                firstDigit = i;
                break;
            }
        }

        if ( firstDigit < 0 )
            return def;
        
        int lastDigit = firstDigit + 1;
        while ( lastDigit < s.length() && Character.isDigit( s.charAt( lastDigit ) ) )
            lastDigit++;
        
        if ( allowNegative && firstDigit > 0 && s.charAt( firstDigit - 1 ) == '-' )
            firstDigit--;
        
        if ( useLastIdx )
            lastIdx[0] = lastDigit;
        return Integer.parseInt( s.substring( firstDigit , lastDigit ) );
    }

    public static Number parseNumber( String s , Number def ){
        if ( s == null )
            return def;
        
        s = s.trim();
        if ( s.length() == 0)
            return def;


        int firstDigit = -1;
        for ( int i=0; i<s.length(); i++ ){
            if ( Character.isDigit( s.charAt( i ) ) ){
                firstDigit = i;
                break;
            }
        }        

        if ( firstDigit < 0 )
            return def;

        int lastDigit = firstDigit + 1;
        while ( lastDigit < s.length() && Character.isDigit( s.charAt( lastDigit ) ) )
            lastDigit++;
        
        if ( firstDigit > 0 && s.charAt( firstDigit - 1 ) == '-' )
            firstDigit--;
        
        if ( lastDigit < s.length() && s.charAt( lastDigit ) == '.' ){
            lastDigit++;
            while ( lastDigit < s.length() && Character.isDigit( s.charAt( lastDigit ) ) )
                lastDigit++;    
            return Double.parseDouble( s.substring( firstDigit , lastDigit ) );
        }

        return Integer.parseInt( s.substring( firstDigit , lastDigit ) );


    }


}
   

@@ -974,24 +974,57 @@
      i = 1;%0A
+        // Find first non-digit.%0A
         for 
@@ -1056,32 +1056,45 @@
             
-char c =
+if ( Character.digit(
  s.charAt ( 
@@ -1094,418 +1094,31 @@
 arAt
- 
 ( i )
-;%0A            if ( '0' %3C= c && c %3C= '9' && c %3C '0' + radix )%7B%0A                // OK; c is 0..n-1%0A            %7D%0A            else if ( 'a' %3C= c && c %3C= 'z' && c %3C 'a' + radix - 10 )%7B%0A                // OK; if radix is 11, c can be 'a', etc.%0A            %7D%0A            else if ( 'A' %3C= c && c %3C= 'Z' && c %3C 'A' + radix - 10 )%7B%0A                // OK; same as above, with caps.%0A            %7D%0A            else %7B
+ , radix ) == -1 )
 %0A   
@@ -1133,38 +1133,24 @@
      break;%0A
-            %7D%0A
         %7D%0A  

Simplify code using Java functions.