Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.github.ncruces.utils;
- public final class CharArraySequence implements CharSequence {
- private final char[] buf;
- private final int off, len;
- public CharArraySequence(char[] value) {
- buf = value;
- off = 0;
- len = value.length;
- }
- public CharArraySequence(char[] value, int offset, int length) {
- if ((offset | length | offset + length | value.length - offset - length) < 0) {
- throw new IndexOutOfBoundsException();
- }
- buf = value;
- off = offset;
- len = length;
- }
- @Override
- public int length() { return len; }
- @Override
- public String toString() { return String.valueOf(buf, off, len); }
- @Override
- public char charAt(int index) {
- if ((index | len - index - 1) < 0) {
- throw new IndexOutOfBoundsException();
- }
- return buf[off + index];
- }
- @Override
- public CharSequence subSequence(int start, int end) {
- int cnt = end - start;
- int rem = len - end;
- if ((start | end | cnt | rem) < 0) {
- throw new IndexOutOfBoundsException();
- }
- if ((start | rem) == 0) {
- return this;
- }
- return new CharArraySequence(off + start, cnt, buf);
- }
- private CharArraySequence(int offset, int length, char[] value) {
- off = offset;
- len = length;
- buf = value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement