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
use core::fmt::Debug;
use core::ops::{Bound, RangeBounds};
#[macro_export]
macro_rules! assert_str_range {
($s:expr, $r:expr) => {{
let s = &$s;
let r = &$r;
if !$crate::str::is_valid_range(s, r) {
$crate::str::range_fail(s, r)
}
}};
}
pub fn is_valid_range<S, R>(s: S, range: &R) -> bool
where
S: AsRef<str>,
R: RangeBounds<usize>,
{
let s = s.as_ref();
validate_start_bound(s, range.start_bound()).is_valid()
&& validate_end_bound(s, range.end_bound()).is_valid()
}
enum BoundValidity {
Valid,
OutOfBuffer,
NotCharBoundary,
}
impl BoundValidity {
#[inline]
fn is_valid(&self) -> bool {
use BoundValidity::*;
match self {
Valid => true,
_ => false,
}
}
}
#[inline]
fn validate_start_bound(s: &str, bound: Bound<&usize>) -> BoundValidity {
use Bound::*;
use BoundValidity::*;
match bound {
Unbounded => Valid,
Included(index) => validate_index(s, *index),
Excluded(index) => validate_next_index(s, *index),
}
}
#[inline]
fn validate_end_bound(s: &str, bound: Bound<&usize>) -> BoundValidity {
use Bound::*;
use BoundValidity::*;
match bound {
Unbounded => Valid,
Excluded(index) => validate_index(s, *index),
Included(index) => validate_next_index(s, *index),
}
}
#[inline]
fn validate_index(s: &str, index: usize) -> BoundValidity {
use BoundValidity::*;
if s.is_char_boundary(index) {
Valid
} else if index > s.len() {
OutOfBuffer
} else {
NotCharBoundary
}
}
#[inline]
fn validate_next_index(s: &str, index: usize) -> BoundValidity {
use BoundValidity::*;
if index >= s.len() {
#[cold]
OutOfBuffer
} else if s.is_char_boundary(index + 1) {
Valid
} else {
NotCharBoundary
}
}
#[doc(hidden)]
#[cold]
pub fn range_fail<S, R>(s: S, range: &R) -> !
where
S: AsRef<str>,
R: RangeBounds<usize> + Debug,
{
range_fail_internal(s.as_ref(), range)
}
#[inline(never)]
fn range_fail_internal<R>(s: &str, range: &R) -> !
where
R: RangeBounds<usize> + Debug,
{
use BoundValidity::*;
let start_validity = validate_start_bound(s, range.start_bound());
let end_validity = validate_end_bound(s, range.end_bound());
match (start_validity, end_validity) {
(OutOfBuffer, _) | (_, OutOfBuffer) => {
panic!("range {:?} is out of bounds", range)
}
(NotCharBoundary, _) | (_, NotCharBoundary) => {
panic!("range {:?} does not split on a UTF-8 boundary", range)
}
(Valid, Valid) => unreachable!("there was no problem with the range"),
}
}
#[cfg(test)]
mod tests {
#[test]
fn assert_str_range_borrows() {
let s = String::new();
let r = 0..0;
assert_str_range!(s, r);
let _ = (s, r);
}
}