1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
package shared;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import fileIO.ByteFile;
import structures.ByteBuilder;
import structures.IntList;
/**
* Finds delimiters of a text line efficiently, to allow for parsing.
* For example:<br>
* Integer.parseInt("a b c 22 jan".split(" ")[3])<br>
*
* could be redone as:<br>
* LineParser lp=new LineParser(' ')<br>
* lp.set("a b c 22 jan".toBytes()).parseInt(3)<br>
*
* Uses memory proportional to 4*(# delimiters per line); for constant memory, use LineParser2.
*
* @author Brian Bushnell
* @date May 24, 2023
*
*/
public final class LineParser1 implements LineParser {
/*--------------------------------------------------------------*/
/*---------------- Main ----------------*/
/*--------------------------------------------------------------*/
//For testing
//Syntax: LineParser fname/literal delimiter
public static void main(String[] args) {
assert(args.length==2);
String fname=args[0];
String dstring=args[1];
assert(dstring.length()==1);
final ArrayList<byte[]> lines;
if(new File(fname).exists()){
lines=ByteFile.toLines(fname);
}else{
lines=new ArrayList<byte[]>(1);
lines.add(fname.getBytes());
}
LineParser lp=new LineParser1(dstring.charAt(0));
for(byte[] line : lines) {
lp.set(line);
System.out.println(lp);
}
}
/*--------------------------------------------------------------*/
/*---------------- Constructors ----------------*/
/*--------------------------------------------------------------*/
public LineParser1(byte delimiter_) {delimiter=delimiter_;}
public LineParser1(int delimiter_) {
assert(delimiter_>=0 && delimiter_<=127);
delimiter=(byte)delimiter_;
}
/*--------------------------------------------------------------*/
/*---------------- Outer Methods ----------------*/
/*--------------------------------------------------------------*/
@Override
public LineParser set(byte[] line_) {
clear();
line=line_;
for(int len=advance(); b<line.length; len=advance()) {
bounds.add(b);
}
bounds.add(b);
return this;
}
@Override
public LineParser set(byte[] line_, int maxTerm) {
clear();
line=line_;
for(int term=0; term<=maxTerm; term++) {
int len=advance();
bounds.add(b);
}
return this;
}
@Override
public LineParser clear() {
line=null;
a=b=-1;
bounds.clear();
return this;
}
@Override
public LineParser reset() {
//Does nothing for this class
return this;
}
/*--------------------------------------------------------------*/
/*---------------- Parse Methods ----------------*/
/*--------------------------------------------------------------*/
public int terms() {return bounds.size();}
@Override
public int parseInt(int term) {
setBounds(term);
return Parse.parseInt(line, a, b);
}
@Override
public long parseLong(int term) {
setBounds(term);
return Parse.parseLong(line, a, b);
}
@Override
public float parseFloat(int term) {
setBounds(term);
return Parse.parseFloat(line, a, b);
}
@Override
public double parseDouble(int term) {
setBounds(term);
return Parse.parseDouble(line, a, b);
}
@Override
public byte parseByte(int term, int offset) {
setBounds(term);
final int index=a+offset;
assert(index<b);
return line[index];
}
@Override
public byte[] parseByteArray(int term) {
final int len=setBounds(term);
return Arrays.copyOfRange(line, a, b);
}
@Override
public byte[] parseByteArrayFromCurrentField() {
return Arrays.copyOfRange(line, a, b);
}
@Override
public String parseString(int term) {
final int len=setBounds(term);
return new String(line, a, len);
}
@Override
public ByteBuilder appendTerm(ByteBuilder bb, int term) {
final int len=setBounds(term);
for(int i=a; i<b; i++) {bb.append(line[i]);}
return bb;
}
/*--------------------------------------------------------------*/
@Override
public int parseIntFromCurrentField() {
return Parse.parseInt(line, a, b);
}
@Override
public String parseStringFromCurrentField() {
return new String(line, a, b-a);
}
/*--------------------------------------------------------------*/
/*---------------- Query Methods ----------------*/
/*--------------------------------------------------------------*/
@Override
public boolean startsWith(String s) {
return Tools.startsWith(line, s);
}
@Override
public boolean startsWith(char c) {
return Tools.startsWith(line, c);
}
@Override
public boolean startsWith(byte b) {
return Tools.startsWith(line, b);
}
@Override
public boolean termStartsWith(String s, int term) {
final int len=setBounds(term);
if(len<s.length()) {return false;}
for(int i=0; i<s.length(); i++) {
char c=s.charAt(i);
if(c!=line[a+i]) {return false;}
}
return true;
}
@Override
public boolean termEquals(String s, int term) {
final int len=setBounds(term);
if(len!=s.length()) {return false;}
for(int i=0; i<s.length(); i++) {
char c=s.charAt(i);
if(c!=line[a+i]) {return false;}
}
return true;
}
@Override
public boolean termEquals(char c, int term) {
final int len=setBounds(term);
return len==1 && line[a]==c;
}
@Override
public boolean termEquals(byte c, int term) {
final int len=setBounds(term);
return len==1 && line[a]==c;
}
@Override
public int length(int term) {
return setBounds(term);
}
@Override
public int currentFieldLength() {
return b-a;
}
@Override
public int incrementA(int amt) {
a+=amt;
return b-a;
}
@Override
public int incrementB(int amt) {
a+=amt;
return b-a;
}
@Override
public boolean hasMore() {
return b<line.length;
}
@Override
public int lineLength() {
return line.length;
}
@Override
public byte[] line() {return line;}
@Override
public int a() {return a;}
@Override
public int b() {return b;}
/*--------------------------------------------------------------*/
/*---------------- Private Methods ----------------*/
/*--------------------------------------------------------------*/
@Override
public int setBounds(int term){
a=(term==0 ? 0 : bounds.get(term-1)+1);
b=bounds.get(term);
return b-a;
}
/**
* Do not make public. This is for internal use making the bounds list,
* not for advancing to a new term like in LineParser2.
* @return Length of new field.
*/
private int advance() {
b++;
a=b;
while(b<line.length && line[b]!=delimiter){b++;}
return b-a;
}
/*--------------------------------------------------------------*/
/*---------------- Methods ----------------*/
/*--------------------------------------------------------------*/
@Override
public String toString() {
return toList().toString();
}
@Override
public ArrayList<String> toList(){
ArrayList<String> list=new ArrayList<String>(bounds.size);
for(int i=0; i<bounds.size; i++){
list.add(parseString(i));
}
return list;
}
/*--------------------------------------------------------------*/
/*---------------- Fields ----------------*/
/*--------------------------------------------------------------*/
private final IntList bounds=new IntList();
private int a=-1;
private int b=-1;
private byte[] line;
public final byte delimiter;
}
|