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
//
// GENERATED FILE
//

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

/// Match string against wildcard template
///
/// Determine whether a string is matched by a template containing
/// wild cards.
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  STRING     I   String to be tested.
///  TEMPL      I   Template (with wild cards) to test against STRING.
///  WSTR       I   Wild string token.
///  WCHR       I   Wild character token.
///
///  The function returns .TRUE. if STRING matches TEMPL and otherwise
///  returns .FALSE.
/// ```
///
/// # Detailed Input
///
/// ```text
///  STRING   is the input character string to be tested for a match
///           against the input template. Leading and trailing blanks
///           are ignored.
///
///  TEMPL    is the input template to be tested for a match against
///           the input string. TEMPL may contain wild cards. Leading
///           and trailing blanks are ignored.
///
///  WSTR     is the wild string token used in the input template. The
///           wild string token may represent from zero to any number
///           of characters.
///
///  WCHR     is the wild character token used in the input template.
///           The wild character token represents exactly one
///           character.
/// ```
///
/// # Detailed Output
///
/// ```text
///  The function returns .TRUE. when the input string matches the
///  input template, and .FALSE. otherwise. The string and template
///  match whenever the template can expand (through replacement of its
///  wild cards) to become the input string.
/// ```
///
/// # Exceptions
///
/// ```text
///  Error free.
/// ```
///
/// # Particulars
///
/// ```text
///  MATCHW ignores leading and trailing blanks in both the string
///  and the template. All of the following are equivalent (they
///  all return .TRUE.).
///
///     MATCHW ( 'ALCATRAZ',     'A*Z',      '*', '%' )
///     MATCHW ( '  ALCATRAZ  ', 'A*Z',      '*', '%' )
///     MATCHW ( 'ALCATRAZ',     '  A*Z  ',  '*', '%' )
///     MATCHW ( '  ALCATRAZ  ', '  A*Z  ',  '*', '%' )
///
///  MATCHW is case-sensitive:  uppercase characters do not match
///  lowercase characters, and vice versa. Wild characters match
///  characters of both cases.
/// ```
///
/// # Examples
///
/// ```text
///  Let
///
///     STRING  = '  ABCDEFGHIJKLMNOPQRSTUVWXYZ  '
///     WSTR    = '*'
///     WCHR    = '%'
///
///  Then
///
///     if TEMPL is  '*A*'        MATCHW is   T
///                  'A%D*'                     F
///                  'A%C*'                   T
///                  '%A*'                      F
///                      '%%CD*Z'                 T
///                      '%%CD'                     F
///                      'A*MN*Y*Z'               T
///                      'A*MN*Y*%Z'                F
///                      '*BCD*Z*'                T
///                      '*bcd*z*'                  F
///                      ' *BCD*Z*  '             T
/// ```
///
/// # 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.4.0, 06-JUL-2021 (JDR)
///
///         Added IMPLICIT NONE statement.
///
///         Edited the header to comply with NAIF standard. Removed
///         unnecessary entries from $Revisions section.
///
/// -    SPICELIB Version 1.3.1, 11-NOV-2005 (NJB)
///
///         Corrected example calls in header; made other minor
///         edits to header.
///
/// -    SPICELIB Version 1.3.0, 08-JUN-1999 (WLT)
///
///         Fixed comments in detailed output and example sections.
///
/// -    SPICELIB Version 1.2.0, 15-MAY-1995 (WLT)
///
///         Direct substring comparisons were replaced with the logical
///         function SAMCH in several cases so as to avoid out of range
///         errors when examining substrings.
///
/// -    SPICELIB Version 1.1.0, 17-MAY-1994 (HAN)
///
///         Set the default function value to either 0, 0.0D0, .FALSE.,
///         or blank depending on the type of the function.
///
/// -    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 (WLT) (IMU)
/// ```
///
/// # Revisions
///
/// ```text
/// -    Beta Version 1.1.0, 06-OCT-1988 (WLT)
///
///         The old algorithm just did not work. Strings with wild
///         characters at the beginning or end of the string were not
///         matched correctly. For example, A% matched APPROX, if the
///         wild character token was % and the wild string token was *.
///         Needless to say, a new algorithm was developed.
/// ```
pub fn matchw(string: &str, templ: &str, wstr: char, wchr: char) -> bool {
    let ret = MATCHW(
        string.as_bytes(),
        templ.as_bytes(),
        &[u8::try_from(wstr).unwrap()],
        &[u8::try_from(wchr).unwrap()],
    );
    ret
}

//$Procedure MATCHW ( Match string against wildcard template )
pub fn MATCHW(STRING: &[u8], TEMPL: &[u8], WSTR: &[u8], WCHR: &[u8]) -> bool {
    let WSTR = &WSTR[..1];
    let WCHR = &WCHR[..1];
    let mut MATCHW: bool = false;
    let mut SFIRST: i32 = 0;
    let mut TFIRST: i32 = 0;
    let mut SLAST: i32 = 0;
    let mut TLAST: i32 = 0;
    let mut SLEN: i32 = 0;
    let mut TLEN: i32 = 0;
    let mut SCUR: i32 = 0;
    let mut TCUR: i32 = 0;
    let mut LEFT: i32 = 0;
    let mut RIGHT: i32 = 0;
    let mut I: i32 = 0;
    let mut J: i32 = 0;
    let mut NOSUBM: bool = false;

    //
    // SPICELIB functions
    //

    //
    // Local Variables
    //

    //
    // Set the default function value to be FALSE.
    //
    MATCHW = false;

    //
    // First let's get everybody's measurements.
    //
    SFIRST = FRSTNB(STRING);
    SLAST = LASTNB(STRING);
    TFIRST = FRSTNB(TEMPL);
    TLAST = LASTNB(TEMPL);

    TLEN = ((TLAST - TFIRST) + 1);
    SLEN = ((SLAST - SFIRST) + 1);

    SCUR = intrinsics::MAX0(&[1, SFIRST]);
    TCUR = TFIRST;

    //
    // A blank template matches a blank string, and nothing else.
    //
    if ((TLAST == 0) && (SLAST == 0)) {
        MATCHW = true;
        return MATCHW;
    } else if (TLAST == 0) {
        MATCHW = false;
        return MATCHW;
    }

    //
    // The beginning of the string and template must be identical
    // up to the first occurrence of a wild string.
    //

    while (((TCUR <= TLAST) && (SCUR <= SLAST)) && !SAMCH(TEMPL, TCUR, WSTR, 1)) {
        if (fstr::ne(
            fstr::substr(TEMPL, TCUR..=TCUR),
            fstr::substr(STRING, SCUR..=SCUR),
        ) && fstr::ne(fstr::substr(TEMPL, TCUR..=TCUR), WCHR))
        {
            MATCHW = false;
            return MATCHW;
        } else {
            TCUR = (TCUR + 1);
            SCUR = (SCUR + 1);
        }
    }

    //
    // There are a three ways we could have finished the loop above
    // without hitting a wild string.
    //
    // Case 1.  Both the string and template ran out of characters at
    // the same time without running into a wild string in the template.
    //
    if ((TCUR > TLAST) && (SCUR > SLAST)) {
        MATCHW = true;
        return MATCHW;
    }

    //
    // Case 2. The template ran out of characters while there were still
    // characters remaining in the in the string.  No match.
    //
    if ((TCUR > TLAST) && (SCUR <= SLAST)) {
        MATCHW = false;
        return MATCHW;
    }

    //
    // Case 3. The string ran out of characters while non-wild characters
    // remain in the template.
    //
    // We have to check to see if any non-wild-string characters
    // remain.  If so, we DO NOT have a match.  On the other hand if
    // only wild string characters remain we DO have a match.
    //
    if ((TCUR <= TLAST) && (SCUR > SLAST)) {
        MATCHW = true;

        {
            let m1__: i32 = TCUR;
            let m2__: i32 = TLAST;
            let m3__: i32 = 1;
            I = m1__;
            for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
                MATCHW = (MATCHW && fstr::eq(fstr::substr(TEMPL, I..=I), WSTR));
                I += m3__;
            }
        }

        return MATCHW;
    }

    //
    // OK. There is only one way that you can get to this point.
    // It must be the case that characters remain in both the template
    // (TCUR .LE. TLAST) and the string (SCUR .LE. SLAST).  Moreover,
    // to get out of the first loop you had to hit a wild string
    // character.  Find the first non-wild-string character in the
    // template. (If there isn't one, we have a match.)
    //

    while ((TCUR <= TLAST) && SAMCH(TEMPL, TCUR, WSTR, 1)) {
        TCUR = (TCUR + 1);
    }

    if (TCUR > TLAST) {
        MATCHW = true;
        return MATCHW;
    }

    //
    // Still here? Ok. We have a non-wild-string character at TCUR. Call
    // this position left and look for the right end of the maximum
    // length substring of TEMPL (starting at left) that does not have
    // a wild string character.
    //
    LEFT = TCUR;

    while ((TCUR <= TLAST) && !SAMCH(TEMPL, TCUR, WSTR, 1)) {
        TCUR = (TCUR + 1);
    }

    RIGHT = (TCUR - 1);

    while (LEFT <= TLAST) {
        //
        // First see if there is enough room left in the string to
        // match TEMPL(LEFT:RIGHT)
        //
        if ((SLAST - SCUR) < (RIGHT - LEFT)) {
            MATCHW = false;
            return MATCHW;
        }

        //
        // The substring TEMPL(LEFT:RIGHT) might be the end of the
        // string.  In such a case the ends of STRING must match
        // exactly with the end of TEMPL.
        //
        if (RIGHT == TLAST) {
            I = SLAST;
            J = TLAST;

            while (J >= LEFT) {
                if (SAMCH(TEMPL, J, WCHR, 1) || SAMCH(TEMPL, J, STRING, I)) {
                    J = (J - 1);
                    I = (I - 1);
                } else {
                    MATCHW = false;
                    return MATCHW;
                }
            }

            //
            // If we made it through the loop, we've got a match.
            //
            MATCHW = true;
            return MATCHW;
        } else {
            //
            // In this case TEMPL(LEFT:RIGHT) is in between wild string
            // characters.  Try to find a substring at or to the right
            // of SCUR in STRING that matches TEMPL(LEFT:RIGHT)
            //
            NOSUBM = true;

            while NOSUBM {
                I = SCUR;
                J = LEFT;

                while ((J <= RIGHT) && (SAMCH(STRING, I, TEMPL, J) || SAMCH(WCHR, 1, TEMPL, J))) {
                    I = (I + 1);
                    J = (J + 1);
                }

                //
                // If J made it past RIGHT, we have a substring match
                //
                if (J > RIGHT) {
                    SCUR = I;
                    NOSUBM = false;

                //
                // Otherwise, try the substring starting 1 to the right
                // of where our last try began.
                //
                } else {
                    SCUR = (SCUR + 1);

                    //
                    // Make sure there's room to even attempt a match.
                    //
                    if ((SLAST - SCUR) < (RIGHT - LEFT)) {
                        MATCHW = false;
                        return MATCHW;
                    }
                }
            }
        }

        //
        // If you have reached this point there must be something left
        // in the template and that something must begin with a wild
        // string character.  Hunt for the next substring that doesn't
        // contain a wild string character.
        //
        while ((TCUR <= TLAST) && SAMCH(TEMPL, TCUR, WSTR, 1)) {
            TCUR = (TCUR + 1);
        }

        if (TCUR > TLAST) {
            //
            // All that was left was wild string characters.  We've
            // got a match.
            //
            MATCHW = true;
            return MATCHW;
        }

        //
        // Still here? Ok. We have a non-wild-string character at TCUR.
        // Call this position left and look for the right end of the
        // maximum length substring of TEMPL (starting at left) that
        // does not have a wild string character.
        //
        LEFT = TCUR;

        while ((TCUR <= TLAST) && !SAMCH(TEMPL, TCUR, WSTR, 1)) {
            TCUR = (TCUR + 1);
        }

        RIGHT = (TCUR - 1);
    }

    MATCHW
}