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
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::{borrow::ToOwned, string::String};
use core::{ops::Deref, slice::SliceIndex};
#[cfg(not(feature = "alloc"))]
use super::Error;
use super::Result;
#[allow(clippy::module_name_repetitions)]
pub enum TriCow<'a> {
#[cfg(feature = "alloc")]
Owned(String),
Borrowed(&'a str),
MutBorrowed(&'a mut str),
}
impl Deref for TriCow<'_> {
type Target = str;
fn deref(&self) -> &Self::Target {
match self {
#[cfg(feature = "alloc")]
Self::Owned(s) => s,
Self::Borrowed(s) => s,
Self::MutBorrowed(s) => s,
}
}
}
impl TriCow<'_> {
#[allow(clippy::unnecessary_wraps)]
pub fn replace_range(&mut self, range: core::ops::Range<usize>, with: &str) -> Result<()> {
match self {
#[cfg(feature = "alloc")]
TriCow::Owned(s) => {
s.replace_range(range, with);
Ok(())
}
#[cfg(feature = "alloc")]
TriCow::Borrowed(s) => {
let mut s = s.to_owned();
s.replace_range(range, with);
*self = TriCow::Owned(s);
Ok(())
}
#[cfg(not(feature = "alloc"))]
TriCow::Borrowed(_) => Err(Error::AllocRequired),
TriCow::MutBorrowed(s) => {
if range.len() == with.len() {
#[cfg_attr(not(feature = "alloc"), allow(clippy::redundant_clone))]
if let Some(slice) = s.get_mut(range.clone()) {
// SAFETY: `range.len() == with.len()` is checked above, so
// `copy_from_slice` can't panic. Both `slice` and `with` originate
// from `&str`, so they are valid UTF-8 of identical byte length;
// overwriting `slice`'s bytes with `with`'s bytes preserves UTF-8
// validity (we're replacing a complete UTF-8 sequence with another
// of the same length). `s.get_mut(range)` returned `Some`, which
// guarantees `range` falls on char boundaries.
unsafe { slice.as_bytes_mut() }.copy_from_slice(with.as_bytes());
return Ok(());
}
}
#[cfg(feature = "alloc")]
{
let mut s = s.to_owned();
s.replace_range(range, with);
*self = TriCow::Owned(s);
Ok(())
}
#[cfg(not(feature = "alloc"))]
Err(Error::AllocRequired)
}
}
}
fn to_mut(&mut self) -> Result<&mut str> {
match self {
#[cfg(feature = "alloc")]
TriCow::Owned(s) => Ok(s.as_mut_str()),
#[cfg(feature = "alloc")]
TriCow::Borrowed(s) => {
*self = TriCow::Owned(s.to_owned());
if let TriCow::Owned(s) = self {
Ok(s.as_mut_str())
} else {
// Just assigned `TriCow::Owned(...)` on the line above; the variant
// can't have changed between the assignment and the match.
unreachable!("cow isn't owned after making it owned, what happened?")
}
}
#[cfg(not(feature = "alloc"))]
TriCow::Borrowed(_) => Err(Error::AllocRequired),
TriCow::MutBorrowed(s) => Ok(s),
}
}
/// # Panics
/// Panics if range isn't at valid character boundaries
pub fn make_uppercase<R>(&mut self, range: R) -> Result<()>
where
R: Clone + SliceIndex<[u8], Output = [u8]> + SliceIndex<str, Output = str>,
{
match self {
#[cfg(feature = "alloc")]
TriCow::Owned(s) => {
s[range].make_ascii_uppercase();
Ok(())
}
TriCow::MutBorrowed(s) => {
s[range].make_ascii_uppercase();
Ok(())
}
// Only promote Borrowed -> Owned if mutation is actually required.
TriCow::Borrowed(_) => {
if self.as_bytes()[range.clone()].iter().any(u8::is_ascii_lowercase) {
self.to_mut()?[range].make_ascii_uppercase();
}
Ok(())
}
}
}
/// # Panics
/// Panics if range isn't at valid character boundaries
pub fn make_lowercase<R>(&mut self, range: R) -> Result<()>
where
R: Clone + SliceIndex<[u8], Output = [u8]> + SliceIndex<str, Output = str>,
{
match self {
#[cfg(feature = "alloc")]
TriCow::Owned(s) => {
// if this isn't ascii, it will fail later
s[range].make_ascii_lowercase();
Ok(())
}
TriCow::MutBorrowed(s) => {
s[range].make_ascii_lowercase();
Ok(())
}
TriCow::Borrowed(_) => {
if self.as_bytes()[range.clone()].iter().any(u8::is_ascii_uppercase) {
self.to_mut()?[range].make_ascii_lowercase();
}
Ok(())
}
}
}
}