StdChar.icl 2.63 KB
Newer Older
1 2 3
implementation module StdChar

// ****************************************************************************************
4 5
//	Concurrent Clean Standard Library Module Version 3.0
//	Copyright 2019 University of Nijmegen
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
// ****************************************************************************************

import	StdOverloaded, StdBool, StdInt, StdClass

instance + Char
where
	(+) :: !Char !Char -> Char
	(+) a b
		= code inline {
			addI
			ItoC
		}

instance - Char
where
	(-) :: !Char !Char -> Char
	(-) a b
		= code inline {
			subI
			ItoC
		}

instance zero Char
where
	zero :: Char
	zero
		= code inline {
			pushI 0
			ItoC
		}

instance one Char
where
	one :: Char
	one
		= code inline {
			pushI 1
			ItoC
		}

instance ==	Char
where
 (==) ::!Char !Char -> Bool
 (==) a b
	= code inline {
			eqC
	}

instance <	Char
where
 (<) ::!Char !Char -> Bool
 (<) a b
	= code inline {
			ltC
	}

instance toChar Char
where
 toChar::!Char -> Char
 toChar a
	= code inline {
			no_op
	}

instance toChar Int
where
 toChar :: !Int -> Char
 toChar a
	= code inline {
			ItoC
	}

instance fromChar Int
where
 fromChar::!Char -> Int
 fromChar a
	= code inline {
			CtoI
	}


instance fromChar {#Char}
where
 fromChar::!Char -> {#Char}
 fromChar a
	= code {
			CtoAC
	}

instance fromChar Char
where
 fromChar::!Char -> Char
 fromChar a
	= code inline {
			no_op
	}
//	Convert Character into:

toUpper::!Char -> Char
toUpper c
	| IsLower c	= clear_lowercase_bit c
    			= c

toLower::!Char -> Char
toLower c
	| IsUpper c	= set_lowercase_bit c
				= c

digitToInt ::!Char -> Int
digitToInt c = toInt c - 48 // toInt '0'

//	Test on Characters:

IsLower c :== c >= 'a' && c <= 'z'
IsUpper c :== c >= 'A' && c <= 'Z'
IsDigit c :== c >= '0'&& c <= '9'
IsAlpha c :== IsUpper (clear_lowercase_bit c)

clear_lowercase_bit :: !Char -> Char
clear_lowercase_bit c = code inline {
		pushI 223
		and%
	}

set_lowercase_bit :: !Char -> Char
set_lowercase_bit c = code inline {
		pushI 32
		or%
	}

isAscii::!Char -> Bool
isAscii c	=  toInt c < 128

isControl::!Char -> Bool
isControl c	=	c < ' '	||	c == '\177'

isPrint::!Char -> Bool
isPrint c	=	c >= ' ' &&	c <= '~'

isSpace::!Char -> Bool
isSpace c	=	c == ' ' || c == '\t' || c == '\n' || c ==  '\r' || c == '\f' || c == '\v'

isUpper::!Char -> Bool
isUpper c = IsUpper c

isLower::!Char -> Bool
isLower c = IsLower c

isAlpha::!Char -> Bool
isAlpha c = IsAlpha c

isDigit::!Char -> Bool
isDigit c = IsDigit c

isOctDigit::!Char -> Bool
isOctDigit c = c >= '0' &&  c <= '7'

isHexDigit::!Char -> Bool
isHexDigit c
	# uc = clear_lowercase_bit c
	= IsDigit c || (uc>='A' &&  uc<='F')

isAlphanum::!Char -> Bool
isAlphanum c = IsAlpha c || IsDigit c