PageRenderTime 114ms CodeModel.GetById 104ms app.highlight 7ms RepoModel.GetById 1ms 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
  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
 22package org.gjt.sp.jedit.buffer;
 23
 24/**
 25 * A read-only text segment from a buffer. Allows concatenation using a
 26 * "linked list" approach.
 27 *
 28 * @author Marcelo Vanzin
 29 * @version $Id: BufferSegment.java 12726 2008-05-29 20:21:14Z k_satoda $
 30 * @since jEdit 4.3pre15
 31 */
 32class BufferSegment implements CharSequence
 33{
 34
 35	public BufferSegment(char[] data,
 36			     int offset,
 37			     int len)
 38	{
 39		this(data,offset,len,null);
 40	}
 41
 42	public BufferSegment(char[] data,
 43			      int offset,
 44			      int len,
 45			      BufferSegment next)
 46	{
 47		this.data = data;
 48		this.offset = offset;
 49		this.len = len;
 50		this.next = next;
 51	}
 52
 53	public char charAt(int index)
 54	{
 55		if (index < len)
 56			return data[offset+index];
 57		else if (next != null)
 58			return next.charAt(index-len);
 59		else
 60			throw new ArrayIndexOutOfBoundsException(index);
 61	}
 62
 63	public int length()
 64	{
 65		return len + ((next != null) ? next.length() : 0);
 66	}
 67
 68	public CharSequence subSequence(int start,
 69					int end)
 70	{
 71		return subSegment(start, end);
 72	}
 73
 74	public String toString()
 75	{
 76		StringBuilder sb = new StringBuilder();
 77		toString(sb);
 78		return sb.toString();
 79	}
 80
 81	private void toString(StringBuilder sb)
 82	{
 83		sb.append(data,offset,len);
 84		if (next != null)
 85			next.toString(sb);
 86	}
 87
 88	private BufferSegment subSegment(int start,
 89					int end)
 90	{
 91		if (0 <= start && start <= end)
 92			if (end <= len)
 93				return new BufferSegment(data,offset+start,
 94					end-start);
 95			else if (next != null)
 96				if (start < len)
 97					return new BufferSegment(data,
 98						offset+start,len-start,
 99						next.subSegment(0,end-len));
100				else
101					return next.subSegment(start-len,
102						end-len);
103			else
104				throw new ArrayIndexOutOfBoundsException();
105		else
106			throw new ArrayIndexOutOfBoundsException();
107	}
108
109	private final char[] data;
110	private final int offset;
111	private final int len;
112	private final BufferSegment next;
113}
114