An immutable on-heap sequence of UTF-8 bytes.
| 33 | * An immutable on-heap sequence of UTF-8 bytes. |
| 34 | */ |
| 35 | public class Utf8String implements Utf8Sequence { |
| 36 | public static final Utf8String EMPTY = new Utf8String(""); |
| 37 | private final boolean ascii; |
| 38 | private final AsciiCharSequence asciiCharSequence = new AsciiCharSequence(); |
| 39 | private final byte[] bytes; |
| 40 | private final long zeroPaddedSixPrefix; |
| 41 | |
| 42 | public Utf8String(byte @NotNull [] bytes, boolean ascii) { |
| 43 | this.bytes = bytes; |
| 44 | this.ascii = ascii; |
| 45 | this.zeroPaddedSixPrefix = Utf8s.zeroPaddedSixPrefix(this); |
| 46 | } |
| 47 | |
| 48 | public Utf8String(@NotNull String str) { |
| 49 | this.bytes = str.getBytes(StandardCharsets.UTF_8); |
| 50 | this.ascii = (str.length() == bytes.length); |
| 51 | this.zeroPaddedSixPrefix = Utf8s.zeroPaddedSixPrefix(this); |
| 52 | } |
| 53 | |
| 54 | public Utf8String(char ch) { |
| 55 | this.bytes = String.valueOf(ch).getBytes(StandardCharsets.UTF_8); |
| 56 | this.ascii = (bytes.length == 1); |
| 57 | this.zeroPaddedSixPrefix = Utf8s.zeroPaddedSixPrefix(this); |
| 58 | } |
| 59 | |
| 60 | public Utf8String(@NotNull CharSequence seq) { |
| 61 | this.bytes = seq.toString().getBytes(StandardCharsets.UTF_8); |
| 62 | this.ascii = (seq.length() == bytes.length); |
| 63 | this.zeroPaddedSixPrefix = Utf8s.zeroPaddedSixPrefix(this); |
| 64 | } |
| 65 | |
| 66 | public static Utf8String newInstance(@NotNull Utf8Sequence src) { |
| 67 | byte[] bytes = new byte[src.size()]; |
| 68 | for (int i = 0, n = src.size(); i < n; i++) { |
| 69 | bytes[i] = src.byteAt(i); |
| 70 | } |
| 71 | return new Utf8String(bytes, src.isAscii()); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public @NotNull CharSequence asAsciiCharSequence() { |
| 76 | return asciiCharSequence.of(this); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public byte byteAt(int index) { |
| 81 | return bytes[index]; |
| 82 | } |
| 83 | |
| 84 | public int intAt(int index) { |
| 85 | return Unsafe.byteArrayGetInt(bytes, index); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean isAscii() { |
| 90 | return ascii; |
| 91 | } |
| 92 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…