001: package sisc.data;
002:
003: import java.io.*;
004:
005: import sisc.io.ValueWriter;
006: import sisc.reader.CharUtil;
007: import sisc.ser.Serializer;
008: import sisc.ser.Deserializer;
009:
010: public class SchemeString extends Value {
011:
012: //trade off speed vs memory
013: public static boolean compactRepresentation = false;
014:
015: private char[] data_c = null;
016: private String data_s = null;
017:
018: public SchemeString() {
019: }
020:
021: public SchemeString(String s) {
022: data_s = s;
023: }
024:
025: public SchemeString(char[] data) {
026: data_c = data;
027: }
028:
029: public String asString() {
030: if (data_s == null) {
031: data_s = new String(data_c);
032: if (compactRepresentation)
033: data_c = null;
034: }
035: return data_s;
036: }
037:
038: private char[] asCharArray() {
039: if (data_c == null) {
040: data_c = data_s.toCharArray();
041: if (compactRepresentation)
042: data_s = null;
043: }
044: return data_c;
045: }
046:
047: public int length() {
048: return (data_s == null) ? data_c.length : data_s.length();
049: }
050:
051: public char charAt(int index) {
052: return (data_s == null) ? data_c[index] : data_s.charAt(index);
053: }
054:
055: public boolean valueEqual(Value v) {
056: if (v == this )
057: return true;
058: if (!(v instanceof SchemeString))
059: return false;
060: SchemeString o = (SchemeString) v;
061:
062: if (data_c != null && o.data_c != null) {
063: char[] oc = o.data_c;
064: if (data_c.length != oc.length)
065: return false;
066: for (int i = 0; i < oc.length; i++) {
067: if (data_c[i] != oc[i])
068: return false;
069: }
070: return true;
071: } else {
072: return asString().equals(o.asString());
073: }
074: }
075:
076: public int valueHashCode() {
077: return asString().hashCode();
078: }
079:
080: public void appendTo(StringBuffer buf) {
081: if (data_c != null) {
082: buf.append(data_c);
083: } else {
084: buf.append(data_s);
085: }
086: }
087:
088: public SchemeString copy() {
089: if (data_c != null) {
090: char[] newstr = new char[data_c.length];
091: System.arraycopy(data_c, 0, newstr, 0, data_c.length);
092: return new SchemeString(newstr);
093: } else {
094: return new SchemeString(asString());
095: }
096: }
097:
098: public SchemeString substring(int from, int to) {
099: if (data_s != null) {
100: return new SchemeString(data_s.substring(from, to));
101: } else {
102: int len = to - from;
103: char[] newstr = new char[len];
104: System.arraycopy(data_c, from, newstr, 0, len);
105: return new SchemeString(newstr);
106: }
107: }
108:
109: public void set(int k, char c) {
110: asCharArray();
111: data_c[k] = c;
112: data_s = null;
113: }
114:
115: public void set(String s) {
116: data_s = s;
117: data_c = null;
118: }
119:
120: public void set(char[] ca) {
121: data_c = ca;
122: data_s = null;
123: }
124:
125: public int readFromReader(Reader r, int off, int len)
126: throws IOException {
127:
128: asCharArray();
129: return r.read(data_c, off, len);
130: }
131:
132: public void writeToWriter(Writer w, int off, int len)
133: throws IOException {
134:
135: asCharArray();
136: w.write(data_c, off, len);
137: }
138:
139: public void display(ValueWriter w) throws IOException {
140: w.append(asString());
141: }
142:
143: public void write(ValueWriter w) throws IOException {
144: w.append(toString());
145: }
146:
147: public String toString() {
148: StringBuffer b = new StringBuffer();
149: b.append('"');
150: int lastGood = 0;
151: asCharArray();
152: for (int i = 0; i < data_c.length; i++) {
153: String escapeString = CharUtil
154: .charToEscapedIfNecessary(data_c[i]);
155: if (escapeString == null)
156: continue;
157:
158: b.append(data_c, lastGood, i - lastGood);
159: b.append('\\').append(escapeString);
160: lastGood = i + 1;
161: }
162:
163: b.append(data_c, lastGood, data_c.length - lastGood);
164: b.append('"');
165: return b.toString();
166: }
167:
168: public void serialize(Serializer s) throws IOException {
169: s.writeUTF(asString());
170: }
171:
172: public void deserialize(Deserializer s) throws IOException {
173: data_s = s.readUTF();
174: }
175: }
176: /*
177: * The contents of this file are subject to the Mozilla Public
178: * License Version 1.1 (the "License"); you may not use this file
179: * except in compliance with the License. You may obtain a copy of
180: * the License at http://www.mozilla.org/MPL/
181: *
182: * Software distributed under the License is distributed on an "AS
183: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
184: * implied. See the License for the specific language governing
185: * rights and limitations under the License.
186: *
187: * The Original Code is the Second Interpreter of Scheme Code (SISC).
188: *
189: * The Initial Developer of the Original Code is Scott G. Miller.
190: * Portions created by Scott G. Miller are Copyright (C) 2000-2007
191: * Scott G. Miller. All Rights Reserved.
192: *
193: * Contributor(s):
194: * Matthias Radestock
195: *
196: * Alternatively, the contents of this file may be used under the
197: * terms of the GNU General Public License Version 2 or later (the
198: * "GPL"), in which case the provisions of the GPL are applicable
199: * instead of those above. If you wish to allow use of your
200: * version of this file only under the terms of the GPL and not to
201: * allow others to use your version of this file under the MPL,
202: * indicate your decision by deleting the provisions above and
203: * replace them with the notice and other provisions required by
204: * the GPL. If you do not delete the provisions above, a recipient
205: * may use your version of this file under either the MPL or the
206: * GPL.
207: */
|