1 /*
2  * $Id: CharStringPointer.java,v 1.7 2003/11/07 20:16:25 dfs Exp $
3  *
4  * ====================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 2000 The Apache Software Foundation.  All rights
8  * reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in
19  *    the documentation and/or other materials provided with the
20  *    distribution.
21  *
22  * 3. The end-user documentation included with the redistribution,
23  *    if any, must include the following acknowledgment:
24  *       "This product includes software developed by the
25  *        Apache Software Foundation (http://www.apache.org/)."
26  *    Alternately, this acknowledgment may appear in the software itself,
27  *    if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
30  *    must not be used to endorse or promote products derived from this
31  *    software without prior written permission. For written
32  *    permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache"
35  *    or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
36  *    name, without prior written permission of the Apache Software Foundation.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation.  For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */
57 
58 
59 package org.apache.oro.text.regex;
60 
61 
62 /**
63  * The CharStringPointer class is used to facilitate traversal of a char[]
64  * in the manner pointer traversals of strings are performed in C/C++.
65  * It is expected that the compiler will inline all the functions.
66  *
67  * @since 1.0
68  * @version @version@
69  */
70 final class CharStringPointer {
71   static final char _END_OF_STRING = Character.MAX_VALUE;
72   int _offset;
73   char[] _array;
74 
CharStringPointer(char[] charArray, int offset)75   CharStringPointer(char[] charArray, int offset) {
76     _array  = charArray;
77     _offset = offset;
78   }
79 
CharStringPointer(char[] charArray)80   CharStringPointer(char[] charArray) {
81     this(charArray, 0);
82   }
83 
_getValue()84   char _getValue()  {
85     return _getValue(_offset);
86   }
87 
_getValue(int offset)88   char _getValue(int offset) {
89     if(offset < _array.length && offset >= 0)
90       return _array[offset];
91     return _END_OF_STRING;
92   }
93 
_getValueRelative(int offset)94   char _getValueRelative(int offset) {
95     return _getValue(_offset + offset);
96   }
97 
_getLength()98   int _getLength() { return _array.length; }
99 
_getOffset()100   int _getOffset() { return _offset; }
101 
_setOffset(int offset)102   void _setOffset(int offset) { _offset = offset; }
103 
_isAtEnd()104   boolean _isAtEnd() {
105     return (_offset >= _array.length);
106   }
107 
_increment(int inc)108   char _increment(int inc) {
109     _offset+=inc;
110     if(_isAtEnd()) {
111       _offset = _array.length;
112       return _END_OF_STRING;
113     }
114 
115     return _array[_offset];
116   }
117 
_increment()118   char _increment() { return _increment(1); }
119 
_decrement(int inc)120   char _decrement(int inc) {
121     _offset-=inc;
122     if(_offset < 0)
123       _offset = 0;
124 
125     return _array[_offset];
126   }
127 
_decrement()128   char _decrement() { return _decrement(1); }
129 
_postIncrement()130   char _postIncrement() {
131     char ret;
132     ret = _getValue();
133     _increment();
134     return ret;
135   }
136 
_postDecrement()137   char _postDecrement() {
138     char ret;
139     ret = _getValue();
140     _decrement();
141     return ret;
142   }
143 
144 
_toString(int offset)145   String _toString(int offset) {
146     return new String(_array, offset, _array.length - offset);
147   }
148 
toString()149   public String toString() {
150     return _toString(0);
151   }
152 }
153