rsspice/generated/spicelib/
posr.rs

1//
2// GENERATED FILE
3//
4
5use super::*;
6use crate::SpiceContext;
7use f2rust_std::*;
8
9/// Position of substring, reverse search
10///
11/// Find the first occurrence in a string of a substring, starting at
12/// a specified location, searching in reverse.
13///
14/// # Required Reading
15///
16/// * [SCANNING](crate::required_reading::scanning)
17///
18/// # Brief I/O
19///
20/// ```text
21///  VARIABLE  I/O  DESCRIPTION
22///  --------  ---  --------------------------------------------------
23///  STR        I   A character string
24///  SUBSTR     I   Substring to locate in the character string.
25///  START      I   Where to start looking for SUBSTR in STR.
26///
27///  The function returns the index of SUBSTR in STR preceding START
28/// ```
29///
30/// # Detailed Input
31///
32/// ```text
33///  STR      is any character string.
34///
35///  SUBSTR   is a substring to look for in STR. Spaces in
36///           SUBSTR are significant.
37///
38///  START    is the position in STR to begin looking for SUBSTR.
39/// ```
40///
41/// # Detailed Output
42///
43/// ```text
44///  The function returns the index of the beginning of the last
45///  substring of STR that begins on or before index START and is
46///  equal to SUBSTR. If the substring cannot be found starting at or
47///  before START, the function is returns 0.
48/// ```
49///
50/// # Exceptions
51///
52/// ```text
53///  Error free.
54///
55///  1)  If START is less than 1, POSR returns zero.
56///
57///  2)  If START is greater than LEN(STRING), the search begins
58///      at the last character of the string.
59/// ```
60///
61/// # Particulars
62///
63/// ```text
64///  POSR is case sensitive.
65///
66///  An entire family of related SPICELIB routines (POS, CPOS, NCPOS,
67///  POSR, CPOSR, NCPOSR) is described in the Required Reading.
68///
69///  Those familiar with the .TRUE. BASIC language should note that
70///  these functions are equivalent to the .TRUE. BASIC intrinsic
71///  functions with the same name.
72/// ```
73///
74/// # Examples
75///
76/// ```text
77///  Let STRING = 'AN ANT AND AN ELEPHANT        '
78///                123456789012345678901234567890
79///
80///  Normal (Sequential) Searching:
81///  ------------------------------
82///
83///        POSR ( STRING, 'AN',  31 ) = 20
84///        POSR ( STRING, 'AN',  19 ) = 12
85///        POSR ( STRING, 'AN',  11 ) =  8
86///        POSR ( STRING, 'AN',   7 ) =  4
87///        POSR ( STRING, 'AN',   3 ) =  1
88///        POSR ( STRING, 'AN',   0 ) =  0
89///
90///  START out of bounds:
91///  --------------------
92///
93///        POSR ( STRING, 'AN', -5 ) =  0
94///        POSR ( STRING, 'AN',  0 ) =  0
95///        POSR ( STRING, 'AN', 31 ) = 20
96///        POSR ( STRING, 'AN', 44 ) = 20
97///
98///  Significance of Spaces:
99///  -----------------------
100///
101///        POSR ( STRING, 'AN',    31 ) =  20
102///        POSR ( STRING, ' AN',   31 ) =  11
103///        POSR ( STRING, ' AN ',  31 ) =  11
104///        POSR ( STRING, ' AN ',  10 ) =   0
105///        POSR ( STRING, ' AN  ', 31 ) =   0
106/// ```
107///
108/// # Author and Institution
109///
110/// ```text
111///  J. Diaz del Rio    (ODC Space)
112///  H.A. Neilan        (JPL)
113///  B.V. Semenov       (JPL)
114///  W.L. Taber         (JPL)
115///  K.S. Zukor         (JPL)
116/// ```
117///
118/// # Version
119///
120/// ```text
121/// -    SPICELIB Version 1.1.0, 13-AUG-2021 (JDR)
122///
123///         Added IMPLICIT NONE statement.
124///
125///         Edited the header to comply with NAIF standard.
126///
127/// -    SPICELIB Version 1.0.4, 31-JAN-2008 (BVS)
128///
129///         Removed non-standard end-of-declarations marker
130///         'C%&END_DECLARATIONS' from comments.
131///
132/// -    SPICELIB Version 1.0.3, 25-AUG-1994 (HAN) (KSZ)
133///
134///         $Examples section of the header used POS instead of POSR.
135///         Also, some examples were incorrect. They have been corrected.
136///
137/// -    SPICELIB Version 1.0.2, 10-MAR-1992 (WLT)
138///
139///         Comment section for permuted index source lines was added
140///         following the header.
141///
142/// -    SPICELIB Version 1.0.1, 26-MAR-1991 (HAN)
143///
144///         The Required Reading file POSITION was renamed to SCANNING.
145///         This header was updated to reflect the change.
146///
147/// -    SPICELIB Version 1.0.0, 31-JAN-1990 (WLT)
148/// ```
149pub fn posr(str: &str, substr: &str, start: i32) -> i32 {
150    let ret = POSR(str.as_bytes(), substr.as_bytes(), start);
151    ret
152}
153
154//$Procedure POSR ( Position of substring, reverse search)
155pub fn POSR(STR: &[u8], SUBSTR: &[u8], START: i32) -> i32 {
156    let mut POSR: i32 = 0;
157    let mut FOUND: bool = false;
158    let mut LENSTR: i32 = 0;
159    let mut OFFSET: i32 = 0;
160    let mut FCHNCE: i32 = 0;
161    let mut B: i32 = 0;
162
163    //
164    // Local variables
165    //
166
167    //
168    // Let's find out how big every body is.
169    //
170    LENSTR = intrinsics::LEN(STR);
171    OFFSET = intrinsics::MAX0(&[0, (intrinsics::LEN(SUBSTR) - 1)]);
172    FCHNCE = (LENSTR - OFFSET);
173
174    //
175    // Look for the string until we run find it or run out of room to
176    // look.
177    //
178    B = intrinsics::MIN0(&[FCHNCE, START]);
179    FOUND = false;
180    POSR = 0;
181
182    while !FOUND {
183        if (B <= 0) {
184            return POSR;
185        } else if fstr::eq(fstr::substr(STR, B..=(B + OFFSET)), SUBSTR) {
186            POSR = B;
187            return POSR;
188        } else {
189            B = (B - 1);
190        }
191    }
192
193    POSR
194}