| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class CharSequences { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public static CharSequence forAsciiBytes(final byte[] bytes) { |
| 33 |
return new CharSequence() { |
| 34 |
public char charAt(int index) { |
| 35 |
return (char) bytes[index]; |
| 36 |
} |
| 37 |
|
| 38 |
public int length() { |
| 39 |
return bytes.length; |
| 40 |
} |
| 41 |
|
| 42 |
public CharSequence subSequence(int start, int end) { |
| 43 |
return forAsciiBytes(bytes, start, end); |
| 44 |
} |
| 45 |
|
| 46 |
public String toString() { |
| 47 |
return new String(bytes); |
| 48 |
} |
| 49 |
}; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static CharSequence forAsciiBytes(final byte[] bytes, |
| 64 |
final int start, final int end) { |
| 65 |
validate(start, end, bytes.length); |
| 66 |
return new CharSequence() { |
| 67 |
public char charAt(int index) { |
| 68 |
return (char) bytes[index + start]; |
| 69 |
} |
| 70 |
|
| 71 |
public int length() { |
| 72 |
return end - start; |
| 73 |
} |
| 74 |
|
| 75 |
public CharSequence subSequence(int newStart, int newEnd) { |
| 76 |
newStart -= start; |
| 77 |
newEnd -= start; |
| 78 |
validate(newStart, newEnd, length()); |
| 79 |
return forAsciiBytes(bytes, newStart, newEnd); |
| 80 |
} |
| 81 |
|
| 82 |
public String toString() { |
| 83 |
return new String(bytes, start, length()); |
| 84 |
} |
| 85 |
}; |
| 86 |
} |
| 87 |
|
| 88 |
static void validate(int start, int end, int length) { |
| 89 |
if (start < 0) throw new IndexOutOfBoundsException(); |
| 90 |
if (end < 0) throw new IndexOutOfBoundsException(); |
| 91 |
if (end > length) throw new IndexOutOfBoundsException(); |
| 92 |
if (start > end) throw new IndexOutOfBoundsException(); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
@UnsupportedAppUsage |
| 99 |
public static boolean equals(CharSequence a, CharSequence b) { |
| 100 |
if (a.length() != b.length()) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
int length = a.length(); |
| 105 |
for (int i = 0; i < length; i++) { |
| 106 |
if (a.charAt(i) != b.charAt(i)) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
} |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
@UnsupportedAppUsage |
| 121 |
public static int compareToIgnoreCase(CharSequence me, CharSequence another) { |
| 122 |
|
| 123 |
int myLen = me.length(), anotherLen = another.length(); |
| 124 |
int myPos = 0, anotherPos = 0, result; |
| 125 |
int end = (myLen < anotherLen) ? myLen : anotherLen; |
| 126 |
|
| 127 |
while (myPos < end) { |
| 128 |
if ((result = Character.toLowerCase(me.charAt(myPos++)) |
| 129 |
- Character.toLowerCase(another.charAt(anotherPos++))) != 0) { |
| 130 |
return result; |
| 131 |
} |
| 132 |
} |
| 133 |
return myLen - anotherLen; |
| 134 |
} |
| 135 |
} |