Skip to content

Commit

Permalink
split string into byte limited chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
lecopivo committed Oct 25, 2023
1 parent 56d4045 commit 387e536
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions SciLean/Lean/String.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


/-- Split a string into substrings that have maximum size in bytes
-/
partial def String.splitToByteChunks (str : String) (chunkByteSize : Nat) : Array Substring := Id.run do
let mut chunks := #[]
let mut s : String.Pos := 0
let e : String.Pos := str.endPos
while s < e do
let mut s' := s
while (str.next s' - s).byteIdx ≤ chunkByteSize && s' < e do
s' := str.next s'
chunks := chunks.push (Substring.mk str s s')
s := s'
chunks

0 comments on commit 387e536

Please sign in to comment.