From 8651b1dc6c3c67b2c64aebebbbad80d1c332bebd Mon Sep 17 00:00:00 2001 From: Jesse Bouwman Date: Tue, 23 Jul 2024 16:59:01 -0700 Subject: [PATCH] char --- coalton.asd | 2 +- library/char.coal | 141 ++++++++++++++++++++++++++++++++++++++++++ library/char.lisp | 153 ---------------------------------------------- 3 files changed, 142 insertions(+), 154 deletions(-) create mode 100644 library/char.coal delete mode 100644 library/char.lisp diff --git a/coalton.asd b/coalton.asd index 0ea2c7819..bb08aca08 100644 --- a/coalton.asd +++ b/coalton.asd @@ -68,7 +68,7 @@ (:file "lisparray") (:coalton-file "list") (:file "vector") - (:file "char") + (:coalton-file "char") (:coalton-file "string") (:file "slice") (:file "hashtable") diff --git a/library/char.coal b/library/char.coal new file mode 100644 index 000000000..07567694a --- /dev/null +++ b/library/char.coal @@ -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)))) diff --git a/library/char.lisp b/library/char.lisp deleted file mode 100644 index 650f06a8e..000000000 --- a/library/char.lisp +++ /dev/null @@ -1,153 +0,0 @@ -(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")