File: IntConv.Mod

package info (click to toggle)
oo2c32 1.5.0-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 8,748 kB
  • ctags: 5,415
  • sloc: ansic: 95,007; sh: 473; makefile: 344; perl: 57; lisp: 21
file content (234 lines) | stat: -rw-r--r-- 8,022 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
(*	$Id: IntConv.Mod,v 1.3 1999/09/02 13:07:24 acken Exp $	*)
MODULE IntConv;
 
 (*
    IntConv -  Low-level integer/string conversions.       
    Copyright (C) 1995 Michael Griebling
 
    This module 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 of the
    License, or (at your option) any later version.
 
    This module 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 program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*)
 
IMPORT
  Char := CharClass, Str := Strings, Conv := ConvTypes;
 
TYPE
  ConvResults = Conv.ConvResults; (* strAllRight, strOutOfRange, strWrongFormat, strEmpty *)

CONST
  strAllRight*=Conv.strAllRight;       (* the string format is correct for the corresponding conversion *)
  strOutOfRange*=Conv.strOutOfRange;   (* the string is well-formed but the value cannot be represented *)
  strWrongFormat*=Conv.strWrongFormat; (* the string is in the wrong format for the conversion *)
  strEmpty*=Conv.strEmpty;             (* the given string is empty *)


VAR
  W, S, SI: Conv.ScanState;

(* internal state machine procedures *)

PROCEDURE WState(inputCh: CHAR; VAR chClass: Conv.ScanClass; VAR nextState: Conv.ScanState);
BEGIN
  IF Char.IsNumeric(inputCh) THEN chClass:=Conv.valid; nextState:=W
  ELSE chClass:=Conv.terminator; nextState:=NIL
  END
END WState;
  
 
PROCEDURE SState(inputCh: CHAR; VAR chClass: Conv.ScanClass; VAR nextState: Conv.ScanState);
BEGIN
  IF Char.IsNumeric(inputCh) THEN chClass:=Conv.valid; nextState:=W 
  ELSE chClass:=Conv.invalid; nextState:=S
  END
END SState;

  
PROCEDURE ScanInt*(inputCh: CHAR; VAR chClass: Conv.ScanClass; VAR nextState: Conv.ScanState);
 (* 
    Represents the start state of a finite state scanner for signed whole 
    numbers - assigns class of inputCh to chClass and a procedure 
    representing the next state to nextState.
     
    The call of ScanInt(inputCh,chClass,nextState) shall assign values to
    `chClass' and `nextState' depending upon the value of `inputCh' as
    shown in the following table.
    
    Procedure       inputCh         chClass         nextState (a procedure
                                                    with behaviour of)
    ---------       ---------       --------        ---------
    ScanInt         space           padding         ScanInt
                    sign            valid           SState
                    decimal digit   valid           WState
                    other           invalid         ScanInt
    SState          decimal digit   valid           WState
                    other           invalid         SState
    WState          decimal digit   valid           WState
                    other           terminator      --
    
    NOTE 1 -- The procedure `ScanInt' corresponds to the start state of a
    finite state machine to scan for a character sequence that forms a
    signed whole number.  Like `ScanCard' and the corresponding procedures
    in the other low-level string conversion modules, it may be used to
    control the actions of a finite state interpreter.  As long as the
    value of `chClass' is other than `terminator' or `invalid', the
    interpreter should call the procedure whose value is assigned to
    `nextState' by the previous call, supplying the next character from
    the sequence to be scanned.  It may be appropriate for the interpreter
    to ignore characters classified as `invalid', and proceed with the
    scan.  This would be the case, for example, with interactive input, if
    only valid characters are being echoed in order to give interactive
    users an immediate indication of badly-formed data.  
      If the character sequence end before one is classified as a
    terminator, the string-terminator character should be supplied as
    input to the finite state scanner.  If the preceeding character
    sequence formed a complete number, the string-terminator will be
    classified as `terminator', otherwise it will be classified as
    `invalid'. 
    
    For examples of how ScanInt is used, refer to the FormatInt and
    ValueInt procedures below.
  *)
BEGIN
  IF Char.IsWhiteSpace(inputCh) THEN chClass:=Conv.padding; nextState:=SI
  ELSIF (inputCh="+") OR (inputCh="-") THEN chClass:=Conv.valid; nextState:=S
  ELSIF Char.IsNumeric(inputCh) THEN chClass:=Conv.valid; nextState:=W
  ELSE chClass:=Conv.invalid; nextState:=SI
  END
END ScanInt;
 
 
PROCEDURE FormatInt*(str: ARRAY OF CHAR): ConvResults;
 (* Returns the format of the string value for conversion to LONGINT. *)
VAR
  ch: CHAR;
  int: LONGINT;
  len, index, digit: INTEGER;
  state: Conv.ScanState;
  positive: BOOLEAN;
  prev, class: Conv.ScanClass;
BEGIN
  len:=Str.Length(str); index:=0;
  class:=Conv.padding; prev:=class;
  state:=SI; int:=0; positive:=TRUE;
  LOOP
    IF index=len THEN EXIT END;
    ch:=str[index];
    state.p(ch, class, state);
    CASE class OF
    | Conv.padding: (* nothing to do *)
    | Conv.valid:
        IF ch="-" THEN positive:=FALSE
        ELSIF ch="+" THEN positive:=TRUE
        ELSE (* must be a digit *)
          digit:=ORD(ch)-ORD("0");
          IF positive THEN
            IF int>(MAX(LONGINT)-digit) DIV 10 THEN RETURN strOutOfRange END;
            int:=int*10+digit
          ELSE
            IF int<(MIN(LONGINT)+digit) DIV 10 THEN RETURN strOutOfRange END;
            int:=int*10-digit
          END
        END
    | Conv.invalid, Conv.terminator: EXIT
    END;
    prev:=class; INC(index)
  END;
  IF class IN {Conv.invalid, Conv.terminator} THEN RETURN strWrongFormat
  ELSIF prev=Conv.padding THEN RETURN strEmpty
  ELSE RETURN strAllRight
  END
END FormatInt;
 
 
PROCEDURE ValueInt*(str: ARRAY OF CHAR): LONGINT;
  (* 
     Returns the value corresponding to the signed whole number string value 
     str if str is well-formed; otherwise raises the WholeConv exception.
  *)
VAR
  ch: CHAR;
  len, index, digit: INTEGER;
  int: LONGINT;  
  state: Conv.ScanState;
  positive: BOOLEAN;
  class: Conv.ScanClass;  
BEGIN
  IF FormatInt(str)=strAllRight THEN
    len:=Str.Length(str); index:=0;
    state:=SI; int:=0; positive:=TRUE;
    FOR index:=0 TO len-1 DO
      ch:=str[index];
      state.p(ch, class, state);
      IF class=Conv.valid THEN
        IF ch="-" THEN positive:=FALSE
        ELSIF ch="+" THEN positive:=TRUE
        ELSE (* must be a digit *)
          digit:=ORD(ch)-ORD("0");
          IF positive THEN int:=int*10+digit
          ELSE int:=int*10-digit
          END
        END
      END
    END;
    RETURN int
  ELSE RETURN 0 (* raise exception here *)
  END
END ValueInt;


PROCEDURE LengthInt*(int: LONGINT): INTEGER;
 (* 
    Returns the number of characters in the string representation of int.
    This value corresponds to the capacity of an array `str' which is 
    of the minimum capacity needed to avoid truncation of the result in 
    the call IntStr.IntToStr(int,str).
  *)
VAR
  cnt: INTEGER;
BEGIN
  IF int=MIN(LONGINT) THEN int:=-(int+1); cnt:=1 (* argh!! *)
  ELSIF int<=0 THEN int:=-int; cnt:=1 
  ELSE cnt:=0 
  END;
  WHILE int>0 DO INC(cnt); int:=int DIV 10 END;
  RETURN cnt
END LengthInt; 

 
PROCEDURE IsIntConvException*(): BOOLEAN;
  (* Returns TRUE if the current coroutine is in the exceptional execution 
     state because of the raising of the IntConv exception; otherwise 
     returns FALSE.
  *)
BEGIN
  RETURN FALSE
END IsIntConvException;


BEGIN
  (* kludge necessary because of recursive procedure declaration *)
  NEW(S); NEW(W); NEW(SI);  
  S.p:=SState; W.p:=WState; SI.p:=ScanInt
END IntConv.