PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/buffer/BufferSegment.java

#
Java | 114 lines | 74 code | 12 blank | 28 comment | 14 complexity | 0a8fda7f4a3c0f48ab4a15231d170e8a MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * :tabSize=8:indentSize=8:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Copyright (C) 2007 Marcelo Vanzin
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package org.gjt.sp.jedit.buffer;
  22. /**
  23. * A read-only text segment from a buffer. Allows concatenation using a
  24. * "linked list" approach.
  25. *
  26. * @author Marcelo Vanzin
  27. * @version $Id: BufferSegment.java 12726 2008-05-29 20:21:14Z k_satoda $
  28. * @since jEdit 4.3pre15
  29. */
  30. class BufferSegment implements CharSequence
  31. {
  32. public BufferSegment(char[] data,
  33. int offset,
  34. int len)
  35. {
  36. this(data,offset,len,null);
  37. }
  38. public BufferSegment(char[] data,
  39. int offset,
  40. int len,
  41. BufferSegment next)
  42. {
  43. this.data = data;
  44. this.offset = offset;
  45. this.len = len;
  46. this.next = next;
  47. }
  48. public char charAt(int index)
  49. {
  50. if (index < len)
  51. return data[offset+index];
  52. else if (next != null)
  53. return next.charAt(index-len);
  54. else
  55. throw new ArrayIndexOutOfBoundsException(index);
  56. }
  57. public int length()
  58. {
  59. return len + ((next != null) ? next.length() : 0);
  60. }
  61. public CharSequence subSequence(int start,
  62. int end)
  63. {
  64. return subSegment(start, end);
  65. }
  66. public String toString()
  67. {
  68. StringBuilder sb = new StringBuilder();
  69. toString(sb);
  70. return sb.toString();
  71. }
  72. private void toString(StringBuilder sb)
  73. {
  74. sb.append(data,offset,len);
  75. if (next != null)
  76. next.toString(sb);
  77. }
  78. private BufferSegment subSegment(int start,
  79. int end)
  80. {
  81. if (0 <= start && start <= end)
  82. if (end <= len)
  83. return new BufferSegment(data,offset+start,
  84. end-start);
  85. else if (next != null)
  86. if (start < len)
  87. return new BufferSegment(data,
  88. offset+start,len-start,
  89. next.subSegment(0,end-len));
  90. else
  91. return next.subSegment(start-len,
  92. end-len);
  93. else
  94. throw new ArrayIndexOutOfBoundsException();
  95. else
  96. throw new ArrayIndexOutOfBoundsException();
  97. }
  98. private final char[] data;
  99. private final int offset;
  100. private final int len;
  101. private final BufferSegment next;
  102. }