Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CharSequences.java « util « holoeverywhere « org « src « library « HoloEverywhere « 3rd_party « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 797ee16b31599c216947c8ed08dcd4531dbbcae0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

package org.holoeverywhere.util;

public class CharSequences {
    public static int compareToIgnoreCase(CharSequence me, CharSequence another) {
        int myLen = me.length(), anotherLen = another.length();
        int myPos = 0, anotherPos = 0, result;
        int end = myLen < anotherLen ? myLen : anotherLen;
        while (myPos < end) {
            if ((result = Character.toLowerCase(me.charAt(myPos++))
                    - Character.toLowerCase(another.charAt(anotherPos++))) != 0) {
                return result;
            }
        }
        return myLen - anotherLen;
    }

    public static boolean equals(CharSequence a, CharSequence b) {
        if (a.length() != b.length()) {
            return false;
        }
        int length = a.length();
        for (int i = 0; i < length; i++) {
            if (a.charAt(i) != b.charAt(i)) {
                return false;
            }
        }
        return true;
    }

    public static CharSequence forAsciiBytes(final byte[] bytes) {
        return new CharSequence() {
            @Override
            public char charAt(int index) {
                return (char) bytes[index];
            }

            @Override
            public int length() {
                return bytes.length;
            }

            @Override
            public CharSequence subSequence(int start, int end) {
                return CharSequences.forAsciiBytes(bytes, start, end);
            }

            @Override
            public String toString() {
                return new String(bytes);
            }
        };
    }

    public static CharSequence forAsciiBytes(final byte[] bytes,
            final int start, final int end) {
        CharSequences.validate(start, end, bytes.length);
        return new CharSequence() {
            @Override
            public char charAt(int index) {
                return (char) bytes[index + start];
            }

            @Override
            public int length() {
                return end - start;
            }

            @Override
            public CharSequence subSequence(int newStart, int newEnd) {
                newStart -= start;
                newEnd -= start;
                CharSequences.validate(newStart, newEnd, length());
                return CharSequences.forAsciiBytes(bytes, newStart, newEnd);
            }

            @Override
            public String toString() {
                return new String(bytes, start, length());
            }
        };
    }

    static void validate(int start, int end, int length) {
        if (start < 0 || end < 0 || end > length || start > end) {
            throw new IndexOutOfBoundsException();
        }
    }
}