package org.builditbreakit.seada.logappend;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class BatchProcessor {
	
	BufferedReader reader;
	
	public BatchProcessor(String path) throws IOException {
		reader = new BufferedReader(new FileReader(path));
	}
	
	public String[] nextLine() throws IOException {
		String line = reader.readLine();
		if(line == null) {
			return null;
		}
		return splitLine(line);
	}

	/**
	 * This logic is super simple and would not be adequate for a robust
	 * cmdline parser that has to handle quoted args, escape sequences,
	 * and so forth. However, it is adequate for the bibifi spec. I tested
	 * this regex split against a scanner; it is faster.
	 */
	public static String[] splitLine(String line) {
		ArrayList<String> tokens = new ArrayList<>(16);
		int length = line.length();
		int offset = 0;
		for (int i = offset; i < length; i++) {
			char c = line.charAt(i);
			// Check whitespace characters in regex set: [\s]
			if ((c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\13')
					&& (offset != i)) {
				tokens.add(line.substring(offset, i));
				offset = i + 1;
			}
		}
		if (offset != length) {
			tokens.add(line.substring(offset, length));
		}

		return tokens.toArray(new String[tokens.size()]);
	}
}

@@ -479,16 +479,154 @@
 ine);%0A%09%7D
+%0A%09%0A%09private static boolean isSplitChar(char c) %7B%0A%09%09return (c == ' ' %7C%7C c == '%5Ct' %7C%7C c == '%5Cr' %7C%7C c == '%5Cn' %7C%7C c == '%5Cf' %7C%7C c == '%5C13');%0A%09%7D
 %0A%0A%09/**%0A%09
@@ -1041,16 +1041,130 @@
 et = 0;%0A
+%09%09// Consume leading whitespace.%0A%09%09while (isSplitChar(line.charAt(offset)) && offset %3C length) %7B%0A%09%09%09++offset;%0A%09%09%7D%0A
 %09%09for (i
@@ -1289,90 +1289,23 @@
 if (
-(c == ' ' %7C%7C c == '%5Ct' %7C%7C c == '%5Cr' %7C%7C c == '%5Cn' %7C%7C c == '%5Cf' %7C%7C c == '%5C13')%0A%09%09%09%09%09
+isSplitChar(c) 
 && (

Fix #26 (bug ids 16663 and 16665)