Debian Codegraph @

texmf-dist/tex/plain/ifis-macros/ifisinteger.tex

raw
1 % This file is part of the CTAN package named ifis-macros.
2 %
3 % ifisinteger.tex: provide macro \ifisint to check if a given
4 % input is a valid number for \TeX.
5 % Usage: \ifisint <argument>\Boolend <true branch>\else <false branch>\fi
6 %
7 % Copyright (C) 2024,2025 Udo Wermuth (author)
8 %
9 % This program is free software: you can redistribute it and/or modify
10 % it under the terms of the GNU General Public License as published by
11 % the Free Software Foundation, either version 3 of the License, or
12 % (at your option) any later version.
13 %
14 % This program is distributed in the hope that it will be useful,
15 % but WITHOUT ANY WARRANTY; without even the implied warranty of
16 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 % GNU General Public License for more details.
18 %
19 % You should have received a copy of the GNU General Public License
20 % along with this program. If not, see <http://www.gnu.org/licenses/>.
21 %
22 \catcode`\@=11 % use the private prefix ``II@''
23 \newif\ifII@itis % main result of the macro
24 %% helper macros
25 \def\II@rmsign #1{\ifx#1+\else\ifx#1-\else
26 \II@endrm#1\fi\fi\II@rmsign}
27 \def\II@endrm #1\fi\fi#2{\fi\fi#1}
28 \def\II@zeros #1{\ifx#1''\else\ifx#1""\else
29 \II@cont#1\fi\fi\II@zeros}
30 \def\II@cont #1\fi\fi#2{\fi\fi\II@hdlzero#1}
31 \def\II@hdlzero #1{\ifx#10 \else
32 \II@xchgfi #1\fi\II@hdlzero}
33 \def\II@xchgfi #1\fi#2{\fi\ifx#1`\else0\fi#1}
34 %% constants with the sentinel `W'
35 \def\II@cfd{02147483647W}% canonical forms with
36 \def\II@cfh{"07FFFFFFFW}% W of TeX's max integer
37 \def\II@cfo{'017777777777W}% in dec, hex, oct
38 \def\II@W{W}\def\II@hexW{"W}% all unsigned input
39 \def\II@octW{'W}% with W for which TeX inserts 0
40 %% assignments
41 \let\Boolend=\iffalse \font\II@font=cmr10
42 \let\IIcurrentmode=\errorstopmode % CONFIGURE
43 %% main macro
44 \def\ifisint #1\Boolend{\II@itisfalse % see S1.2
45 \edef\II@digs{\II@rmsign#1W}% S1.1 with 2 \edef
46 \edef\II@digs{\expandafter\II@rmsign\II@digs}%
47 \ifx\II@digs\II@W\else\ifx\II@digs\II@octW
48 \else\ifx\II@digs\II@hexW\else % S1.2 finished
49 \edef\II@cf{\expandafter\II@zeros\II@digs}% S2
50 \wlog{=== start ignore}\batchmode\begingroup
51 \setbox0=\hbox{\count255=\II@cf
52 \xdef\II@val{\the\count255}}%
53 \setbox0=\hbox{\II@font\count255=#1W}% S3.1
54 \xdef\II@wd{\the\wd0}%
55 \endgroup\IIcurrentmode\wlog{=== stop ignore}%
56 \ifdim\II@wd=10.2778pt % \wd of hbox `W'; S3.2
57 \II@itistrue \ifnum\II@val=2147483647 % S4.1
58 \ifx\II@cf\II@cfd
59 \else\ifx\II@cf\II@cfh
60 \else\ifx\II@cf\II@cfo % S4.2
61 \else \II@itisfalse % S4.3
62 \fi\fi\fi\fi
63 \else \II@itisfalse % S3.3
64 \fi\fi\fi\fi \ifII@itis}
65 \catcode`\@=12