Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.internal.util;
     18 
     19 /**
     20  * {@link CharSequence} utility methods.
     21  */
     22 public class CharSequences {
     23 
     24     /**
     25      * Adapts {@link CharSequence} to an array of ASCII (7-bits per character)
     26      * bytes.
     27      *
     28      * @param bytes ASCII bytes
     29      */
     30     public static CharSequence forAsciiBytes(final byte[] bytes) {
     31         return new CharSequence() {
     32             public char charAt(int index) {
     33                 return (char) bytes[index];
     34             }
     35 
     36             public int length() {
     37                 return bytes.length;
     38             }
     39 
     40             public CharSequence subSequence(int start, int end) {
     41                 return forAsciiBytes(bytes, start, end);
     42             }
     43 
     44             public String toString() {
     45                 return new String(bytes);
     46             }
     47         };
     48     }
     49 
     50     /**
     51      * Adapts {@link CharSequence} to an array of ASCII (7-bits per character)
     52      * bytes.
     53      *
     54      * @param bytes ASCII bytes
     55      * @param start index, inclusive
     56      * @param end index, exclusive
     57      *
     58      * @throws IndexOutOfBoundsException if start or end are negative, if end
     59      *  is greater than length(), or if start is greater than end
     60      */
     61     public static CharSequence forAsciiBytes(final byte[] bytes,
     62             final int start, final int end) {
     63         validate(start, end, bytes.length);
     64         return new CharSequence() {
     65             public char charAt(int index) {
     66                 return (char) bytes[index + start];
     67             }
     68 
     69             public int length() {
     70                 return end - start;
     71             }
     72 
     73             public CharSequence subSequence(int newStart, int newEnd) {
     74                 newStart -= start;
     75                 newEnd -= start;
     76                 validate(newStart, newEnd, length());
     77                 return forAsciiBytes(bytes, newStart, newEnd);
     78             }
     79 
     80             public String toString() {
     81                 return new String(bytes, start, length());
     82             }
     83         };
     84     }
     85 
     86     static void validate(int start, int end, int length) {
     87         if (start < 0) throw new IndexOutOfBoundsException();
     88         if (end < 0) throw new IndexOutOfBoundsException();
     89         if (end > length) throw new IndexOutOfBoundsException();
     90         if (start > end) throw new IndexOutOfBoundsException();
     91     }
     92 
     93     /**
     94      * Compares two character sequences for equality.
     95      */
     96     public static boolean equals(CharSequence a, CharSequence b) {
     97         if (a.length() != b.length()) {
     98             return false;
     99         }
    100 
    101         int length = a.length();
    102         for (int i = 0; i < length; i++) {
    103             if (a.charAt(i) != b.charAt(i)) {
    104                 return false;
    105             }
    106         }
    107         return true;
    108     }
    109 
    110     /**
    111      * Compares two character sequences with API like {@link Comparable#compareTo}.
    112      *
    113      * @param me The CharSequence that receives the compareTo call.
    114      * @param another The other CharSequence.
    115      * @return See {@link Comparable#compareTo}.
    116      */
    117     public static int compareToIgnoreCase(CharSequence me, CharSequence another) {
    118         // Code adapted from String#compareTo
    119         int myLen = me.length(), anotherLen = another.length();
    120         int myPos = 0, anotherPos = 0, result;
    121         int end = (myLen < anotherLen) ? myLen : anotherLen;
    122 
    123         while (myPos < end) {
    124             if ((result = Character.toLowerCase(me.charAt(myPos++))
    125                     - Character.toLowerCase(another.charAt(anotherPos++))) != 0) {
    126                 return result;
    127             }
    128         }
    129         return myLen - anotherLen;
    130     }
    131 }
    132