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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// GENERATED FILE
//
use super::*;
use crate::SpiceContext;
use f2rust_std::*;
/// Retain significant digits
///
/// Retain only the significant digits in a numeric string.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// IN I Input numeric string.
/// OUT O Numeric string, with insignificant digits removed.
/// ```
///
/// # Detailed Input
///
/// ```text
/// IN is a numeric string.
/// ```
///
/// # Detailed Output
///
/// ```text
/// OUT is the same numeric string with insignificant
/// zeros and spaces removed. The special case '.000...'
/// becomes just '0'. OUT may overwrite IN. If the
/// output string is too long, it is truncated on the
/// right.
/// ```
///
/// # Exceptions
///
/// ```text
/// Error free.
///
/// 1) If IN is a non-numeric string, the contents of OUT are
/// unpredictable.
/// ```
///
/// # Particulars
///
/// ```text
/// There are only two interesting cases:
///
/// 1) There is a decimal point and an exponent immediately
/// preceded by zero ('...0E', '...0D', '...0e', '...0d')
/// or by a space ('... E', '... D', '... e', '... d').
///
/// 2) There is a decimal point and no exponent, and the last non-
/// blank character is a zero ('...0').
///
/// In each of these cases, go to the zero in question, and step
/// backwards until you find something other than a blank or a zero.
///
/// Finally, remove all leading spaces, and all occurrences of more
/// than one consecutive space within the string.
/// ```
///
/// # Examples
///
/// ```text
/// The following examples illustrate the use of SIGDGT.
///
/// '0.123456000000D-04' becomes '0.123456D-04'
/// ' -9.2100000000000' '-9.21'
/// ' 13' '13'
/// ' 00013' '00013'
/// ' .314 159 265 300 000 e1' '.314 159 265 3e1'
/// ' 123 45 6' '123 45 6'
/// ' .000000000' '0'
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// H.A. Neilan (JPL)
/// W.L. Taber (JPL)
/// I.M. Underwood (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.0, 12-AUG-2021 (JDR)
///
/// Added IMPLICIT NONE statement.
///
/// Edited the header to comply with NAIF standard.
///
/// - SPICELIB Version 1.0.1, 10-MAR-1992 (WLT)
///
/// Comment section for permuted index source lines was added
/// following the header.
///
/// - SPICELIB Version 1.0.0, 31-JAN-1990 (IMU) (HAN) (NJB) (WLT)
/// ```
///
/// # Revisions
///
/// ```text
/// - Beta Version 1.3.0, 21-MAR-1989 (WLT)
///
/// Previous fix was unbelievably bad, very buggy. This
/// has been fixed along with other bugs and non-standard
/// code has been removed.
///
/// - Beta Version 1.2.0, 28-FEB-1989 (WLT)
///
/// Reference to INSSUB replaced by SUFFIX
///
/// - Beta Version 1.1.1, 17-FEB-1989 (HAN) (NJB)
///
/// Declaration of the unused function ISRCHC removed.
/// ```
pub fn sigdgt(in_: &str, out: &mut str) {
SIGDGT(in_.as_bytes(), fstr::StrBytes::new(out).as_mut());
}
//$Procedure SIGDGT ( Retain significant digits )
pub fn SIGDGT(IN: &[u8], OUT: &mut [u8]) {
let mut BEGIN: i32 = 0;
let mut END: i32 = 0;
let mut LCHAR = [b' '; 1];
let mut ZERO: i32 = 0;
let mut I: i32 = 0;
let mut J: i32 = 0;
let mut K: i32 = 0;
let mut L: i32 = 0;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Find the first and last non-blank characters in the string.
//
BEGIN = intrinsics::MAX0(&[1, FRSTNB(IN)]);
END = intrinsics::MAX0(&[1, LASTNB(IN)]);
fstr::assign(&mut LCHAR, b" ");
//
// Trivial case.
//
if (BEGIN == END) {
fstr::assign(
fstr::substr_mut(OUT, 1..=1),
fstr::substr(IN, BEGIN..=BEGIN),
);
if (intrinsics::LEN(OUT) > 1) {
fstr::assign(fstr::substr_mut(OUT, 2..), b" ");
}
//
// If there is no decimal point, all zeros are significant.
//
} else if (intrinsics::INDEX(IN, b".") == 0) {
L = 1;
K = BEGIN;
while ((L <= intrinsics::LEN(OUT)) && (K <= END)) {
fstr::assign(fstr::substr_mut(OUT, L..=L), fstr::substr(IN, K..=K));
//
// Don't increment L if the last item copied was a space
// (we don't want to copy extra spaces).
//
if (fstr::ne(fstr::substr(IN, K..=K), b" ") || fstr::ne(&LCHAR, b" ")) {
L = (L + 1);
}
fstr::assign(&mut LCHAR, fstr::substr(IN, K..=K));
K = (K + 1);
}
if (L <= intrinsics::LEN(OUT)) {
fstr::assign(fstr::substr_mut(OUT, L..), b" ");
}
} else {
//
// Is there is a decimal point and an exponent immediately
// preceded by zero ('...0E', '...0D', '...0e', '...0d') or
// by a space ('... E', '... D', '... e', '... d')?
//
ZERO = intrinsics::INDEX(IN, b"0E");
if (ZERO == 0) {
ZERO = intrinsics::INDEX(IN, b"0D");
}
if (ZERO == 0) {
ZERO = intrinsics::INDEX(IN, b"0e");
}
if (ZERO == 0) {
ZERO = intrinsics::INDEX(IN, b"0d");
}
if (ZERO == 0) {
ZERO = intrinsics::INDEX(IN, b" E");
}
if (ZERO == 0) {
ZERO = intrinsics::INDEX(IN, b" D");
}
if (ZERO == 0) {
ZERO = intrinsics::INDEX(IN, b" e");
}
if (ZERO == 0) {
ZERO = intrinsics::INDEX(IN, b" d");
}
//
// Begin there, and move toward the front of the string until
// something other than a blank or a zero is encountered. Then
// remove the superfluous characters.
//
if (ZERO > 0) {
J = (ZERO + 1);
I = ZERO;
while (fstr::eq(fstr::substr(IN, I..=I), b"0")
|| fstr::eq(fstr::substr(IN, I..=I), b" "))
{
I = (I - 1);
}
L = 1;
K = BEGIN;
while ((L <= intrinsics::LEN(OUT)) && (K <= I)) {
fstr::assign(fstr::substr_mut(OUT, L..=L), fstr::substr(IN, K..=K));
//
// Don't increment L if the last item copied was a space.
//
if (fstr::ne(fstr::substr(IN, K..=K), b" ") || fstr::ne(&LCHAR, b" ")) {
L = (L + 1);
}
fstr::assign(&mut LCHAR, fstr::substr(IN, K..=K));
K = (K + 1);
}
K = J;
while ((L <= intrinsics::LEN(OUT)) && (K <= END)) {
fstr::assign(fstr::substr_mut(OUT, L..=L), fstr::substr(IN, K..=K));
//
// Increment L only if we don't have two consecutive
// spaces.
//
if (fstr::ne(fstr::substr(IN, K..=K), b" ") || fstr::ne(&LCHAR, b" ")) {
L = (L + 1);
}
fstr::assign(&mut LCHAR, fstr::substr(IN, K..=K));
K = (K + 1);
}
if (L <= intrinsics::LEN(OUT)) {
fstr::assign(fstr::substr_mut(OUT, L..), b" ");
}
//
//
// Is there is a decimal point and no exponent, and is the last
// non-blank character a zero ('...0')? Then truncate the string
// after the last character that is neither a blank nor a zero.
//
} else if (fstr::eq(fstr::substr(IN, END..=END), b"0") && (CPOS(IN, b"EeDd", 1) == 0)) {
I = END;
while (fstr::eq(fstr::substr(IN, I..=I), b"0")
|| fstr::eq(fstr::substr(IN, I..=I), b" "))
{
I = (I - 1);
}
L = 1;
K = BEGIN;
while ((L <= intrinsics::LEN(OUT)) && (K <= I)) {
fstr::assign(fstr::substr_mut(OUT, L..=L), fstr::substr(IN, K..=K));
//
// Increment L only if we don't have two consecutive
// spaces.
//
if (fstr::ne(fstr::substr(IN, K..=K), b" ") || fstr::ne(&LCHAR, b" ")) {
L = (L + 1);
}
fstr::assign(&mut LCHAR, fstr::substr(IN, K..=K));
K = (K + 1);
}
if (L <= intrinsics::LEN(OUT)) {
fstr::assign(fstr::substr_mut(OUT, L..), b" ");
}
} else {
L = 1;
K = BEGIN;
while ((L <= intrinsics::LEN(OUT)) && (K <= END)) {
fstr::assign(fstr::substr_mut(OUT, L..=L), fstr::substr(IN, K..=K));
//
// Increment L only if we don't have two consecutive spaces.
//
if (fstr::ne(fstr::substr(IN, K..=K), b" ") || fstr::ne(&LCHAR, b" ")) {
L = (L + 1);
}
fstr::assign(&mut LCHAR, fstr::substr(IN, K..=K));
K = (K + 1);
}
if (L <= intrinsics::LEN(OUT)) {
fstr::assign(fstr::substr_mut(OUT, L..), b" ");
}
}
}
//
// Special case. The string '.0000....' reduces to '.' after the
// zeros are removed.
//
if fstr::eq(OUT, b".") {
fstr::assign(OUT, b"0");
}
}