]>
Commit | Line | Data |
---|---|---|
33a3f85e GM |
1 | (* ShortStr.mod implement the ISO ShortStr specification. |
2 | ||
a945c346 | 3 | Copyright (C) 2009-2024 Free Software Foundation, Inc. |
33a3f85e GM |
4 | Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>. |
5 | ||
6 | This file is part of GNU Modula-2. | |
7 | ||
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) | |
11 | any later version. | |
12 | ||
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. | |
17 | ||
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. | |
21 | ||
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/>. *) | |
26 | ||
27 | IMPLEMENTATION MODULE ShortStr; | |
28 | ||
29 | (* REAL/string conversions *) | |
30 | ||
31 | IMPORT ShortConv ; | |
32 | ||
33 | FROM DynamicStrings IMPORT String, InitString, KillString, Length, CopyOut ; | |
34 | ||
35 | FROM 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 | ||
49 | PROCEDURE 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 | *) | |
56 | BEGIN | |
57 | res := ShortConv.FormatReal(str) ; | |
58 | IF res=strAllRight | |
59 | THEN | |
60 | real := ShortConv.ValueReal(str) | |
61 | END | |
62 | END StrToReal ; | |
63 | ||
64 | ||
65 | PROCEDURE 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 | *) | |
71 | VAR | |
72 | s: String ; | |
73 | BEGIN | |
74 | s := RealToFloatString(real, sigFigs) ; | |
75 | CopyOut(str, s) ; | |
76 | s := KillString(s) | |
77 | END RealToFloat ; | |
78 | ||
79 | ||
80 | PROCEDURE 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 | *) | |
88 | VAR | |
89 | s: String ; | |
90 | BEGIN | |
91 | s := RealToEngString(real, sigFigs) ; | |
92 | CopyOut(str, s) ; | |
93 | s := KillString(s) | |
94 | END RealToEng ; | |
95 | ||
96 | ||
97 | PROCEDURE 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 | *) | |
103 | VAR | |
104 | s: String ; | |
105 | BEGIN | |
106 | s := RealToFixedString(real, place) ; | |
107 | CopyOut(str, s) ; | |
108 | s := KillString(s) | |
109 | END RealToFixed ; | |
110 | ||
111 | ||
112 | PROCEDURE 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 | *) | |
119 | VAR | |
120 | s : String ; | |
121 | sigFigs: CARDINAL ; | |
122 | BEGIN | |
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 | |
147 | END RealToStr ; | |
148 | ||
149 | ||
150 | END ShortStr. |