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
#![deny(
missing_docs,
warnings,
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, allow(unused_attributes))]
#[cfg(feature = "alloc")]
extern crate alloc;
#[macro_use]
extern crate paste;
macro_rules! cfg_alloc {
($($item:item)*) => {
$(
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
$item
)*
}
}
macro_rules! has_prefix {
($trait:tt::$fn:tt) => {
#[inline]
fn has_prefix(&self, prefix: impl $trait) -> bool {
let src = $trait::$fn(self);
let prefix = $trait::$fn(&prefix);
let pl = prefix.len();
if src.len() < pl {
return false;
}
src[0..pl].eq(prefix)
}
};
}
macro_rules! has_suffix {
($trait:tt::$fn:tt) => {
#[inline]
fn has_suffix(&self, suffix: impl $trait) -> bool {
let src = $trait::$fn(self);
let suffix = $trait::$fn(&suffix);
let pl = suffix.len() - 1;
if src.len() <= pl {
return false;
}
src[pl..].eq(suffix)
}
};
}
macro_rules! longest_prefix {
($trait:tt::$fn:tt, $ty: ty) => {
#[inline]
fn longest_prefix(&self, other: impl $trait) -> &[$ty] {
let k1 = $trait::$fn(self);
let k2 = $trait::$fn(&other);
let max = k1.len().min(k2.len());
let mut n = max - 1;
for i in 0..max {
if k1[i].ne(&k2[i]) {
n = i;
break;
}
}
&k1[..n]
}
};
}
macro_rules! longest_suffix {
($trait:tt::$fn:tt, $ty: ty) => {
#[inline]
fn longest_suffix(&self, other: impl $trait) -> &[$ty] {
let k1 = $trait::$fn(self);
let k1_len = k1.len();
let k2 = $trait::$fn(&other);
let k2_len = k2.len();
return if k1_len < k2_len {
let max = k1_len;
let mut n = max;
for i in 0..max {
if k1[k1_len - i - 1].ne(&k2[k2_len - i - 1]) {
n = i;
break;
}
}
&k1[max - n..]
} else {
let max = k2_len;
let mut n = max;
for i in 0..max {
if k1[k1_len - i - 1].ne(&k2[k2_len - i - 1]) {
n = i;
break;
}
}
&k1[k1_len - k2_len + max - n..]
};
}
}
}
#[cfg(feature = "alloc")]
macro_rules! longest_prefix_lossy {
($trait:tt::$fn:tt, $ty: ty, $ty_literal: literal) => {
#[doc = concat!("Finds the longest shared prefix, return a Cow<'_, [", $ty_literal, "]>.")]
#[inline]
fn longest_prefix_lossy(&self, other: impl $trait) -> Cow<'_, [$ty]> {
Cow::Borrowed(self.longest_prefix(other))
}
};
}
#[cfg(feature = "alloc")]
macro_rules! longest_suffix_lossy {
($trait:tt::$fn:tt, $ty: ty, $ty_literal: literal) => {
#[doc = concat!("Finds the longest shared suffix, return a Cow<'_, [", $ty_literal, "]>.")]
#[inline]
fn longest_suffix_lossy(&self, other: impl $trait) -> Cow<'_, [$ty]> {
Cow::Borrowed(self.longest_suffix(other))
}
};
}
macro_rules! impl_psfix_suites {
($trait:tt::$fn:tt, $ty: ty, $ty_literal: literal) => {
has_prefix!($trait::$fn);
has_suffix!($trait::$fn);
longest_prefix!($trait::$fn, $ty);
longest_suffix!($trait::$fn, $ty);
cfg_alloc!{
longest_prefix_lossy!($trait::$fn, $ty, $ty_literal);
longest_suffix_lossy!($trait::$fn, $ty, $ty_literal);
}
};
}
mod bytes_ext;
mod slice_ext;
pub use bytes_ext::*;
pub use slice_ext::*;