01: /*
02: * StringPosition.java
03: *
04: * Copyright (C) 1998-2002 Peter Graves
05: * $Id: StringPosition.java,v 1.1.1.1 2002/09/24 16:09:25 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * 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: private final String text;
26: private final int length;
27:
28: private int offset;
29:
30: public StringPosition(String s) {
31: text = s;
32: length = s.length();
33: }
34:
35: public StringPosition(String s, int offset) {
36: this (s);
37: this .offset = offset;
38: }
39:
40: public final String getText() {
41: return text;
42: }
43:
44: public final int getOffset() {
45: return offset;
46: }
47:
48: public final void setOffset(int n) {
49: Debug.assertTrue(n <= length);
50: this .offset = n;
51: }
52:
53: public final char getChar() {
54: Debug.assertTrue(offset <= length);
55: if (offset == length)
56: return EOL;
57: return text.charAt(offset);
58: }
59:
60: public final boolean lookingAt(String s) {
61: return s.regionMatches(0, text, offset, s.length());
62: }
63:
64: public final boolean atEnd() {
65: return offset >= length;
66: }
67:
68: public final boolean charIsWhitespace() {
69: return Character.isWhitespace(text.charAt(offset));
70: }
71:
72: public final boolean next() {
73: if (offset < length) {
74: ++offset;
75: return true;
76: }
77: return false;
78: }
79:
80: public final void skip(int count) {
81: offset += count;
82: }
83: }
|