1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 1985-2012, University of Amsterdam 7 VU University Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(ctypes, 37 [ is_alnum/1, 38 is_alpha/1, 39 is_ascii/1, 40 is_cntrl/1, 41 is_csym/1, 42 is_csymf/1, 43 is_digit/1, 44 is_digit/3, 45 is_endfile/1, 46 is_endline/1, 47 is_graph/1, 48 is_lower/1, 49 is_newline/1, 50 is_newpage/1, 51 is_paren/2, 52 is_period/1, 53 is_print/1, 54 is_punct/1, 55 is_quote/1, 56 is_space/1, 57 is_upper/1, 58 is_white/1, 59 to_lower/2, 60 to_upper/2, 61 upper_lower/2 62 ]). 63 64/** <module> Character code classification 65 66This file implements the functionality of the corresponding Quintus 67library based on SWI-Prolog's code_type/2 predicate. Please check the 68documentation of this predicate to find the definitions of the classes. 69 70@see code_type/2 71@see char_type/2 72*/ 73 74is_alnum(C) :- code_type(C, alnum). 75is_alpha(C) :- code_type(C, alpha). 76is_ascii(C) :- code_type(C, ascii). 77is_cntrl(C) :- code_type(C, cntrl). 78is_csym(C) :- code_type(C, csym). 79is_csymf(C) :- code_type(C, csymf). 80is_digit(C) :- code_type(C, digit). 81is_graph(C) :- code_type(C, graph). 82is_lower(C) :- code_type(C, lower). 83is_upper(C) :- code_type(C, upper). 84is_period(C) :- code_type(C, period). 85is_endline(C) :- code_type(C, end_of_line). 86is_print(C) :- is_graph(C). 87is_punct(C) :- code_type(C, punct). 88is_quote(C) :- code_type(C, quote). 89is_space(C) :- code_type(C, space). 90is_white(C) :- code_type(C, white). 91 92is_endfile(-1). 93is_newpage(12). % Control-L 94is_newline(10). 95 96%! is_paren(?Open, ?Close) is semidet. 97% 98% True if Open is the open-parenthesis of Close. 99 100is_paren(0'(, 0')). % Prolog is too good at this 101is_paren(0'[, 0']). 102is_paren(0'{, 0'}). 103 104%! to_lower(+U, -L) is det. 105% 106% Downcase a character code. If U is the character code of an 107% uppercase character, unify L with the character code of the 108% lowercase version. Else unify L with U. 109 110to_lower(U, L) :- 111 code_type(L, to_lower(U)). 112 113%! to_upper(+L, -U) is det. 114% 115% Upcase a character code. If L is the character code of a 116% lowercase character, unify L with the character code of the 117% uppercase version. Else unify U with L. 118 119to_upper(L, U) :- 120 code_type(U, to_upper(L)). 121 122%! upper_lower(?U, ?L) is det. 123% 124% True when U is the character code of an uppercase character and 125% L is the character code of the corresponding lowercase 126% character. 127 128upper_lower(Upper, Lower) :- 129 nonvar(Upper), 130 !, 131 code_type(Lower, lower(Upper)). 132upper_lower(Upper, Lower) :- 133 code_type(Upper, upper(Lower)). 134 135 136%! is_digit(+C, +Base, -Weight) is det. 137%! is_digit(-C, +Base, +Weight) is det. 138% 139% Succeeds if `C' is a digit using `Base' as base and `Weight' 140% represents its value. Only the base-10 case is handled by code_type. 141 142is_digit(C, Base, Weight) :- 143 Base == 10, 144 !, 145 code_type(C, digit(Weight)). 146is_digit(C, Base, Weight) :- 147 between(2, 36, Base), 148 succ(X, Base), 149 between(0, X, Weight), 150 is_digit(C, Weight). 151 152is_digit(C, Weight) :- 153 Weight < 10, 154 !, 155 plus(Weight, 0'0, C). 156is_digit(C, Weight) :- 157 plus(Weight, 87, C), 158 !. /* `a`-10 */ 159is_digit(C, Weight) :- 160 plus(Weight, 55, C). /* `A`-10 */