001: /*
002: * $Id: CharStringPointer.java,v 1.7 2003/11/07 20:16:25 dfs Exp $
003: *
004: * ====================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Copyright (c) 2000 The Apache Software Foundation. All rights
008: * reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions
012: * are met:
013: *
014: * 1. Redistributions of source code must retain the above copyright
015: * notice, this list of conditions and the following disclaimer.
016: *
017: * 2. Redistributions in binary form must reproduce the above copyright
018: * notice, this list of conditions and the following disclaimer in
019: * the documentation and/or other materials provided with the
020: * distribution.
021: *
022: * 3. The end-user documentation included with the redistribution,
023: * if any, must include the following acknowledgment:
024: * "This product includes software developed by the
025: * Apache Software Foundation (http://www.apache.org/)."
026: * Alternately, this acknowledgment may appear in the software itself,
027: * if and wherever such third-party acknowledgments normally appear.
028: *
029: * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
030: * must not be used to endorse or promote products derived from this
031: * software without prior written permission. For written
032: * permission, please contact apache@apache.org.
033: *
034: * 5. Products derived from this software may not be called "Apache"
035: * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
036: * name, without prior written permission of the Apache Software Foundation.
037: *
038: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
039: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
040: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
041: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
042: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
045: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
046: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
047: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
048: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: * ====================================================================
051: *
052: * This software consists of voluntary contributions made by many
053: * individuals on behalf of the Apache Software Foundation. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.oro.text.regex;
059:
060: /**
061: * The CharStringPointer class is used to facilitate traversal of a char[]
062: * in the manner pointer traversals of strings are performed in C/C++.
063: * It is expected that the compiler will inline all the functions.
064: *
065: * @since 1.0
066: * @version @version@
067: */
068: final class CharStringPointer {
069: static final char _END_OF_STRING = Character.MAX_VALUE;
070: int _offset;
071: char[] _array;
072:
073: CharStringPointer(char[] charArray, int offset) {
074: _array = charArray;
075: _offset = offset;
076: }
077:
078: CharStringPointer(char[] charArray) {
079: this (charArray, 0);
080: }
081:
082: char _getValue() {
083: return _getValue(_offset);
084: }
085:
086: char _getValue(int offset) {
087: if (offset < _array.length && offset >= 0)
088: return _array[offset];
089: return _END_OF_STRING;
090: }
091:
092: char _getValueRelative(int offset) {
093: return _getValue(_offset + offset);
094: }
095:
096: int _getLength() {
097: return _array.length;
098: }
099:
100: int _getOffset() {
101: return _offset;
102: }
103:
104: void _setOffset(int offset) {
105: _offset = offset;
106: }
107:
108: boolean _isAtEnd() {
109: return (_offset >= _array.length);
110: }
111:
112: char _increment(int inc) {
113: _offset += inc;
114: if (_isAtEnd()) {
115: _offset = _array.length;
116: return _END_OF_STRING;
117: }
118:
119: return _array[_offset];
120: }
121:
122: char _increment() {
123: return _increment(1);
124: }
125:
126: char _decrement(int inc) {
127: _offset -= inc;
128: if (_offset < 0)
129: _offset = 0;
130:
131: return _array[_offset];
132: }
133:
134: char _decrement() {
135: return _decrement(1);
136: }
137:
138: char _postIncrement() {
139: char ret;
140: ret = _getValue();
141: _increment();
142: return ret;
143: }
144:
145: char _postDecrement() {
146: char ret;
147: ret = _getValue();
148: _decrement();
149: return ret;
150: }
151:
152: String _toString(int offset) {
153: return new String(_array, offset, _array.length - offset);
154: }
155:
156: public String toString() {
157: return _toString(0);
158: }
159: }
|