`String.Slice.revSplit` drops the empty piece at the start of the slice

Aperta Adatta ai principianti
#16,305 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva

Direzione di ricerca

Inizia con RevSplitIterator.step in src/Init/Data/String/Slice/Pattern.lean e confronta il suo caso di ricerca completata con SplitIterator. Riproduci il problema con il programma Lean minimo nel report, includendo i casi di slice vuota e di separatore iniziale. Il lavoro è completato quando revSplit restituisce gli stessi elementi di split in ordine inverso e la suite di test esistente copre questo comportamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Prerequisites
  • Check that your issue is not already filed: searched for revSplit, Slice.split, String.Slice split and split empty slice.
  • Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release.
Description

String.Slice.revSplit leaves out the leftmost piece when it is empty. A slice that starts with a separator, or an empty slice, gives one piece fewer than String.Slice.split. A separator at the end does give an empty piece, so the two ends behave differently:

def main : IO Unit := do
  IO.println (repr <| (",x".toSlice.split ',').toList.map (·.copy))
  IO.println (repr <| (",x".toSlice.revSplit ',').toList.map (·.copy))
  IO.println (repr <| ("x,".toSlice.revSplit ',').toList.map (·.copy))
  IO.println (repr <| ("".toSlice.revSplit ',').toList.map (·.copy))
lean --run RevSplit.lean
# expected:      # actual:
["", "x"]        ["", "x"]
["x", ""]        ["x"]
["", "x"]        ["", "x"]
[""]             []

Likewise "aa".toSlice.split 'a' gives three empty pieces and "aa".toSlice.revSplit 'a' gives two. The Char → Bool pattern form behaves the same way.

The revSplit docstring matches the split docstring apart from the direction ("starting from the end of the slice and traversing towards the start ... If multiple subslices in a row match the pattern, the resulting list will contain empty slices"), so revSplit should give the pieces of split in reverse order.

In RevSplitIterator.step (src/Init/Data/String/Slice/Pattern.lean), when the searcher is done, the remaining piece s.sliceTo currPos is yielded only if currPos ≠ s.startPos. The forward SplitIterator yields its remaining piece unconditionally.

Expected behavior: revSplit gives the same pieces as split, in reverse order.

Actual behavior: the leftmost empty piece is missing.

Versions
4.37.0-nightly-2026-11-05   ["x"] / []
4.36.0-rc2                   ["x"] / []
4.35.0                       ["x"] / []

These are on macOS arm64 (official toolchains via elan), and Linux x86_64 gives the same results. Nothing under src/Init/Data/String/ has changed on master since the nightly. The code came in with #10514.

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask
them to add 👍 to it.

Found by an automated differential tester (ephyra); this report was drafted with AI assistance.

Lingua principale
Lean
Stelle
10.3k
Fork
1.1k
Merge medio
19h 8m
PR unite (30g)
421

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.