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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//
// GENERATED FILE
//

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

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

/// Parse a list of items; return a set.
///
/// Parse a list of items delimited by multiple delimiters,
/// placing the resulting items into a set.
///
/// # Required Reading
///
/// * [CELLS](crate::required_reading::cells)
/// * [SETS](crate::required_reading::sets)
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  LIST       I   List of items delimited by DELIMS on input.
///  DELIMS     I   Single characters which delimit items.
///  SET        O   Items in the list, validated, left justified.
/// ```
///
/// # Detailed Input
///
/// ```text
///  LIST     is a list of items delimited by any one of the characters
///           in the string DELIMS. 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. Leading and
///           trailing blanks in list are ignored.
///
///  DELIMS   contains the individual characters which delimit the
///           items in the list. These may be any ASCII characters,
///           including blanks.
///
///           However, by definition, consecutive blanks are NOT
///           considered to be consecutive delimiters. Nor are a blank
///           and any other delimiter considered to be consecutive
///           delimiters. In addition, leading and trailing blanks are
///           ignored.
/// ```
///
/// # Detailed Output
///
/// ```text
///  SET      is a SPICE set containing the items in the list, left
///           justified. Any item in the list too long to fit into an
///           element of SET is truncated on the right.
///
///           The strings in SET will be sorted in increasing order,
///           and duplicates will be removed. Trailing blanks are
///           ignored in string comparisons.
///
///           The size of the set must be initialized prior to calling
///           LPARSS.
/// ```
///
/// # Exceptions
///
/// ```text
///  1)  If the size of the set is not large enough to accommodate all
///      of the items in the set, an error is signaled by a routine in
///      the call tree of this routine.
///
///  2)  If the string length of SET is too short to accommodate an
///      item, the item will be truncated on the right.
///
///  3)  If the string length of SET is too short to permit encoding of
///      integers via the SPICELIB routine ENCHAR, an error is signaled
///      by a routine in the call tree of this routine.
/// ```
///
/// # Examples
///
/// ```text
///  The following examples illustrate the operation of LPARSS.
///
///  1) Let
///           LIST        = 'A number of words   separated   by
///                           spaces.'
///           DELIMS      = ' ,.'
///           SIZE (SET)  = 20
///
///     Then
///
///           CARDC (SET) = 8
///
///           SET (1)     = ' '
///           SET (2)     = 'A'
///           SET (3)     = 'by'
///           SET (4)     = 'number'
///           SET (5)     = 'of'
///           SET (6)     = 'separated'
///           SET (7)     = 'spaces'
///           SET (8)     = 'words'
///
///
///  2) Let
///
///           LIST        = '  1986-187// 13:15:12.184 '
///           DELIMS      = ' ,/-:'
///           SIZE (SET)  = 20
///
///     Then
///
///           CARDC (SET) = 6
///
///           SET (1)     = ' '
///           SET (2)     = '12.184'
///           SET (3)     = '13'
///           SET (4)     = '15'
///           SET (5)     = '187'
///           SET (6)     = '1986'
///
///
///  3) Let   LIST        = '  ,This,  is, ,an,, example, '
///           DELIMS      = ' ,'
///           SIZE (SET)  = 20
///
///     Then
///           CARDC (SET) = 5
///
///           SET (1)     = ' '
///           SET (2)     = 'This'
///           SET (3)     = 'an'
///           SET (4)     = 'example'
///           SET (5)     = 'is'
///
///
///  4) Let   LIST        = 'Mary had a little lamb, little lamb
///                          whose fleece was white      as snow.'
///           DELIMS      = ' ,.'
///           SIZE (SET)  = 6
///
///     An error would be signaled because the set is not
///     large enough to accommodate all of the items in the
///     list.
///
///
///  5) Let   LIST        = '1 2 3 4 5 6 7 8 9 10.'
///           DELIMS      = ' .'
///           SIZE (SET)  = 10
///
///     An error would be signaled because the set is not
///     large enough to accommodate all of the items in the
///     list. Note that delimiters at the end (or beginning)
///     of list are considered to delimit blank items.
///
///
///  6) Let   LIST        = '1 2 3 4 5 6 7 8 9 10.'
///           DELIMS      = '.'
///           SIZE (SET)  = 10
///
///     Then
///
///           CARDC (SET) = 2
///
///           SET (1)     = ' '
///           SET (2)     = '1 2 3 4 5 6 7 8 9 10'
/// ```
///
/// # 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, 24-AUG-2021 (JDR)
///
///         Added IMPLICIT NONE statement.
///
///         Edited the header to comply with NAIF standard. Improved
///         documentation of arguments LIST, DELIM and SET.
///
///         Updated entries #2 and #3 in $Exceptions section: changed
///         wrong argument name, and indicated that the routine used
///         for encoding is part of SPICELIB.
///
/// -    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 (HAN) (IMU)
/// ```
///
/// # 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.
///
///
/// -    Beta Version 2.0.0, 10-JAN-1989 (HAN)
///
///         Error handling was added, and old error flags and their
///         checks were removed. An error is signaled if the set
///         is not large enough to accommodate all of the items in
///         the list.
///
///         The header documentation was updated to reflect the error
///         handling changes, and more examples were added.
/// ```
pub fn lparss(
    ctx: &mut SpiceContext,
    list: &str,
    delims: &str,
    set: CharArrayMut,
) -> crate::Result<()> {
    LPARSS(list.as_bytes(), delims.as_bytes(), set, ctx.raw_context())?;
    ctx.handle_errors()?;
    Ok(())
}

//$Procedure LPARSS ( Parse a list of items; return a set. )
pub fn LPARSS(
    LIST: &[u8],
    DELIMS: &[u8],
    SET: CharArrayMut,
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    let mut SET = DummyCharArrayMut::new(SET, None, LBCELL..);
    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;
    let mut N: i32 = 0;
    let mut NMAX: i32 = 0;
    let mut VALID: bool = false;

    //
    // SPICELIB functions
    //

    //
    // Local parameters
    //

    //
    // Local variables
    //

    //
    // Standard SPICE error handling.
    //
    if RETURN(ctx) {
        return Ok(());
    } else {
        CHKIN(b"LPARSS", ctx)?;
    }

    //
    // Because speed is essential in many list parsing applications,
    // LPARSS, like LPARSE, parses the input list in a single pass.
    // What follows is nearly identical to LPARSE, except the FORTRAN
    // INDEX function is used to test for delimiters, instead of testing
    // each character for simple equality. Also, the items are inserted
    // into a set instead of simply placed at the end of an array.
    //
    // No items yet.
    //
    N = 0;

    //
    // What is the size of the set?
    //
    NMAX = SIZEC(SET.as_arg(), ctx)?;

    //
    // The array has not been validated yet.
    //
    VALID = false;

    //
    // Blank list contains a blank item.  No need to validate.
    //
    if fstr::eq(LIST, BLANK) {
        SCARDC(0, SET.as_arg_mut(), ctx)?;
        INSRTC(BLANK, SET.as_arg_mut(), ctx)?;

        VALID = true;
    } else {
        //
        // Eliminate trailing blanks.  EOL is the last non-blank
        // character in the list.
        //
        EOL = LASTNB(LIST);

        //
        // 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) && (intrinsics::INDEX(DELIMS, &ECHR) == 0)) {
                E = (E + 1);

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

            //
            // (This is different from LPARSE. If the delimiter was
            // a blank, find the next non-blank character. If it's not
            // a delimiter, back up. This prevents constructions
            // like 'a , b', where the delimiters are blank and comma,
            // from being interpreted as three items instead of two.
            // By definition, consecutive blanks, or a blank and any
            // other delimiter, do not count as consecutive delimiters.)
            //
            if ((E <= EOL) && (intrinsics::ICHAR(&ECHR) == ISPACE)) {
                //
                // Find the next non-blank character.
                //
                while ((E <= EOL) && (intrinsics::ICHAR(&ECHR) == ISPACE)) {
                    E = (E + 1);

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

                if (E <= EOL) {
                    if (intrinsics::INDEX(DELIMS, &ECHR) == 0) {
                        //
                        // We're looking at a non-delimiter character.
                        //
                        // E is guaranteed to be > 1 if we're here, so the
                        // following subtraction is valid.
                        //
                        E = (E - 1);
                    }
                }
            }

            //
            // 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.
            //
            if !VALID {
                //
                // If the array has not been validated, it's just an
                // array, and we can insert items directly into it.
                // Unless it's full, in which case we validate now and
                // insert later.
                //
                if (N < NMAX) {
                    N = (N + 1);

                    if (E > B) {
                        fstr::assign(SET.get_mut(N), fstr::substr(LIST, B..=(E - 1)));
                    } else {
                        fstr::assign(SET.get_mut(N), BLANK);
                    }
                } else {
                    VALIDC(NMAX, NMAX, SET.as_arg_mut(), ctx)?;
                    VALID = true;
                }
            }

            //
            // Once the set has been validated, the strings are inserted
            // into the set if there's room. If there is not enough room
            // in the set, let INSRTC signal the error.
            //
            if VALID {
                if (E > B) {
                    INSRTC(fstr::substr(LIST, B..=(E - 1)), SET.as_arg_mut(), ctx)?;
                } else {
                    INSRTC(BLANK, SET.as_arg_mut(), ctx)?;
                }

                if FAILED(ctx) {
                    CHKOUT(b"LPARSS", ctx)?;
                    return Ok(());
                }
            }

            //
            // If there are more items to be found, continue with the
            // character following E (which is a delimiter).
            //
            B = (E + 1);
        }

        //
        // If the array has not yet been validated, validate it before
        // returning.
        //
        if !VALID {
            VALIDC(NMAX, N, SET.as_arg_mut(), ctx)?;
        }

        //
        // If the list ended with a (non-blank) delimiter, insert a
        // blank item into the set. If there isn't any room, signal
        // an error.
        //
        if (intrinsics::INDEX(DELIMS, fstr::substr(LIST, EOL..=EOL)) != 0) {
            INSRTC(BLANK, SET.as_arg_mut(), ctx)?;
            //
            // If INSRTC failed to insert the blank because the set
            // was already full, INSRTC will have signaled an error.
            // No action is necessary here.
            //
        }
    }

    CHKOUT(b"LPARSS", ctx)?;
    Ok(())
}