80 lines
2.0 KiB
Plaintext
80 lines
2.0 KiB
Plaintext
|
package slangc.sdk;
|
||
|
|
||
|
import slangc.parser.Source;
|
||
|
|
||
|
public class ArgsLoader {
|
||
|
|
||
|
public static String[] remove(String[] array, int index) {
|
||
|
if (index < 0 || index >= array.length) {
|
||
|
return array;
|
||
|
}
|
||
|
if (array.length == 1) {
|
||
|
return new String[0];
|
||
|
}
|
||
|
String[] a = new String[array.length - 1];
|
||
|
for (int i = 0; i < a.length; i++) {
|
||
|
if (i < index) {
|
||
|
a[i] = array[i];
|
||
|
} else {
|
||
|
a[i] = array[i + 1];
|
||
|
}
|
||
|
}
|
||
|
return a;
|
||
|
}
|
||
|
|
||
|
public static String[] insert(String[] array, int index, String[] values) {
|
||
|
String[] a = new String[array.length + values.length];
|
||
|
for (int i = 0; i < a.length; i++) {
|
||
|
if (i < index) {
|
||
|
a[i] = array[i];
|
||
|
} else if (i < index + values.length) {
|
||
|
a[i] = values[i - index];
|
||
|
} else {
|
||
|
a[i] = array[i - values.length];
|
||
|
}
|
||
|
}
|
||
|
return a;
|
||
|
}
|
||
|
|
||
|
public static String[] loadArgs(Source source) {
|
||
|
int i = 0;
|
||
|
int lastStart = -1;
|
||
|
int count = 0;
|
||
|
String[] result = new String[0];
|
||
|
|
||
|
while (source.isIndexWithinBounds(i)) {
|
||
|
int c = source.getCharacter(i);
|
||
|
String str = String.construct(new int[]{c});
|
||
|
//Log.line("Reading new character '" + str + "'");
|
||
|
if (c == '\n' || c == ' ' || c == '\r' || c == '\f' || c == '\t' || c == '\n') {
|
||
|
//Log.line("Is separator");
|
||
|
if (lastStart >= 0) {
|
||
|
result = innerAppend(result, source, lastStart, count);
|
||
|
//Log.line("Loaded arg '" + result[result.length-1] + "'");
|
||
|
}
|
||
|
lastStart = -1;
|
||
|
count = 0;
|
||
|
} else {
|
||
|
//Log.line("Is not separator");
|
||
|
if (lastStart < 0) {
|
||
|
lastStart = i;
|
||
|
}
|
||
|
count++;
|
||
|
}
|
||
|
i = source.getNextIndex(i);
|
||
|
}
|
||
|
|
||
|
result = innerAppend(result, source, lastStart, count);
|
||
|
//Log.line("Loaded arg '" + result[result.length-1] + "'");
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
private static String[] innerAppend(String[] result, Source source, int lastStart, int count) {
|
||
|
if (count > 0) {
|
||
|
result = insert(result, result.length, new String[] {source.getString(lastStart, count)});
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
}
|