rsspice 0.1.0

Pure Rust port of the SPICE Toolkit for space geometry
Documentation
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//
// GENERATED FILE
//

use super::*;
use crate::SpiceContext;
use f2rust_std::*;

const BLANK: &[u8; 1 as usize] = &fstr::extend_const::<{ 1 as usize }>(b" ");
const ISPACE: i32 = 32;

/// Parse items from a list
///
/// Parse a list of items delimited by a single character.
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  LIST       I   List of items delimited by DELIM.
///  DELIM      I   Single character used to delimit items.
///  NMAX       I   Maximum number of items to return.
///  N          O   Number of items in the list.
///  ITEMS      O   Items in the list, left justified.
/// ```
///
/// # Detailed Input
///
/// ```text
///  LIST     is a list of items delimited by the single character
///           DELIM. Consecutive delimiters, and delimiters at the
///           beginning and end of the list, are considered to
///           delimit blank items. A blank list is considered to
///           contain a single (blank) item.
///
///  DELIM    is the character delimiting the items in the list.
///           This may be any ASCII character, including a blank.
///           However, by definition, consecutive blanks are NOT
///           considered to be consecutive delimiters. In addition,
///           leading and trailing blanks are ignored.
///
///  NMAX     is the maximum number of items to be returned from
///           the list. This allows the user to guard against
///           overflow from a list containing more items than
///           expected.
/// ```
///
/// # Detailed Output
///
/// ```text
///  N        is the number of items in the list. N may be
///           any number between one and NMAX. N is always the
///           number of delimiters plus one.
///
///  ITEMS    are the items in the list, left justified. Any item
///           in the list too long to fit into an element of ITEMS
///           is truncated on the right.
/// ```
///
/// # Exceptions
///
/// ```text
///  Error free.
///
///  1)  If the string length of ITEMS is too short to accommodate
///      an item, the item will be truncated on the right.
/// ```
///
/// # Examples
///
/// ```text
///  The numerical results shown for these examples may differ across
///  platforms. The results depend on the SPICE kernels used as
///  input, the compiler and supporting libraries, and the machine
///  specific arithmetic implementation.
///
///  1) Parse a character string to retrieve the words contained
///     within.
///
///     Example code begins here.
///
///
///           PROGRAM LPARSE_EX1
///           IMPLICIT NONE
///
///     C
///     C     Local constants.
///     C
///           INTEGER                 NMAX
///           PARAMETER             ( NMAX   = 25  )
///
///           INTEGER                 STRLEN
///           PARAMETER             ( STRLEN = 255 )
///
///     C
///     C     Local variables.
///     C
///           CHARACTER*(1)           DELIM
///           CHARACTER*(STRLEN)      ITEMS  ( NMAX )
///           CHARACTER*(STRLEN)      LIST
///
///           INTEGER                 I
///           INTEGER                 N
///
///     C
///     C     Define the list of delimited items.
///     C
///     C     Think of a sentence as a list delimited by a space.
///     C     DELIM is assigned to a space.
///     C
///           LIST  = 'Run and find out.'
///           DELIM = ' '
///
///     C
///     C     Parse the items from LIST.
///     C
///           CALL LPARSE ( LIST, DELIM, NMAX, N, ITEMS )
///
///     C
///     C     Output the ITEMS.
///     C
///           DO I = 1, N
///
///              WRITE(*,'(A,I3,2A)') 'Item', I, ': ', ITEMS(I)
///
///           END DO
///
///           END
///
///
///     When this program was executed on a Mac/Intel/gfortran/64-bit
///     platform, the output was:
///
///
///     Item  1: Run
///     Item  2: and
///     Item  3: find
///     Item  4: out.
///
///
///  2) Repeat the previous example with different character
///     delimiting the items in the list and different maximum number
///     of items to return.
///
///     Example code begins here.
///
///
///           PROGRAM LPARSE_EX2
///           IMPLICIT NONE
///
///     C
///     C     SPICELIB functions.
///     C
///           INTEGER                 RTRIM
///
///     C
///     C     Local constants.
///     C
///           INTEGER                 NCASES
///           PARAMETER             ( NCASES = 2   )
///
///           INTEGER                 NMAXT
///           PARAMETER             ( NMAXT  = 25  )
///
///           INTEGER                 STRLEN
///           PARAMETER             ( STRLEN = 255 )
///
///     C
///     C     Local variables.
///     C
///           CHARACTER*(1)           DELIM  ( NCASES )
///           CHARACTER*(STRLEN)      ITEMS  ( NMAXT  )
///           CHARACTER*(STRLEN)      LIST   ( NCASES )
///
///           INTEGER                 I
///           INTEGER                 J
///           INTEGER                 N
///           INTEGER                 NMAX   ( NCASES )
///
///     C
///     C     Define the lists of delimited items, the delimiting
///     C     character and the maximum number of items to return.
///     C
///           LIST(1)  = '//option1//option2/ //'
///           DELIM(1) = '/'
///           NMAX(1)  = 20
///
///           LIST(2)  = ' ,bob,   carol,, ted,  alice'
///           DELIM(2) = ','
///           NMAX(2)  = 4
///
///           DO I = 1, NCASES
///
///              WRITE(*,'(A,I2,A)') 'Case', I, ':'
///              WRITE(*,'(3A)')   '   String: ''',
///          .                     LIST(I)(:RTRIM(LIST(I))), ''''
///              WRITE(*,'(3A)')   '   DELIM : ''', DELIM(I), ''''
///              WRITE(*,'(A,I3)') '   NMAX  :', NMAX(I)
///              WRITE(*,'(A)')    '   Output items:'
///
///     C
///     C        Parse the items from LIST.
///     C
///              CALL LPARSE ( LIST(I), DELIM(I), NMAX(I), N, ITEMS )
///
///     C
///     C        Output the ITEMS.
///     C
///              DO J = 1, N
///
///                 WRITE(*,'(A,I3,3A)') '      Item', J, ': ''',
///          .                  ITEMS(J)(:RTRIM(ITEMS(J))), ''''
///
///              END DO
///
///           END DO
///
///           END
///
///
///     When this program was executed on a Mac/Intel/gfortran/64-bit
///     platform, the output was:
///
///
///     Case 1:
///        String: '//option1//option2/ //'
///        DELIM : '/'
///        NMAX  : 20
///        Output items:
///           Item  1: ' '
///           Item  2: ' '
///           Item  3: 'option1'
///           Item  4: ' '
///           Item  5: 'option2'
///           Item  6: ' '
///           Item  7: ' '
///           Item  8: ' '
///     Case 2:
///        String: ' ,bob,   carol,, ted,  alice'
///        DELIM : ','
///        NMAX  :  4
///        Output items:
///           Item  1: ' '
///           Item  2: 'bob'
///           Item  3: 'carol'
///           Item  4: ' '
/// ```
///
/// # 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.2.0, 06-JUL-2021 (JDR)
///
///         Added IMPLICIT NONE statement.
///
///         Edited the header to comply with NAIF standard. Removed
///         unnecessary entries from $Revisions section.
///
///         Added complete code example.
///
/// -    SPICELIB Version 1.1.0, 26-OCT-2005 (NJB)
///
///         Bug fix: code was modified to avoid out-of-range
///         substring bound conditions.
///
/// -    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)
/// ```
///
/// # Revisions
///
/// ```text
/// -    SPICELIB Version 1.1.0, 26-OCT-2005 (NJB)
///
///         Bug fix: code was modified to avoid out-of-range
///         substring bound conditions. The previous version
///         of this routine used DO WHILE statements of the form
///
///                   DO WHILE (      ( B         .LE. EOL   )
///            .                .AND. ( LIST(B:B) .EQ. BLANK ) )
///
///         Such statements can cause index range violations when the
///         index B is greater than the length of the string LIST.
///         Whether or not such violations occur is platform-dependent.
/// ```
pub fn lparse(list: &str, delim: char, nmax: i32, n: &mut i32, items: CharArrayMut) {
    LPARSE(
        list.as_bytes(),
        &[u8::try_from(delim).unwrap()],
        nmax,
        n,
        items,
    );
}

//$Procedure LPARSE ( Parse items from a list )
pub fn LPARSE(LIST: &[u8], DELIM: &[u8], NMAX: i32, N: &mut i32, ITEMS: CharArrayMut) {
    let DELIM = &DELIM[..1 as usize];
    let mut ITEMS = DummyCharArrayMut::new(ITEMS, None, 1..);
    let mut BCHR = [b' '; 1 as usize];
    let mut ECHR = [b' '; 1 as usize];
    let mut B: i32 = 0;
    let mut E: i32 = 0;
    let mut EOL: i32 = 0;

    //
    // Local parameters
    //

    //
    // Local variables
    //

    //
    // Because speed is essential in many list parsing applications,
    // LPARSE parses the input list in a single pass.
    //

    //
    // Nothing yet.
    //
    *N = 0;

    //
    // Blank list contains a blank item.
    //
    if fstr::eq(LIST, BLANK) {
        *N = 1;
        fstr::assign(ITEMS.get_mut(1), BLANK);
    } else {
        //
        // Eliminate trailing blanks. EOL is the last non-blank
        // character in the list.
        //
        EOL = intrinsics::LEN(LIST);

        while fstr::eq(fstr::substr(LIST, EOL..=EOL), BLANK) {
            EOL = (EOL - 1);
        }

        //
        // As the king said to Alice: 'Begin at the beginning.
        // Continue until you reach the end. Then stop.'
        //
        // When searching for items, B is the beginning of the current
        // item; E is the end.  E points to the next non-blank delimiter,
        // if any; otherwise E points to either the last character
        // preceding the next item, or to the last character of the list.
        //
        B = 1;

        while (B <= EOL) {
            //
            // Skip any blanks before the next item or delimiter.
            //
            // At this point in the loop, we know
            //
            //    B <= EOL
            //
            fstr::assign(&mut BCHR, fstr::substr(LIST, B..=B));

            while ((B <= EOL) && (intrinsics::ICHAR(&BCHR) == ISPACE)) {
                B = (B + 1);

                if (B <= EOL) {
                    fstr::assign(&mut BCHR, fstr::substr(LIST, B..=B));
                }
            }

            //
            // At this point B is the index of the next non-blank
            // character BCHR, or else
            //
            //    B == EOL + 1
            //
            // The item ends at the next delimiter.
            //
            E = B;

            if (E <= EOL) {
                fstr::assign(&mut ECHR, fstr::substr(LIST, E..=E));
            } else {
                fstr::assign(&mut ECHR, BLANK);
            }

            while ((E <= EOL) && fstr::ne(&ECHR, DELIM)) {
                E = (E + 1);

                if (E <= EOL) {
                    fstr::assign(&mut ECHR, fstr::substr(LIST, E..=E));
                }
            }

            //
            // The item now lies between B and E. Unless, of course, B and
            // E are the same character; this can happen if the list
            // starts or ends with a non-blank delimiter, or if we have
            // stumbled upon consecutive delimiters.
            //
            *N = (*N + 1);

            if (E > B) {
                fstr::assign(ITEMS.get_mut(*N), fstr::substr(LIST, B..=(E - 1)));
            } else {
                fstr::assign(ITEMS.get_mut(*N), BLANK);
            }

            //
            // If there are more items to be found, continue with
            // character following E (which is a delimiter).
            //
            if (*N < NMAX) {
                B = (E + 1);
            } else {
                return;
            }
        }

        //
        // If the list ended with a (non-blank) delimiter, add a blank
        // item to the end.
        //
        if (fstr::eq(fstr::substr(LIST, EOL..=EOL), DELIM) && (*N < NMAX)) {
            *N = (*N + 1);
            fstr::assign(ITEMS.get_mut(*N), BLANK);
        }
    }
}