File Source: IRCStringConverter.java

         /* 
    P/P   *  Method: com.dmdirc.parser.irc.IRCStringConverter__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.parser.irc;
    24  
    25  /**
    26   * IRC String Converter.
    27   *
    28   * @author Shane Mc Cormack
    29   */
    30  public class IRCStringConverter {
    31  	/** Characters to use when converting tolowercase. */
    32  	private final char[] lowercase;
    33  	/** Characters to use when converting touppercase. */
    34  	private final char[] uppercase;
    35  	/** limit */
    36  	private final byte limit;
    37  
    38  	/**
    39  	 * Create a new IRCStringConverter with rfc1459 encoding.
    40  	 */
    41  	public IRCStringConverter() {
        		 /* 
    P/P 		  *  Method: void com.dmdirc.parser.irc.IRCStringConverter()
        		  * 
        		  *  Postconditions:
        		  *    this.limit == 4
        		  *    this.lowercase == &new char[](IRCStringConverter#1)
        		  *    this.uppercase == &new char[](IRCStringConverter#2)
        		  *    new char[](IRCStringConverter#1) num objects == 1
        		  *    new char[](IRCStringConverter#2) num objects == 1
        		  *    new char[](IRCStringConverter#1).length == 127
        		  *    new char[](IRCStringConverter#2).length == 127
        		  *    possibly_updated(new char[](IRCStringConverter#1)[...])
        		  *    possibly_updated(new char[](IRCStringConverter#2)[...])
        		  */
    42  		this((byte)4);
    43  	}
    44  	
    45  	/**
    46  	 * Create a new IRCStringConverter.
    47  	 * @param limit Number of post-alphabetical characters to convert
    48  	 *              0 = ascii encoding
    49  	 *              3 = strict-rfc1459 encoding
    50  	 *              4 = rfc1459 encoding
    51  	 */
        	 /* 
    P/P 	  *  Method: void com.dmdirc.parser.irc.IRCStringConverter(byte)
        	  * 
        	  *  Postconditions:
        	  *    this.limit == One-of{4, limit}
        	  *    this.limit in {0..4}
        	  *    this.lowercase == &new char[](IRCStringConverter#1)
        	  *    this.uppercase == &new char[](IRCStringConverter#2)
        	  *    new char[](IRCStringConverter#1) num objects == 1
        	  *    new char[](IRCStringConverter#2) num objects == 1
        	  *    this.lowercase.length == 127
        	  *    this.uppercase.length == 127
        	  *    this.lowercase[...] == One-of{VN37:[...] + 32, this.lowercase[...]}
        	  *    this.uppercase[...] == One-of{VN42:[...] - 32, this.uppercase[...]}
        	  * 
        	  *  Test Vectors:
        	  *    limit: {5..255}, {0..4}, {-128..-1}
        	  */
    52  	public IRCStringConverter(final byte limit) {
    53  		// If limit is out side the boundries, use rfc1459
    54  		if (limit > 4 || limit < 0 ) { this.limit = (byte)4; }
    55  		else { this.limit = limit; }
    56  		
    57  		lowercase = new char[127];
    58  		uppercase = new char[127];
    59  		// Normal Chars
    60  		for (char i = 0; i < lowercase.length; ++i) {
    61  			lowercase[i] = i;
    62  			uppercase[i] = i;
    63  		}
    64  
    65  		// Replace the uppercase chars with lowercase
    66  		for (char i = 65; i <= (90 + this.limit); ++i) {
    67  			lowercase[i] = (char)(i + 32);
    68  			uppercase[i + 32] = i;
    69  		}
    70  	}
    71  
    72  	/**
    73  	 * Get last used chararray limit.
    74  	 *
    75  	 * @return last used chararray limit
    76  	 */
        	 /* 
    P/P 	  *  Method: int getLimit()
        	  * 
        	  *  Postconditions:
        	  *    return_value == this.limit
        	  *    return_value in {-128..255}
        	  */
    77  	protected int getLimit() { return limit; }
    78  	
    79  	/**
    80  	 * Get the lowercase version of a String for this Server.
    81  	 *
    82  	 * @param input String to convert lowercase
    83  	 * @return input String converterd to lowercase
    84  	 */
    85  	public String toLowerCase(final String input) {
        		 /* 
    P/P 		  *  Method: String toLowerCase(String)
        		  * 
        		  *  Preconditions:
        		  *    input != null
        		  *    (soft) this.lowercase != null
        		  *    (soft) init'ed(this.lowercase[...])
        		  * 
        		  *  Presumptions:
        		  *    result.length@86 >= 1
        		  *    java.lang.String:length(...)@87 <= result.length@86
        		  * 
        		  *  Postconditions:
        		  *    return_value == &amp;new String(toLowerCase#1)
        		  *    new String(toLowerCase#1) num objects == 1
        		  */
    86  		final char[] result = input.toCharArray();
    87  		for (int i = 0; i < input.length(); ++i) {
    88  			if (result[i] >= 0 && result[i] < lowercase.length) {
    89  				result[i] = lowercase[result[i]];
    90  			} else {
    91  				result[i] = result[i];
    92  			}
    93  		}
    94  		return new String(result);
    95  	}
    96  
    97  	/**
    98  	 * Get the uppercase version of a String for this Server.
    99  	 *
   100  	 * @param input String to convert uppercase
   101  	 * @return input String converterd to uppercase
   102  	 */
   103  	public String toUpperCase(final String input) {
        		 /* 
    P/P 		  *  Method: String toUpperCase(String)
        		  * 
        		  *  Preconditions:
        		  *    input != null
        		  *    (soft) this.uppercase != null
        		  *    (soft) init'ed(this.uppercase[...])
        		  * 
        		  *  Presumptions:
        		  *    result.length@104 >= 1
        		  *    java.lang.String:length(...)@105 <= result.length@104
        		  * 
        		  *  Postconditions:
        		  *    return_value == &amp;new String(toUpperCase#1)
        		  *    new String(toUpperCase#1) num objects == 1
        		  */
   104  		final char[] result = input.toCharArray();
   105  		for (int i = 0; i < input.length(); ++i) {
   106  			if (result[i] >= 0 && result[i] < uppercase.length) {
   107  				result[i] = uppercase[result[i]];
   108  			} else {
   109  				result[i] = result[i];
   110  			}
   111  		}
   112  		return new String(result);
   113  	}
   114  
   115  	/**
   116  	 * Check if 2 strings are equal to each other ignoring case.
   117  	 *
   118  	 * @param first First string to check
   119  	 * @param second Second string to check
   120  	 * @return True if both strings are equal after being lowercased
   121  	 */
   122  	public boolean equalsIgnoreCase(final String first, final String second) {
        		 /* 
    P/P 		  *  Method: bool equalsIgnoreCase(String, String)
        		  * 
        		  *  Preconditions:
        		  *    (soft) this.lowercase != null
        		  *    (soft) this.lowercase.length >= 1
        		  *    (soft) init'ed(this.lowercase[...])
        		  * 
        		  *  Presumptions:
        		  *    firstChar[i]@127 < this.lowercase.length
        		  *    java.lang.String:length(...)@129 <= firstChar.length@127
        		  *    secondChar.length@128 >= 1
        		  *    java.lang.String:length(...)@129 <= secondChar.length@128
        		  *    secondChar[i]@128 < this.lowercase.length
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    first: Inverse{null}, Addr_Set{null}
        		  *    second: Inverse{null}, Addr_Set{null}
        		  *    firstChar[i]@127 - this.lowercase.length: {0..216-2}, {-216+1..-1}
        		  *    secondChar[i]@128 - this.lowercase.length: {0..216-2}, {-Inf..-1}
        		  */
   123  		if (first == null && second == null) { return true; }
   124  		if (first == null || second == null) { return false; }
   125  		boolean result = (first.length() == second.length());
   126  		if (result) {
   127  			final char[] firstChar = first.toCharArray();
   128  			final char[] secondChar = second.toCharArray();
   129  			for (int i = 0; i < first.length(); ++i) {
   130  				if (firstChar[i] < lowercase.length && secondChar[i] < lowercase.length) {
   131  					result = (lowercase[firstChar[i]] == lowercase[secondChar[i]]);
   132  				} else {
   133  					result = firstChar[i] == secondChar[i];
   134  				}
   135  				if (!result) { break; }
   136  			}
   137  		}
   138  
   139  		return result;
   140  	}
   141  
   142  }








SofCheck Inspector Build Version : 2.17854
IRCStringConverter.java 2009-Jun-25 01:54:24
IRCStringConverter.class 2009-Sep-02 17:04:12