Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. package com.github.ncruces.utils;
  2.  
  3. public final class CharArraySequence implements CharSequence {
  4. private final char[] buf;
  5. private final int off, len;
  6.  
  7. public CharArraySequence(char[] value) {
  8. buf = value;
  9. off = 0;
  10. len = value.length;
  11. }
  12.  
  13. public CharArraySequence(char[] value, int offset, int length) {
  14. if ((offset | length | offset + length | value.length - offset - length) < 0) {
  15. throw new IndexOutOfBoundsException();
  16. }
  17. buf = value;
  18. off = offset;
  19. len = length;
  20. }
  21.  
  22. @Override
  23. public int length() { return len; }
  24.  
  25. @Override
  26. public String toString() { return String.valueOf(buf, off, len); }
  27.  
  28. @Override
  29. public char charAt(int index) {
  30. if ((index | len - index - 1) < 0) {
  31. throw new IndexOutOfBoundsException();
  32. }
  33. return buf[off + index];
  34. }
  35.  
  36. @Override
  37. public CharSequence subSequence(int start, int end) {
  38. int cnt = end - start;
  39. int rem = len - end;
  40. if ((start | end | cnt | rem) < 0) {
  41. throw new IndexOutOfBoundsException();
  42. }
  43. if ((start | rem) == 0) {
  44. return this;
  45. }
  46. return new CharArraySequence(off + start, cnt, buf);
  47. }
  48.  
  49. private CharArraySequence(int offset, int length, char[] value) {
  50. off = offset;
  51. len = length;
  52. buf = value;
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement