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::*;
/// Extract a substring starting with a keyword
///
/// Locate a keyword in a string and extract the substring from
/// the beginning of the first word following the keyword to the
/// beginning of the first subsequent recognized terminator of a list.
///
/// # Brief I/O
///
/// ```text
/// VARIABLE I/O DESCRIPTION
/// -------- --- --------------------------------------------------
/// KEYWD I Word that marks the beginning of text of interest.
/// TERMS I Set of words, any of which marks the end of text.
/// NTERMS I Number of TERMS.
/// WORDSQ I-O String containing a sequence of words.
/// FOUND O .TRUE. if the keyword is found in the string.
/// SUBSTR O String from end of KEYWD to beginning of first
/// TERMS item found.
/// ```
///
/// # Detailed Input
///
/// ```text
/// KEYWD is a word used to mark the start of text of interest.
///
/// TERMS is a set of words, any one of which may signal the end of
/// text of interest.
///
/// NTERMS is the number of TERMS.
///
/// WORDSQ is a character string made up of words, that may contain
/// the keyword in KEYWD.
/// ```
///
/// # Detailed Output
///
/// ```text
/// WORDSQ is the input string stripped of all words from the
/// beginning of the keyword KEYWD to the end of the last
/// word preceding one of the words in TERMS (or the end of
/// the string if none of the TERMS follows KEYWD in the
/// string).
///
/// FOUND is .TRUE. if KEYWD is present in the input WORDSQ.
///
/// SUBSTR is the substring that begins with the first word
/// following KEYWD up to the beginning of any of the words
/// in TERM or the end of the string.
/// ```
///
/// # Exceptions
///
/// ```text
/// Error free.
/// ```
///
/// # Particulars
///
/// ```text
/// Definitions:
///
/// A WORD is a set of consecutive non-blank characters
/// delimited by blanks or either end of the string
/// that contains them.
///
/// Given a string and a keyword this routine locates the first
/// occurrence of the keyword in the string and returns the
/// substring between the end of the keyword and the first occurrence
/// of any of the words in a list of terminating words. If none
/// of the terminating words follows the keyword in the string,
/// the routine returns all of the string following the keyword.
///
/// If the next word following the keyword is a terminating word,
/// the substring returned will be a blank.
///
/// If the keyword can not be located in the string, the variable
/// FOUND will be returned as .FALSE. and the input string will be
/// unchanged. The substring will be returned as a blank.
///
/// In all other cases, the part of the input string from the
/// beginning of the keyword to the start of the first terminating
/// word will be removed. If no terminating word follows the keyword
/// the portion of the string from the keyword to the last non-blank
/// character of the string will be removed.
/// ```
///
/// # Examples
///
/// ```text
/// Example 1.
/// ----------
/// Input: WORDSQ 'FROM 1 October 1984 12:00:00 TO 1 January 1987'
/// KEYWD 'TO'
/// TERMS 'FROM'
/// 'TO'
/// 'BEGINNING'
/// 'ENDING'
///
/// Output: WORDSQ 'FROM 1 October 1984 12:00:00 '
/// FOUND .TRUE.
/// SUBSTR '1 January 1987'
///
///
/// Example 2.
/// ----------
/// Input: WORDSQ 'FROM 1 October 1984 12:00:00 TO 1 January 1987'
/// KEYWD 'FROM'
/// TERMS 'FROM'
/// 'TO'
/// 'BEGINNING'
/// 'ENDING'
///
/// Output: WORDSQ ' TO 1 January 1987'
/// FOUND .TRUE.
/// SUBSTR '1 October 1984 12:00:00'
///
///
/// Example 3.
/// ----------
/// Input: WORDSQ 'ADDRESS: 4800 OAK GROVE DRIVE PHONE: 354-4321 '
/// KEYWD 'ADDRESS:'
/// TERMS 'ADDRESS:'
/// 'PHONE:'
/// 'NAME:'
///
/// Output: WORDSQ ' PHONE: 354-4321 '
/// FOUND .TRUE.
/// SUBSTR '4800 OAK GROVE DRIVE'
///
///
/// Example 4.
/// ----------
/// Input: WORDSQ 'ADDRESS: 4800 OAK GROVE DRIVE PHONE: 354-4321 '
/// KEYWD 'NAME:'
/// TERMS 'ADDRESS:'
/// 'PHONE:'
/// 'NAME:'
///
/// Output: WORDSQ 'ADDRESS: 4800 OAK GROVE DRIVE PHONE: 354-4321 '
/// FOUND .FALSE.
/// SUBSTR ' '
/// ```
///
/// # Restrictions
///
/// ```text
/// 1) It is the user's responsibility to make sure there is adequate
/// room in SUBSTR to contain the substring.
///
/// 2) SUBSTR cannot overwrite WORDSQ.
/// ```
///
/// # Author and Institution
///
/// ```text
/// J. Diaz del Rio (ODC Space)
/// H.A. Neilan (JPL)
/// W.L. Taber (JPL)
/// ```
///
/// # Version
///
/// ```text
/// - SPICELIB Version 1.1.0, 17-JUN-2021 (JDR)
///
/// Added IMPLICIT NONE statement. Changed the input argument
/// STRING to WORDSQ for consistency with other routines.
///
/// 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 (WLT) (HAN)
/// ```
///
/// # Revisions
///
/// ```text
/// - Beta Version 1.1.0, 28-FEB-1989 (WLT)
///
/// Reference to REMSUB replaced by SHIFTL.
///
/// - Beta Version 1.0.1, 10-FEB-1989 (HAN)
///
/// Contents of the $Exceptions section was changed
/// to "error free" to reflect the decision that the
/// module will never participate in error handling.
/// ```
pub fn kxtrct(
keywd: &str,
terms: CharArray,
nterms: i32,
wordsq: &mut str,
found: &mut bool,
substr: &mut str,
) {
KXTRCT(
keywd.as_bytes(),
terms,
nterms,
fstr::StrBytes::new(wordsq).as_mut(),
found,
fstr::StrBytes::new(substr).as_mut(),
);
}
//$Procedure KXTRCT ( Extract a substring starting with a keyword )
pub fn KXTRCT(
KEYWD: &[u8],
TERMS: CharArray,
NTERMS: i32,
WORDSQ: &mut [u8],
FOUND: &mut bool,
SUBSTR: &mut [u8],
) {
let TERMS = DummyCharArray::new(TERMS, None, 1..);
let mut POSITN: i32 = 0;
let mut BERASE: i32 = 0;
let mut EERASE: i32 = 0;
let mut BEGSTR: i32 = 0;
let mut ENDSTR: i32 = 0;
let mut START: i32 = 0;
let mut B: i32 = 0;
let mut E: i32 = 0;
let mut DELIMS: i32 = 0;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Locate the keyword within the string.
//
POSITN = WDINDX(WORDSQ, KEYWD);
//
// If the keyword wasn't found, set the outputs and head for home.
//
if (POSITN == 0) {
*FOUND = false;
fstr::assign(SUBSTR, b" ");
return;
} else {
*FOUND = true;
}
//
// Set the begin erase marker to the start of the current word
// Set the end erase marker to the end of the current word
//
BERASE = POSITN;
EERASE = ((POSITN + NBLEN(KEYWD)) - 1);
START = (EERASE + 1);
//
// Find the begin and end of the next word.
//
FNDNWD(WORDSQ, START, &mut B, &mut E);
//
// If there is a next word ( E came back non-zero ) see if its a
// terminator.
//
if (E != 0) {
DELIMS = ISRCHC(fstr::substr(WORDSQ, B..=E), NTERMS, TERMS.as_arg());
}
//
// If we found a terminator, or were already at the end of the
// string, we are done. Remove the keyword and put a blank in
// SUBSTR
//
if ((E == 0) || (DELIMS != 0)) {
SHIFTL(
&fstr::substr(WORDSQ, BERASE..).to_vec(),
((EERASE - BERASE) + 1),
b" ",
fstr::substr_mut(WORDSQ, BERASE..),
);
fstr::assign(SUBSTR, b" ");
return;
}
//
// Ok. If we made it this far, we have at least one legitimate word
// following the keyword, set the pointer for the start of the
// substring (to return) to the beginning of this word.
//
BEGSTR = B;
//
// Now we just examine each word until we run out of string or we
// run into a terminator.
//
while ((E != 0) && (DELIMS == 0)) {
ENDSTR = E;
EERASE = E;
START = (E + 1);
FNDNWD(WORDSQ, START, &mut B, &mut E);
if (E != 0) {
DELIMS = ISRCHC(fstr::substr(WORDSQ, B..=E), NTERMS, TERMS.as_arg());
}
}
//
// That's it, load the substring variable and remove the keyword
// and words up to the terminator or end of the string --- whichever
// came first.
//
fstr::assign(SUBSTR, fstr::substr(WORDSQ, BEGSTR..=ENDSTR));
SHIFTL(
&fstr::substr(WORDSQ, BERASE..).to_vec(),
((EERASE - BERASE) + 1),
b" ",
fstr::substr_mut(WORDSQ, BERASE..),
);
}