11import type * as Schema from "../../Schema.ts"
2+ import * as Str from "../../String.ts"
23import * as Model from "./model.ts"
34
45type Pattern = Schema . Annotations . ToArbitrary . Pattern
@@ -146,13 +147,16 @@ function concatenation(nodes: ReadonlyArray<Node>): Node {
146147 }
147148}
148149
149- function alternation ( nodes : ReadonlyArray < Node > ) : Node {
150+ function alternation ( nodes : ReadonlyArray < Node > , wellFormed = false ) : Node {
150151 const flattened : Array < Node > = [ ]
151152 for ( const node of nodes ) {
152153 if ( node . _tag === "Alternation" ) flattened . push ( ...node . nodes )
153154 else flattened . push ( node )
154155 }
155- return flattened . length === 1 ? flattened [ 0 ] : { _tag : "Alternation" , nodes : flattened }
156+ const choices = wellFormed && flattened . length > 1
157+ ? flattened . filter ( ( node ) => node . _tag !== "Literal" || Str . isWellFormed ( node . value ) )
158+ : flattened
159+ return choices . length === 1 ? choices [ 0 ] : { _tag : "Alternation" , nodes : choices }
156160}
157161
158162function normalizeIntervals ( intervals : ReadonlyArray < Interval > ) : ReadonlyArray < Interval > {
@@ -276,25 +280,28 @@ class Parser {
276280 private index = 0
277281 private supported = true
278282 private readonly source : string
279- private readonly dotAll : boolean
283+ private readonly flags : string
284+ private readonly wellFormed : boolean
280285
281- constructor ( source : string , dotAll : boolean ) {
286+ constructor ( { flags , source } : Pattern , wellFormed : boolean ) {
282287 this . source = source
283- this . dotAll = dotAll
288+ this . flags = flags
289+ this . wellFormed = wellFormed
284290 }
285291
286292 parse ( ) : Node | undefined {
287- const node = this . parseAlternation ( )
293+ const node = this . parseAlternation ( true )
288294 return this . supported && this . index === this . source . length ? node : undefined
289295 }
290296
291- private parseAlternation ( ) : Node {
297+ private parseAlternation ( root = false ) : Node {
292298 const nodes = [ this . parseConcatenation ( ) ]
293299 while ( this . peek ( ) === "|" ) {
294300 this . index ++
295301 nodes . push ( this . parseConcatenation ( ) )
296302 }
297- return alternation ( nodes )
303+ // Without the Unicode flag, a surrogate can pair with a literal outside its group.
304+ return alternation ( nodes , this . wellFormed && ( root || this . flags . includes ( "u" ) ) )
298305 }
299306
300307 private parseConcatenation ( ) : Node {
@@ -317,7 +324,7 @@ class Parser {
317324 case "$" :
318325 return empty
319326 case "." :
320- return character ( this . dotAll ? codePointIntervals : complementIntervals ( lineTerminatorIntervals ) )
327+ return character ( this . flags . includes ( "s" ) ? codePointIntervals : complementIntervals ( lineTerminatorIntervals ) )
321328 case "[" :
322329 return this . parseCharacterClass ( )
323330 case "(" :
@@ -789,7 +796,7 @@ function minimumLength(node: Node): number {
789796 case "Concatenation" :
790797 return node . nodes . reduce ( ( sum , child ) => sum + minimumLength ( child ) , 0 )
791798 case "Repetition" :
792- return minimumLength ( node . node ) * node . minimum
799+ return node . minimum === 0 ? 0 : minimumLength ( node . node ) * node . minimum
793800 }
794801}
795802
@@ -826,12 +833,12 @@ function addUnanchoredSurroundings(node: Node, source: string, flags: string): N
826833 * https://github.com/dubzzz/fast-check/blob/v4.9.0/packages/fast-check/src/arbitrary/stringMatching.ts
827834 */
828835/** @internal */
829- export function compile ( pattern : Pattern ) : Compiled | undefined {
836+ export function compile ( pattern : Pattern , wellFormed = false ) : Compiled | undefined {
830837 const { flags, source } = pattern
831838 // Case folding, multiline assertions, and Unicode sets require dedicated AST semantics. Falling back preserves the
832839 // contract that a constructively supported pattern never silently ignores one of its flags.
833840 if ( flags . includes ( "i" ) || flags . includes ( "m" ) || flags . includes ( "v" ) ) return undefined
834- const parsed = new Parser ( source , flags . includes ( "s" ) ) . parse ( )
841+ const parsed = new Parser ( pattern , wellFormed ) . parse ( )
835842 if ( parsed === undefined ) return undefined
836843 const restricted = flags . includes ( "u" ) ? parsed : restrictCharacterCodePoints ( parsed , 0xffff )
837844 const node = addUnanchoredSurroundings ( restricted , source , flags )
@@ -841,6 +848,7 @@ export function compile(pattern: Pattern): Compiled | undefined {
841848 return {
842849 minimumLength : minimum ,
843850 generate : ( state , minimumLength , maximumLength ) => {
851+ if ( minimum === Number . POSITIVE_INFINITY ) return undefined
844852 const lengths = possibleLengths ( node , maximumLength , cache )
845853 const candidates : Array < number > = [ ]
846854 for ( let length = minimumLength ; length <= maximumLength ; length ++ ) {
0 commit comments