001: /*
002: * CharPointer.java --
003: *
004: * Used in the Parser, this class implements the functionality
005: * of a C character pointer. CharPointers referencing the same
006: * script share a reference to one array, while maintaining there
007: * own current index into the array.
008: *
009: * Copyright (c) 1997 by Sun Microsystems, Inc.
010: *
011: * See the file "license.terms" for information on usage and redistribution
012: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
013: *
014: * RCS: @(#) $Id: CharPointer.java,v 1.5 2005/10/19 23:37:38 mdejong Exp $
015: */
016:
017: package tcl.lang;
018:
019: class CharPointer {
020:
021: // A string of characters.
022:
023: char[] array;
024:
025: // The current index into the array.
026:
027: int index;
028:
029: /*
030: *----------------------------------------------------------------------
031: *
032: * CharPointer --
033: *
034: * Default initialization.
035: *
036: * Side effects:
037: * None.
038: *
039: *----------------------------------------------------------------------
040: */
041:
042: CharPointer() {
043: this .array = null;
044: this .index = -1;
045: }
046:
047: /*
048: *----------------------------------------------------------------------
049: *
050: * CharPointer --
051: *
052: * Make a "copy" of the argument. This is used when the index
053: * of the original CharPointer shouldn't change.
054: *
055: * Side effects:
056: * None.
057: *
058: *----------------------------------------------------------------------
059: */
060:
061: CharPointer(CharPointer c) {
062: this .array = c.array;
063: this .index = c.index;
064: }
065:
066: /*
067: *----------------------------------------------------------------------
068: *
069: * CharPointer --
070: *
071: * Create an array of chars that is one char more than the length
072: * of str. This is used to store \0 after the last char in the
073: * string without causing exceptions.
074: *
075: *
076: * Side effects:
077: * None.
078: *
079: *----------------------------------------------------------------------
080: */
081:
082: CharPointer(String str) {
083: int len = str.length();
084: this .array = new char[len + 1];
085: str.getChars(0, len, this .array, 0);
086: this .array[len] = '\0';
087: this .index = 0;
088: }
089:
090: /*
091: *----------------------------------------------------------------------
092: *
093: * charAt --
094: *
095: * Used to map C style '*ptr' into Java.
096: *
097: * Results:
098: * A character at the current index
099: *
100: * Side effects:
101: * None.
102: *
103: *----------------------------------------------------------------------
104: */
105:
106: char charAt() {
107: return (array[index]);
108: }
109:
110: /*
111: *----------------------------------------------------------------------
112: *
113: * charAt --
114: * Used to map C style 'ptr[x]' into Java.
115: *
116: *
117: * Results:
118: * A character at the current index plus some value.
119: *
120: * Side effects:
121: * None.
122: *
123: *----------------------------------------------------------------------
124: */
125:
126: char charAt(int x) {
127: return (array[index + x]);
128: }
129:
130: /*
131: *----------------------------------------------------------------------
132: *
133: * length --
134: *
135: * Since a '\0' char is stored at the end of the script the true
136: * length of the string is one less than the length of array.
137: *
138: * Results:
139: * The true size of the string.
140: *
141: * Side effects:
142: * None.
143: *
144: *----------------------------------------------------------------------
145: */
146:
147: int length() {
148: return (array.length - 1);
149: }
150:
151: /*
152: *----------------------------------------------------------------------
153: *
154: * toString --
155: *
156: * Get the entire string held in this CharPointer's array.
157: *
158: * Results:
159: * A String used for debug.
160: *
161: * Side effects:
162: * None.
163: *
164: *----------------------------------------------------------------------
165: */
166:
167: public String toString() {
168: return new String(array, 0, array.length - 1);
169: }
170: } // end CharPointer
|