Index: src/java/org/apache/regexp/RE.java =================================================================== RCS file: /home/cvspublic/jakarta-regexp/src/java/org/apache/regexp/RE.java,v retrieving revision 1.21 diff -c -r1.21 RE.java *** src/java/org/apache/regexp/RE.java 27 Feb 2004 02:41:20 -0000 1.21 --- src/java/org/apache/regexp/RE.java 19 Mar 2004 06:51:26 -0000 *************** *** 1443,1448 **** --- 1443,1453 ---- // Can we optimize the search by looking for a prefix string? if (program.prefix == null) { + if ((program.flags & REProgram.OPT_STARTS_WITH_BOL) != 0 + && (matchFlags & MATCH_MULTILINE) == 0) + { + return (i == 0) && matchAt(0); + } // Unprefixed matching must try for a match at each character for ( ;! search.isEnd(i - 1); i++) { Index: src/java/org/apache/regexp/REProgram.java =================================================================== RCS file: /home/cvspublic/jakarta-regexp/src/java/org/apache/regexp/REProgram.java,v retrieving revision 1.4 diff -c -r1.4 REProgram.java *** src/java/org/apache/regexp/REProgram.java 17 Feb 2004 13:37:54 -0000 1.4 --- src/java/org/apache/regexp/REProgram.java 19 Mar 2004 06:51:26 -0000 *************** *** 33,38 **** --- 33,39 ---- public class REProgram implements Serializable { static final int OPT_HASBACKREFS = 1; + static final int OPT_STARTS_WITH_BOL = 2; char[] instruction; // The compiled regular expression 'program' int lenInstruction; // The amount of the instruction buffer in use *************** *** 117,131 **** { // to the end node int next = instruction[0 + RE.offsetNext]; ! if (instruction[next + RE.offsetOpcode] == RE.OP_END) { ! // and the branch starts with an atom ! if (lenInstruction >= (RE.nodeSize * 2) && instruction[RE.nodeSize + RE.offsetOpcode] == RE.OP_ATOM) { // then get that atom as an prefix because there's no other choice int lenAtom = instruction[RE.nodeSize + RE.offsetOpdata]; prefix = new char[lenAtom]; System.arraycopy(instruction, RE.nodeSize * 2, prefix, 0, lenAtom); } } } --- 118,140 ---- { // to the end node int next = instruction[0 + RE.offsetNext]; ! if (instruction[next + RE.offsetOpcode] == RE.OP_END ! && lenInstruction >= (RE.nodeSize * 2)) { ! final char nextOp = ! instruction[RE.nodeSize + RE.offsetOpcode]; ! // the branch starts with an atom ! if (nextOp == RE.OP_ATOM) { // then get that atom as an prefix because there's no other choice int lenAtom = instruction[RE.nodeSize + RE.offsetOpdata]; prefix = new char[lenAtom]; System.arraycopy(instruction, RE.nodeSize * 2, prefix, 0, lenAtom); + } + // the branch starts with an BOL + else if (nextOp == RE.OP_BOL) + { + flags |= OPT_STARTS_WITH_BOL; } } }