PageRenderTime 72ms CodeModel.GetById 52ms app.highlight 5ms RepoModel.GetById 13ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/util/CharIndexedSegment.java

#
Java | 96 lines | 43 code | 10 blank | 43 comment | 4 complexity | 92cddf964da22163163579e24d6caacf MD5 | raw file
 1/*
 2 * CharIndexedSegment.java
 3 * :tabSize=8:indentSize=8:noTabs=false:
 4 * :folding=explicit:collapseFolds=1:
 5 *
 6 * Copyright (C) 1998 Wes Biggs
 7 * Copyright (C) 2000, 2001 Slava Pestov
 8 *
 9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Library General Public License as published
11 * by the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23package org.gjt.sp.util;
24
25//{{{ Imports
26import java.io.Serializable;
27import javax.swing.text.Segment;
28import gnu.regexp.*;
29//}}}
30
31/**
32 * Lets <code>gnu.regexp</code> search within <code>Segment</code> objects.
33 */
34public class CharIndexedSegment implements CharIndexed, Serializable
35{
36	//{{{ CharIndexedSegment constructor
37	/**
38	 * Creates a new <code>CharIndexedSegment</code>.
39	 * @since jEdit 4.1pre3
40	 */
41	public CharIndexedSegment(Segment seg, int index)
42	{
43		this.seg = seg;
44		m_index = index;
45	} //}}}
46
47	//{{{ CharIndexedSegment constructor
48	/**
49	 * Creates a new <code>CharIndexedSegment</code>.
50	 * @since jEdit 4.1pre1
51	 */
52	public CharIndexedSegment(Segment seg, boolean reverse)
53	{
54		this.seg = seg;
55		m_index = (reverse ? seg.count - 1 : 0);
56		this.reverse = reverse;
57	} //}}}
58
59	//{{{ charAt() method
60	public char charAt(int index)
61	{
62		if(reverse)
63			index = -index;
64
65		return ((m_index + index) < seg.count && m_index + index >= 0)
66			? seg.array[seg.offset + m_index + index]
67			: CharIndexed.OUT_OF_BOUNDS;
68	} //}}}
69
70	//{{{ isValid() method
71	public boolean isValid()
72	{
73		return (m_index >=0 && m_index < seg.count);
74	} //}}}
75
76	//{{{ reset() method
77	public void reset()
78	{
79		m_index = (reverse ? seg.count - 1 : 0);
80	} //}}}
81
82	//{{{ move() method
83	public boolean move(int index)
84	{
85		if(reverse)
86			index = -index;
87
88		return ((m_index += index) < seg.count);
89	} //}}}
90
91	//{{{ Private members
92	private Segment seg;
93	private int m_index;
94	private boolean reverse;
95	//}}}
96}