You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/10/09 13:03:29 UTC

svn commit: r307394 - /directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java

Author: elecharny
Date: Sun Oct  9 04:03:23 2005
New Revision: 307394

URL: http://svn.apache.org/viewcvs?rev=307394&view=rev
Log:
* added a lot of functions like trim, trimleft, trimright, lowercase ... that accepts a char[] instead of a String
* added functions which accepts char[] instead of byte[]
* renamed some methods.

Modified:
    directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java

Modified: directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java
URL: http://svn.apache.org/viewcvs/directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java?rev=307394&r1=307393&r2=307394&view=diff
==============================================================================
--- directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java (original)
+++ directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/util/StringUtils.java Sun Oct  9 04:03:23 2005
@@ -16,6 +16,8 @@
  */
 package org.apache.asn1new.util;
 
+import java.io.UnsupportedEncodingException;
+
 /**
  * Little helper class. Nothing that should stay here, but I need those 
  * to debug.
@@ -107,6 +109,19 @@
         false, false, false, false, false, false, false, false, false
     };
 
+    private static int CHAR_ONE_BYTE_MASK    = 0xFFFFFF80;
+    
+    private static int CHAR_TWO_BYTES_MASK   = 0xFFFFF800;
+    
+    private static int CHAR_THREE_BYTES_MASK = 0xFFFF0000;
+    
+    private static int CHAR_FOUR_BYTES_MASK  = 0xFFE00000;
+    
+    private static int CHAR_FIVE_BYTES_MASK  = 0xFC000000;
+    
+    private static int CHAR_SIX_BYTES_MASK   = 0x80000000;
+    
+    public static final int NOT_EQUAL = -1;
 
     //~ Methods ------------------------------------------------------------------------------------
 
@@ -193,6 +208,69 @@
     }
     
     /**
+     * Return the number of bytes that hold an Unicode char.
+     * 
+     * @param car The character to be decoded
+     * @return The number of bytes to hold the char.
+     * 
+     * TODO : Should stop after the third byte, as a char is only 2 bytes long.
+     */
+    public static int countNbBytesPerChar( char car )
+    {
+        if ( ( car & CHAR_ONE_BYTE_MASK ) == 0 )
+        {
+            return 1;
+        }
+        else if ( ( car & CHAR_TWO_BYTES_MASK ) == 0 )
+        {
+            return 2;
+        }
+        else if ( ( car & CHAR_THREE_BYTES_MASK ) == 0 )
+        {
+            return 3;
+        }
+        else if ( ( car & CHAR_FOUR_BYTES_MASK ) == 0 )
+        {
+            return 4;
+        }
+        else if ( ( car & CHAR_FIVE_BYTES_MASK ) == 0 )
+        {
+            return 5;
+        }
+        else if ( ( car & CHAR_SIX_BYTES_MASK ) == 0 )
+        {
+            return 6;
+        } 
+        else
+        {
+            return -1;
+        }
+    }
+    
+    /**
+     * Count the number of bytes included in the given char[].  
+     * @param chars The char array to decode
+     * @return The number of bytes in the char array
+     */
+    public static int countBytes(char[] chars) throws UnsupportedEncodingException
+    {
+        int nbBytes = 0;
+        int currentPos = 0;
+        
+        while ( currentPos < chars.length )
+        {
+            int nbb = countNbBytesPerChar( chars[currentPos] );
+            
+            // If the number of bytes necessary to encode a character is
+            // above 3, we will need two UTF-16 chars
+            currentPos += (nbb < 4 ? 1 : 2 );
+            nbBytes += nbb;
+        }
+
+        return nbBytes;
+    }
+    
+    /**
      * Return the Unicode char which is coded in the bytes at the given position. 
      * @param bytes The byte[] represntation of an Unicode string. 
      * @param pos The current position to start decoding the char
@@ -344,13 +422,44 @@
         if ( ( byteArray == null ) || ( byteArray.length == 0 ) || ( byteArray.length <= index ) ||
                 ( index < 0 ) || ( text == null ) )
         {
-            return -1;
+            return NOT_EQUAL;
+        }
+        else
+        {
+            try
+            {
+                byte[] data = text.getBytes( "UTF-8" );
+
+                return areEquals( byteArray, index, data );
+            }
+            catch ( UnsupportedEncodingException uee )
+            {
+                return NOT_EQUAL;
+            }
+        }
+    }
+
+    /**
+     * 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( char[] charArray, int index, String text )
+    {
+        if ( ( charArray == null ) || ( charArray.length == 0 ) || ( charArray.length <= index ) ||
+                ( index < 0 ) || ( text == null ) )
+        {
+            return NOT_EQUAL;
         }
         else
         {
-            byte[] data = text.getBytes();
+            char[] data = text.toCharArray();
 
-            return areEquals( byteArray, index, data );
+            return areEquals( charArray, index, data );
         }
     }
 
@@ -374,7 +483,7 @@
                 ( charArray2.length == 0 ) ||
                 ( charArray2.length > ( charArray.length + index ) ) )
         {
-            return -1;
+            return NOT_EQUAL;
         }
         else
         {
@@ -382,7 +491,7 @@
             {
                 if ( charArray[index++] != charArray2[i] )
                 {
-                    return -1;
+                    return NOT_EQUAL;
                 }
             }
 
@@ -410,7 +519,7 @@
                 ( byteArray2.length == 0 ) ||
                 ( byteArray2.length > ( byteArray.length + index ) ) )
         {
-            return -1;
+            return NOT_EQUAL;
         }
         else
         {
@@ -418,7 +527,7 @@
             {
                 if ( byteArray[index++] != byteArray2[i] )
                 {
-                    return -1;
+                    return NOT_EQUAL;
                 }
             }
 
@@ -452,6 +561,29 @@
     }
 
     /**
+     * Test if the current character is equal to a specific character.
+     * 
+     *
+     * @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( char[] chars, int index, char car )
+    {
+        if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+                ( index >= chars.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            return ( ( chars[index] == car ) ? true : false );
+        }
+    }
+
+    /**
      * Check if the current character is an Hex Char
      *  <hex>    ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
      * 
@@ -483,6 +615,37 @@
     }
 
     /**
+     * Check if the current character is an Hex Char
+     *  <hex>    ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
+     * 
+     * @param chars 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( char[] chars, int index )
+    {
+        if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+                ( index >= chars.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            char c = chars[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'
      *
@@ -534,6 +697,37 @@
     }
 
     /**
+     * Test if the current character is an Alpha character :
+     *  <alpha>    ::= [0x41-0x5A] | [0x61-0x7A]
+     * 
+     * @param chars 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( char[] chars, int index )
+    {
+        if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+                ( index >= chars.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            char c = chars[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'
      *
@@ -556,6 +750,48 @@
     }
 
     /**
+     * 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( char[] chars, int index )
+    {
+        if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+                ( index >= chars.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            return ( ( ( chars[index] > 127 ) || ! StringUtils.DIGIT[chars[index]] ) ? false : 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( char[] chars )
+    {
+        if ( ( chars == null ) || ( chars.length == 0 ) )
+        {
+            return false;
+        }
+        else
+        {
+            return ( ( ( chars[0] > 127 ) || ! StringUtils.DIGIT[chars[0]] ) ? false : true );
+        }
+    }
+
+    /**
      * Check if the current character is an 7 bits ASCII CHAR (between 0 and 127).
      *   <char>    ::= <alpha> | <digit> | '-'
      *
@@ -586,5 +822,398 @@
         }
     }
 
+    /**
+     * Check if the current character is an 7 bits ASCII CHAR (between 0 and 127).
+     *   <char>    ::= <alpha> | <digit> | '-'
+     *
+     * @param chars 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( char[] chars, int index )
+    {
+        if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) ||
+                ( index >= chars.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            char c = chars[index++];
+
+            if ( ( c > 127 ) || ( StringUtils.CHAR[c] == false ) )
+            {
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
     
+    // The following methods are taken from org.apache.commons.lang.StringUtils
+    
+    /**
+     * The empty String <code>""</code>.
+     * @since 2.0
+     */
+    public static final String EMPTY = "";
+
+    // Empty checks
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Checks if a String is empty ("") or null.</p>
+     *
+     * <pre>
+     * StringUtils.isEmpty(null)      = true
+     * StringUtils.isEmpty("")        = true
+     * StringUtils.isEmpty(" ")       = false
+     * StringUtils.isEmpty("bob")     = false
+     * StringUtils.isEmpty("  bob  ") = false
+     * </pre>
+     *
+     * <p>NOTE: This method changed in Lang version 2.0.
+     * It no longer trims the String.
+     * That functionality is available in isBlank().</p>
+     *
+     * @param str  the String to check, may be null
+     * @return <code>true</code> if the String is empty or null
+     */
+    public static boolean isEmpty(String str) {
+        return str == null || str.length() == 0;
+    }
+
+    /**
+     * <p>Checks if a String is not empty ("") and not null.</p>
+     *
+     * <pre>
+     * StringUtils.isNotEmpty(null)      = false
+     * StringUtils.isNotEmpty("")        = false
+     * StringUtils.isNotEmpty(" ")       = true
+     * StringUtils.isNotEmpty("bob")     = true
+     * StringUtils.isNotEmpty("  bob  ") = true
+     * </pre>
+     *
+     * @param str  the String to check, may be null
+     * @return <code>true</code> if the String is not empty and not null
+     */
+    public static boolean isNotEmpty(String str) {
+        return str != null && str.length() > 0;
+    }
+
+    /**
+     * <p>Removes spaces (char &lt;= 32) from both start and
+     * ends of this String, handling <code>null</code> by returning
+     * <code>null</code>.</p>
+     *
+     * Trim removes start and end characters &lt;= 32.
+     *
+     * <pre>
+     * StringUtils.trim(null)          = null
+     * StringUtils.trim("")            = ""
+     * StringUtils.trim("     ")       = ""
+     * StringUtils.trim("abc")         = "abc"
+     * StringUtils.trim("    abc    ") = "abc"
+     * </pre>
+     *
+     * @param str  the String to be trimmed, may be null
+     * @return the trimmed string, <code>null</code> if null String input
+     */
+    public static String trim(String str) {
+        if ( isEmpty( str ) )
+        {
+            return str;
+        }
+        
+        char[] array = str.toCharArray();
+        int start = 0;
+        int end = array.length;
+        
+        while ( ( start < end ) && ( array[start] == ' ' ) )
+        {
+            start++;
+        }
+        
+        while ( ( end > start ) && ( array[end - 1] == ' ' ) )
+        {
+            end--;
+        }
+        
+        return new String( array, start, ( end - start ) );
+    }
+
+    /**
+     * <p>Removes spaces (char &lt;= 32) from start
+     * of this String, handling <code>null</code> by returning
+     * <code>null</code>.</p>
+     *
+     * Trim removes start characters &lt;= 32.
+     *
+     * <pre>
+     * StringUtils.trimLeft(null)          = null
+     * StringUtils.trimLeft("")            = ""
+     * StringUtils.trimLeft("     ")       = ""
+     * StringUtils.trimLeft("abc")         = "abc"
+     * StringUtils.trimLeft("    abc    ") = "abc    "
+     * </pre>
+     *
+     * @param str  the String to be trimmed, may be null
+     * @return the trimmed string, <code>null</code> if null String input
+     */
+    public static String trimLeft( String str ) {
+        if ( isEmpty( str ) )
+        {
+            return str;
+        }
+        
+        char[] array = str.toCharArray();
+        int start = 0;
+        
+        while ( ( start < array.length ) && ( array[start] == ' ' ) )
+        {
+            start++;
+        }
+        
+        return new String( array, start, array.length - start );
+    }
+    /**
+     * <p>Removes spaces (char &lt;= 32) from start
+     * of this array, handling <code>null</code> by returning
+     * <code>null</code>.</p>
+     *
+     * Trim removes start characters &lt;= 32.
+     *
+     * <pre>
+     * StringUtils.trimLeft(null)          = null
+     * StringUtils.trimLeft("")            = ""
+     * StringUtils.trimLeft("     ")       = ""
+     * StringUtils.trimLeft("abc")         = "abc"
+     * StringUtils.trimLeft("    abc    ") = "abc    "
+     * </pre>
+     *
+     * @param chars  the chars array to be trimmed, may be null
+     * @return the position of the first char which is not a space,
+     * or the last position of the array.
+     */
+    public static int trimLeft( char[] chars, int pos ) {
+        if ( chars == null )
+        {
+            return pos;
+        }
+        
+        while ( ( pos < chars.length ) && ( chars[pos] == ' ' ) )
+        {
+            pos++;
+        }
+        
+        return pos;
+    }
+
+    /**
+     * <p>Removes spaces (char &lt;= 32) from start
+     * of this array, handling <code>null</code> by returning
+     * <code>null</code>.</p>
+     *
+     * Trim removes start characters &lt;= 32.
+     *
+     * <pre>
+     * StringUtils.trimLeft(null)          = null
+     * StringUtils.trimLeft("")            = ""
+     * StringUtils.trimLeft("     ")       = ""
+     * StringUtils.trimLeft("abc")         = "abc"
+     * StringUtils.trimLeft("    abc    ") = "abc    "
+     * </pre>
+     *
+     * @param bytes  the byte array to be trimmed, may be null
+     * @return the position of the first byte which is not a space,
+     * or the last position of the array.
+     */
+    public static int trimLeft( byte[] bytes, int pos ) {
+        if ( bytes == null )
+        {
+            return pos;
+        }
+        
+        while ( ( pos < bytes.length ) && ( bytes[pos] == ' ' ) )
+        {
+            pos++;
+        }
+        
+        return pos;
+    }
+
+
+    /**
+     * <p>Removes spaces (char &lt;= 32) from end
+     * of this String, handling <code>null</code> by returning
+     * <code>null</code>.</p>
+     *
+     * Trim removes start characters &lt;= 32.
+     *
+     * <pre>
+     * StringUtils.trimRight(null)          = null
+     * StringUtils.trimRight("")            = ""
+     * StringUtils.trimRight("     ")       = ""
+     * StringUtils.trimRight("abc")         = "abc"
+     * StringUtils.trimRight("    abc    ") = "    abc"
+     * </pre>
+     *
+     * @param str  the String to be trimmed, may be null
+     * @return the trimmed string, <code>null</code> if null String input
+     */
+    public static String trimRight( String str ) {
+        if ( isEmpty( str ) )
+        {
+            return str;
+        }
+        
+        char[] array = str.toCharArray();
+        int start = 0;
+        int end = array.length;
+        
+        while ( ( start < end ) && ( array[start] == ' ' ) )
+        {
+            start++;
+        }
+        
+        return new String( array, start, ( end - start ) );
+    }
+
+    /**
+     * <p>Removes spaces (char &lt;= 32) from end
+     * of this array, handling <code>null</code> by returning
+     * <code>null</code>.</p>
+     *
+     * Trim removes start characters &lt;= 32.
+     *
+     * <pre>
+     * StringUtils.trimRight(null)          = null
+     * StringUtils.trimRight("")            = ""
+     * StringUtils.trimRight("     ")       = ""
+     * StringUtils.trimRight("abc")         = "abc"
+     * StringUtils.trimRight("    abc    ") = "    abc"
+     * </pre>
+     *
+     * @param chars  the chars array to be trimmed, may be null
+     * @return the position of the first char which is not a space,
+     * or the last position of the array.
+     */
+    public static int trimRight( char[] chars, int pos ) {
+        if ( chars == null )
+        {
+            return pos;
+        }
+        
+        while ( ( pos > 0 ) && ( chars[pos - 1] == ' ' ) )
+        {
+            pos--;
+        }
+        
+        return pos;
+    }
+
+    /**
+     * <p>Removes spaces (char &lt;= 32) from end
+     * of this array, handling <code>null</code> by returning
+     * <code>null</code>.</p>
+     *
+     * Trim removes start characters &lt;= 32.
+     *
+     * <pre>
+     * StringUtils.trimRight(null)          = null
+     * StringUtils.trimRight("")            = ""
+     * StringUtils.trimRight("     ")       = ""
+     * StringUtils.trimRight("abc")         = "abc"
+     * StringUtils.trimRight("    abc    ") = "    abc"
+     * </pre>
+     *
+     * @param chars  the chars array to be trimmed, may be null
+     * @return the position of the first char which is not a space,
+     * or the last position of the array.
+     */
+    public static int trimRight( byte[] bytes, int pos ) {
+        if ( bytes == null )
+        {
+            return pos;
+        }
+        
+        while ( ( pos >= 0 ) && ( bytes[pos] == ' ' ) )
+        {
+            pos--;
+        }
+        
+        return pos;
+    }
+
+    // Case conversion
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Converts a String to upper case as per {@link String#toUpperCase()}.</p>
+     *
+     * <p>A <code>null</code> input String returns <code>null</code>.</p>
+     *
+     * <pre>
+     * StringUtils.upperCase(null)  = null
+     * StringUtils.upperCase("")    = ""
+     * StringUtils.upperCase("aBc") = "ABC"
+     * </pre>
+     *
+     * @param str  the String to upper case, may be null
+     * @return the upper cased String, <code>null</code> if null String input
+     */
+    public static String upperCase(String str) {
+        if (str == null) {
+            return null;
+        }
+        return str.toUpperCase();
+    }
+
+    /**
+     * <p>Converts a String to lower case as per {@link String#toLowerCase()}.</p>
+     *
+     * <p>A <code>null</code> input String returns <code>null</code>.</p>
+     *
+     * <pre>
+     * StringUtils.lowerCase(null)  = null
+     * StringUtils.lowerCase("")    = ""
+     * StringUtils.lowerCase("aBc") = "abc"
+     * </pre>
+     *
+     * @param str  the String to lower case, may be null
+     * @return the lower cased String, <code>null</code> if null String input
+     */
+    public static String lowerCase(String str) {
+        if (str == null) {
+            return null;
+        }
+        return str.toLowerCase();
+    }
+
+    // Equals
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Compares two Strings, returning <code>true</code> if they are equal.</p>
+     *
+     * <p><code>null</code>s are handled without exceptions. Two <code>null</code>
+     * references are considered to be equal. The comparison is case sensitive.</p>
+     *
+     * <pre>
+     * StringUtils.equals(null, null)   = true
+     * StringUtils.equals(null, "abc")  = false
+     * StringUtils.equals("abc", null)  = false
+     * StringUtils.equals("abc", "abc") = true
+     * StringUtils.equals("abc", "ABC") = false
+     * </pre>
+     *
+     * @see java.lang.String#equals(Object)
+     * @param str1  the first String, may be null
+     * @param str2  the second String, may be null
+     * @return <code>true</code> if the Strings are equal, case sensitive, or
+     *  both <code>null</code>
+     */
+    public static boolean equals(String str1, String str2) {
+        return str1 == null ? str2 == null : str1.equals(str2);
+    }
 }