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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
//
// GENERATED FILE
//

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

/// Scan a character string
///
/// This routine serves as an umbrella routine for routines
/// that are used to scan a string for recognized and unrecognized
/// substrings.
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  STRING     I   a string to be scanned.
///  ROOM       I   space available for located substrings.
///  NMARKS    I-O  number of recognizable substrings.
///  MARKS     I-O  recognizable substrings.
///  MRKLEN    I-O  an auxiliary array describing MARKS.
///  PNTERS    I-O  an auxiliary array describing MARKS.
///  START     I-O  position from which to commence/resume scanning.
///  NTOKNS     O   number of scanned substrings.
///  BEG        O   beginnings of scanned substrings.
///  END        O   endings of scanned substrings.
///  IDENT      O   position of scanned substring within array MARKS.
/// ```
///
/// # Detailed Input
///
/// ```text
///  STRING   is any character string that is to be scanned
///           to locate recognized and unrecognized substrings.
///
///  ROOM     is the amount of space available for storing the
///           results of scanning the string.
///
///  NMARKS   is the number of marks that will be
///           recognized substrings of STRING.
///
///  MARKS    is an array of marks that will be recognized
///           by the scanning routine. The array must be
///           processed by a call to SCANPR before it can
///           be used by SCAN. Further details are given
///           in documentation for the individual entry points.
///
///  MRKLEN   is an auxiliary array populated by SCANPR
///           for use by SCAN. It should be declared with
///           length equal to the length of MARKS.
///
///  PNTERS   is an auxiliary array populated by SCANPR for
///           use by SCAN. It should be declared in the
///           calling program as
///
///              INTEGER  PNTERS ( RCHARS )
///
///           RCHARS is given by the expression
///
///             MAX - MIN + 5
///
///           where
///
///           MAX is the maximum value of ICHAR(MARKS(I)(1:1))
///               over the range I = 1, NMARKS
///
///           MIN is the minimum value of ICHAR(MARKS(I)(1:1))
///               over the range I = 1, NMARKS
///
///            Further details are provided in the entry point
///            SCANPR.
///
///  START    is the position in the STRING from which scanning
///           should commence.
/// ```
///
/// # Detailed Output
///
/// ```text
///  NMARKS   is the number of marks in the array MARKS after it
///           has been prepared for SCANPR.
///
///  MARKS    is an array of recognizable substrings that has
///           been prepared for SCAN by SCANPR. Note that MARKS
///           will be sorted in increasing order.
///
///  MRKLEN   is an auxiliary array, populated by SCANPR for
///           use by SCAN.
///
///  PNTERS   is an auxiliary array, populated by a call to
///           SCANPR and is intended for use by SCAN.
///
///  START    is the position from which scanning should continue
///           in order to fully scan STRING (if sufficient memory was
///           not provided in BEG, END, and IDENT on the current
///           call to SCAN).
///
///  NTOKNS   is the number of substrings identified in the current
///           scan of STRING.
///
///  BEG      beginnings of scanned substrings.
///           This should be declared so that it is at least
///           as large as ROOM.
///
///  END      endings of scanned substrings.
///           This should be declared so that it is at least
///           as large as ROOM.
///
///  IDENT    positions of scanned substring within array MARKS.
///           If the substring STRING(BEG(I):END(I)) is not in the
///           list of MARKS then IDENT(I) will have the value 0.
///           This should be declared so that it is at least
///           as large as ROOM.
/// ```
///
/// # Exceptions
///
/// ```text
///  1)  If this routine is called directly, the error
///      SPICE(BOGUSENTRY) is signaled.
/// ```
///
/// # Particulars
///
/// ```text
///  This routine serves as an umbrella routine for the two entry
///  points SCANPR and SCAN. It can be used to locate keywords
///  or delimited substrings within a string.
///
///  The process of breaking a string into those substrings that
///  have recognizable meaning, is called "scanning." The substrings
///  identified by the scanning process are called "tokens."
///
///  Scanning has many applications including:
///
///  -- the parsing of algebraic expressions
///
///  -- parsing calendar dates
///
///  -- processing text with embedded directions for displaying
///     the text.
///
///  -- interpretation of command languages
///
///  -- compilation of programming languages
///
///  This routine simplifies the process of scanning a string for
///  its tokens.
/// ```
///
/// # Examples
///
/// ```text
///  Example 1.
///  ----------
///
///  Suppose you need to identify all of the words within a string
///  and wish to ignore punctuation marks such as ',', ':', ';', ' ',
///  '---'.
///
///  The first step is to load the array of marks as shown here:
///
///     The minimum ASCII code for the first character of a marker is
///     32 ( for ' ').
///
///     INTEGER               FCHAR
///     PARAMETER           ( FCHAR = 32 )
///
///     The maximum ASCII code for the first character of a marker is
///     59 (for ';' )
///
///     INTEGER               LCHAR
///     PARAMETER           ( LCHAR = 59 )
///
///     INTEGER               RCHAR
///     PARAMETER           ( RCHAR = LCHAR - FCHAR + 5 )
///
///     LOGICAL               FIRST
///     CHARACTER*(3)         MARKS
///     INTEGER               NMARKS ( 5     )
///     INTEGER               MRKLEN ( 5     )
///     INTEGER               PNTERS ( RCHAR )
///
///     INTEGER               ROOM
///     PARAMETER           ( ROOM = 50 )
///
///     INTEGER               BEG    ( ROOM  )
///     INTEGER               END    ( ROOM  )
///     INTEGER               IDENT  ( ROOM  )
///
///     SAVE                  FIRST
///     SAVE                  MARKS
///     SAVE                  MRKLEN
///     SAVE                  PNTERS
///
///     IF ( FIRST ) THEN
///
///        FIRST    = .FALSE.
///
///        MARKS(1) = ' '
///        MARKS(2) = '---'
///        MARKS(3) = ':'
///        MARKS(4) = ','
///        MARKS(5) = ';'
///
///        NMARKS   = 5
///
///        CALL SCANPR ( NMARKS, MARKS, MRKLEN, PNTERS )
///
///     END IF
///
///  Notice that the call to SCANPR is nested inside an
///  IF ( FIRST ) THEN ... END IF block. In this and many applications
///  the marks that will be used in the scan are fixed. Since the
///  marks are not changing, you need to process MARKS and set up
///  the auxiliary arrays MRKLEN and PNTERS only once (assuming that
///  you SAVE the appropriate variables as has been done above).
///  In this way if the code is executed many times, there is only
///  a small overhead required for preparing the data so that it
///  can be used efficiently in scanning.
///
///  To identify the substrings that represent words we scan the
///  string using the prepared MARKS, MRKLEN and PNTERS.
///
///     CALL SCAN ( STRING, MARKS,  MRKLEN, PNTERS, ROOM,
///    .            START,  NTOKNS, IDENT,  BEG,    END   )
///
///  To isolate only the words of the string, we examine the
///  array IDENT and keep only those Begin and Ends for which
///  the corresponding identity is non-positive.
///
///     KEPT = 0
///
///     DO I = 1, NTOKNS
///
///        IF ( IDENT(I) .LE. 0 ) THEN
///
///           KEPT      = KEPT + 1
///           BEG(KEPT) = BEG(I)
///           END(KEPT) = END(I)
///
///        END IF
///
///     END DO
///
///
///  Example 2.
///  ----------
///
///  To parse an algebraic expression such as
///
///     ( X + Y ) * ( 2*Z + SIN(W) ) ** 2
///
///  You would select '**', '*', '+', '-', '(', ')' and ' '
///  to be the markers. Note that all of these begin with one
///  of the characters in the string ' !"#$%&''()*+,-./'
///  so that we can declare PNTERS to have length 20.
///
///  Prepare the MARKS, MRKLEN, and PNTERS.
///
///     LOGICAL               FIRST
///     CHARACTER*(4)         MARKS
///     INTEGER               NMARKS ( 8  )
///     INTEGER               MRKLEN ( 8  )
///     INTEGER               PNTERS ( 20 )
///
///     SAVE                  FIRST
///     SAVE                  MARKS
///     SAVE                  MRKLEN
///     SAVE                  PNTERS
///
///     IF ( FIRST ) THEN
///
///        MARKS(1) = '('
///        MARKS(2) = ')'
///        MARKS(3) = '+'
///        MARKS(4) = '-'
///        MARKS(5) = '*'
///        MARKS(6) = '/'
///        MARKS(7) = '**'
///        MARKS(8) = ' '
///
///        NMARKS   = 8
///
///        CALL SCANPR ( NMARKS, MARKS, MRKLEN, PNTERS )
///
///        Locate the blank character in MARKS once it has
///        been prepared.
///
///        BLANK = BSRCHC ( ' ', NMARKS, MARKS )
///
///     END IF
///
///
///  Once all of the initializations are out of the way,
///  we can scan an input string.
///
///     CALL SCAN ( STRING, MARKS,  MRKLEN, PNTERS, ROOM,
///    .            START,  NTOKNS, IDENT,  BEG,    END   )
///
///
///  Next eliminate any white space that was returned in the
///  list of tokens.
///
///  KEPT = 0
///
///  DO I = 1, NTOKNS
///
///     IF ( IDENT(I) .NE. BLANK ) THEN
///        KEPT        = KEPT + 1
///        BEG  (KEPT) = BEG   (I)
///        END  (KEPT) = END   (I)
///        IDENT(KEPT) = IDENT (I)
///     END IF
///
///  END DO
///
///  Now all of the substrings remaining point to grouping symbols,
///  operators, functions, or variables. Given that the individual
///  "words" of the expression are now in hand, the meaning of the
///  expression is much easier to determine.
///
///  The rest of the routine is left as a non-trivial exercise
///  for the reader.
/// ```
///
/// # Restrictions
///
/// ```text
///  1)  The array of MARKS, MRKLEN, and PNTERS must be properly
///      formatted prior to calling SCAN. This is accomplished by
///      calling SCANPR.
/// ```
///
/// # Author and Institution
///
/// ```text
///  J. Diaz del Rio    (ODC Space)
///  W.L. Taber         (JPL)
/// ```
///
/// # Version
///
/// ```text
/// -    SPICELIB Version 1.1.0, 26-OCT-2021 (JDR)
///
///         Added IMPLICIT NONE statement.
///
///         Edited the header to comply with NAIF standard.
///
/// -    SPICELIB Version 1.0.0, 26-JUL-1996 (WLT)
/// ```
pub fn scanit(
    ctx: &mut SpiceContext,
    string: &str,
    start: i32,
    room: i32,
    nmarks: i32,
    marks: CharArray,
    mrklen: &[i32],
    pnters: &[i32],
    ntokns: i32,
    ident: &[i32],
    beg: &[i32],
    end: &[i32],
) -> crate::Result<()> {
    SCANIT(
        string.as_bytes(),
        start,
        room,
        nmarks,
        marks,
        mrklen,
        pnters,
        ntokns,
        ident,
        beg,
        end,
        ctx.raw_context(),
    )?;
    ctx.handle_errors()?;
    Ok(())
}

//$Procedure SCANIT ( Scan a character string )
pub fn SCANIT(
    STRING: &[u8],
    START: i32,
    ROOM: i32,
    NMARKS: i32,
    MARKS: CharArray,
    MRKLEN: &[i32],
    PNTERS: &[i32],
    NTOKNS: i32,
    IDENT: &[i32],
    BEG: &[i32],
    END: &[i32],
    ctx: &mut Context,
) -> f2rust_std::Result<()> {
    //
    // SPICELIB functions
    //

    //
    // Local variables
    //

    if !RETURN(ctx) {
        CHKIN(b"SCANIT", ctx)?;
        SETMSG(b"Your program has referenced the umbrella subroutine SCANIT.  This may indicate a programming error.", ctx);
        SIGERR(b"SPICE(BOGUSENTRY)", ctx)?;
        CHKOUT(b"SCANIT", ctx)?;
    }

    Ok(())
}

/// Scanning preparation
///
/// Prepare recognized markers and auxiliary arrays for the
/// routine SCAN.
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  NMARKS    I-O  Number of recognizable substrings.
///  MARKS     I-O  Recognizable substrings.
///  MRKLEN     O   auxiliary array describing MARKS.
///  PNTERS     O   auxiliary array describing MARKS.
/// ```
///
/// # Detailed Input
///
/// ```text
///  NMARKS   is the number of recognized marks that will be
///           recognized substrings of STRING.
///
///  MARKS    is an array of marks that will be recognized
///           by the scanning routine. Leading and trailing
///           blanks are not significant. (Except for the
///           blank character ' ', itself.  After all, some
///           part of it must be significant.)  Case of the
///           entries in MARKS is significant. The MARKS
///           'XX' and 'xx' are regarded as different MARKS.
/// ```
///
/// # Detailed Output
///
/// ```text
///  NMARKS   is the number of marks in the array MARKS after it
///           has been prepared for SCAN.
///
///  MARKS    is an array of recognizable substrings.
///           It has been prepared for use by SCAN
///           so as to be compatible with the other arrays.
///           It will be sorted in ascending order, left
///           justified and contain no duplicate entries.
///
///  MRKLEN   is an auxiliary array populated by SCANPR
///           for use by SCAN that describes MARKS.
///
///  PNTERS   is an auxiliary array populated by SCANPR for
///           use by SCAN. It should be declared in the
///           calling program as
///
///              INTEGER   PNTERS ( RCHARS )
///
///           RCHARS is given by the expression
///
///             MAX - MIN + 5
///
///           where
///
///           MAX is the maximum value of ICHAR(MARKS(I)(1:1))
///               over the range I = 1, NMARKS
///
///           MIN is the minimum value of ICHAR(MARKS(I)(1:1))
///               over the range I = 1, NMARKS
///
///           Here are some typical values that may help you avoid
///           going through the computations above. (This assumes
///           that ICHAR returns the ASCII code for a character.)
///
///           Scanning Situation           RCHAR
///           ------------------          -------------------
///           If NMARKS = 1
///           or all MARKS                   5
///           begin with the same
///           character.
///
///           All MARKS begin with
///           one of the characters          20
///           in the string
///           ' !"#$%&''()*+,-./'
///
///           All MARKS begin with
///           one of the characters          11
///           in the string
///           ':;<=>?@'
///
///           All MARKS begin with
///           one of the characters          37
///           in the string
///           ' !"#$%&''()*+,-./:;<=>?@'
///
///           All MARKS begin with
///           an upper case English letter   30
///
///           All MARKS begin with a
///           decimal digit                  14
///
///           All Marks begin with a
///           lower case English letter      30
///
///           All Marks begin with
///           a digit or upper case          47
///           character.
///
///           All Marks begin with a
///           printing character or          100
///           a blank.
///
///           Anything might be a mark       132
///
///           Finally, so you won't have to look it up elsewhere
///           here are the ASCII codes for the printing
///           characters and blanks.
///
///           (Common Punctuations) Character     ASCII Code
///                                 -----------   ----------
///                                 ' ' (space)     32
///                                 '!'             33
///                                 '"'             34
///                                 '#'             35
///                                 '$'             36
///                                 '%'             37
///                                 '&'             38
///                                 ''''            39
///                                 '('             40
///                                 ')'             41
///                                 '*'             42
///                                 '+'             43
///                                 ','             44
///                                 '-'             45
///                                 '.'             46
///                                 '/'             47
///
///
///           (Decimal Digits)      Character     ASCII Code
///                                 -----------   ----------
///                                 '0'             48
///                                 '1'             49
///                                 '2'             50
///                                 '3'             51
///                                 '4'             52
///                                 '5'             53
///                                 '6'             54
///                                 '7'             55
///                                 '8'             56
///                                 '9'             57
///
///           (More punctuation)    Character     ASCII Code
///                                 -----------   ----------
///                                 ':'             58
///                                 ';'             59
///                                 '<'             60
///                                 '='             61
///                                 '>'             62
///                                 '?'             63
///                                 '@'             64
///
///           (Uppercase characters)  Character     ASCII Code
///                                 -----------   ----------
///                                 'A'             65
///                                 'B'             66
///                                 'C'             67
///                                 'D'             68
///                                 'E'             69
///                                 'F'             70
///                                 'G'             71
///                                 'H'             72
///                                 'I'             73
///                                 'J'             74
///                                 'K'             75
///                                 'L'             76
///                                 'M'             77
///                                 'N'             78
///                                 'O'             79
///                                 'P'             80
///                                 'Q'             81
///                                 'R'             82
///                                 'S'             83
///                                 'T'             84
///                                 'U'             85
///                                 'V'             86
///                                 'W'             87
///                                 'X'             88
///                                 'Y'             89
///                                 'Z'             90
///
///           (More punctuation)    Character     ASCII Code
///                                 -----------   ----------
///                                 '['             91
///                                 '\'             92
///                                 ']'             93
///                                 '^'             94
///                                 '_'             95
///                                 '`'             96
///
///           (Lowercase characters)  Character     ASCII Code
///                                 -----------   ----------
///                                 'a'             97
///                                 'b'             98
///                                 'c'             99
///                                 'd'            100
///                                 'e'            101
///                                 'f'            102
///                                 'g'            103
///                                 'h'            104
///                                 'i'            105
///                                 'j'            106
///                                 'k'            107
///                                 'l'            108
///                                 'm'            109
///                                 'n'            110
///                                 'o'            111
///                                 'p'            112
///                                 'q'            113
///                                 'r'            114
///                                 's'            115
///                                 't'            116
///                                 'u'            117
///                                 'v'            118
///                                 'w'            119
///                                 'x'            120
///                                 'y'            121
///                                 'z'            122
///
///           (More punctuation)      Character     ASCII Code
///                                 -----------   ----------
///                                 '{'            123
///                                 '|'            124
///                                 '}'            125
///                                 '~'            126
/// ```
///
/// # Exceptions
///
/// ```text
///  Error free.
///
///  1)  A space is regarded as a special mark. If MARKS(I) = ' ',
///      then MARKS(I) will match any consecutive sequence of blanks.
///
///  2)  If NMARKS is less than or equal to zero, SCAN will always
///      find a single token, namely the entire string to be scanned.
/// ```
///
/// # Particulars
///
/// ```text
///  This routine prepares the arrays MARKS, MRKLEN and PNTERS
///  so that they are suitable for input to the routine SCAN.
///
///  It is expected that users will need to scan many strings
///  and that from the programming point of view it is
///  easiest to simply supply a list of MARKS to a "formatting"
///  routine such as this so that the strings can then
///  be efficiently scanned by the routine SCAN. This formatting
///  is the function of this routine.
/// ```
///
/// # Examples
///
/// ```text
///  Suppose you need to identify all of the words within a string
///  and wish to ignore punctuation marks such as ' ', ',', ':', ';'
///  '---'.  Then the first step is to load the array of marks as
///  shown here:
///
///     The minimum ASCII code for the first character of a marker is
///     32 (for ' ').
///
///     INTEGER               FCHAR
///     PARAMETER           ( FCHAR = 32 )
///
///     The maximum ASCII code for the first character of a marker is
///     59 (for ';').
///
///     INTEGER               LCHAR
///     PARAMETER           ( LCHAR = 59 )
///
///
///     The proper size to declare PNTERS is given by the parameter
///     RCHAR defined in terms of LCHAR and FCHAR.
///
///     INTEGER               RCHAR
///     PARAMETER           ( RCHAR = LCHAR - FCHAR + 5 )
///
///     LOGICAL               FIRST
///     CHARACTER*(4)         MARKS
///     INTEGER               NMARKS ( 5     )
///     INTEGER               MRKLEN ( 5     )
///     INTEGER               PNTERS ( RCHAR )
///
///     SAVE                  FIRST
///     SAVE                  MARKS
///     SAVE                  MRKLEN
///     SAVE                  PNTERS
///
///     IF ( FIRST ) THEN
///
///        FIRST    = .FALSE.
///
///        MARKS(1) = ' '
///        MARKS(2) = '---'
///        MARKS(3) = ':'
///        MARKS(4) = ','
///        MARKS(5) = ';'
///
///        NMARKS   = 5
///
///        CALL SCANPR ( NMARKS, MARKS, MRKLEN, PNTERS )
///
///     END IF
///
///  Notice that the call to SCANPR is nested inside an
///  IF ( FIRST ) THEN ... END IF block. In this and many applications
///  the marks that will used in the scan are fixed. Since the marks
///  are not changing, you need to process MARKS and set up
///  the auxiliary arrays MRKLEN and PNTERS only once (assuming that
///  you SAVE the appropriate variables as has been done above).
///  In this way if the code is executed many times, there is only
///  a small overhead required for preparing the data so that it
///  can be used efficiently in scanning.
/// ```
///
/// # Restrictions
///
/// ```text
///  1)  MRKLEN and PNTERS must be declared to be at least as large
///      as indicated above. If not, this routine will write
///      past the ends of these arrays. Much unpleasantness may
///      ensue in the attempt to debug such problems.
/// ```
///
/// # Author and Institution
///
/// ```text
///  J. Diaz del Rio    (ODC Space)
///  W.L. Taber         (JPL)
/// ```
///
/// # Version
///
/// ```text
/// -    SPICELIB Version 1.1.0, 26-OCT-2021 (JDR)
///
///         Added IMPLICIT NONE statement.
///
///         Edited the header to comply with NAIF standard.
///
/// -    SPICELIB Version 1.0.0, 26-JUL-1996 (WLT)
/// ```
pub fn scanpr(nmarks: &mut i32, marks: CharArrayMut, mrklen: &mut [i32], pnters: &mut [i32]) {
    SCANPR(nmarks, marks, mrklen, pnters);
}

//$Procedure SCANPR ( Scanning preparation )
pub fn SCANPR(NMARKS: &mut i32, MARKS: CharArrayMut, MRKLEN: &mut [i32], PNTERS: &mut [i32]) {
    let mut MARKS = DummyCharArrayMut::new(MARKS, None, 1..);
    let mut MRKLEN = DummyArrayMut::new(MRKLEN, 1..);
    let mut PNTERS = DummyArrayMut::new(PNTERS, 1..);
    let mut EBLOCK: i32 = 0;
    let mut FCHAR: i32 = 0;
    let mut JUMP: i32 = 0;
    let mut LAST1: i32 = 0;
    let mut LCHAR: i32 = 0;
    let mut N: i32 = 0;
    let mut SLOT: i32 = 0;
    let mut THIS1: i32 = 0;

    //
    // We handle the case where NMARKS is non-positive separately.
    //
    if (*NMARKS <= 0) {
        PNTERS[1] = 0;
        PNTERS[2] = 0;
        PNTERS[3] = 0;
        PNTERS[4] = 0;
        PNTERS[5] = 0;

        return;
    }
    //
    // First left justify MARKS and remove duplicates.
    //
    for I in 1..=*NMARKS {
        LJUST(&MARKS[I].to_vec(), &mut MARKS[I]);
    }

    N = *NMARKS;

    //
    // Sort and remove duplicates from the array MARKS.
    //
    RMDUPC(&mut N, MARKS.as_arg_mut());

    //
    // All of the MARKS have the same declared length.
    // However, since all of your marks may not have
    // the same intended length (for example '*' and
    // '**') it is desirable to be able to specify
    // how much of MARKS(I) should actually be used
    // when examining STRING for a substring match.
    // This is done with the array MRKLEN.
    // MARKS(I)(1:MRKLEN(I)) will be used when
    // scanning STRING.
    //
    // Here is the expected structure of PNTERS.
    //
    //         PNTERS(1) = MIN ( ICHAR(MARKS(I)(1:1)  ), I=1,NMARKS )
    //         PNTERS(2) = MAX ( ICHAR(MARKS(I)(1:1)  ), I=1,NMARKS )
    //
    // For ease of further discussion let
    // MYCHAR(I) represent the characters from PNTERS(1)
    // to PNTERS(2), and assume that legitimate values of
    // I are from 1 to M.
    //
    //         PNTERS(3)   = 0
    //         PNTERS(4)   = index of the last entry of MARKS
    //                       that begins with the character
    //                       MYCHAR(1).
    //
    //         PNTERS(5)   = index of the last entry of MARKS
    //                       that begins with the character
    //                       MYCHAR(2), if there is no such element
    //                       of MARKS let PNTERS(5) = PNTERS(4)
    //            .
    //            .
    //            .
    //
    //         PNTERS(3+K) = index of the last entry of MARKS
    //                       that begins with the character
    //                       MYCHAR(K), if there is no such element
    //                       of MARKS, let PNTERS(3+K) =
    //                       PNTERS(3+K-1)
    //            .
    //            .
    //            .
    //
    //         PNTERS(3+M) = index of the last entry of MARKS
    //                       that begins with the character
    //                       MYCHAR(M).
    //
    //         PNTERS(4+M) = PNTERS(3+M)
    //
    //

    //
    // Next determine the minimum and maximum ASCII values
    // of the first characters of the MARKS.
    //
    FCHAR = intrinsics::ICHAR(fstr::substr(&MARKS[1], 1..=1));
    LCHAR = intrinsics::ICHAR(fstr::substr(&MARKS[N], 1..=1));

    PNTERS[1] = FCHAR;
    PNTERS[2] = LCHAR;

    //
    // For the purposes of getting started, we will say the last
    // character that started a MARK was one before FCHAR.  We
    // will record the end of its block in slot 3 of PNTERS.
    //
    LAST1 = (FCHAR - 1);
    SLOT = 3;

    for I in 1..=N {
        MRKLEN[I] = RTRIM(&MARKS[I]);
        THIS1 = intrinsics::ICHAR(fstr::substr(&MARKS[I], 1..=1));

        if (THIS1 != LAST1) {
            //
            // We need to record the address of the end of the last
            // block of MARKS that began with the same character.
            // This is of course one before the current value of I.
            //
            // While we are at it, we might as well determine how
            // many possible first letters were "jumped" over in
            // going from the last first character to the current
            // first character.
            //
            EBLOCK = (I - 1);
            JUMP = (THIS1 - LAST1);

            //
            // The end of the block for all of the MARKS having
            // first character between the last one and this one
            // is the same.
            //
            for J in SLOT..=((SLOT + JUMP) - 1) {
                PNTERS[J] = EBLOCK;
            }

            SLOT = (SLOT + JUMP);
            LAST1 = THIS1;
        }
    }

    PNTERS[SLOT] = N;
    PNTERS[(SLOT + 1)] = N;
    *NMARKS = N;
}

/// Scan a string for tokens
///
/// Scan a string and return the beginnings and ends of recognized
/// and unrecognized substrings. The full collection of these
/// substrings partitions the string.
///
/// # Brief I/O
///
/// ```text
///  VARIABLE  I/O  DESCRIPTION
///  --------  ---  --------------------------------------------------
///  STRING     I   string to be scanned.
///  MARKS      I   recognizable substrings.
///  MRKLEN     I   an auxiliary array describing MARKS.
///  PNTERS     I   an auxiliary array describing MARKS.
///  ROOM       I   space available for storing substring descriptions.
///  START     I-O  position from which to begin/resume scanning.
///  NTOKNS     O   number of scanned substrings.
///  BEG        O   beginnings of scanned substrings.
///  END        O   endings of scanned substrings.
///  IDENT      O   position of scanned substring within array MARKS.
/// ```
///
/// # Detailed Input
///
/// ```text
///  STRING   is any character string that is to be scanned
///           to locate recognized and unrecognized substrings.
///
///  MARKS    is an array of marks that will be recognized
///           by the scanning routine. This array must be prepared
///           by calling the routine SCANPR.
///
///           Note that the blank string is interpreted
///           in a special way by SCAN. If the blank character,
///           ' ', is one of the MARKS, it will match any unbroken
///           sequence of blanks in string.  Thus if ' ' is the only
///           marks supplied and STRING is
///
///              'A   lot of      space '
///               ......................
///
///           Then scan will locate the following substrings
///
///           'A'          STRING(1:1)    (unrecognized)
///           '   '        STRING(2:4)    (recognized --- all blanks)
///           'lot'        STRING(5:7)    (unrecognized)
///           ' '          STRING(8:8)    (recognized --- a blank)
///           'of'         STRING(9:10)   (unrecognized)
///           '      '     STRING(11:16)  (recognized --- all blanks)
///           'space'      STRING(17:21)  (unrecognized)
///           ' '          STRING(22:22)  (recognized --- a blank)
///
///  MRKLEN   is an auxiliary array populated by SCANPR
///           for use by SCAN. It should be declared with
///           length equal to the length of MARKS. It must
///           be prepared for use by the routine SCANPR.
///
///  PNTERS   is a specially structured array of integers that
///           describes the array MARKS. It is must be filled
///           in by the routine SCANPR. It should be declared
///           by the calling program as shown here:
///
///              INTEGER  PNTERS ( RCHARS )
///
///           RCHARS is given by the expression
///
///             MAX - MIN + 5
///
///           where
///
///           MAX is the maximum value of ICHAR(MARKS(I)(1:1))
///               over the range I = 1, NMARKS
///
///           MIN is the minimum value of ICHAR(MARKS(I)(1:1))
///               over the range I = 1, NMARKS
///
///           See SCANPR for a more detailed description of the
///           declaration of PNTERS.
///
///  ROOM     is the amount of space available for storing the
///           results of scanning the string.
///
///  START    is the position from which scanning should commence.
///           Values of START less than 1 are treated as 1.
/// ```
///
/// # Detailed Output
///
/// ```text
///  START    is the position from which scanning should continue
///           in order to fully scan STRING (if sufficient memory was
///           not provided in BEG, END, and IDENT on the current
///           call to SCAN).
///
///  NTOKNS   is the number of substrings identified in the current
///           scan of STRING.
///
///  BEG      beginnings of scanned substrings. This should be
///           declared so that it is at least as large as ROOM.
///
///  END      endings of scanned substrings. This should be declared
///           so that it is at least as large as ROOM.
///
///  IDENT    positions of scanned substring within array MARKS.
///           If the substring STRING(BEG(I):END(I)) is in the array
///           MARKS, then MARKS(IDENT(I)) will equal
///           STRING(BEG(I):END(I)).
///
///           If the substring STRING(BEG(I):END(I)) is not in the
///           list of MARKS then IDENT(I) will have the value 0.
///
///           IDENT should be declared so that it can contain at least
///           ROOM integers.
/// ```
///
/// # Exceptions
///
/// ```text
///  Error free.
///
///  1)  A space is regarded as a special mark. If MARKS(I) = ' ',
///      then MARKS(I) will match any consecutive sequence of blanks.
///
///  2)  If START is less than 1 on input, it will be treated as
///      if it were 1.
///
///  3)  If START is greater than the length of the string, no
///      tokens will be found and the value of START will return
///      unchanged.
/// ```
///
/// # Particulars
///
/// ```text
///  This routine allows you to scan a string and partition it into
///  recognized and unrecognized substrings.
///
///  For some applications the recognized substrings serve only as
///  delimiters between the portions of the string
///  that are of interest to your application. For other
///  applications the recognized substrings are equally important as
///  they may indicate operations that are to be performed on the
///  unrecognized portions of the string. However, the techniques
///  required to scan the string are the same in both instances. The
///  examples below illustrate some common situations.
/// ```
///
/// # Examples
///
/// ```text
///  Example 1.
///  ----------
///
///  Suppose you wished to write a routine that would return the words
///  of a string. The following routine shows how SCANPR and SCAN can
///  be used to accomplish this task.
///
///     SUBROUTINE GETWDS ( STRING, WDROOM, NWORDS, WORDS )
///
///     CHARACTER*(*)      STRING
///     INTEGER            WDROOM
///     INTEGER            NWORDS
///     CHARACTER*(*)      WORDS  ( * )
///
///
///     CHARACTER*(1)      MARKS  ( 1 )
///     INTEGER            MRKLEN ( 1 )
///     INTEGER            PNTERS ( 5 )
///
///     INTEGER            ROOM
///     PARAMETER        ( ROOM = 50 )
///
///     INTEGER            BEG   ( ROOM )
///     INTEGER            END   ( ROOM )
///     INTEGER            I
///     INTEGER            IDENT ( ROOM )
///     INTEGER            NMARKS
///     INTEGER            NTOKNS
///     INTEGER            START
///
///     LOGICAL            FIRST
///     SAVE               FIRST
///     DATA               FIRST  / .TRUE. /
///
///
///     On the first time through the routine, set up the MARKS
///     MRKLEN, and PNTERS arrays.
///
///     IF( FIRST ) THEN
///
///        FIRST    = .FALSE.
///        MARKS(1) = ' '
///        NMARKS   = 1
///
///        CALL SCANPR ( NMARKS, MARKS, MRKLEN, PNTERS )
///
///     END IF
///
///     Now simply scan the input string for words until we have
///     them all or until we run out of room.
///
///     START  = 1
///     NWORDS = 0
///
///     CALL SCAN ( STRING,
///                 MARKS,  MRKLEN, PNTERS, ROOM, START,
///                 NTOKNS, IDENT,  BEG,    END          )
///
///     If we found something in our scan, copy the substrings into the
///     words array.
///
///     DO WHILE (       ( NWORDS .LT. WDROOM )
///    .           .AND. ( NTOKNS .GT. 0      ) )
///
///
///        Step through the scanned substrings, looking for those
///        that are not blank ...
///
///        I = 1
///
///        DO WHILE (       ( NWORDS .LT. WDROOM )
///       .           .AND. ( I      .LE. NTOKNS ) )
///
///           Copy the non-blank substrings (those unidentified by
///           SCAN) into WORDS.
///
///           IF ( IDENT(I) .EQ. 0 ) THEN
///              NWORDS        = NWORDS + 1
///              WORDS(NWORDS) = STRING(BEG(I):END(I))
///           END IF
///
///           I      = I      + 1
///
///        END DO
///
///
///        Scan the STRING again for any substrings that might
///        remain. Note that START is already pointing at the
///        point in the string from which to resume scanning.
///
///        CALL SCAN ( STRING,
///                    MARKS,  MRKLEN, PNTERS, ROOM, START,
///                    NTOKNS, IDENT,  BEG,    END          )
///     END DO
///
///     That's all, we've got all the substrings there were (or
///     that we had room for).
///
///     RETURN
///
///
///  Example 2.
///  ----------
///
///  To parse an algebraic expression such as
///
///     ( X + Y ) * ( 2*Z + SIN(W) ) ** 2
///
///  You would select '**', '*', '+', '-', '(', ')' and ' '
///  to be the markers. Note that all of these begin with one
///  of the characters in the string ' !"#$%&''()*+,-./'
///  so that we can declare PNTERS to have length 20.
///
///  Prepare the MARKS, MRKLEN, and PNTERS.
///
///     CHARACTER*(4)         MARKS
///     INTEGER               NMARKS ( 8  )
///     INTEGER               MRKLEN ( 8  )
///     INTEGER               PNTERS ( 20 )
///
///     INTEGER               ROOM
///     PARAMETER           ( ROOM = 20 )
///
///     INTEGER               NTOKNS
///     INTEGER               BEG    ( ROOM )
///     INTEGER               END    ( ROOM )
///     INTEGER               IDENT  ( ROOM )
///
///     LOGICAL               FIRST
///     SAVE                  FIRST
///     SAVE                  MARKS
///     SAVE                  MRKLEN
///     SAVE                  PNTERS
///
///     DATA                  FIRST  / .TRUE. /
///
///     IF ( FIRST ) THEN
///
///        MARKS(1) = '('
///        MARKS(2) = ')'
///        MARKS(3) = '+'
///        MARKS(4) = '-'
///        MARKS(5) = '*'
///        MARKS(6) = '/'
///        MARKS(7) = '**'
///        MARKS(8) = ' '
///
///        NMARKS   = 8
///
///        CALL SCANPR ( NMARKS, MARKS, MRKLEN, PNTERS )
///
///        BLANK = BSRCHC ( ' ', NMARKS, MARKS )
///
///     END IF
///
///
///     Once all of the initializations are out of the way,
///     we can scan an input string.
///
///     CALL SCAN ( STRING, MARKS,  MRKLEN, PNTERS, ROOM,
///    .            START,  NTOKNS, IDENT,  BEG,    END  )
///
///
///     Next eliminate any white space that was returned in the
///     list of tokens.
///
///     KEPT = 0
///
///     DO I = 1, NTOKNS
///
///        IF ( IDENT(I) .NE. BLANK ) THEN
///
///           KEPT        = KEPT + 1
///           BEG  (KEPT) = BEG(I)
///           END  (KEPT) = END(I)
///           IDENT(KEPT) = IDENT(I)
///
///        END IF
///
///     END DO
///
///     Now all of the substrings remaining point to grouping symbols,
///     operators, functions, or variables. Given that the individual
///     "words" of the expression are now in hand, the meaning of the
///     expression is much easier to determine.
///
///     The rest of the routine is left as a non-trivial exercise
///     for the reader.
/// ```
///
/// # Restrictions
///
/// ```text
///  1)  The arrays MARKS, MRKLEN, and PNTERS must be prepared by the
///      routine SCANPR prior to supplying them for use by SCAN.
/// ```
///
/// # Author and Institution
///
/// ```text
///  J. Diaz del Rio    (ODC Space)
///  W.L. Taber         (JPL)
/// ```
///
/// # Version
///
/// ```text
/// -    SPICELIB Version 1.1.0, 26-OCT-2021 (JDR)
///
///         Added IMPLICIT NONE statement.
///
///         Edited the header to comply with NAIF standard.
///
/// -    SPICELIB Version 1.0.0, 26-JUL-1996 (WLT)
/// ```
pub fn scan(
    string: &str,
    marks: CharArray,
    mrklen: &[i32],
    pnters: &[i32],
    room: i32,
    start: &mut i32,
    ntokns: &mut i32,
    ident: &mut [i32],
    beg: &mut [i32],
    end: &mut [i32],
) {
    SCAN(
        string.as_bytes(),
        marks,
        mrklen,
        pnters,
        room,
        start,
        ntokns,
        ident,
        beg,
        end,
    );
}

//$Procedure SCAN ( Scan a string for tokens )
pub fn SCAN(
    STRING: &[u8],
    MARKS: CharArray,
    MRKLEN: &[i32],
    PNTERS: &[i32],
    ROOM: i32,
    START: &mut i32,
    NTOKNS: &mut i32,
    IDENT: &mut [i32],
    BEG: &mut [i32],
    END: &mut [i32],
) {
    let MARKS = DummyCharArray::new(MARKS, None, 1..);
    let MRKLEN = DummyArray::new(MRKLEN, 1..);
    let PNTERS = DummyArray::new(PNTERS, 1..);
    let mut IDENT = DummyArrayMut::new(IDENT, 1..);
    let mut BEG = DummyArrayMut::new(BEG, 1..);
    let mut END = DummyArrayMut::new(END, 1..);
    let mut LETTER = [b' '; 1 as usize];
    let mut BACKUP: i32 = 0;
    let mut FINISH: i32 = 0;
    let mut INTVAL: i32 = 0;
    let mut L: i32 = 0;
    let mut LAST: i32 = 0;
    let mut LBOUND: i32 = 0;
    let mut OFFSET: i32 = 0;
    let mut STOP: i32 = 0;
    let mut TEST: i32 = 0;
    let mut UBOUND: i32 = 0;
    let mut EQUAL: bool = false;
    let mut KNOWN: bool = false;

    //
    // All of the MARKS have the same declared length.
    // However, since all of your marks may not have
    // the same intended length (for example '*' and
    // '**') it is desirable to be able to specify
    // how much of MARKS(I) should actually be used
    // when examining STRING for a substring match.
    // This is done with the array MRKLEN.
    // MARKS(I)(1:MRKLEN(I)) will be used when
    // scanning STRING.
    //
    // Here is the expected structure of PNTERS.
    //
    //         PNTERS(1) = MIN ( ICHAR(MARKS(I)(1:1)  )
    //         PNTERS(2) = MAX ( ICHAR(MARKS(I)(1:1)  )
    //
    // where I ranges from 1 to the number of MARKS stored
    // in MARKS.  For ease of further discussion let
    // MYCHAR(I) represent the characters from PNTERS(1)
    // to PNTERS(2), and assume that legitimate values of
    // I are from 1 to N.
    //
    //         PNTERS(3)   = 0
    //         PNTERS(4)   = index of the last entry of MARKS
    //                       that begins with the character
    //                       MYCHAR(1).
    //
    //         PNTERS(5)   = index of the last entry of MARKS
    //                       that begins with the character
    //                       MYCHAR(2), if there is no such element
    //                       of MARKS let PNTERS(5) = PNTERS(4)
    //            .
    //            .
    //            .
    //
    //         PNTERS(3+K) = index of the last entry of MARKS
    //                       that begins with the character
    //                       MYCHAR(K), if there is no such element
    //                       of MARKS, let PNTERS(3+K) =
    //                       PNTERS(3+K-1)
    //            .
    //            .
    //            .
    //
    //         PNTERS(3+N) = index of the last entry of MARKS
    //                       that begins with the character
    //                       MYCHAR(N).
    //
    //         PNTERS(4+N) = PNTERS(3+N)
    //
    //
    // Get the information concerning the range of the
    // marks from the PNTERS array.
    //
    OFFSET = (PNTERS[1] - 4);
    LBOUND = (PNTERS[1] - 1);
    UBOUND = (PNTERS[2] + 1);

    LAST = intrinsics::LEN(STRING);
    *NTOKNS = 0;
    BACKUP = (*START - 1);
    KNOWN = true;
    *START = intrinsics::MAX0(&[1, *START]);

    while (*START <= LAST) {
        //
        // Get the numeric code for this letter, and look up
        // the range of markers that begin with this letter.
        //
        fstr::assign(&mut LETTER, fstr::substr(STRING, *START..=*START));

        INTVAL = intrinsics::MAX0(&[
            LBOUND,
            intrinsics::MIN0(&[intrinsics::ICHAR(&LETTER), UBOUND]),
        ]);

        TEST = PNTERS[(INTVAL - OFFSET)];
        FINISH = PNTERS[((INTVAL - OFFSET) - 1)];

        EQUAL = false;
        //
        // If TEST is greater than FINISH, then there is a range of
        // markers that start with this letter.
        //
        while (TEST > FINISH) {
            //
            // Look up the length of the next marker to test for
            // and compute where it would end in STRING if there
            // is a match.
            //
            L = MRKLEN[TEST];
            STOP = (BACKUP + L);

            //
            // Make sure that we are not going to violate any substring
            // references when we compare the current candidate mark with
            // the substring having the same length and starting at START.
            //
            if (STOP > LAST) {
                TEST = (TEST - 1);
            } else {
                //
                // OK. The substring reference STRING(START:STOP) is
                // legal.  See if it is equal to the current test mark.
                //
                EQUAL = fstr::eq(
                    fstr::substr(MARKS.get(TEST), 1..=L),
                    fstr::substr(STRING, *START..=STOP),
                );

                //
                // If it isn't equal, just set up to test the next mark.
                //
                if !EQUAL {
                    TEST = (TEST - 1);
                } else {
                    //
                    // If we were in the middle of an unrecognized string
                    // then, we need to check whether or not we have room
                    // to identify another token. If we don't we must return
                    // now.
                    //
                    if (!KNOWN && (*NTOKNS == ROOM)) {
                        return;
                    }

                    //
                    // A space is a special kind of mark.  All white space
                    // is regarded as being the same.  If the current mark
                    // is a space, we need to collect all of the consecutive
                    // blanks beginning with the one at the START position.
                    //
                    if fstr::eq(MARKS.get(TEST), b" ") {
                        STOP = (NCPOS(STRING, b" ", *START) - 1);

                        if (STOP < 0) {
                            STOP = LAST;
                        }
                    }

                    //
                    // Ok. We have a new known token.
                    //
                    // 1)  Record its begin, end, and identity.
                    //
                    // 2)  Set TEST to FINISH so that the loop will end.
                    //
                    // 3)  Set START to the current STOP so that later when
                    //     we add 1, START will point to the beginning
                    //     of the remainder of the string that needs to be
                    //     scanned.
                    //
                    KNOWN = true;
                    *NTOKNS = (*NTOKNS + 1);
                    BEG[*NTOKNS] = *START;
                    END[*NTOKNS] = STOP;
                    IDENT[*NTOKNS] = TEST;
                    TEST = FINISH;
                    *START = STOP;

                    //
                    // If we have just used up all available room,
                    // position START so that we will be ready
                    // to continue scanning on a subsequent call
                    // and return.
                    //
                    if (*NTOKNS == ROOM) {
                        *START = (*START + 1);
                        return;
                    }
                }
            }
        }

        //
        // If none of the markers matched a substring starting at
        // the current position, we are beginning or continuing
        // an unrecognized substring.
        //
        if !EQUAL {
            //
            // If we are already in the middle of an unrecognized
            // substring, just extend our current unrecognized string.
            //
            if !KNOWN {
                END[*NTOKNS] = *START;

            //
            // Otherwise, start up a new unrecognized substring.
            //
            } else {
                *NTOKNS = (*NTOKNS + 1);
                BEG[*NTOKNS] = *START;
                END[*NTOKNS] = *START;
                IDENT[*NTOKNS] = 0;
                KNOWN = false;
            }
        }

        BACKUP = *START;
        *START = (*START + 1);
    }
}