1 (* StringConvert.def provides functions to convert numbers to and from strings.
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
27 DEFINITION MODULE StringConvert
;
29 FROM DynamicStrings
IMPORT String
;
30 EXPORT QUALIFIED IntegerToString
, StringToInteger
,
31 StringToLongInteger
, LongIntegerToString
,
32 StringToCardinal
, CardinalToString
,
33 StringToLongCardinal
, LongCardinalToString
,
34 StringToShortCardinal
, ShortCardinalToString
,
35 StringToLongreal
, LongrealToString
,
37 stoi
, itos
, ctos
, stoc
, hstoi
, ostoi
, bstoi
,
43 IntegerToString - converts INTEGER, i, into a String. The field with
44 can be specified if non zero. Leading characters
45 are defined by padding and this function will
46 prepend a + if sign is set to TRUE.
47 The base allows the caller to generate binary,
48 octal, decimal, hexidecimal numbers.
49 The value of lower is only used when hexidecimal
50 numbers are generated and if TRUE then digits
51 abcdef are used, and if FALSE then ABCDEF are used.
54 PROCEDURE IntegerToString (i
: INTEGER; width
: CARDINAL; padding
: CHAR; sign
: BOOLEAN;
55 base
: CARDINAL; lower
: BOOLEAN) : String
;
59 CardinalToString - converts CARDINAL, c, into a String. The field
60 width can be specified if non zero. Leading
61 characters are defined by padding.
62 The base allows the caller to generate binary,
63 octal, decimal, hexidecimal numbers.
64 The value of lower is only used when hexidecimal
65 numbers are generated and if TRUE then digits
66 abcdef are used, and if FALSE then ABCDEF are used.
69 PROCEDURE CardinalToString (c
: CARDINAL; width
: CARDINAL; padding
: CHAR;
70 base
: CARDINAL; lower
: BOOLEAN) : String
;
74 StringToInteger - converts a string, s, of, base, into an INTEGER.
75 Leading white space is ignored. It stops converting
76 when either the string is exhausted or if an illegal
78 The parameter found is set TRUE if a number was found.
81 PROCEDURE StringToInteger (s
: String
; base
: CARDINAL; VAR found
: BOOLEAN) : INTEGER ;
85 StringToCardinal - converts a string, s, of, base, into a CARDINAL.
86 Leading white space is ignored. It stops converting
87 when either the string is exhausted or if an illegal
89 The parameter found is set TRUE if a number was found.
92 PROCEDURE StringToCardinal (s
: String
; base
: CARDINAL; VAR found
: BOOLEAN) : CARDINAL ;
96 LongIntegerToString - converts LONGINT, i, into a String. The field with
97 can be specified if non zero. Leading characters
98 are defined by padding and this function will
99 prepend a + if sign is set to TRUE.
100 The base allows the caller to generate binary,
101 octal, decimal, hexidecimal numbers.
102 The value of lower is only used when hexidecimal
103 numbers are generated and if TRUE then digits
104 abcdef are used, and if FALSE then ABCDEF are used.
107 PROCEDURE LongIntegerToString (i
: LONGINT; width
: CARDINAL; padding
: CHAR;
108 sign
: BOOLEAN; base
: CARDINAL; lower
: BOOLEAN) : String
;
113 StringToLongInteger - converts a string, s, of, base, into an LONGINT.
114 Leading white space is ignored. It stops converting
115 when either the string is exhausted or if an illegal
117 The parameter found is set TRUE if a number was found.
120 PROCEDURE StringToLongInteger (s
: String
; base
: CARDINAL; VAR found
: BOOLEAN) : LONGINT ;
124 LongCardinalToString - converts LONGCARD, c, into a String. The field
125 width can be specified if non zero. Leading
126 characters are defined by padding.
127 The base allows the caller to generate binary,
128 octal, decimal, hexidecimal numbers.
129 The value of lower is only used when hexidecimal
130 numbers are generated and if TRUE then digits
131 abcdef are used, and if FALSE then ABCDEF are used.
134 PROCEDURE LongCardinalToString (c
: LONGCARD; width
: CARDINAL; padding
: CHAR;
135 base
: CARDINAL; lower
: BOOLEAN) : String
;
139 StringToLongCardinal - converts a string, s, of, base, into a LONGCARD.
140 Leading white space is ignored. It stops converting
141 when either the string is exhausted or if an illegal
143 The parameter found is set TRUE if a number was found.
146 PROCEDURE StringToLongCardinal (s
: String
; base
: CARDINAL; VAR found
: BOOLEAN) : LONGCARD ;
150 ShortCardinalToString - converts SHORTCARD, c, into a String. The field
151 width can be specified if non zero. Leading
152 characters are defined by padding.
153 The base allows the caller to generate binary,
154 octal, decimal, hexidecimal numbers.
155 The value of lower is only used when hexidecimal
156 numbers are generated and if TRUE then digits
157 abcdef are used, and if FALSE then ABCDEF are used.
160 PROCEDURE ShortCardinalToString (c
: SHORTCARD
; width
: CARDINAL; padding
: CHAR;
161 base
: CARDINAL; lower
: BOOLEAN) : String
;
165 StringToShortCardinal - converts a string, s, of, base, into a SHORTCARD.
166 Leading white space is ignored. It stops converting
167 when either the string is exhausted or if an illegal
169 The parameter found is set TRUE if a number was found.
172 PROCEDURE StringToShortCardinal (s
: String
; base
: CARDINAL;
173 VAR found
: BOOLEAN) : SHORTCARD
;
177 stoi - decimal string to INTEGER
180 PROCEDURE stoi (s
: String
) : INTEGER ;
184 itos - integer to decimal string.
187 PROCEDURE itos (i
: INTEGER; width
: CARDINAL; padding
: CHAR; sign
: BOOLEAN) : String
;
191 ctos - cardinal to decimal string.
194 PROCEDURE ctos (c
: CARDINAL; width
: CARDINAL; padding
: CHAR) : String
;
198 stoc - decimal string to CARDINAL
201 PROCEDURE stoc (s
: String
) : CARDINAL ;
205 hstoi - hexidecimal string to INTEGER
208 PROCEDURE hstoi (s
: String
) : INTEGER ;
212 ostoi - octal string to INTEGER
215 PROCEDURE ostoi (s
: String
) : INTEGER ;
219 bstoi - binary string to INTEGER
222 PROCEDURE bstoi (s
: String
) : INTEGER ;
226 hstoc - hexidecimal string to CARDINAL
229 PROCEDURE hstoc (s
: String
) : CARDINAL ;
233 ostoc - octal string to CARDINAL
236 PROCEDURE ostoc (s
: String
) : CARDINAL ;
240 bstoc - binary string to CARDINAL
243 PROCEDURE bstoc (s
: String
) : CARDINAL ;
247 StringToLongreal - returns a LONGREAL and sets found to TRUE
248 if a legal number is seen.
251 PROCEDURE StringToLongreal (s
: String
; VAR found
: BOOLEAN) : LONGREAL ;
255 LongrealToString - converts a LONGREAL number, Real, which has,
256 TotalWidth, and FractionWidth into a string.
260 LongrealToString(1.0, 4, 2) -> '1.00'
261 LongrealToString(12.3, 5, 2) -> '12.30'
262 LongrealToString(12.3, 6, 2) -> ' 12.30'
263 LongrealToString(12.3, 6, 3) -> '12.300'
265 if total width is too small then the fraction
268 LongrealToString(12.3, 5, 3) -> '12.30'
270 If TotalWidth is 0 then the function
271 will return the value of x which is converted
272 into as a fixed point number with exhaustive
276 PROCEDURE LongrealToString (x
: LONGREAL;
277 TotalWidth
, FractionWidth
: CARDINAL) : String
;
281 stor - returns a REAL given a string.
284 PROCEDURE stor (s
: String
) : REAL ;
288 stolr - returns a LONGREAL given a string.
291 PROCEDURE stolr (s
: String
) : LONGREAL ;
295 ToSigFig - returns a floating point or base 10 integer
296 string which is accurate to, n, significant
297 figures. It will return a new String
298 and, s, will be destroyed.
303 rounded to the following significant figures yields
312 PROCEDURE ToSigFig (s
: String
; n
: CARDINAL) : String
;
316 ToDecimalPlaces - returns a floating point or base 10 integer
317 string which is accurate to, n, decimal
318 places. It will return a new String
319 and, s, will be destroyed.
320 Decimal places yields, n, digits after
325 rounded to the following decimal places yields
334 PROCEDURE ToDecimalPlaces (s
: String
; n
: CARDINAL) : String
;