forked from coalton-lang/coalton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
char.lisp
153 lines (131 loc) · 4.02 KB
/
char.lisp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
(coalton-library/utils:defstdlib-package #:coalton-library/char
(:use
#:coalton
#:coalton-library/classes
#:coalton-library/builtin
#:coalton-library/functions)
(:import-from
#:coalton-library/hash
#:define-sxhash-hasher)
(:local-nicknames
(#:iter #:coalton-library/iterator))
(:export
#:char-code
#:char-code-unchecked
#:code-char
#:alpha?
#:ascii-alpha?
#:digit?
#:ascii-digit?
#:ascii-alphanumeric?
#:uppercase?
#:ascii-uppercase?
#:lowercase?
#:ascii-lowercase?
#:upcase
#:downcase
#:range))
(in-package #:coalton-library/char)
(named-readtables:in-readtable coalton:coalton)
#+coalton-release
(cl:declaim #.coalton-impl/settings:*coalton-optimize-library*)
(coalton-toplevel
(declare char-code (Char -> UFix))
(define (char-code char)
"Convert a character to its ASCII representation."
(lisp UFix (char)
(cl:char-code char)))
(declare code-char-unchecked (UFix -> Char))
(define (code-char-unchecked code)
"Convert a number to its ASCII character. This function is partial."
(lisp Char (code)
(cl:code-char code)))
(declare code-char (UFix -> (Optional Char)))
(define (code-char code)
"Convert a number to its ASCII character, returning None on failure."
(lisp (Optional Char) (code)
;; not sufficient to compare against `cl:char-code-limit', because the char-code space may be sparse.
(alexandria:if-let (char (cl:code-char code))
(Some char)
None)))
(define-instance (Eq Char)
(define (== x y)
(lisp Boolean (x y) (to-boolean (cl:char= x y)))))
(define-instance (Ord Char)
(define (<=> x y)
(if (== x y)
EQ
(if (lisp Boolean (x y) (to-boolean (cl:char> x y)))
GT
LT))))
(declare alpha? (Char -> Boolean))
(define (alpha? c)
"Is C an alphabetic character?"
(lisp Boolean (c)
(cl:alpha-char-p c)))
(declare ascii-alpha? (Char -> Boolean))
(define (ascii-alpha? c)
"Is C an ASCII alphabetic character?"
(lisp Boolean (c)
(cl:or
(cl:<= 65 (cl:char-code c) 90)
(cl:<= 97 (cl:char-code c) 122))))
(declare digit? (Char -> Boolean))
(define (digit? c)
"Is C a digit character?"
(lisp Boolean (c)
(to-boolean (cl:digit-char-p c))))
(declare ascii-digit? (Char -> Boolean))
(define (ascii-digit? c)
"Is C an ASCII digit character?"
(lisp Boolean (c)
(cl:<= 48 (cl:char-code c) 57)))
(declare ascii-alphanumeric? (Char -> Boolean))
(define (ascii-alphanumeric? c)
"Is C an ASCII alphanumeric character?"
(or (ascii-alpha? c)
(ascii-digit? c)))
(declare uppercase? (Char -> Boolean))
(define (uppercase? c)
"Is C an uppercase character?"
(lisp Boolean (c)
(cl:upper-case-p c)))
(declare ascii-uppercase? (Char -> Boolean))
(define (ascii-uppercase? c)
"Is C an ASCII uppercase character?"
(lisp Boolean (c)
(cl:or
(cl:<= 65 (cl:char-code c) 90))))
(declare lowercase? (Char -> Boolean))
(define (lowercase? c)
"Is C a lowercase character?"
(lisp Boolean (c)
(cl:lower-case-p c)))
(declare ascii-lowercase? (Char -> Boolean))
(define (ascii-lowercase? c)
"Is C an ASCII lowercase character?"
(lisp Boolean (c)
(cl:or
(cl:<= 97 (cl:char-code c) 122))))
(declare upcase (Char -> Char))
(define (upcase c)
"Returns the upcased version of C, returning C when there is none."
(lisp Char (c)
(cl:char-upcase c)))
(declare downcase (Char -> Char))
(define (downcase c)
"Returns the downcased version of C, returning C when there is none."
(lisp Char (c)
(cl:char-downcase c)))
(declare range (Char -> Char -> iter:Iterator Char))
(define (range start end)
"An inclusive range of characters from START to END by cl:char-code."
(iter:filter-map!
code-char
(iter:range-increasing
1
(char-code start)
(+ 1 (char-code end))))))
(define-sxhash-hasher Char)
#+sb-package-locks
(sb-ext:lock-package "COALTON-LIBRARY/CHAR")