slcom/slangc/sdk/UnicodeSource.sauce

98 lines
2.6 KiB
Plaintext

package slangc.sdk;
import slangc.streams.FileInput;
import slangc.streams.File;
import slangc.parser.Source;
public class UnicodeSource extends Source {
int[] source;
public UnicodeSource(File file) throws Error {
this(file.path(), file);
}
public UnicodeSource(String filename, File file) throws Error {
super(filename);
FileInput is = file.openInput();
byte[] fileData = new byte[(int) file.size()];
int ln = is.read(fileData);
//System.out.println("Read " + i + " bytes from " + file);
is.close();
// This is a lot easier in Slang since the String class handles both 8-bit and 32-bit conversions naturally:
//Log.line("About to construct...");
String strsource = String.construct(fileData); // This will assume a standard 8-bit encoding (compressed characters)
//Log.line("Done constructing!");
source = strsource.ints(); // This will assume a standard 32-bit encoding (one value per character)
//String strsource = new String(fileData, "UTF-8");
//int ncp = strsource.codePointCount(0, strsource.length());
//source = new int[ncp];
//int cpi = 0;
//int i = 0;
//while (i < strsource.length()) {
// if (cpi >= ncp) {
// throw new Error("Unicode error: Number of characters (at least " + cpi + ") is greater than number of expected characters ("+ncp+")");
// }
// int cp = strsource.codePointAt(i);
// int cpl = Character.toChars(cp).length;
// source[cpi] = cp;
// i += cpl;
// cpi++;
//}
//source = this.source.replace("\r\n", "\n");
//System.out.println("GOT:\n" + source);
}
public UnicodeSource(String filename, int[] source) {
super(filename);
this.source = source;
}
public UnicodeSource(int[] source) {
this.source = source;
}
@Override
public int getIndexLength() {
return source.length;
}
@Override
public int getCharacter(int index) {
if (isIndexWithinBounds(index)) {
return source[index];
} else {
return 0;
}
}
/*
int lastidx = 0;
int lastlen = 0;
String laststr = null;
public String getString(int index, int len) {
if (index == lastidx && len == lastlen) {
return laststr;
} else {
lastidx = index;
lastlen = len;
laststr = super.getString(index, len);
return laststr;
}
}*/
public String getString(int index, int len) {
if (index + len > source.length) {
return super.getString(index, len);
}
int[] vals = new int[len];
for (int i = 0; i < len; i++) {
vals[i] = source[index+i];
}
return String.construct(vals);
}
@Override
public int getNextIndex(int currentIndex) {
return currentIndex + 1;
}
}