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
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
//$Procedure ZZREPSUB ( Replace one substring with another )
pub fn ZZREPSUB(IN: &[u8], LEFT: i32, RIGHT: i32, STRING: &[u8], OUT: &mut [u8]) {
let mut INLEN: i32 = 0;
let mut STRLEN: i32 = 0;
let mut OUTLEN: i32 = 0;
let mut REMAIN: i32 = 0;
let mut USE = StackArray::<i32, 3>::new(1..=3);
let mut END: i32 = 0;
let mut NEXT: i32 = 0;
let mut MYLEFT: i32 = 0;
let mut MYRGHT: i32 = 0;
//
// SPICELIB functions
//
//
// Local variables
//
//
// Get the lengths of all the strings involved in this transaction.
//
INLEN = intrinsics::LEN(IN);
STRLEN = intrinsics::LEN(STRING);
OUTLEN = intrinsics::LEN(OUT);
MYLEFT = intrinsics::MIN0(&[(INLEN + 1), intrinsics::MAX0(&[1, LEFT])]);
MYRGHT = intrinsics::MIN0(&[INLEN, intrinsics::MAX0(&[0, RIGHT])]);
//
// Reject bad inputs.
//
if (MYLEFT < 1) {
MYLEFT = 1;
} else if (MYRGHT > INLEN) {
MYRGHT = INLEN;
} else if (RIGHT < (LEFT - 1)) {
return;
}
//
// Consider three separate sections:
//
// 1) The front of the original string.
//
// 2) The replacement string.
//
// 3) The end of the original string.
//
// Determine how much of each section to use in the output string.
// REMAIN is the number of characters that will fit in the output
// string.
//
REMAIN = OUTLEN;
USE[1] = intrinsics::MIN0(&[REMAIN, (MYLEFT - 1)]);
REMAIN = (REMAIN - USE[1]);
USE[2] = intrinsics::MIN0(&[REMAIN, STRLEN]);
REMAIN = (REMAIN - USE[2]);
USE[3] = intrinsics::MIN0(&[REMAIN, (INLEN - RIGHT)]);
//
// Move the third section first. It gets moved back to front
// or front to back, depending on whether the replacement string
// is longer than the original substring. The main thing is to
// avoid overwriting characters that have yet to be moved.
//
END = SUMAI(USE.as_slice(), 3);
if ((MYLEFT + STRLEN) > RIGHT) {
NEXT = END;
for I in intrinsics::range(USE[3], 1, -1) {
fstr::assign(
fstr::substr_mut(OUT, NEXT..=NEXT),
fstr::substr(IN, (RIGHT + I)..=(RIGHT + I)),
);
NEXT = (NEXT - 1);
}
} else {
NEXT = (MYLEFT + STRLEN);
for I in 1..=USE[3] {
fstr::assign(
fstr::substr_mut(OUT, NEXT..=NEXT),
fstr::substr(IN, (RIGHT + I)..=(RIGHT + I)),
);
NEXT = (NEXT + 1);
}
}
//
// The first two sections can be moved directly to the front of
// the output string.
//
NEXT = 1;
for I in 1..=USE[1] {
fstr::assign(fstr::substr_mut(OUT, NEXT..=NEXT), fstr::substr(IN, I..=I));
NEXT = (NEXT + 1);
}
for I in 1..=USE[2] {
fstr::assign(
fstr::substr_mut(OUT, NEXT..=NEXT),
fstr::substr(STRING, I..=I),
);
NEXT = (NEXT + 1);
}
//
// Pad with blanks, if the output string was not filled.
//
if (END < OUTLEN) {
fstr::assign(fstr::substr_mut(OUT, (END + 1)..), b" ");
}
}