import java.io.*; import java.util.*; /* * @author Aleksander Slominski [aslom@extreme.indiana.edu] */ public class Scanner implements Tester { int count; String elementName; public Scanner() { } public void setUp(Properties props) throws Exception { } void scanCount(String data) { int len = data.length(); char[] target = ('<'+elementName).toCharArray(); int targetLen = target.length; int j = 0; for(int i = 0; i < len; ++i) { char ch = data.charAt(i); if(j == targetLen) { j = 0; if(! Character.isUnicodeIdentifierPart(ch)) { ++count; } } else if(ch == target[j]) { ++j; } else { j = 0; } } } void scanCountSimple(Reader in) throws IOException { char[] target = ('<'+elementName).toCharArray(); int targetLen = target.length; int j = 0; int i = -1; while((i = in.read()) != -1) { char ch = (char)i; if(j == targetLen) { j = 0; if(! Character.isUnicodeIdentifierPart(ch)) { ++count; } } else if(ch == target[j]) { ++j; } else { j = 0; } } } void scanCountBuf(Reader in, int size) throws IOException { char[] buf = new char[size]; scanCountBuf(in, buf); } int bufPos; int bufRead; char more(Reader in, char buf[]) throws IOException { if(bufPos >= bufRead) { bufPos = 0; bufRead = in.read(buf); if(bufRead == -1) { //break; //EOF throw new EOFException(); } } char ch = buf[bufPos++]; return ch; } static final int DUPBUF_LEN = 1024; char[] dupBuf = new char[DUPBUF_LEN]; int dupBufLen; void scanCountBufMore(Reader in, char[] buf) throws IOException { char[] target = ('<'+elementName).toCharArray(); int targetLen = target.length; int j = 0; int bufLen = buf.length; bufRead = 0; bufPos = 0; try { while(true) { char ch = more(in, buf); /* if(i >= bufRead) { i = 0; bufRead = in.read(buf); if(bufRead == -1) break; //EOF } char ch = buf[i++]; */ //outBuf[j]=ch; if(j == targetLen) { j = 0; if(! Character.isUnicodeIdentifierPart(ch)) { ++count; } } else if(ch == target[j]) { ++j; } else { j = 0; } } } catch(EOFException ex) { } } void scanCountBuf(Reader in, char[] buf) throws IOException { char[] target = ('<'+elementName).toCharArray(); int targetLen = target.length; int j = 0; int bufLen = buf.length; bufRead = 0; bufPos = 0; int i = 0; try { while(true) { //if(dupBufLen >= DUPBUF_LEN) //dupBuf.length) // dupBufLen = 0; //dupBuf[dupBufLen++] = ch; if(i >= bufRead) { i = 0; bufRead = in.read(buf); if(bufRead == -1) break; //EOF } char ch = buf[i++]; if(j == targetLen) { j = 0; // elimante case of falsy counting "abcabs" as "ab" element if(! Character.isUnicodeIdentifierPart(ch)) { ++count; } } else if(ch == target[j]) { ++j; } else { j = 0; } } } catch(EOFException ex) { } } public TestResult testCount(String data, int runs, String elementName) throws Exception { this.elementName = elementName; // one run for warmup char[] buf = new char[1024]; //scanCountBuf(new StringReader(data), buf); // prepare input streams Reader[] readers = new Reader[runs]; for(int i = 0; i < runs ; ++i) { readers[i] = new StringReader(data); } // start timing double start = TestTimer.tick(); for(int i = 0; i < runs ; ++i) { count = 0; //scanCountBuf(readers[i], 1024); //scanCountBufMore(readers[i], buf); //slower as uses more() virtual function scanCountBuf(readers[i], buf); } double end = TestTimer.tick(); TestResult result = new TestResult(); result.elementCount = count; result.timeSecs = (end - start) / runs; return result; } }