-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsplit.rs
153 lines (134 loc) · 3.83 KB
/
split.rs
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
use std::{
fmt::{self, Debug},
ops::{
Bound::{self, Unbounded},
RangeBounds, RangeFrom, RangeFull, RangeTo, RangeToInclusive,
},
};
pub enum SplitRange {
Full(RangeFull),
From(RangeFrom<usize>),
To(RangeTo<usize>),
}
impl Debug for SplitRange {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
SplitRange::Full(r) => r.fmt(f),
SplitRange::From(r) => r.fmt(f),
SplitRange::To(r) => r.fmt(f),
}
}
}
impl RangeBounds<usize> for SplitRange {
#[inline]
fn start_bound(&self) -> Bound<&usize> {
match *self {
SplitRange::Full(_) => Unbounded,
SplitRange::From(ref r) => r.start_bound(),
SplitRange::To(ref r) => r.start_bound(),
}
}
#[inline]
fn end_bound(&self) -> Bound<&usize> {
match *self {
SplitRange::Full(_) => Unbounded,
SplitRange::From(ref r) => r.end_bound(),
SplitRange::To(ref r) => r.end_bound(),
}
}
}
pub trait BindSlice<S: ?Sized>:
RangeBounds<usize> + bind_slice_private::Sealed
{
fn bind_slice(&self, slice: &S) -> SplitRange;
}
mod bind_slice_private {
use std::ops::{RangeFrom, RangeFull, RangeTo, RangeToInclusive};
pub trait Sealed {}
impl Sealed for RangeFull {}
impl Sealed for RangeFrom<usize> {}
impl Sealed for RangeTo<usize> {}
impl Sealed for RangeToInclusive<usize> {}
impl Sealed for super::SplitRange {}
}
impl BindSlice<str> for RangeFull {
#[inline]
fn bind_slice(&self, _slice: &str) -> SplitRange {
SplitRange::Full(*self)
}
}
impl BindSlice<str> for RangeFrom<usize> {
#[inline]
fn bind_slice(&self, slice: &str) -> SplitRange {
let range = SplitRange::From(self.clone());
if !slice.is_char_boundary(self.start) {
str_split_fail(slice, range);
}
range
}
}
impl BindSlice<str> for RangeTo<usize> {
#[inline]
fn bind_slice(&self, slice: &str) -> SplitRange {
let range = SplitRange::To(*self);
if !slice.is_char_boundary(self.end) {
str_split_fail(slice, range);
}
range
}
}
impl BindSlice<str> for RangeToInclusive<usize> {
#[inline]
fn bind_slice(&self, slice: &str) -> SplitRange {
let excl_range = convert_inclusive_range(*self);
excl_range.bind_slice(slice)
}
}
impl BindSlice<str> for SplitRange {
#[inline]
fn bind_slice(&self, slice: &str) -> SplitRange {
match self {
SplitRange::Full(r) => r.bind_slice(slice),
SplitRange::From(r) => r.bind_slice(slice),
SplitRange::To(r) => r.bind_slice(slice),
}
}
}
#[inline(never)]
#[cold]
fn str_split_fail(s: &str, range: SplitRange) -> ! {
let index = match range {
SplitRange::From(ref r) => r.start,
SplitRange::To(ref r) => r.end,
SplitRange::Full(_) => unreachable!(),
};
if index > s.len() {
panic!("range {:?} is out of bounds of the string buffer", range);
} else {
panic!("range {:?} does not split on a UTF-8 boundary", range);
}
}
fn convert_inclusive_range(range: RangeToInclusive<usize>) -> RangeTo<usize> {
// This should be always Ok for valid ranges, because usize is
// capable of representing the length of any slice in memory.
let excl_end = range.end.checked_add(1).unwrap_or_else(|| {
panic!(
"upper bound of range {:?} is too large for a buffer in memory",
range
)
});
..excl_end
}
pub trait Take {
type Slice: ?Sized;
type Output;
fn take_range<R>(&mut self, range: R) -> Self::Output
where
R: BindSlice<Self::Slice>;
fn remove_range<R>(&mut self, range: R)
where
R: BindSlice<Self::Slice>,
{
self.take_range(range);
}
}