Skip to content

Commit 4b313ff

Browse files
committed
chore: add String.ofList redefine String.toList
1 parent 106b0fa commit 4b313ff

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/Init/Data/String/Basic.lean

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,32 @@ def String.Internal.toArray (b : String) : Array Char :=
235235
theorem String.Internal.toArray_empty : String.Internal.toArray "" = #[] := by
236236
simp [toArray]
237237

238+
/--
239+
Converts a string to a list of characters.
240+
241+
Since strings are represented as dynamic arrays of bytes containing the string encoded using
242+
UTF-8, this operation takes time and space linear in the length of the string.
243+
244+
Examples:
245+
* `"abc".toList = ['a', 'b', 'c']`
246+
* `"".toList = []`
247+
* `"\n".toList = ['\n']`
248+
-/
249+
@[extern "lean_string_data", expose]
250+
def toList (s : String) : List Char :=
251+
(String.Internal.toArray s).toList
252+
253+
/--
254+
Converts a string to a list of characters.
255+
256+
Since strings are represented as dynamic arrays of bytes containing the string encoded using
257+
UTF-8, this operation takes time and space linear in the length of the string.
258+
259+
Examples:
260+
* `"abc".toList = ['a', 'b', 'c']`
261+
* `"".toList = []`
262+
* `"\n".toList = ['\n']`
263+
-/
238264
@[extern "lean_string_data", expose]
239265
def String.data (b : String) : List Char :=
240266
(String.Internal.toArray b).toList
@@ -396,21 +422,6 @@ instance : LE String :=
396422
instance decLE (s₁ s₂ : String) : Decidable (s₁ ≤ s₂) :=
397423
inferInstanceAs (Decidable (Not _))
398424

399-
/--
400-
Converts a string to a list of characters.
401-
402-
Since strings are represented as dynamic arrays of bytes containing the string encoded using
403-
UTF-8, this operation takes time and space linear in the length of the string.
404-
405-
Examples:
406-
* `"abc".toList = ['a', 'b', 'c']`
407-
* `"".toList = []`
408-
* `"\n".toList = ['\n']`
409-
-/
410-
@[inline, expose]
411-
def toList (s : String) : List Char :=
412-
s.data
413-
414425
theorem _root_.List.isPrefix_of_utf8Encode_append_eq_utf8Encode {l m : List Char} (b : ByteArray)
415426
(h : l.utf8Encode ++ b = m.utf8Encode) : l <+: m := by
416427
induction l generalizing m with

src/Init/Data/String/Bootstrap.lean

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ Examples:
134134
* `[].asString = ""`
135135
* `['a', 'a', 'a'].asString = "aaa"`
136136
-/
137+
@[extern "lean_string_mk", expose]
138+
def String.ofList (data : List Char) : String :=
139+
⟨List.utf8Encode data,.intro data rfl⟩
140+
137141
@[extern "lean_string_mk", expose]
138142
def String.mk (data : List Char) : String :=
139143
⟨List.utf8Encode data,.intro data rfl⟩

0 commit comments

Comments
 (0)