]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/m2/gm2-libs-iso/ShortStr.mod
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-libs-iso / ShortStr.mod
CommitLineData
33a3f85e
GM
1(* ShortStr.mod implement the ISO ShortStr specification.
2
a945c346 3Copyright (C) 2009-2024 Free Software Foundation, Inc.
33a3f85e
GM
4Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
5
6This file is part of GNU Modula-2.
7
8GNU Modula-2 is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GNU Modula-2 is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16General Public License for more details.
17
18Under Section 7 of GPL version 3, you are granted additional
19permissions described in the GCC Runtime Library Exception, version
203.1, as published by the Free Software Foundation.
21
22You should have received a copy of the GNU General Public License and
23a copy of the GCC Runtime Library Exception along with this program;
24see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25<http://www.gnu.org/licenses/>. *)
26
27IMPLEMENTATION MODULE ShortStr;
28
29(* REAL/string conversions *)
30
31IMPORT ShortConv ;
32
33FROM DynamicStrings IMPORT String, InitString, KillString, Length, CopyOut ;
34
35FROM ConvStringShort IMPORT RealToFixedString, RealToFloatString,
36 RealToEngString ;
37
38
39(* the string form of a signed fixed-point real number is
40 ["+" | "-"], decimal digit, {decimal digit}, [".",
41 {decimal digit}]
42*)
43
44(* the string form of a signed floating-point real number is
45 signed fixed-point real number, "E", ["+" | "-"],
46 decimal digit, {decimal digit}
47*)
48
49PROCEDURE StrToReal (str: ARRAY OF CHAR; VAR real: SHORTREAL;
50 VAR res: ConvResults) ;
51 (* Ignores any leading spaces in str. If the subsequent characters
52 in str are in the format of a signed real number, assigns a
53 corresponding value to real. Assigns a value indicating the
54 format of str to res.
55 *)
56BEGIN
57 res := ShortConv.FormatReal(str) ;
58 IF res=strAllRight
59 THEN
60 real := ShortConv.ValueReal(str)
61 END
62END StrToReal ;
63
64
65PROCEDURE RealToFloat (real: SHORTREAL; sigFigs: CARDINAL;
66 VAR str: ARRAY OF CHAR) ;
67 (* Converts the value of real to floating-point string form, with
68 sigFigs significant figures, and copies the possibly truncated
69 result to str.
70 *)
71VAR
72 s: String ;
73BEGIN
74 s := RealToFloatString(real, sigFigs) ;
75 CopyOut(str, s) ;
76 s := KillString(s)
77END RealToFloat ;
78
79
80PROCEDURE RealToEng (real: SHORTREAL; sigFigs: CARDINAL;
81 VAR str: ARRAY OF CHAR) ;
82 (* Converts the value of real to floating-point string form, with
83 sigFigs significant figures, and copies the possibly truncated
84 result to str. The number is scaled with one to three digits
85 in the whole number part and with an exponent that is a multiple
86 of three.
87 *)
88VAR
89 s: String ;
90BEGIN
91 s := RealToEngString(real, sigFigs) ;
92 CopyOut(str, s) ;
93 s := KillString(s)
94END RealToEng ;
95
96
97PROCEDURE RealToFixed (real: SHORTREAL; place: INTEGER;
98 VAR str: ARRAY OF CHAR) ;
99 (* Converts the value of real to fixed-point string form, rounded
100 to the given place relative to the decimal point, and copies
101 the possibly truncated result to str.
102 *)
103VAR
104 s: String ;
105BEGIN
106 s := RealToFixedString(real, place) ;
107 CopyOut(str, s) ;
108 s := KillString(s)
109END RealToFixed ;
110
111
112PROCEDURE RealToStr (real: SHORTREAL; VAR str: ARRAY OF CHAR) ;
113 (* Converts the value of real as RealToFixed if the sign and
114 magnitude can be shown within the capacity of str, or
115 otherwise as RealToFloat, and copies the possibly truncated
116 result to str. The number of places or significant digits
117 are implementation-defined.
118 *)
119VAR
120 s : String ;
121 sigFigs: CARDINAL ;
122BEGIN
123 sigFigs := HIGH(str) ;
124 WHILE sigFigs>1 DO
125 s := RealToFixedString(real, sigFigs) ;
126 IF Length(s)<=HIGH(str)
127 THEN
128 CopyOut(str, s) ;
129 s := KillString(s) ;
130 RETURN
131 END ;
132 s := KillString(s) ;
133 DEC(sigFigs)
134 END ;
135 sigFigs := HIGH(str) ;
136 WHILE sigFigs#0 DO
137 s := RealToFloatString(real, sigFigs) ;
138 IF Length(s)<=HIGH(str)
139 THEN
140 CopyOut(str, s) ;
141 s := KillString(s) ;
142 RETURN
143 END ;
144 s := KillString(s) ;
145 DEC(sigFigs)
146 END
147END RealToStr ;
148
149
150END ShortStr.