PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/buffer/ContentManager.java

#
Java | 177 lines | 113 code | 18 blank | 46 comment | 23 complexity | 9665706266c490d45a1c180ab62ad32d 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. * ContentManager.java - Manages text content
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.buffer;
  23. import javax.swing.text.Segment;
  24. /**
  25. * A class internal to jEdit's document model. You should not use it
  26. * directly. To improve performance, none of the methods in this class
  27. * check for out of bounds access, nor are they thread-safe. The
  28. * <code>Buffer</code> class, through which these methods must be
  29. * called through, implements such protection.
  30. *
  31. * @author Slava Pestov
  32. * @version $Id: ContentManager.java 3909 2001-11-24 02:27:08Z spestov $
  33. * @since jEdit 4.0pre1
  34. */
  35. public class ContentManager
  36. {
  37. //{{{ ContentManager constructor
  38. public ContentManager()
  39. {
  40. text = new char[1024];
  41. } //}}}
  42. //{{{ getLength() method
  43. public final int getLength()
  44. {
  45. return length;
  46. } //}}}
  47. //{{{ getText() method
  48. public String getText(int start, int len)
  49. {
  50. if(start >= gapStart)
  51. return new String(text,start + gapEnd - gapStart,len);
  52. else if(start + len <= gapStart)
  53. return new String(text,start,len);
  54. else
  55. {
  56. return new String(text,start,gapStart - start)
  57. .concat(new String(text,gapEnd,start + len - gapStart));
  58. }
  59. } //}}}
  60. //{{{ getText() method
  61. public void getText(int start, int len, Segment seg)
  62. {
  63. if(start >= gapStart)
  64. {
  65. seg.array = text;
  66. seg.offset = start + gapEnd - gapStart;
  67. seg.count = len;
  68. }
  69. else if(start + len <= gapStart)
  70. {
  71. seg.array = text;
  72. seg.offset = start;
  73. seg.count = len;
  74. }
  75. else
  76. {
  77. seg.array = new char[len];
  78. // copy text before gap
  79. System.arraycopy(text,start,seg.array,0,gapStart - start);
  80. // copy text after gap
  81. System.arraycopy(text,gapEnd,seg.array,gapStart - start,
  82. len + start - gapStart);
  83. seg.offset = 0;
  84. seg.count = len;
  85. }
  86. } //}}}
  87. //{{{ insert() method
  88. public void insert(int start, String str)
  89. {
  90. int len = str.length();
  91. if(gapStart != start || gapEnd - gapStart < len)
  92. {
  93. ensureCapacity(length + len + 200);
  94. close(start,start + len + 200);
  95. }
  96. str.getChars(0,len,text,start);
  97. gapStart += len;
  98. length += len;
  99. } //}}}
  100. //{{{ insert() method
  101. public void insert(int start, Segment seg)
  102. {
  103. if(gapStart != start || gapEnd - gapStart < seg.count)
  104. {
  105. ensureCapacity(length + seg.count + 200);
  106. close(start,start + seg.count + 200);
  107. }
  108. System.arraycopy(seg.array,seg.offset,text,start,seg.count);
  109. gapStart += seg.count;
  110. length += seg.count;
  111. } //}}}
  112. //{{{ remove() method
  113. public void remove(int start, int len)
  114. {
  115. close(start,start);
  116. gapEnd += len;
  117. length -= len;
  118. } //}}}
  119. //{{{ Private members
  120. private char[] text;
  121. private int gapStart;
  122. private int gapEnd;
  123. private int length;
  124. //{{{ close() method
  125. private void close(int newStart, int newEnd)
  126. {
  127. // Optimization
  128. if(gapStart == newStart)
  129. {
  130. System.arraycopy(text,gapEnd,text,newEnd,length - gapStart);
  131. }
  132. else
  133. {
  134. if(gapStart != gapEnd && gapStart != length)
  135. {
  136. System.arraycopy(text,gapEnd,text,gapStart,
  137. length - gapStart);
  138. }
  139. if(newStart != newEnd && newStart != length)
  140. {
  141. System.arraycopy(text,newStart,text,newEnd,
  142. length - newStart);
  143. }
  144. }
  145. gapStart = newStart;
  146. gapEnd = newEnd;
  147. } //}}}
  148. //{{{ ensureCapacity() method
  149. private void ensureCapacity(int capacity)
  150. {
  151. if(capacity >= text.length)
  152. {
  153. char[] textN = new char[capacity * 2];
  154. System.arraycopy(text,0,textN,0,length + (gapEnd - gapStart));
  155. text = textN;
  156. }
  157. } //}}}
  158. //}}}
  159. }