public static String insignificantSpacesStringFinal()

in ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/PrepareString.java [4015:4216]


    public static String insignificantSpacesStringFinal( char[] origin )
    {
        if ( origin == null )
        {
            // Special case : a null strings is replaced by 1 spaces
            return " ";
        }

        int pos = 0;

        // Create a target char array which is longer than the original String, as we will
        // have 2 more spaces (one at the beginning, one at the end, and each space in the 
        // middle will be doubled).
        char[] target = new char[origin.length * 2 + 1];
        int newPos = 0;
        
        NormStateEnum normState = NormStateEnum.START;
        
        while ( normState != NormStateEnum.END )
        {
            switch ( normState )
            {
                case START :
                    if ( pos == origin.length )
                    {
                        // We are done, it's an empty string
                        return " ";
                    }
                    
                    char c = origin[pos];
                    
                    if ( c == ' ' )
                    {
                        pos++;
                        normState = NormStateEnum.INITIAL_SPACES;
                    }
                    else
                    {
                        // Add the char
                        target[newPos++] = c;
                        
                        pos++;
                        normState = NormStateEnum.INITIAL_CHAR;
                    }
                    
                    break;
                    
                case INITIAL_CHAR :
                    if ( pos == origin.length )
                    {
                        // We are done, add a space
                        target[newPos++] = ' ';
                        normState = NormStateEnum.END;
                        
                        break;
                    }
                    
                    c = origin[pos];
                    
                    if ( c == ' ' )
                    {
                        // Switch to the SPACES state
                        pos++;
                        normState = NormStateEnum.SPACES;
                    }
                    else
                    {
                        // Add the char
                        target[newPos++] = c;
                        pos++;
                        normState = NormStateEnum.CHARS;
                    }
                    
                    break;

                case INITIAL_SPACES :
                    if ( pos == origin.length )
                    {
                        // We are done, this is an empty String
                        return " ";
                    }
                    
                    c = origin[pos];
                    
                    if ( c == ' ' )
                    {
                        pos++;
                        // Keep going with the current state
                    }
                    else
                    {
                        // Add a space
                        target[newPos++] = ' ';
                        
                        // Add the char
                        target[newPos++] = c;
                        pos++;
                        normState = NormStateEnum.INITIAL_CHAR;
                    }
                    
                    break;

                case CHARS :
                    if ( pos == origin.length )
                    {
                        // We are done, add a Space
                        target[newPos++] = ' ';
                        normState = NormStateEnum.END;
                        
                        break;
                    }
                    
                    c = origin[pos];
                    
                    if ( c == ' ' )
                    {
                        pos++;
                        normState = NormStateEnum.SPACES;
                    }
                    else
                    {
                        // Add the char
                        target[newPos++] = c;
                        pos++;
                        // We keep going on the same state
                    }
                    
                    break;

                case SPACES :
                    if ( pos == origin.length )
                    {
                        // We are done, add a Space
                        target[newPos++] = ' ';
                        normState = NormStateEnum.END;

                        break;
                    }
                    
                    c = origin[pos];
                    
                    if ( c == ' ' )
                    {
                        pos++;
                        // We keep going on the same state
                    }
                    else
                    {
                        // Add the two spaces
                        target[newPos++] = ' ';
                        target[newPos++] = ' ';
                        
                        // Add the char
                        target[newPos++] = c;
                        pos++;
                        
                        // Switch to SPACE_CHAR state
                        normState = NormStateEnum.SPACE_CHAR;
                    }
                    
                    break;

                case SPACE_CHAR :
                    if ( pos == origin.length )
                    {
                        // We are done, add a Space
                        target[newPos++] = ' ';
                        normState = NormStateEnum.END;

                        break;
                    }
                    
                    c = origin[pos];
                    
                    if ( c == ' ' )
                    {
                        pos++;
                        
                        // Switch to Spaces state
                        normState = NormStateEnum.SPACES;
                    }
                    else
                    {
                        // Add the char
                        target[newPos++] = c;
                        pos++;
                        
                        // Switch to CHARS state
                        normState = NormStateEnum.CHARS;
                    }
                    
                    break;
                    
                default :
                    // Do nothing
                    break;
            }
        }

        // create the resulting String
        return new String( target, 0, newPos );
    }