unit ctype;

{
This code is based on John Millaway's ctype.h implementation in PHP as part of
porting TIdBiT (https://tidbit99.com/) to Pascal/Lazarus.

Copyright (c) 2023, Stefan Bauch

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
}                                                           

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;

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;


implementation

const
  ctype__ : array[Char] of Integer = (
    32,32,32,32,32,32,32,32,32,40,40,40,40,40,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
    160,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,4,4,4,4,4,4,4,4,4,4,16,16,16,16,16,16,
    16,65,65,65,65,65,65,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,16,16,16,16,16,
    16,66,66,66,66,66,66,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,16,16,16,16,32,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);


function isalnum(c : char):Boolean;
//    private function isalnum ($c){ return ((($this->ctype__[( ord($c) )]&(01 | 02 | 04 )) != 0)?1:0);}
begin
  isalnum := (ctype__[c] AND 7) > 0
end;

function isalpha(c : char):Boolean;
//    private function isalpha ($c){ return ((($this->ctype__[( ord($c) )]&(01 | 02 )) != 0)?1:0);}
begin
  isalpha := (ctype__[c] AND 3) > 0
end;

function isascii(c : char):Boolean;
//    private function isascii ($c){ return (((( ord($c) )<=0177) != 0)?1:0);}
begin
  isascii := (ord(c)<128)
end;

function iscntrl(c : char):Boolean;
//    private function iscntrl ($c){ return ((($this->ctype__[( ord($c) )]& 040 ) != 0)?1:0);}
begin
  iscntrl:= (ctype__[c] AND 8) > 0
end;

function isdigit(c : char):Boolean;
//    private function isdigit ($c){ return ((($this->ctype__[( ord($c) )]& 04 ) != 0)?1:0);}
begin
  isdigit:= (ctype__[c] AND 4) > 0
end;

function isgraph(c : char):Boolean;
//    private function isgraph ($c){ return ((($this->ctype__[( ord($c) )]&(020 | 01 | 02 | 04 )) != 0)?1:0);}
begin
  isgraph:= (ctype__[c] AND 23) > 0
end;

function islower(c : char):Boolean;
//    private function islower ($c){ return ((($this->ctype__[( ord($c) )]& 02 ) != 0)?1:0);}
begin
  islower:= (ctype__[c] AND 2) > 0
end;

function isprint(c : char):Boolean;
//    private function isprint ($c){ return ((($this->ctype__[( ord($c) )]&(020 | 01 | 02 | 04 | 0200 )) != 0)?1:0);}
begin
  isprint:= (ctype__[c] AND 151) > 0
end;

function ispunct(c : char):Boolean;
//    private function ispunct ($c){ return ((($this->ctype__[( ord($c) )]& 020 ) != 0)?1:0);}
begin
  ispunct:= (ctype__[c] AND 16) > 0
end;

function isspace(c : char):Boolean;
//    private function isspace ($c){ return ((($this->ctype__[( ord($c) )]& 010 ) != 0)?1:0);}
begin
  isspace := (ctype__[c] AND 32) > 0
end;

function isupper(c : char):Boolean;
//    private function isupper ($c){ return ((($this->ctype__[( ord($c) )]& 01 ) != 0)?1:0);}
begin
  isupper := (ctype__[c] AND 1) > 0
end;

function isxdigit(c : char):Boolean;
//    private function isxdigit ($c){ return ((($this->ctype__[( ord($c) )]&(0100 | 04 )) != 0)?1:0);}
begin
  isxdigit := (ctype__[c] AND 68) > 0
end;

//    /* Functions: Converted from <ctype.h>.
//     * Author: John Millaway
//     *
//     * Note: These functions expect a character,
//     * such as 'a', or '?', not an integer.
//     * If you want to use integers, first convert
//     * the integer using the chr() function.
//     *
//     * Examples:
//     *
//     * isalpha('a'); // returns true
//     * isalpha(chr(97)); // same thing
//     *
//     * isdigit(1); // NO!
//     * isdigit('1'); // yes.
//     */
//    private function isalnum ($c){ return ((($this->ctype__[( ord($c) )]&(01 | 02 | 04 )) != 0)?1:0);}
//    private function isalpha ($c){ return ((($this->ctype__[( ord($c) )]&(01 | 02 )) != 0)?1:0);}
//    private function isascii ($c){ return (((( ord($c) )<=0177) != 0)?1:0);}
//    private function iscntrl ($c){ return ((($this->ctype__[( ord($c) )]& 040 ) != 0)?1:0);}
//    private function isdigit ($c){ return ((($this->ctype__[( ord($c) )]& 04 ) != 0)?1:0);}
//    private function isgraph ($c){ return ((($this->ctype__[( ord($c) )]&(020 | 01 | 02 | 04 )) != 0)?1:0);}
//    private function islower ($c){ return ((($this->ctype__[( ord($c) )]& 02 ) != 0)?1:0);}
//    private function isprint ($c){ return ((($this->ctype__[( ord($c) )]&(020 | 01 | 02 | 04 | 0200 )) != 0)?1:0);}
//    private function ispunct ($c){ return ((($this->ctype__[( ord($c) )]& 020 ) != 0)?1:0);}
//    private function isspace ($c){ return ((($this->ctype__[( ord($c) )]& 010 ) != 0)?1:0);}
//    private function isupper ($c){ return ((($this->ctype__[( ord($c) )]& 01 ) != 0)?1:0);}
//    private function isxdigit ($c){ return ((($this->ctype__[( ord($c) )]&(0100 | 04 )) != 0)?1:0);}
//    private $ctype__ = array(
//    32,32,32,32,32,32,32,32,32,40,40,40,40,40,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
//    -120,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,4,4,4,4,4,4,4,4,4,4,16,16,16,16,16,16,
//    16,65,65,65,65,65,65,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,16,16,16,16,16,
//    16,66,66,66,66,66,66,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,16,16,16,16,32,
//    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
//    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
//    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
//    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
//}

end.

