KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > StringPosition


1 /*
2  * StringPosition.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: StringPosition.java,v 1.1.1.1 2002/09/24 16:09:25 piso Exp $
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 (at your option) 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
22 package org.armedbear.j;
23
24 public final class StringPosition implements Constants
25 {
26     private final String JavaDoc text;
27     private final int length;
28
29     private int offset;
30
31     public StringPosition(String JavaDoc s)
32     {
33         text = s;
34         length = s.length();
35     }
36
37     public StringPosition(String JavaDoc s, int offset)
38     {
39         this(s);
40         this.offset = offset;
41     }
42
43     public final String JavaDoc getText()
44     {
45         return text;
46     }
47
48     public final int getOffset()
49     {
50         return offset;
51     }
52
53     public final void setOffset(int n)
54     {
55         Debug.assertTrue(n <= length);
56         this.offset = n;
57     }
58
59     public final char getChar()
60     {
61         Debug.assertTrue(offset <= length);
62         if (offset == length)
63             return EOL;
64         return text.charAt(offset);
65     }
66
67     public final boolean lookingAt(String JavaDoc s)
68     {
69         return s.regionMatches(0, text, offset, s.length());
70     }
71
72     public final boolean atEnd()
73     {
74         return offset >= length;
75     }
76
77     public final boolean charIsWhitespace()
78     {
79         return Character.isWhitespace(text.charAt(offset));
80     }
81
82     public final boolean next()
83     {
84         if (offset < length) {
85             ++offset;
86             return true;
87         }
88         return false;
89     }
90
91     public final void skip(int count)
92     {
93         offset += count;
94     }
95 }
96
Popular Tags