-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsciiStringPrograms.lean
More file actions
74 lines (59 loc) · 2.26 KB
/
Copy pathAsciiStringPrograms.lean
File metadata and controls
74 lines (59 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import LeanExe.AsciiString
namespace LeanExe
namespace Examples.AsciiStringPrograms
def validAscii (input : ByteArray) : Bool :=
AsciiString.isAscii input
def checkedSize (input : ByteArray) : Nat :=
match AsciiString.ofByteArray? input with
| some text => text.size
| none => 0
def firstOrQuestion (input : ByteArray) : UInt64 :=
match AsciiString.ofByteArray? input with
| some text => text.getD 0 (63 : UInt8) |>.toUInt64
| none => 63
def identityTrusted (text : AsciiString) : AsciiString :=
text
def appendBangOrQuestion (input : ByteArray) : ByteArray :=
match AsciiString.ofByteArray? input with
| some text =>
AsciiString.toByteArray
(AsciiString.pushTrustedByte text (33 : UInt8))
| none =>
AsciiString.toByteArray (AsciiString.singletonTrusted (63 : UInt8))
def pushIfAscii (input : ByteArray) (byte : UInt64) : ByteArray :=
match AsciiString.ofByteArray? input with
| some text =>
match text.pushByte? byte.toUInt8 with
| some output => output.toByteArray
| none => ByteArray.empty
| none => ByteArray.empty
def appendSelfTrusted (input : ByteArray) : ByteArray :=
let text := AsciiString.ofTrustedByteArray input
AsciiString.toByteArray (AsciiString.append text text)
def prefixBangTrusted (input : ByteArray) : ByteArray :=
let text := AsciiString.ofTrustedByteArray input
AsciiString.toByteArray
(AsciiString.append
(AsciiString.singletonTrusted (33 : UInt8))
text)
def middle (input : ByteArray) : ByteArray :=
match AsciiString.ofByteArray? input with
| some text => AsciiString.toByteArray (text.extract 1 3)
| none => ByteArray.empty
def equalsABC (input : ByteArray) : Bool :=
match AsciiString.ofByteArray? input with
| some text => text.equals (AsciiString.ofTrustedByteArray "abc".toUTF8)
| none => false
def startsWithAB (input : ByteArray) : Bool :=
match AsciiString.ofByteArray? input with
| some text => text.startsWith (AsciiString.ofTrustedByteArray "ab".toUTF8)
| none => false
def containsColon (input : ByteArray) : Bool :=
match AsciiString.ofByteArray? input with
| some text => text.containsByte (58 : UInt8)
| none => false
def trustedStringLength : Nat :=
let text : String := "json"
text.length
end Examples.AsciiStringPrograms
end LeanExe