@@ -22,7 +22,8 @@ public section
2222namespace String.Slice.Pattern
2323
2424inductive ForwardSliceSearcher (s : Slice) where
25- | empty (pos : s.Pos)
25+ | emptyBefore (pos : s.Pos)
26+ | emptyAt (pos : s.Pos) (h : pos ≠ s.endPos)
2627 | proper (needle : Slice) (table : Array String.Pos.Raw) (stackPos : String.Pos.Raw) (needlePos : String.Pos.Raw)
2728 | atEnd
2829deriving Inhabited
5657@[inline]
5758def iter (s : Slice) (pat : Slice) : Std.Iter (α := ForwardSliceSearcher s) (SearchStep s) :=
5859 if pat.utf8ByteSize == 0 then
59- { internalState := .empty s.startPos }
60+ { internalState := .emptyBefore s.startPos }
6061 else
6162 { internalState := .proper pat (buildTable pat) s.startPos.offset pat.startPos.offset }
6263
@@ -71,9 +72,8 @@ instance (s : Slice) : Std.Iterators.Iterator (ForwardSliceSearcher s) Id (Searc
7172 IsPlausibleStep it
7273 | .yield it' out =>
7374 match it.internalState with
74- | .empty pos =>
75- (∃ newPos, pos < newPos ∧ it'.internalState = .empty newPos) ∨
76- it'.internalState = .atEnd
75+ | .emptyBefore pos => (∃ h, it'.internalState = .emptyAt pos h) ∨ it'.internalState = .atEnd
76+ | .emptyAt pos h => ∃ newPos, pos < newPos ∧ it'.internalState = .emptyBefore newPos
7777 | .proper needle table stackPos needlePos =>
7878 (∃ newStackPos newNeedlePos,
7979 stackPos < newStackPos ∧
@@ -85,12 +85,15 @@ instance (s : Slice) : Std.Iterators.Iterator (ForwardSliceSearcher s) Id (Searc
8585 | .done => True
8686 step := fun ⟨iter⟩ =>
8787 match iter with
88- | .empty pos =>
88+ | .emptyBefore pos =>
8989 let res := .matched pos pos
9090 if h : pos ≠ s.endPos then
91- pure (.deflate ⟨.yield ⟨.empty ( pos.next h) ⟩ res, by simp⟩)
91+ pure (.deflate ⟨.yield ⟨.emptyAt pos h ⟩ res, by simp [h] ⟩)
9292 else
9393 pure (.deflate ⟨.yield ⟨.atEnd⟩ res, by simp⟩)
94+ | .emptyAt pos h =>
95+ let res := .rejected pos (pos.next h)
96+ pure (.deflate ⟨.yield ⟨.emptyBefore (pos.next h)⟩ res, by simp⟩)
9497 | .proper needle table stackPos needlePos =>
9598 let rec findNext (startPos : String.Pos.Raw)
9699 (currStackPos : String.Pos.Raw) (needlePos : String.Pos.Raw) (h : stackPos ≤ currStackPos) :=
@@ -148,15 +151,17 @@ instance (s : Slice) : Std.Iterators.Iterator (ForwardSliceSearcher s) Id (Searc
148151 findNext stackPos stackPos needlePos (by simp)
149152 | .atEnd => pure (.deflate ⟨.done, by simp⟩)
150153
151- private def toPair : ForwardSliceSearcher s → (Nat × Nat)
152- | .empty pos => (1 , s.utf8ByteSize - pos.offset.byteIdx)
153- | .proper _ _ sp _ => (1 , s.utf8ByteSize - sp.byteIdx)
154- | .atEnd => (0 , 0 )
154+ private def toOption : ForwardSliceSearcher s → Option (Nat × Nat)
155+ | .emptyBefore pos => some (s.utf8ByteSize - pos.offset.byteIdx, 1 )
156+ | .emptyAt pos _ => some (s.utf8ByteSize - pos.offset.byteIdx, 0 )
157+ | .proper _ _ sp _ => some (s.utf8ByteSize - sp.byteIdx, 0 )
158+ | .atEnd => none
155159
156160private instance : WellFoundedRelation (ForwardSliceSearcher s) where
157- rel s1 s2 := Prod.Lex (· < ·) (· < ·) s1.toPair s2.toPair
161+ rel := InvImage (Option.lt ( Prod.Lex (· < ·) (· < ·))) ForwardSliceSearcher.toOption
158162 wf := by
159163 apply InvImage.wf
164+ apply Option.wellFounded_lt
160165 apply (Prod.lex _ _).wf
161166
162167private def finitenessRelation :
@@ -168,30 +173,26 @@ private def finitenessRelation :
168173 obtain ⟨step, h, h'⟩ := h
169174 cases step
170175 · cases h
171- simp only [Std.Iterators.IterM.IsPlausibleStep, Std.Iterators.Iterator.IsPlausibleStep] at h'
172- split at h'
173- · next heq =>
174- rw [heq]
175- rcases h' with ⟨np, h1', h2'⟩ | h'
176- · rw [h2']
177- apply Prod.Lex.right'
178- · simp
179- · have haux := np.isValidForSlice.le_utf8ByteSize
180- simp [Slice.Pos.lt_iff, String.Pos.Raw.le_iff, String.Pos.Raw.lt_iff] at h1' haux ⊢
181- omega
182- · apply Prod.Lex.left
183- simp [h']
184- · next heq =>
185- rw [heq]
186- rcases h' with ⟨np, sp, h1', h2', h3'⟩ | h'
187- · rw [h3']
188- apply Prod.Lex.right'
189- · simp
190- · simp [String.Pos.Raw.le_iff, String.Pos.Raw.lt_iff] at h1' h2' ⊢
191- omega
192- · apply Prod.Lex.left
193- simp [h']
194- · contradiction
176+ revert h'
177+ simp only [Std.Iterators.IterM.IsPlausibleStep, Std.Iterators.Iterator.IsPlausibleStep]
178+ match it.internalState with
179+ | .emptyBefore pos =>
180+ rintro (⟨h, h'⟩|h') <;> simp [h', ForwardSliceSearcher.toOption, Option.lt, Prod.lex_def]
181+ | .emptyAt pos h =>
182+ simp only [forall_exists_index, and_imp]
183+ intro x hx h
184+ have := x.isValidForSlice.le_utf8ByteSize
185+ simp [h, ForwardSliceSearcher.toOption, Option.lt, Prod.lex_def, Pos.lt_iff,
186+ Pos.Raw.lt_iff, Pos.Raw.le_iff] at hx ⊢ this
187+ omega
188+ | .proper needle table stackPos needlePos =>
189+ simp only [exists_and_left]
190+ rintro (⟨newStackPos, h₁, h₂, ⟨x, hx⟩⟩|h)
191+ · simp [hx, ForwardSliceSearcher.toOption, Option.lt, Prod.lex_def, Pos.Raw.lt_iff,
192+ Pos.Raw.le_iff] at ⊢ h₁ h₂
193+ omega
194+ · simp [h, ForwardSliceSearcher.toOption, Option.lt]
195+ | .atEnd .. => simp
195196 · cases h'
196197 · cases h
197198
0 commit comments