Skip to content

Commit 51b6738

Browse files
authored
refactor: better name for String.replaceStart and variants (#11290)
This PR renames `String.replaceStartEnd` to `String.slice`, `String.replaceStart` to `String.sliceFrom`, and `String.replaceEnd` to `String.sliceTo`, and similar for the corresponding functions on `String.Slice`.
1 parent 556e960 commit 51b6738

File tree

8 files changed

+316
-209
lines changed

8 files changed

+316
-209
lines changed

src/Init/Data/String/Basic.lean

Lines changed: 260 additions & 153 deletions
Large diffs are not rendered by default.

src/Init/Data/String/Lemmas/Splits.lean

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ theorem ValidPos.Splits.toSlice {s : String} {p : s.ValidPos} {t₁ t₂ : Strin
8181
splits_toSlice_iff.mpr h
8282

8383
theorem ValidPos.splits {s : String} (p : s.ValidPos) :
84-
p.Splits (s.replaceEnd p).copy (s.replaceStart p).copy where
84+
p.Splits (s.sliceTo p).copy (s.sliceFrom p).copy where
8585
eq_append := by simp [← bytes_inj, Slice.bytes_copy, ← size_bytes]
8686
offset_eq_rawEndPos := by simp
8787

8888
theorem Slice.Pos.splits {s : Slice} (p : s.Pos) :
89-
p.Splits (s.replaceEnd p).copy (s.replaceStart p).copy where
90-
eq_append := copy_eq_copy_replaceEnd
89+
p.Splits (s.sliceTo p).copy (s.sliceFrom p).copy where
90+
eq_append := copy_eq_copy_sliceTo
9191
offset_eq_rawEndPos := by simp
9292

9393
theorem ValidPos.Splits.bytes_left_eq {s : String} {p : s.ValidPos} {t₁ t₂}
@@ -182,40 +182,40 @@ theorem Slice.Pos.Splits.eq_startPos_iff {s : Slice} {p : s.Pos} (h : p.Splits t
182182
rw [← toCopy_inj, ← startValidPos_copy, h.toCopy.eq_startValidPos_iff]
183183

184184
theorem ValidPos.splits_next_right {s : String} (p : s.ValidPos) (hp : p ≠ s.endValidPos) :
185-
p.Splits (s.replaceEnd p).copy (singleton (p.get hp) ++ (s.replaceStart (p.next hp)).copy) where
186-
eq_append := by simpa [← append_assoc] using p.eq_copy_replaceEnd_append_get hp
185+
p.Splits (s.sliceTo p).copy (singleton (p.get hp) ++ (s.sliceFrom (p.next hp)).copy) where
186+
eq_append := by simpa [← append_assoc] using p.eq_copy_sliceTo_append_get hp
187187
offset_eq_rawEndPos := by simp
188188

189189
theorem ValidPos.splits_next {s : String} (p : s.ValidPos) (hp : p ≠ s.endValidPos) :
190-
(p.next hp).Splits ((s.replaceEnd p).copy ++ singleton (p.get hp)) (s.replaceStart (p.next hp)).copy where
191-
eq_append := p.eq_copy_replaceEnd_append_get hp
190+
(p.next hp).Splits ((s.sliceTo p).copy ++ singleton (p.get hp)) (s.sliceFrom (p.next hp)).copy where
191+
eq_append := p.eq_copy_sliceTo_append_get hp
192192
offset_eq_rawEndPos := by simp
193193

194194
theorem Slice.Pos.splits_next_right {s : Slice} (p : s.Pos) (hp : p ≠ s.endPos) :
195-
p.Splits (s.replaceEnd p).copy (singleton (p.get hp) ++ (s.replaceStart (p.next hp)).copy) where
196-
eq_append := by simpa [← append_assoc] using p.copy_eq_copy_replaceEnd_append_get hp
195+
p.Splits (s.sliceTo p).copy (singleton (p.get hp) ++ (s.sliceFrom (p.next hp)).copy) where
196+
eq_append := by simpa [← append_assoc] using p.copy_eq_copy_sliceTo_append_get hp
197197
offset_eq_rawEndPos := by simp
198198

199199
theorem Slice.Pos.splits_next {s : Slice} (p : s.Pos) (hp : p ≠ s.endPos) :
200-
(p.next hp).Splits ((s.replaceEnd p).copy ++ singleton (p.get hp)) (s.replaceStart (p.next hp)).copy where
201-
eq_append := p.copy_eq_copy_replaceEnd_append_get hp
200+
(p.next hp).Splits ((s.sliceTo p).copy ++ singleton (p.get hp)) (s.sliceFrom (p.next hp)).copy where
201+
eq_append := p.copy_eq_copy_sliceTo_append_get hp
202202
offset_eq_rawEndPos := by simp
203203

204204
theorem ValidPos.Splits.exists_eq_singleton_append {s : String} {p : s.ValidPos}
205205
(hp : p ≠ s.endValidPos) (h : p.Splits t₁ t₂) : ∃ t₂', t₂ = singleton (p.get hp) ++ t₂' :=
206-
⟨(s.replaceStart (p.next hp)).copy, h.eq_right (p.splits_next_right hp)⟩
206+
⟨(s.sliceFrom (p.next hp)).copy, h.eq_right (p.splits_next_right hp)⟩
207207

208208
theorem ValidPos.Splits.exists_eq_append_singleton {s : String} {p : s.ValidPos}
209209
(hp : p ≠ s.endValidPos) (h : (p.next hp).Splits t₁ t₂) : ∃ t₁', t₁ = t₁' ++ singleton (p.get hp) :=
210-
⟨(s.replaceEnd p).copy, h.eq_left (p.splits_next hp)⟩
210+
⟨(s.sliceTo p).copy, h.eq_left (p.splits_next hp)⟩
211211

212212
theorem Slice.Pos.Splits.exists_eq_singleton_append {s : Slice} {p : s.Pos}
213213
(hp : p ≠ s.endPos) (h : p.Splits t₁ t₂) : ∃ t₂', t₂ = singleton (p.get hp) ++ t₂' :=
214-
⟨(s.replaceStart (p.next hp)).copy, h.eq_right (p.splits_next_right hp)⟩
214+
⟨(s.sliceFrom (p.next hp)).copy, h.eq_right (p.splits_next_right hp)⟩
215215

216216
theorem Slice.Pos.Splits.exists_eq_append_singleton {s : Slice} {p : s.Pos}
217217
(hp : p ≠ s.endPos) (h : (p.next hp).Splits t₁ t₂) : ∃ t₁', t₁ = t₁' ++ singleton (p.get hp) :=
218-
⟨(s.replaceEnd p).copy, h.eq_left (p.splits_next hp)⟩
218+
⟨(s.sliceTo p).copy, h.eq_left (p.splits_next hp)⟩
219219

220220
theorem ValidPos.Splits.ne_endValidPos_of_singleton {s : String} {p : s.ValidPos}
221221
(h : p.Splits t₁ (singleton c ++ t₂)) : p ≠ s.endValidPos := by

src/Init/Data/String/Modify.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ def ValidPos.set {s : String} (p : s.ValidPos) (c : Char) (hp : p ≠ s.endValid
4141
refine ByteArray.IsValidUTF8.append ?_ (p.next hp).isValid.isValidUTF8_extract_utf8ByteSize
4242
exact p.isValid.isValidUTF8_extract_zero.push hc.1)
4343
else
44-
(s.replaceEnd p).copy ++ singleton c ++ (s.replaceStart (p.next hp)).copy
44+
(s.sliceTo p).copy ++ singleton c ++ (s.sliceFrom (p.next hp)).copy
4545

4646
theorem ValidPos.set_eq_append {s : String} {p : s.ValidPos} {c : Char} {hp} :
47-
p.set c hp = (s.replaceEnd p).copy ++ singleton c ++ (s.replaceStart (p.next hp)).copy := by
47+
p.set c hp = (s.sliceTo p).copy ++ singleton c ++ (s.sliceFrom (p.next hp)).copy := by
4848
rw [set]
4949
split
5050
· rename_i h
@@ -56,7 +56,7 @@ theorem Pos.Raw.IsValid.set_of_le {s : String} {p : s.ValidPos} {c : Char} {hp :
5656
{q : Pos.Raw} (hq : q.IsValid s) (hpq : q ≤ p.offset) : q.IsValid (p.set c hp) := by
5757
rw [ValidPos.set_eq_append, String.append_assoc]
5858
apply append_right
59-
rw [isValid_copy_iff, isValidForSlice_stringReplaceEnd]
59+
rw [isValid_copy_iff, isValidForSlice_stringSliceTo]
6060
exact ⟨hpq, hq⟩
6161

6262
/-- Given a valid position in a string, obtain the corresponding position after setting a character on
@@ -91,7 +91,7 @@ def ValidPos.appendRight {s : String} (p : s.ValidPos) (t : String) : (s ++ t).V
9191
isValid := p.isValid.append_right t
9292

9393
theorem ValidPos.splits_pastSet {s : String} {p : s.ValidPos} {c : Char} {hp} :
94-
(p.pastSet c hp).Splits ((s.replaceEnd p).copy ++ singleton c) (s.replaceStart (p.next hp)).copy where
94+
(p.pastSet c hp).Splits ((s.sliceTo p).copy ++ singleton c) (s.sliceFrom (p.next hp)).copy where
9595
eq_append := set_eq_append
9696
offset_eq_rawEndPos := by simp
9797

src/Init/Data/String/Search.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Examples:
6464
@[inline]
6565
def Slice.Pos.find? {s : Slice} (pos : s.Pos) (pattern : ρ) [ToForwardSearcher pattern σ] :
6666
Option s.Pos :=
67-
((s.replaceStart pos).find? pattern).map ofReplaceStart
67+
((s.sliceFrom pos).find? pattern).map ofSliceFrom
6868

6969
/--
7070
Finds the position of the first match of the pattern {name}`pattern` in after the position

src/Init/Data/String/Slice.lean

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ instance : Std.Iterators.Iterator (SplitIterator pat s) Id Slice where
145145
| ⟨.operating currPos searcher⟩ =>
146146
match h : searcher.step with
147147
| ⟨.yield searcher' (.matched startPos endPos), hps⟩ =>
148-
let slice := s.replaceStartEnd! currPos startPos
148+
let slice := s.slice! currPos startPos
149149
let nextIt := ⟨.operating endPos searcher'⟩
150150
pure (.deflate ⟨.yield nextIt slice, by simp [nextIt, hps.isPlausibleSuccessor_of_yield]⟩)
151151
| ⟨.yield searcher' (.rejected ..), hps⟩ =>
@@ -155,7 +155,7 @@ instance : Std.Iterators.Iterator (SplitIterator pat s) Id Slice where
155155
pure (.deflate ⟨.skip ⟨.operating currPos searcher'⟩,
156156
by simp [hps.isPlausibleSuccessor_of_skip]⟩)
157157
| ⟨.done, _⟩ =>
158-
let slice := s.replaceStart currPos
158+
let slice := s.sliceFrom currPos
159159
pure (.deflate ⟨.yield ⟨.atEnd⟩ slice, by simp⟩)
160160
| ⟨.atEnd⟩ => pure (.deflate ⟨.done, by simp⟩)
161161

@@ -241,7 +241,7 @@ instance : Std.Iterators.Iterator (SplitInclusiveIterator pat s) Id Slice where
241241
| ⟨.operating currPos searcher⟩ =>
242242
match h : searcher.step with
243243
| ⟨.yield searcher' (.matched _ endPos), hps⟩ =>
244-
let slice := s.replaceStartEnd! currPos endPos
244+
let slice := s.slice! currPos endPos
245245
let nextIt := ⟨.operating endPos searcher'⟩
246246
pure (.deflate ⟨.yield nextIt slice,
247247
by simp [nextIt, hps.isPlausibleSuccessor_of_yield]⟩)
@@ -253,7 +253,7 @@ instance : Std.Iterators.Iterator (SplitInclusiveIterator pat s) Id Slice where
253253
by simp [hps.isPlausibleSuccessor_of_skip]⟩)
254254
| ⟨.done, _⟩ =>
255255
if currPos != s.endPos then
256-
let slice := s.replaceStart currPos
256+
let slice := s.sliceFrom currPos
257257
pure (.deflate ⟨.yield ⟨.atEnd⟩ slice, by simp⟩)
258258
else
259259
pure (.deflate ⟨.done, by simp⟩)
@@ -337,7 +337,7 @@ Examples:
337337
-/
338338
@[inline]
339339
def dropPrefix? (s : Slice) (pat : ρ) [ForwardPattern pat] : Option Slice :=
340-
(ForwardPattern.dropPrefix? pat s).map s.replaceStart
340+
(ForwardPattern.dropPrefix? pat s).map s.sliceFrom
341341

342342
/--
343343
If {name}`pat` matches a prefix of {name}`s`, returns the remainder. Returns {name}`s` unmodified
@@ -377,7 +377,7 @@ def replace [ToSlice α] (s : Slice) (pattern : ρ) [ToForwardSearcher pattern
377377
String :=
378378
(ToForwardSearcher.toSearcher pattern s).fold (init := "") (fun
379379
| sofar, .matched .. => sofar ++ ToSlice.toSlice replacement
380-
| sofar, .rejected start stop => sofar ++ s.replaceStartEnd! start stop)
380+
| sofar, .rejected start stop => sofar ++ s.slice! start stop)
381381

382382
/--
383383
Removes the specified number of characters (Unicode code points) from the start of the slice.
@@ -391,7 +391,7 @@ Examples:
391391
-/
392392
@[inline]
393393
def drop (s : Slice) (n : Nat) : Slice :=
394-
s.replaceStart (s.startPos.nextn n)
394+
s.sliceFrom (s.startPos.nextn n)
395395

396396
/--
397397
Creates a new slice that contains the longest prefix of {name}`s` for which {name}`pat` matched
@@ -409,13 +409,13 @@ def dropWhile (s : Slice) (pat : ρ) [ForwardPattern pat] : Slice :=
409409
where
410410
@[specialize pat]
411411
go (curr : s.Pos) : Slice :=
412-
if let some nextCurr := ForwardPattern.dropPrefix? pat (s.replaceStart curr) then
413-
if curr < Pos.ofReplaceStart nextCurr then
414-
go (Pos.ofReplaceStart nextCurr)
412+
if let some nextCurr := ForwardPattern.dropPrefix? pat (s.sliceFrom curr) then
413+
if curr < Pos.ofSliceFrom nextCurr then
414+
go (Pos.ofSliceFrom nextCurr)
415415
else
416-
s.replaceStart curr
416+
s.sliceFrom curr
417417
else
418-
s.replaceStart curr
418+
s.sliceFrom curr
419419
termination_by curr
420420

421421
/--
@@ -449,7 +449,7 @@ Examples:
449449
-/
450450
@[inline]
451451
def take (s : Slice) (n : Nat) : Slice :=
452-
s.replaceEnd (s.startPos.nextn n)
452+
s.sliceTo (s.startPos.nextn n)
453453

454454
/--
455455
Creates a new slice that contains the longest prefix of {name}`s` for which {name}`pat` matched
@@ -469,13 +469,13 @@ def takeWhile (s : Slice) (pat : ρ) [ForwardPattern pat] : Slice :=
469469
where
470470
@[specialize pat]
471471
go (curr : s.Pos) : Slice :=
472-
if let some nextCurr := ForwardPattern.dropPrefix? pat (s.replaceStart curr) then
473-
if curr < Pos.ofReplaceStart nextCurr then
474-
go (Pos.ofReplaceStart nextCurr)
472+
if let some nextCurr := ForwardPattern.dropPrefix? pat (s.sliceFrom curr) then
473+
if curr < Pos.ofSliceFrom nextCurr then
474+
go (Pos.ofSliceFrom nextCurr)
475475
else
476-
s.replaceEnd curr
476+
s.sliceTo curr
477477
else
478-
s.replaceEnd curr
478+
s.sliceTo curr
479479
termination_by curr
480480

481481
/--
@@ -579,7 +579,7 @@ instance [Pure m] : Std.Iterators.Iterator (RevSplitIterator ρ s) m Slice where
579579
| ⟨.operating currPos searcher⟩ =>
580580
match h : searcher.step with
581581
| ⟨.yield searcher' (.matched startPos endPos), hps⟩ =>
582-
let slice := s.replaceStartEnd! endPos currPos
582+
let slice := s.slice! endPos currPos
583583
let nextIt := ⟨.operating startPos searcher'⟩
584584
pure (.deflate ⟨.yield nextIt slice, by simp [nextIt, hps.isPlausibleSuccessor_of_yield]⟩)
585585
| ⟨.yield searcher' (.rejected ..), hps⟩ =>
@@ -590,7 +590,7 @@ instance [Pure m] : Std.Iterators.Iterator (RevSplitIterator ρ s) m Slice where
590590
by simp [hps.isPlausibleSuccessor_of_skip]⟩)
591591
| ⟨.done, _⟩ =>
592592
if currPos ≠ s.startPos then
593-
let slice := s.replaceEnd currPos
593+
let slice := s.sliceTo currPos
594594
pure (.deflate ⟨.yield ⟨.atEnd⟩ slice, by simp⟩)
595595
else
596596
pure (.deflate ⟨.done, by simp⟩)
@@ -672,7 +672,7 @@ Examples:
672672
-/
673673
@[inline]
674674
def dropSuffix? (s : Slice) (pat : ρ) [BackwardPattern pat] : Option Slice :=
675-
(BackwardPattern.dropSuffix? pat s).map s.replaceEnd
675+
(BackwardPattern.dropSuffix? pat s).map s.sliceTo
676676

677677
/--
678678
If {name}`pat` matches a suffix of {name}`s`, returns the remainder. Returns {name}`s` unmodified
@@ -705,7 +705,7 @@ Examples:
705705
-/
706706
@[inline]
707707
def dropEnd (s : Slice) (n : Nat) : Slice :=
708-
s.replaceEnd (s.endPos.prevn n)
708+
s.sliceTo (s.endPos.prevn n)
709709

710710
/--
711711
Creates a new slice that contains the longest suffix of {name}`s` for which {name}`pat` matched
@@ -722,13 +722,13 @@ def dropEndWhile (s : Slice) (pat : ρ) [BackwardPattern pat] : Slice :=
722722
where
723723
@[specialize pat]
724724
go (curr : s.Pos) : Slice :=
725-
if let some nextCurr := BackwardPattern.dropSuffix? pat (s.replaceEnd curr) then
726-
if Pos.ofReplaceEnd nextCurr < curr then
727-
go (Pos.ofReplaceEnd nextCurr)
725+
if let some nextCurr := BackwardPattern.dropSuffix? pat (s.sliceTo curr) then
726+
if Pos.ofSliceTo nextCurr < curr then
727+
go (Pos.ofSliceTo nextCurr)
728728
else
729-
s.replaceEnd curr
729+
s.sliceTo curr
730730
else
731-
s.replaceEnd curr
731+
s.sliceTo curr
732732
termination_by curr.down
733733

734734
/--
@@ -762,7 +762,7 @@ Examples:
762762
-/
763763
@[inline]
764764
def takeEnd (s : Slice) (n : Nat) : Slice :=
765-
s.replaceStart (s.endPos.prevn n)
765+
s.sliceFrom (s.endPos.prevn n)
766766

767767
/--
768768
Creates a new slice that contains the suffix prefix of {name}`s` for which {name}`pat` matched
@@ -781,13 +781,13 @@ def takeEndWhile (s : Slice) (pat : ρ) [BackwardPattern pat] : Slice :=
781781
where
782782
@[specialize pat]
783783
go (curr : s.Pos) : Slice :=
784-
if let some nextCurr := BackwardPattern.dropSuffix? pat (s.replaceEnd curr) then
785-
if Pos.ofReplaceEnd nextCurr < curr then
786-
go (Pos.ofReplaceEnd nextCurr)
784+
if let some nextCurr := BackwardPattern.dropSuffix? pat (s.sliceTo curr) then
785+
if Pos.ofSliceTo nextCurr < curr then
786+
go (Pos.ofSliceTo nextCurr)
787787
else
788-
s.replaceStart curr
788+
s.sliceFrom curr
789789
else
790-
s.replaceStart curr
790+
s.sliceFrom curr
791791
termination_by curr.down
792792

793793
/--

src/Lean/ErrorExplanation.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ where
9898
if nonempty && it' == it.2 then
9999
.error ⟨_, it'⟩ (.other "Expected a nonempty string")
100100
else
101-
.success ⟨_, it'⟩ (it.1.replaceStartEnd! it.2 it').copy
101+
.success ⟨_, it'⟩ (it.1.slice! it.2 it').copy
102102

103103
/-- Parses a named attribute, and returns its name and value. -/
104104
namedAttr : Parser (String × String) := attempt do

src/Lean/Server/FileWorker/ExampleHover.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private def addCommentAt (indent : Nat) (line : String) : String := Id.run do
4242
else
4343
-- The line was entirely ' ', and was shorter than the indentation level. No `--` added.
4444
return line
45-
let remaining := line.replaceStart iter
45+
let remaining := line.sliceFrom iter
4646
if remaining.all (· == ' ') then
4747
return line
4848
else

src/Std/Internal/Parsec/String.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected def Parser.run (p : Parser α) (s : String) : Except String α :=
4141
Parses the given string.
4242
-/
4343
def pstring (s : String) : Parser String := fun it =>
44-
if (it.1.replaceStart it.2).startsWith s then
44+
if (it.1.sliceFrom it.2).startsWith s then
4545
.success ⟨_, it.2.nextn s.length⟩ s
4646
else
4747
.error it (.other s!"expected: {s}")

0 commit comments

Comments
 (0)