Skip to content

Commit f7ed158

Browse files
authored
chore: introduce and immediately deprecate String.Slice.length (#11286)
This PR adds a function `String.Slice.length`, with the following deprecation string: There is no constant-time length function on slices. Use `s.positions.count` instead, or `isEmpty` if you only need to know whether the slice is empty.
1 parent cf0e444 commit f7ed158

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

src/Init/Data/String/Slice.lean

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,10 @@ Examples:
930930
def chars (s : Slice) :=
931931
Std.Iterators.Iter.map (fun ⟨pos, h⟩ => pos.get h) (positions s)
932932

933+
@[deprecated "There is no constant-time length function on slices. Use `s.positions.count` instead, or `isEmpty` if you only need to know whether the slice is empty." (since := "2025-11-20")]
934+
def length (s : Slice) : Nat :=
935+
s.positions.count
936+
933937
structure RevPosIterator (s : Slice) where
934938
currPos : s.Pos
935939
deriving Inhabited

src/Lean/ErrorExplanation.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ where
277277
fence (ticksToClose : Option Nat := none) := attempt do
278278
let line ← any
279279
if line.startsWith "```" then
280-
let numTicks := line.takeWhile (· == '`') |>.copy |>.length
280+
let numTicks := line.takeWhile (· == '`') |>.utf8ByteSize -- this makes sense because we know the slice consists only of ticks
281281
match ticksToClose with
282282
| none => return (numTicks, line.drop numTicks)
283283
| some n =>

src/Lean/Server/FileWorker/ExampleHover.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ def rewriteExamples (docstring : String) : String := Id.run do
8888
-- The current state, which tracks the context of the line being processed
8989
let mut inOutput : RWState := .normal
9090
for l in lines do
91-
let indent := l.takeWhile (· == ' ') |>.copy |>.length
91+
let indent := l.takeWhile (· == ' ') |>.utf8ByteSize -- this makes sense because we know the slice consists only of spaces
9292
let mut l' := l.trimAsciiStart
9393
-- Is this a code block fence?
9494
if l'.startsWith "```" then
95-
let count := l'.takeWhile (· == '`') |>.copy |>.length
95+
let count := l'.takeWhile (· == '`') |>.utf8ByteSize -- this makes sense because we know the slice consists only of ticks
9696
l' := l'.dropWhile (· == '`')
9797
l' := l'.dropWhile (· == ' ')
9898
match inOutput with

src/lake/Lake/Toml/Data/DateTime.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public def ofString? (t : String) : Option Time := do
6969
match s.split '.' |>.toList with
7070
| [s,f] =>
7171
let time ← ofValid? (← h.toNat?) (← m.toNat?) (← s.toNat?)
72-
return {time with fracExponent := f.copy.length-1, fracMantissa := ← f.toNat?}
72+
return {time with fracExponent := f.positions.count-1, fracMantissa := ← f.toNat?}
7373
| [s] =>
7474
ofValid? (← h.toNat?) (← m.toNat?) (← s.toNat?)
7575
| _ => none

0 commit comments

Comments
 (0)