directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r208736 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StringUtils.java
Date Fri, 01 Jul 2005 09:16:26 GMT
Author: elecharny
Date: Fri Jul  1 02:16:24 2005
New Revision: 208736

URL: http://svn.apache.org/viewcvs?rev=208736&view=rev
Log:
Moved some methods from DNUtils to this class. 

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StringUtils.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StringUtils.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StringUtils.java?rev=208736&r1=208735&r2=208736&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StringUtils.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StringUtils.java
Fri Jul  1 02:16:24 2005
@@ -27,7 +27,7 @@
     //~ Static fields/initializers -----------------------------------------------------------------
 
     /** Hex chars */
-    private static final byte[] HEX =
+    private static final byte[] HEX_CHAR =
         new byte[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F' };
     
     private static int UTF8_MULTI_BYTES_MASK = 0x0080;
@@ -47,6 +47,66 @@
     private static int UTF8_SIX_BYTES_MASK = 0x00FE;
     private static int UTF8_SIX_BYTES = 0x00FC;
     
+    /** <alpha>    ::= [0x41-0x5A] | [0x61-0x7A] */
+    public static final boolean[] ALPHA =
+    {
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        true, true, true, true, true, true, true, true, true, true, true, true, true, true,
true,
+        true, true, true, true, true, true, true, true, true, true, true, false, false, false,
+        false, false, false, true, true, true, true, true, true, true, true, true, true,
true,
+        true, true, true, true, true, true, true, true, true, true, true, true, true, true,
true,
+        false, false, false, false, false
+    };
+
+    /** <alpha> | <digit> | '-' */
+    public static final boolean[] CHAR =
+    {
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, true, false, false, true, true, true, true,
true,
+        true, true, true, true, true, false, false, false, false, false, false, false, true,
true,
+        true, true, true, true, true, true, true, true, true, true, true, true, true, true,
true,
+        true, true, true, true, true, true, true, true, true, false, false, false, false,
false,
+        false, true, true, true, true, true, true, true, true, true, true, true, true, true,
true,
+        true, true, true, true, true, true, true, true, true, true, true, true, false, false,
+        false, false, false
+    };
+
+    /** '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' */
+    public static final boolean[] DIGIT =
+    {
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, true, true, true,
true,
+        true, true, true, true, true, true, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false,
+    };
+
+    /** <hex>    ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66] */
+    private static final boolean[] HEX =
+    {
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false, true, true, true,
true,
+        true, true, true, true, true, true, false, false, false, false, false, false, false,
true,
+        true, true, true, true, true, false, false, false, false, false, false, false, false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, true, true, true, true, true, true, false, false,
false,
+        false, false, false, false, false, false, false, false, false, false, false, false,
false,
+        false, false, false, false, false, false, false, false, false
+    };
+
 
     //~ Methods ------------------------------------------------------------------------------------
 
@@ -59,7 +119,7 @@
     public static String dumpByte( byte octet )
     {
         return new String(
-                new byte[] { '[', HEX[( octet & 0x00F0 ) >> 4], HEX[octet &
0x000F], ']' } );
+                new byte[] { '[', HEX_CHAR[( octet & 0x00F0 ) >> 4], HEX_CHAR[octet
& 0x000F], ']' } );
     }
     
     /**
@@ -251,4 +311,262 @@
 
         return nbChars;
     }
+    
+    /**
+     * Check if a text is present at the current position in a buffer.
+     *
+     * @param byteArray The buffer which contains the data
+     * @param index Current position in the buffer
+     * @param text The text we want to check
+     *
+     * @return <code>true</code> if the buffer contains the text.
+     */
+    public static int areEquals( byte[] byteArray, int index, String text )
+    {
+        if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( byteArray.length <=
index ) ||
+                ( index < 0 ) || ( text == null ) )
+        {
+            return -1;
+        }
+        else
+        {
+            byte[] data = text.getBytes();
+
+            return areEquals( byteArray, index, data );
+        }
+    }
+
+    /**
+     * Check if a text is present at the current position in a buffer.
+     *
+     * @param charArray The buffer which contains the data
+     * @param index Current position in the buffer
+     * @param charArray2 The text we want to check
+     *
+     * @return <code>true</code> if the buffer contains the text.
+     */
+    public static int areEquals( char[] charArray, int index, char[] charArray2 )
+    {
+
+        if ( ( charArray == null ) ||
+                ( charArray.length == 0 ) ||
+                ( charArray.length <= index ) ||
+                ( index < 0 ) ||
+                ( charArray2 == null ) ||
+                ( charArray2.length == 0 ) ||
+                ( charArray2.length > ( charArray.length + index ) ) )
+        {
+            return -1;
+        }
+        else
+        {
+            for ( int i = 0; i < charArray2.length; i++ )
+            {
+                if ( charArray[index++] != charArray2[i] )
+                {
+                    return -1;
+                }
+            }
+
+            return index;
+        }
+    }
+
+    /**
+     * Check if a text is present at the current position in a buffer.
+     *
+     * @param byteArray The buffer which contains the data
+     * @param index Current position in the buffer
+     * @param byteArray2 The text we want to check
+     *
+     * @return <code>true</code> if the buffer contains the text.
+     */
+    public static int areEquals( byte[] byteArray, int index, byte[] byteArray2 )
+    {
+
+        if ( ( byteArray == null ) ||
+                ( byteArray.length == 0 ) ||
+                ( byteArray.length <= index ) ||
+                ( index < 0 ) ||
+                ( byteArray2 == null ) ||
+                ( byteArray2.length == 0 ) ||
+                ( byteArray2.length > ( byteArray.length + index ) ) )
+        {
+            return -1;
+        }
+        else
+        {
+            for ( int i = 0; i < byteArray2.length; i++ )
+            {
+                if ( byteArray[index++] != byteArray2[i] )
+                {
+                    return -1;
+                }
+            }
+
+            return index;
+        }
+    }
+
+    /**
+     * Test if the current character is equal to a specific character.
+     * This function works only for character between 0 and 127, as it
+     * does compare a byte and a char (which is 16 bits wide)
+     * 
+     *
+     * @param byteArray The buffer which contains the data
+     * @param index Current position in the buffer
+     * @param car The character we want to compare with the current buffer position
+     *
+     * @return <code>true</code> if the current character equals the given character.
+     */
+    public static boolean isCharASCII( byte[] byteArray, int index, char car )
+    {
+        if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+                ( index >= byteArray.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            return ( ( byteArray[index] == car ) ? true : false );
+        }
+    }
+
+    /**
+     * Check if the current character is an Hex Char
+     *  <hex>    ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
+     * 
+     * @param byteArray The buffer which contains the data
+     * @param index Current position in the buffer
+     *
+     * @return <code>true</code> if the current character is a Hex Char
+     */
+    public static boolean isHex( byte[] byteArray, int index )
+    {
+        if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+                ( index >= byteArray.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            byte c = byteArray[index];
+
+            if ( ( c > 127 ) || ( StringUtils.HEX[c] == false ) )
+            {
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
+
+    /**
+     * Test if the current character is a digit
+     * <digit>    ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
+     *
+     * @param byteArray The buffer which contains the data
+     *
+     * @return <code>true</code> if the current character is a Digit
+     */
+    public static boolean isDigit( byte[] byteArray )
+    {
+        if ( ( byteArray == null ) || ( byteArray.length == 0 ) )
+        {
+            return false;
+        }
+        else
+        {
+            return ( ( ( byteArray[0] > 127 ) || ! StringUtils.DIGIT[byteArray[0]] ) ?
false : true );
+        }
+    }
+
+    /**
+     * Test if the current character is an Alpha character :
+     *  <alpha>    ::= [0x41-0x5A] | [0x61-0x7A]
+     * 
+     * @param byteArray The buffer which contains the data
+     * @param index Current position in the buffer
+     *
+     * @return <code>true</code> if the current character is an Alpha character
+     */
+    public static boolean isAlphaASCII( byte[] byteArray, int index )
+    {
+        if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+                ( index >= byteArray.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            byte c = byteArray[index++];
+
+            if ( ( c > 127 ) || ( StringUtils.ALPHA[c] == false ) )
+            {
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
+
+    /**
+     * Test if the current character is a digit
+     * <digit>    ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
+     *
+     * @param byteArray The buffer which contains the data
+     * @param index Current position in the buffer
+     *
+     * @return <code>true</code> if the current character is a Digit
+     */
+    public static boolean isDigit( byte[] byteArray, int index )
+    {
+        if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+                ( index >= byteArray.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            return ( ( ( byteArray[index] > 127 ) || ! StringUtils.DIGIT[byteArray[index]]
) ? false : true );
+        }
+    }
+
+    /**
+     * Check if the current character is an 7 bits ASCII CHAR (between 0 and 127).
+     *   <char>    ::= <alpha> | <digit> | '-'
+     *
+     * @param byteArray The buffer which contains the data
+     * @param index Current position in the buffer
+     *
+     * @return The position of the next character, if the current one is a CHAR.
+     */
+    public static boolean isAlphaDigitMinus( byte[] byteArray, int index )
+    {
+        if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( index < 0 ) ||
+                ( index >= byteArray.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            byte c = byteArray[index++];
+
+            if ( ( c > 127 ) || ( StringUtils.CHAR[c] == false ) )
+            {
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
+
+    
 }



Mime
View raw message