Skip to content

Commit 8651b1d

Browse files
author
Jesse Bouwman
committed
char
1 parent 1415187 commit 8651b1d

File tree

3 files changed

+142
-154
lines changed

3 files changed

+142
-154
lines changed

coalton.asd

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
(:file "lisparray")
6969
(:coalton-file "list")
7070
(:file "vector")
71-
(:file "char")
71+
(:coalton-file "char")
7272
(:coalton-file "string")
7373
(:file "slice")
7474
(:file "hashtable")

library/char.coal

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
(package coalton-library/char
2+
(import
3+
coalton-library/classes
4+
coalton-library/builtin
5+
coalton-library/functions
6+
(coalton-library/iterator as iter))
7+
(import-from coalton-library/hash Hash)
8+
(export
9+
char-code
10+
char-code-unchecked
11+
code-char
12+
alpha?
13+
ascii-alpha?
14+
digit?
15+
ascii-digit?
16+
ascii-alphanumeric?
17+
uppercase?
18+
ascii-uppercase?
19+
lowercase?
20+
ascii-lowercase?
21+
upcase
22+
downcase
23+
range))
24+
25+
(declare char-code (Char -> UFix))
26+
(define (char-code char)
27+
"Convert a character to its ASCII representation."
28+
(lisp UFix (char)
29+
(cl:char-code char)))
30+
31+
(declare code-char-unchecked (UFix -> Char))
32+
(define (code-char-unchecked code)
33+
"Convert a number to its ASCII character. This function is partial."
34+
(lisp Char (code)
35+
(cl:code-char code)))
36+
37+
(declare code-char (UFix -> (Optional Char)))
38+
(define (code-char code)
39+
"Convert a number to its ASCII character, returning None on failure."
40+
(lisp (Optional Char) (code)
41+
;; not sufficient to compare against `cl:char-code-limit', because the char-code space may be sparse.
42+
(alexandria:if-let (char (cl:code-char code))
43+
(Some char)
44+
None)))
45+
46+
(define-instance (Eq Char)
47+
(define (== x y)
48+
(lisp Boolean (x y) (to-boolean (cl:char= x y)))))
49+
50+
(define-instance (Ord Char)
51+
(define (<=> x y)
52+
(if (== x y)
53+
EQ
54+
(if (lisp Boolean (x y) (to-boolean (cl:char> x y)))
55+
GT
56+
LT))))
57+
58+
(declare alpha? (Char -> Boolean))
59+
(define (alpha? c)
60+
"Is C an alphabetic character?"
61+
(lisp Boolean (c)
62+
(cl:alpha-char-p c)))
63+
64+
(declare ascii-alpha? (Char -> Boolean))
65+
(define (ascii-alpha? c)
66+
"Is C an ASCII alphabetic character?"
67+
(lisp Boolean (c)
68+
(cl:or
69+
(cl:<= 65 (cl:char-code c) 90)
70+
(cl:<= 97 (cl:char-code c) 122))))
71+
72+
(declare digit? (Char -> Boolean))
73+
(define (digit? c)
74+
"Is C a digit character?"
75+
(lisp Boolean (c)
76+
(to-boolean (cl:digit-char-p c))))
77+
78+
(declare ascii-digit? (Char -> Boolean))
79+
(define (ascii-digit? c)
80+
"Is C an ASCII digit character?"
81+
(lisp Boolean (c)
82+
(cl:<= 48 (cl:char-code c) 57)))
83+
84+
(declare ascii-alphanumeric? (Char -> Boolean))
85+
(define (ascii-alphanumeric? c)
86+
"Is C an ASCII alphanumeric character?"
87+
(or (ascii-alpha? c)
88+
(ascii-digit? c)))
89+
90+
(declare uppercase? (Char -> Boolean))
91+
(define (uppercase? c)
92+
"Is C an uppercase character?"
93+
(lisp Boolean (c)
94+
(cl:upper-case-p c)))
95+
96+
(declare ascii-uppercase? (Char -> Boolean))
97+
(define (ascii-uppercase? c)
98+
"Is C an ASCII uppercase character?"
99+
(lisp Boolean (c)
100+
(cl:or
101+
(cl:<= 65 (cl:char-code c) 90))))
102+
103+
(declare lowercase? (Char -> Boolean))
104+
(define (lowercase? c)
105+
"Is C a lowercase character?"
106+
(lisp Boolean (c)
107+
(cl:lower-case-p c)))
108+
109+
(declare ascii-lowercase? (Char -> Boolean))
110+
(define (ascii-lowercase? c)
111+
"Is C an ASCII lowercase character?"
112+
(lisp Boolean (c)
113+
(cl:or
114+
(cl:<= 97 (cl:char-code c) 122))))
115+
116+
(declare upcase (Char -> Char))
117+
(define (upcase c)
118+
"Returns the upcased version of C, returning C when there is none."
119+
(lisp Char (c)
120+
(cl:char-upcase c)))
121+
122+
(declare downcase (Char -> Char))
123+
(define (downcase c)
124+
"Returns the downcased version of C, returning C when there is none."
125+
(lisp Char (c)
126+
(cl:char-downcase c)))
127+
128+
(declare range (Char -> Char -> iter:Iterator Char))
129+
(define (range start end)
130+
"An inclusive range of characters from START to END by cl:char-code."
131+
(iter:filter-map!
132+
code-char
133+
(iter:range-increasing
134+
1
135+
(char-code start)
136+
(+ 1 (char-code end)))))
137+
138+
(define-instance (Hash Char)
139+
(define (hash c)
140+
(lisp Hash (c)
141+
(cl:sxhash c))))

library/char.lisp

-153
This file was deleted.

0 commit comments

Comments
 (0)