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
|
package fun;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Pattern;
import shared.Parse;
public class Foo2 {
static final Pattern pipePattern=Pattern.compile("\\|");
public static void main(String[] args) throws Exception{
final boolean slow=args.length<2 ? true : "slow".equalsIgnoreCase(args[1]);
final BufferedReader reader=new BufferedReader(new FileReader(args[0]));
long sum=0, lines=0, chars=0;
// ArrayList<Pair> list=new ArrayList<Pair>(10000000);
for(String line=reader.readLine(); line!=null; line=reader.readLine()) {
final long size=(slow ? processSlow(line) : processFast(line));
if(size>=0) {
sum+=size;
lines++;
chars+=line.length();
}
}
reader.close();
System.out.println("sum="+sum);
System.out.println("lines="+lines);
System.out.println("chars="+chars);
}
static long processSlow(String line) {
String[] split=pipePattern.split(line);
if(split[6].charAt(0)!='F') {return -1;}
long size=Long.parseLong(split[3]);
assert(size>=0) : line;
return size;
}
static long processFast(String line) {
final int delimiter='|';
final int len=line.length();
int a=0, b=0;
while(b<len && line.charAt(b)!=delimiter){b++;}
assert(b>a) : "Missing term : '"+new String(line)+"'";
// long w=Parse.parseLong(line, a, b);
b++;
a=b;
while(b<len && line.charAt(b)!=delimiter){b++;}
assert(b>a) : "Missing term : '"+new String(line)+"'";
// long w=Parse.parseLong(line, a, b);
b++;
a=b;
while(b<len && line.charAt(b)!=delimiter){b++;}
assert(b>a) : "Missing term : '"+new String(line)+"'";
// long w=Parse.parseLong(line, a, b);
b++;
a=b;
while(b<len && line.charAt(b)!=delimiter){b++;}
assert(b>a) : "Missing term : '"+new String(line)+"'";
long size=Parse.parseLong(line, a, b);
b++;
a=b;
while(b<len && line.charAt(b)!=delimiter){b++;}
assert(b>a) : "Missing term : '"+new String(line)+"'";
b++;
a=b;
while(b<len && line.charAt(b)!=delimiter){b++;}
assert(b>a) : "Missing term : '"+new String(line)+"'";
b++;
a=b;
while(b<len && line.charAt(b)!=delimiter){b++;}
assert(b>a) : "Missing term : '"+new String(line)+"'";
if(line.charAt(a)!='F') {return -1;}
b++;
a=b;
return size;
}
}
|