Skip to content

Commit

Permalink
char
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Bouwman committed Jul 23, 2024
1 parent 1415187 commit 8651b1d
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 154 deletions.
2 changes: 1 addition & 1 deletion coalton.asd
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
(:file "lisparray")
(:coalton-file "list")
(:file "vector")
(:file "char")
(:coalton-file "char")
(:coalton-file "string")
(:file "slice")
(:file "hashtable")
Expand Down
141 changes: 141 additions & 0 deletions library/char.coal
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
(package coalton-library/char
(import
coalton-library/classes
coalton-library/builtin
coalton-library/functions
(coalton-library/iterator as iter))
(import-from coalton-library/hash Hash)
(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))

(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-instance (Hash Char)
(define (hash c)
(lisp Hash (c)
(cl:sxhash c))))
153 changes: 0 additions & 153 deletions library/char.lisp

This file was deleted.

0 comments on commit 8651b1d

Please sign in to comment.