@@ -65,32 +65,29 @@ export const reset = (): void => {
6565export const connect_node_compile_to_regexp = < T > ( connectNode : ConnectNode < T > ) : string => {
6666 if ( connectNode [ 0 ] !== null ) {
6767 HANDLERS . push ( connectNode [ 0 ] ) ;
68- return connectNode [ 1 ] === null ? '()|' : `(?:()|${ node_compile_to_regexp ( connectNode [ 1 ] ) } )|` ;
68+ return connectNode [ 1 ] === null ? '()$ |' : `(?:()$ |${ node_compile_to_regexp ( connectNode [ 1 ] ) } )|` ;
6969 }
7070
7171 return node_compile_to_regexp ( connectNode [ 1 ] ! ) + '|' ;
7272} ;
7373
7474export const node_compile_to_regexp = < T > ( node : Node < T > ) : string => {
75- let parts = '' ,
76- partsCnt = 0 ;
75+ let parts = '(?:' ;
7776
7877 if ( node [ 1 ] !== null ) {
79- partsCnt = 1 ;
80-
8178 HANDLERS . push ( node [ 1 ] ) ;
82- parts += '()|' ;
79+ parts += '()$ |' ;
8380 }
8481
8582 if ( node [ 2 ] !== null )
86- for ( let i = 0 , staticChildren = node [ 2 ] [ 1 ] ; i < staticChildren . length ; i ++ , partsCnt ++ )
83+ for ( let i = 0 , staticChildren = node [ 2 ] [ 1 ] ; i < staticChildren . length ; i ++ )
8784 parts += node_compile_to_regexp ( staticChildren [ i ] ) + '|' ;
8885
8986 if ( node [ 3 ] !== null ) {
9087 for (
9188 let i = 0 , patterns = node [ 3 ] [ 0 ] , connectNodes = node [ 3 ] [ 1 ] ;
9289 i < connectNodes . length ;
93- i ++ , partsCnt ++
90+ i ++
9491 ) {
9592 let patternPrevIdx = 0 ,
9693 pattern = patterns [ i ] ,
@@ -136,30 +133,22 @@ export const node_compile_to_regexp = <T>(node: Node<T>): string => {
136133 }
137134
138135 if ( node [ 4 ] !== null )
139- for (
140- let i = 0 , regexps = node [ 4 ] [ 0 ] , connectNodes = node [ 4 ] [ 1 ] ;
141- i < regexps . length ;
142- i ++ , partsCnt ++
143- )
136+ for ( let i = 0 , regexps = node [ 4 ] [ 0 ] , connectNodes = node [ 4 ] [ 1 ] ; i < regexps . length ; i ++ )
144137 parts += '(?:' + regexps [ i ] . slice ( 1 ) + connect_node_compile_to_regexp ( connectNodes [ i ] ) ;
145138
146139 if ( node [ 5 ] !== null )
147- for (
148- let i = 0 , keys = node [ 5 ] [ 0 ] , connectNodes = node [ 5 ] [ 1 ] ;
149- i < keys . length ;
150- i ++ , partsCnt ++
151- ) {
140+ for ( let i = 0 , keys = node [ 5 ] [ 0 ] , connectNodes = node [ 5 ] [ 1 ] ; i < keys . length ; i ++ ) {
152141 HANDLERS . push ( null ) ;
153142 parts +=
154143 parseNamedGroup ( keys [ i ] , 0 , keys [ i ] . length ) +
155144 connect_node_compile_to_regexp ( connectNodes [ i ] ) ;
156145 }
157146
158147 if ( node [ 6 ] !== null ) {
159- partsCnt ++ ;
160148 parts += '.*' + connect_node_compile_to_regexp ( node [ 6 ] ) ;
161149 }
162150
163- const str = partsCnt > 1 ? `(?:${ parts . slice ( 0 , - 1 ) } )` : parts . slice ( 0 , - 1 ) ;
164- return node [ 0 ] . length > 0 ? escapeStaticPart ( node [ 0 ] ) + str : str ;
151+ // early terminate
152+ parts += '$)' ;
153+ return node [ 0 ] . length > 0 ? escapeStaticPart ( node [ 0 ] ) + parts : parts ;
165154} ;
0 commit comments