Carp/core/Char.carp

76 lines
1.8 KiB
Plaintext
Raw Normal View History

2018-01-24 18:08:18 +03:00
(system-include "carp_char.h")
2018-01-24 17:53:18 +03:00
2021-07-05 15:48:35 +03:00
(doc Char "is the single character data type.")
2017-06-26 12:15:03 +03:00
(defmodule Char
2017-11-29 17:28:21 +03:00
(register = (Fn [Char Char] Bool))
(register < (Fn [Char Char] Bool))
(register > (Fn [Char Char] Bool))
(register to-int (Fn [Char] Int))
(register from-int (Fn [Int] Char))
(register to-byte (Fn [Char] Byte))
(register from-byte (Fn [Byte] Char))
(register copy (Fn [&Char] Char))
(implements < Char.<)
(implements > Char.>)
(implements = Char.=)
(implements copy Char.copy)
(implements to-int Char.to-int)
(implements from-int Char.from-int)
(doc meaning "converts a numerical char into the appropriate number (e.g. from `\1` to `1`).")
(defn meaning [char-ref]
2019-10-28 19:19:31 +03:00
(let [c @char-ref]
(case c
\0 0
\1 1
\2 2
\3 3
\4 4
\5 5
\6 6
\7 7
\8 8
\9 9
-1)))
(doc lower-case? "tests whether a character is lower case.")
(defn lower-case? [c]
(and (<= \a c) (<= c \z)))
(doc upper-case? "tests whether a character is upper case.")
(defn upper-case? [c]
(and (<= \A c) (<= c \Z)))
(doc alpha? "tests whether a character is alphabetical.")
(defn alpha? [c]
(or (lower-case? c) (upper-case? c)))
(doc num? "tests whether a character is numerical.")
(defn num? [c]
(and (<= \0 c) (<= c \9)))
(doc alphanum? "tests whether a character is alphanumerical.")
(defn alphanum? [c]
(or (alpha? c) (num? c)))
2019-06-14 14:02:03 +03:00
2020-01-29 13:48:31 +03:00
(defn zero [] (the Char (from-int 0)))
(implements zero Char.zero)
)
(defmodule CharRef
(defn = [a b]
(Char.= @a @b))
(implements = CharRef.=)
(defn < [a b]
(Char.< @a @b))
(implements < CharRef.<)
(defn > [a b]
(Char.> @a @b))
(implements > CharRef.>)
)
(defmodule PtrChar
(register str (Fn [(Ptr Char)] String))
(implements str PtrChar.str))