PASCAL - CTYPEPAS.ZIP - CTYPE.PAS

 
Output of file : CTYPE.PAS contained in archive : CTYPEPAS.ZIP

{******************************************************************************
* UNIT CType -- Various useful character functions for Turbo Pascal 4.0
*
* This unit duplicates the character classification and conversion functions
* and macros available in C.
*------------------------------------------------------------------------------
* I tried writing these subroutines three ways and settled on this as best.
* The other two ways were using sets (c IN ['0'..'9']) and table look-up. I
* found that using sets generates an amazing amount of code and that table
* look-up generated 5% less code but added a 128 byte look-up table. Plus,
* the table look-up was only 4% faster than the method used here.
*------------------------------------------------------------------------------
* Version 1.0, 09 MAR 88, Scott Ainsworth -- Released to Public Domain.
******************************************************************************}

{$B- Short-curcuit boolean evaluation}
{$R- No range checking }
{$S- No stack checking }

UNIT CType;

INTERFACE

{******************************************************************************
*
* Classification Functions (Is...)
* --------------------------------
*
* These functions classify characters.
*
* IsAlnum Test for alphanumeric [0..9 A..Z a..z]
* IsAlpha Test for alphabetic [A..Z a..z]
* IsASCII Test for ASCII [#0..#127]
* IsCntrl Test for control [#0..#31 #127]
* IsDigit Test for decimal digit [0..9]
* IsGraph Test for printable except space [!..~]
* IsLower Test for lower-case [a..z]
* IsPunct Test for punctuation [all graphical which are not alphanumeric]
* IsPrint Test for printable [space..~]
* IsSpace Test for whitespace [#9..#13 space]
* IsUpper Test for upper-case [A..Z]
* IsXDigit Test for hexidecmal digit [0..9 A..F a..f]
*
* Parameters:
* c : CHAR -- The character to be tested.
*
* Return Value:
* BOOLEAN -- TRUE if the character is in the test class, otherwise FALSE.
*
******************************************************************************}

FUNCTION IsAlnum (c:CHAR): BOOLEAN;
FUNCTION IsAlpha (c:CHAR): BOOLEAN;
FUNCTION IsASCII (c:CHAR): BOOLEAN;
FUNCTION IsCntrl (c:CHAR): BOOLEAN;
FUNCTION IsDigit (c:CHAR): BOOLEAN;
FUNCTION IsGraph (c:CHAR): BOOLEAN;
FUNCTION IsLower (c:CHAR): BOOLEAN;
FUNCTION IsPrint (c:CHAR): BOOLEAN;
FUNCTION IsPunct (c:CHAR): BOOLEAN;
FUNCTION IsSpace (c:CHAR): BOOLEAN;
FUNCTION IsUpper (c:CHAR): BOOLEAN;
FUNCTION IsXDigit (c:CHAR): BOOLEAN;


{******************************************************************************
*
* Conversion Functions (To...)
* ----------------------------
*
* These functions convert a single character as specified.
*
* ToASCII Forces a CHAR to ASCII by stripping the high-order bit.
* ToLower Convert a letter to lower-case, if it is not already lower-case.
* ToUpper Convert a letter to upper-case, if it is not already upper-case.
* _ToLower Convert an upper-case letter to lower-case.
* _ToUpper Convert a lower-case letter to upper-case.
*
* Parameters:
* c : CHAR -- The character to be converted.
*
* Return Value:
* CHAR -- The converted character.
*
* Notes:
* '_ToLower' and '_ToUpper' are blind conversions which are only valid on
* upper-case and lower-case letters respectfully. 'ToLower' and
* 'ToUpper' only convert if necessary.
*
******************************************************************************}

FUNCTION ToASCII (c:CHAR): CHAR;
FUNCTION ToLower (c:CHAR): CHAR;
FUNCTION ToUpper (c:CHAR): CHAR;
FUNCTION _ToLower (c:CHAR): CHAR;
FUNCTION _ToUpper (c:CHAR): CHAR;


{******************************************************************************
* Implementation
******************************************************************************}

IMPLEMENTATION


FUNCTION IsAlnum;
BEGIN
IF (c >= 'a') AND (c <= 'z')
OR (c >= 'A') AND (c <= 'Z')
OR (c >= '0') AND (c <= '9') THEN
IsAlnum := TRUE
ELSE
IsAlnum := FALSE;
END;


FUNCTION IsAlpha;
BEGIN
IF (c >= 'a') AND (c <= 'z')
OR (c >= 'A') AND (c <= 'Z') THEN
IsAlpha := TRUE
ELSE
IsAlpha := FALSE;
END;


FUNCTION IsASCII;
BEGIN
IF (Ord (c) <= $7F) THEN
IsASCII := TRUE
ELSE
IsASCII := FALSE;
END;


FUNCTION IsCntrl;
BEGIN
IF (Ord (c) >= $00) AND (Ord (c) <= $1F)
OR (Ord (c) = $7F) THEN
IsCntrl := TRUE
ELSE
IsCntrl := FALSE;
END;


FUNCTION IsDigit;
BEGIN
IF (c >= '0') AND (c <= '9') THEN
IsDigit := TRUE
ELSE
IsDigit := FALSE;
END;


FUNCTION IsGraph;
BEGIN
IF (c >= '!') AND (c <= '~') THEN
IsGraph := TRUE
ELSE
IsGraph := FALSE;
END;


FUNCTION IsLower;
BEGIN
IF (c >= 'a') AND (c <= 'z') THEN
IsLower := TRUE
ELSE
IsLower := FALSE;
END;


FUNCTION IsPrint;
BEGIN
IF (c >= ' ') AND (c <= '~') THEN
IsPrint := TRUE
ELSE
IsPrint := FALSE;
END;


FUNCTION IsPunct;
BEGIN
IF NOT IsAlnum (c) AND NOT IsCntrl (c) AND NOT IsSpace (c) THEN
IsPunct := TRUE
ELSE
IsPunct := FALSE;
END;


FUNCTION IsSpace;
BEGIN
IF (c = ' ') OR (c >= Chr($9)) AND (c <= Chr($D)) THEN
IsSpace := TRUE
ELSE
IsSpace := FALSE;
END;


FUNCTION IsUpper;
BEGIN
IF (c >= 'A') AND (c <= 'Z') THEN
IsUpper := TRUE
ELSE
IsUpper := FALSE;
END;


FUNCTION IsXDigit;
BEGIN
IF (c >= '0') AND (c <= '9')
OR (c >= 'A') AND (c <= 'F')
OR (c >= 'a') AND (c <= 'f') THEN
IsXDigit := TRUE
ELSE
IsXDigit := FALSE;
END;


FUNCTION ToASCII;
BEGIN
ToASCII := Chr (Ord (c) AND $7F);
END;


FUNCTION ToLower;
BEGIN
IF (c >= 'A') AND (c <= 'Z') THEN
ToLower := Chr (Ord (c) + 32)
ELSE
ToLower := c;
END;


FUNCTION ToUpper;
BEGIN
IF (c >= 'a') AND (c <= 'z') THEN
ToUpper := Chr (Ord (c) - 32)
ELSE
ToUpper := c;
END;

FUNCTION _ToLower;
BEGIN
_ToLower := Chr (Ord (c) + 32);
END;


FUNCTION _ToUpper;
BEGIN
_ToUpper := Chr (Ord (c) - 32);
END;


END.