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
//
// 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 a list of items
///
/// Parse a list of items separated by multiple delimiters.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// LIST I List of items delimited by DELIMS.
/// DELIMS I Single characters which 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 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.
///
/// 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.
///
/// 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 to 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 LPARSM_EX1
/// IMPLICIT NONE
///
/// C
/// C Local constants.
/// C
/// INTEGER DELMLN
/// PARAMETER ( DELMLN = 1 )
///
/// INTEGER NMAX
/// PARAMETER ( NMAX = 25 )
///
/// INTEGER STRLEN
/// PARAMETER ( STRLEN = 255 )
///
/// C
/// C Local variables.
/// C
/// CHARACTER*(DELMLN) DELIMS
/// 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 DELIMS is assigned to a space.
/// C
/// LIST = 'Run and find out.'
/// DELIMS = ' '
///
/// C
/// C Parse the items from LIST.
/// C
/// CALL LPARSM ( LIST, DELIMS, 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) Parse a character string to retrieve the items contained
/// within, when then items are separated by multiple delimiters.
///
///
/// Example code begins here.
///
///
/// PROGRAM LPARSM_EX2
/// IMPLICIT NONE
///
/// C
/// C SPICELIB functions.
/// C
/// INTEGER RTRIM
///
/// C
/// C Local constants.
/// C
/// INTEGER DELMLN
/// PARAMETER ( DELMLN = 5 )
///
/// INTEGER NMAX
/// PARAMETER ( NMAX = 25 )
///
/// INTEGER STRLEN
/// PARAMETER ( STRLEN = 255 )
///
/// C
/// C Local variables.
/// C
/// CHARACTER*(DELMLN) DELIMS
/// 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 DELIMS is assigned to a space.
/// C
/// LIST = ' 1986-187// 13:15:12.184 '
/// DELIMS = ' ,/-:'
///
/// C
/// C Parse the items from LIST.
/// C
/// CALL LPARSM ( LIST, DELIMS, NMAX, N, ITEMS )
///
/// C
/// C Output the ITEMS.
/// C
/// DO I = 1, N
///
/// WRITE(*,'(A,I3,3A)') 'Item', I, ': ''',
/// . ITEMS(I)(:RTRIM(ITEMS(I))), ''''
///
/// END DO
///
/// END
///
///
/// When this program was executed on a Mac/Intel/gfortran/64-bit
/// platform, the output was:
///
///
/// Item 1: '1986'
/// Item 2: '187'
/// Item 3: ' '
/// Item 4: '13'
/// Item 5: '15'
/// Item 6: '12.184'
/// ```
///
/// # Author and Institution
///
/// ```text
/// N.J. Bachman (JPL)
/// J. Diaz del Rio (ODC Space)
/// W.L. Taber (JPL)
/// I.M. Underwood (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.1, 13-AUG-2021 (JDR)
///
/// Edited the header to comply with NAIF standard.
/// 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)
/// ```
///
/// # 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 lparsm(list: &str, delims: &str, nmax: i32, n: &mut i32, items: CharArrayMut) {
LPARSM(list.as_bytes(), delims.as_bytes(), nmax, n, items);
}
//$Procedure LPARSM ( Parse a list of items )
pub fn LPARSM(LIST: &[u8], DELIMS: &[u8], NMAX: i32, N: &mut i32, ITEMS: CharArrayMut) {
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,
// LPARSM 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.
//
//
// 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 (intrinsics::ICHAR(fstr::substr(LIST, EOL..=EOL)) == ISPACE) {
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) && (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.
//
*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 ((intrinsics::INDEX(DELIMS, fstr::substr(LIST, EOL..=EOL)) != 0) && (*N < NMAX)) {
*N = (*N + 1);
fstr::assign(ITEMS.get_mut(*N), BLANK);
}
}
}