@@ -74,11 +74,11 @@ Encodes a string in UTF-8 as an array of bytes.
7474-/
7575@[extern "lean_string_to_utf8"]
7676def String.toUTF8 (a : @& String) : ByteArray :=
77- a.bytes
77+ a.toByteArray
7878
79- @[simp] theorem String.toUTF8_eq_bytes {s : String} : s.toUTF8 = s.bytes := (rfl)
79+ @[simp] theorem String.toUTF8_eq_toByteArray {s : String} : s.toUTF8 = s.toByteArray := (rfl)
8080
81- @[simp] theorem String.bytes_empty : "" .bytes = ByteArray.empty := (rfl)
81+ @[simp] theorem String.toByteArray_empty : "" .toByteArray = ByteArray.empty := (rfl)
8282
8383/--
8484Appends two strings. Usually accessed via the `++` operator.
@@ -92,33 +92,33 @@ Examples:
9292 -/
9393@[extern "lean_string_append", expose]
9494def String.append (s : String) (t : @& String) : String where
95- bytes := s.bytes ++ t.bytes
95+ toByteArray := s.toByteArray ++ t.toByteArray
9696 isValidUTF8 := s.isValidUTF8.append t.isValidUTF8
9797
9898instance : Append String where
9999 append s t := s.append t
100100
101101@[simp]
102- theorem String.bytes_append {s t : String} : (s ++ t).bytes = s.bytes ++ t.bytes := (rfl)
102+ theorem String.toByteArray_append {s t : String} : (s ++ t).toByteArray = s.toByteArray ++ t.toByteArray := (rfl)
103103
104- theorem String.bytes_inj {s t : String} : s.bytes = t.bytes ↔ s = t := by
104+ theorem String.toByteArray_inj {s t : String} : s.toByteArray = t.toByteArray ↔ s = t := by
105105 refine ⟨fun h => ?_, (· ▸ rfl)⟩
106106 rcases s with ⟨s⟩
107107 rcases t with ⟨t⟩
108108 subst h
109109 rfl
110110
111- @[simp] theorem String.bytes_ofList {l : List Char} : (String.ofList l).bytes = l.utf8Encode := by
111+ @[simp] theorem String.toByteArray_ofList {l : List Char} : (String.ofList l).toByteArray = l.utf8Encode := by
112112 simp [String.ofList]
113113
114- @[deprecated String.bytes_ofList (since := "2025-10-30")]
115- theorem List.bytes_asString {l : List Char} : (String.ofList l).bytes = l.utf8Encode :=
116- String.bytes_ofList
114+ @[deprecated String.toByteArray_ofList (since := "2025-10-30")]
115+ theorem List.toByteArray_asString {l : List Char} : (String.ofList l).toByteArray = l.utf8Encode :=
116+ String.toByteArray_ofList
117117
118118theorem String.exists_eq_ofList (s : String) :
119119 ∃ l : List Char, s = String.ofList l := by
120120 rcases s with ⟨_, ⟨l, rfl⟩⟩
121- refine ⟨l, by simp [← String.bytes_inj ]⟩
121+ refine ⟨l, by simp [← String.toByteArray_inj ]⟩
122122
123123@[deprecated String.exists_eq_ofList (since := "2025-10-30")]
124124theorem String.exists_eq_asString (s : String) :
@@ -134,10 +134,10 @@ theorem String.utf8ByteSize_append {s t : String} :
134134 simp [utf8ByteSize]
135135
136136@[simp]
137- theorem String.size_bytes {s : String} : s.bytes .size = s.utf8ByteSize := rfl
137+ theorem String.size_toByteArray {s : String} : s.toByteArray .size = s.utf8ByteSize := rfl
138138
139139@[simp]
140- theorem String.bytes_push {s : String} {c : Char} : (s.push c).bytes = s.bytes ++ [c].utf8Encode := by
140+ theorem String.toByteArray_push {s : String} {c : Char} : (s.push c).toByteArray = s.toByteArray ++ [c].utf8Encode := by
141141 simp [push]
142142
143143namespace String
@@ -160,32 +160,32 @@ theorem utf8ByteSize_ofByteArray {b : ByteArray} {h} :
160160 (String.ofByteArray b h).utf8ByteSize = b.size := rfl
161161
162162@[simp]
163- theorem bytes_singleton {c : Char} : (String.singleton c).bytes = [c].utf8Encode := by
163+ theorem toByteArray_singleton {c : Char} : (String.singleton c).toByteArray = [c].utf8Encode := by
164164 simp [singleton]
165165
166166theorem singleton_eq_ofList {c : Char} : String.singleton c = String.ofList [c] := by
167- simp [← String.bytes_inj ]
167+ simp [← String.toByteArray_inj ]
168168
169169@[deprecated singleton_eq_ofList (since := "2025-10-30")]
170170theorem singleton_eq_asString {c : Char} : String.singleton c = String.ofList [c] :=
171171 singleton_eq_ofList
172172
173173@[simp]
174174theorem append_singleton {s : String} {c : Char} : s ++ singleton c = s.push c := by
175- simp [← bytes_inj ]
175+ simp [← toByteArray_inj ]
176176
177177@[simp]
178178theorem append_left_inj {s₁ s₂ : String} (t : String) :
179179 s₁ ++ t = s₂ ++ t ↔ s₁ = s₂ := by
180- simp [← bytes_inj ]
180+ simp [← toByteArray_inj ]
181181
182182theorem append_assoc {s₁ s₂ s₃ : String} : s₁ ++ s₂ ++ s₃ = s₁ ++ (s₂ ++ s₃) := by
183- simp [← bytes_inj , ByteArray.append_assoc]
183+ simp [← toByteArray_inj , ByteArray.append_assoc]
184184
185185@[simp]
186186theorem utf8ByteSize_eq_zero_iff {s : String} : s.utf8ByteSize = 0 ↔ s = "" := by
187187 refine ⟨fun h => ?_, fun h => h ▸ utf8ByteSize_empty⟩
188- simpa [← bytes_inj , ← ByteArray.size_eq_zero_iff] using h
188+ simpa [← toByteArray_inj , ← ByteArray.size_eq_zero_iff] using h
189189
190190theorem rawEndPos_eq_zero_iff {b : String} : b.rawEndPos = 0 ↔ b = "" := by
191191 simp
@@ -296,14 +296,14 @@ Examples:
296296 -/
297297structure Pos.Raw.IsValid (s : String) (off : String.Pos.Raw) : Prop where private mk ::
298298 le_rawEndPos : off ≤ s.rawEndPos
299- isValidUTF8_extract_zero : (s.bytes .extract 0 off.byteIdx).IsValidUTF8
299+ isValidUTF8_extract_zero : (s.toByteArray .extract 0 off.byteIdx).IsValidUTF8
300300
301301theorem Pos.Raw.IsValid.le_utf8ByteSize {s : String} {off : String.Pos.Raw} (h : off.IsValid s) :
302302 off.byteIdx ≤ s.utf8ByteSize := by
303303 simpa [Pos.Raw.le_iff] using h.le_rawEndPos
304304
305305theorem Pos.Raw.isValid_iff_isValidUTF8_extract_zero {s : String} {p : Pos.Raw} :
306- p.IsValid s ↔ p ≤ s.rawEndPos ∧ (s.bytes .extract 0 p.byteIdx).IsValidUTF8 :=
306+ p.IsValid s ↔ p ≤ s.rawEndPos ∧ (s.toByteArray .extract 0 p.byteIdx).IsValidUTF8 :=
307307 ⟨fun ⟨h₁, h₂⟩ => ⟨h₁, h₂⟩, fun ⟨h₁, h₂⟩ => ⟨h₁, h₂⟩⟩
308308
309309@[deprecated le_rawEndPos (since := "2025-10-20")]
@@ -319,7 +319,7 @@ theorem Pos.Raw.isValid_zero {s : String} : (0 : Pos.Raw).IsValid s where
319319@[simp]
320320theorem Pos.Raw.isValid_rawEndPos {s : String} : s.rawEndPos.IsValid s where
321321 le_rawEndPos := by simp
322- isValidUTF8_extract_zero := by simp [← size_bytes , s.isValidUTF8]
322+ isValidUTF8_extract_zero := by simp [← size_toByteArray , s.isValidUTF8]
323323
324324theorem Pos.Raw.isValid_of_eq_rawEndPos {s : String} {p : Pos.Raw} (h : p = s.rawEndPos) :
325325 p.IsValid s := by
@@ -650,4 +650,8 @@ abbrev startValidPos (s : String) : s.Pos :=
650650abbrev endValidPos (s : String) : s.Pos :=
651651 s.endPos
652652
653+ @[deprecated String.toByteArray (since := "2025-11-24")]
654+ abbrev String.bytes (s : String) : ByteArray :=
655+ s.toByteArray
656+
653657end String
0 commit comments