123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132(*
* BatChar - Additional character operations
* Copyright (C) 1996 Xavier Leroy
* 2008 David Teller
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)includeChar##V<4.3##letlowercase_ascii=function##V<4.3##|('A'..'Z')asc->unsafe_chr(codec-code'A'+code'a')##V<4.3##|c->c##V<4.3####V<4.3##letuppercase_ascii=function##V<4.3##|('a'..'z')asc->unsafe_chr(codec-code'a'+code'A')##V<4.3##|c->c(*$T lowercase_ascii
lowercase_ascii 'A' = 'a'
lowercase_ascii 'À' = 'À'
*)(*$T uppercase_ascii
uppercase_ascii 'a' = 'A'
uppercase_ascii 'à' = 'à'
*)letis_whitespace=function|' '|'\010'|'\013'|'\009'|'\026'|'\012'->true|_->falseletis_newline=function|'\010'|'\013'->true|_->falseletis_digit=function|'0'..'9'->true|_->falseletis_uppercase c='A'<=c&&c<='Z'letis_lowercasec='a'<=c&&c<='z'letis_uppercase_latin1c=is_uppercasec||('\192'(*À*)<=c&&c<='\214'(*Ö*))||('\216'(*Ø*)<=c&&c<='\221'(*Ý*))letis_lowercase_latin1c=is_lowercasec||('\222'(*Þ*)<=c&&c<='\246'(*ö*))||('\248'(*ø*)<=c&&c<='\255'(*'ÿ'*))letis_latin1c=is_uppercase_latin1c||is_lowercase_latin1cletis_symbol=function|'!'|'%'|'&'|'$'|'#'|'+'|'-'|'/'|':'|'<'|'='|'>'|'?'|'@'|'\\'|'~'|'^'|'|'|'*'->true|_->falseletis_letterc=is_uppercasec||is_lowercase cexternalunsafe_int:char->int="%identity"externalunsafe_chr:int->char="%identity"letof_digiti=ifi>=0&&i<10thenChar.unsafe_chr(i+Char.code'0')elseinvalid_arg"Char.of_digit"(*$T of_digit
of_digit 6 = '6'
try ignore (of_digit (-2)); false with Invalid_argument _ -> true
try ignore (of_digit (46)); false with Invalid_argument _ -> true
*)letenum()=BatEnum.mapunsafe_chr(BatEnum.(--)0255)(*$T enum
let e = enum ()in for i = 0 to 255 do assert (Char.chr i = BatEnum.get_exn e) done; BatEnum.is_empty e
*)let(--)fromlast=BatEnum.mapunsafe_chr(BatEnum.(--)(unsafe_intfrom)(unsafe_intlast))(*$T (--)
let e = Char.chr 12 -- Char.chr 52 in for i = 12 to 52 do assert (Char.chr i = BatEnum.get_exn e) done; BatEnum.is_empty e
*)letrange?until:(last=unsafe_chr255)from=from--lastmoduleInfix=structlet(--)=(--)endletprintoutt=BatInnerIO.writeoutt(*$T printBatIO.to_string print 'a' ="a"
BatIO.to_string print '\n' = "\n"
*)letord(x:char)y=ifx>ythenBatOrd.Gtelseify>xthen BatOrd.LtelseBatOrd.Eqletequal(x:char)y=x==y(* safe because int-like value *)lethash=codemoduleIncubator=structmoduleComp=structtypet=charletcompare=compareendmoduleOrd=BatOrd.Ord(Comp)moduleEq=BatOrd.EqComp(Comp)end