aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/text/Utf8String.java
blob: b36f29332911f6f8b13ec1c7b2ca6ddb648248ee (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;

/**
 * String with Utf8 backing.
 *
 * @author baldersheim
 */
public final class Utf8String extends Utf8Array implements CharSequence {

    private final String s;

    /**
     * This will construct a utf8 backing of the given string.
     *
     * @param str The string that will be utf8 encoded
     */
    public Utf8String(String str) {
        super(Utf8.toBytes(str));
        s = str;
    }

    /**
     * This will create a string based on the utf8 sequence.
     *
     * @param utf8 The backing array
     */
    public Utf8String(AbstractUtf8Array utf8) {
        super(utf8.getBytes(), utf8.getByteOffset(), utf8.getByteLength());
        s = utf8.toString();
    }

    @Override
    public char charAt(int index) {
        return toString().charAt(index);
    }
    @Override
    public int length() {
        return toString().length();
    }
    @Override
    public CharSequence subSequence(int start, int end) {
        return toString().subSequence(start, end);
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Utf8String) {
            return s.equals(o.toString());
        }
        return super.equals(o);
    }

    @Override
    public String toString() {
        return s;
    }

}