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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// devela::sys::mem::view::slice::namespace::bytes
#[cfg(any(doc, unsafe··))]
use crate::Ptr;
use crate::{Char, Cmp, Slice};
/// # Methods for byte slices.
// TODO: add index
#[rustfmt::skip]
impl Slice<u8> {
/// Copies bytes from `src` into `dst` starting at `dst_pos`, up to the remaining capacity.
///
/// Returns the number of bytes copied (possibly less than `src.len()` if truncated).
///
/// # Example
/// ```
/// # use devela::Slice;
/// let mut buf = [0u8; 4];
/// assert_eq!(Slice::copy_into(&mut buf, 1, b"abc"), 3);
/// assert_eq!(&buf, b"\0abc");
/// ```
#[must_use]
pub const fn copy_into(dst: &mut [u8], dst_pos: usize, src: &[u8]) -> usize {
let remaining = dst.len().saturating_sub(dst_pos);
let len = if src.len() < remaining { src.len() } else { remaining };
let dst = Slice::range_mut(dst, dst_pos, dst_pos + len);
let src = Slice::range_to(src, len);
dst.copy_from_slice(src);
len
}
/// Copies UTF-8 bytes from `src` into `dst` starting at `dst_pos`,
/// truncating only at valid UTF-8 character boundaries.
///
/// Returns the number of bytes copied (never splits a character).
///
/// # Example
/// ```
/// # use devela::Slice;
/// // 'o' (1 byte) would fit, but 'ø' (2 bytes) must be dropped
/// let mut buf = [0u8; 5];
/// assert_eq!(Slice::copy_str_into(&mut buf, 0, "hellø"), 4);
/// assert_eq!(&buf[..4], b"hell");
/// ```
#[must_use] #[rustfmt::skip]
pub const fn copy_str_into(dst: &mut [u8], dst_pos: usize, src: &str) -> usize {
let src_bytes = src.as_bytes();
let remaining = dst.len().saturating_sub(dst_pos);
if remaining == 0 { return 0; }
let mut len = if src_bytes.len() < remaining { src_bytes.len() } else { remaining };
// backtrack to nearest valid UTF-8 boundary
while len > 0 && !src.is_char_boundary(len) { len -= 1; }
let dst = Slice::range_mut(dst, dst_pos, dst_pos + len);
let src = Slice::range_to(src_bytes, len);
dst.copy_from_slice(src);
len
}
/// Copies bytes from a UTF-8–encoded slice into `dst` starting at `dst_pos`,
/// truncating only at valid UTF-8 character boundaries.
///
/// Returns the number of bytes copied (never splits a UTF-8 sequence).
///
/// # Example
/// ```
/// # use devela::Slice;
/// // 'o' (1 byte) would fit, but 'ø' (2 bytes) must be dropped
/// let mut buf = [0u8; 5];
/// assert_eq!(Slice::copy_utf8_into(&mut buf, 0, "hellø".as_bytes()), 4);
/// assert_eq!(&buf[..4], b"hell");
/// ```
#[must_use] #[rustfmt::skip]
pub const fn copy_utf8_into(dst: &mut [u8], dst_pos: usize, src: &[u8]) -> usize {
let remaining = dst.len().saturating_sub(dst_pos);
if remaining == 0 { return 0; }
let mut len = if src.len() < remaining { src.len() } else { remaining };
// backtrack to nearest valid UTF-8 boundary
while len > 0 && !Char(src).is_utf8_boundary(len) { len -= 1; }
let dst = Slice::range_mut(dst, dst_pos, dst_pos + len);
let src = Slice::range_to(src, len);
dst.copy_from_slice(src);
len
}
/* array */
/// Copies the `src` byte array into `dst` in compile-time.
///
/// # Features
/// - Uses `Ptr::copy_nonoverlapping` when unsafe operations are allowed.
pub const fn copy_array<const N: usize>(dst: &mut [u8; N], src: &[u8; N]) {
#[cfg(any(feature = "safe_mem", not(unsafe··)))]
{ let mut i = 0; while i < N { dst[i] = src[i]; i += 1; } }
#[cfg(all(not(feature = "safe_mem"), unsafe··))]
unsafe { Ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), N); }
}
/// Copies all elements from `src` into a fixed-size array starting at `offset`.
///
/// # Panics
/// Panics if `src.len() + offset > LEN`.
///
/// # Features
/// - Uses `Ptr::copy_nonoverlapping` when unsafe operations are allowed.
/// - Falls back to safe element-wise copy otherwise.
pub const fn copy_array_at<const LEN: usize>(dst: &mut [u8; LEN], src: &[u8], offset: usize) {
assert!(src.len() + offset <= LEN, "source slice does not fit in destination array");
#[cfg(any(feature = "safe_mem", not(unsafe··)))]
{ let mut i = 0; while i < src.len() { dst[offset + i] = src[i]; i += 1; } }
#[cfg(all(not(feature = "safe_mem"), unsafe··))]
// SAFETY: Length checked via assert, u8 is Copy, offset + src.len() is bounds-checked
unsafe { Ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr().add(offset), src.len()); }
}
/// A convenience wrapper over [`Slice::copy_array_at`].
// #[inline(always)] // MAYBE
// // Auto-select best version
// macro_rules! copy_array { // IDEA
// ($dst:expr, $src:expr, $at:expr) => {
// if $dst.len() <= 32 { copied_array_at($dst, $src, $at) }
// else { let mut tmp = $dst; copy_array_at(&mut tmp, $src, $at); tmp }
// }
// }
pub const fn copied_array_at<const LEN: usize>(base: [u8; LEN], src: &[u8], offset: usize)
-> [u8; LEN] {
let mut new = base;
Slice::copy_array_at(&mut new, src, offset);
new
}
/// Copies all elements from `src` into a new fixed-size array.
///
/// # Features
/// - Uses `Ptr::copy_nonoverlapping` when unsafe operations are allowed
/// - Falls back to safe element-wise copy otherwise
///
/// # Panics
/// Panics if `src.len() != LEN`.
pub const fn to_array<const LEN: usize>(src: &[u8]) -> [u8; LEN] {
assert!(src.len() == LEN, "source slice length must match destination array length");
let mut buf = [0; LEN];
#[cfg(any(feature = "safe_mem", not(unsafe··)))]
{ let mut i = 0; while i < src.len() { buf[i] = src[i]; i += 1; } }
#[cfg(all(not(feature = "safe_mem"), unsafe··))]
// SAFETY: Lengths are equal (checked by assert), u8 is Copy, entire range is bounds-checked
unsafe { Ptr::copy_nonoverlapping(src.as_ptr(), buf.as_mut_ptr(), src.len()); }
buf
}
/* replace */
/// Replaces the `old` leading byte with a `new` byte.
pub const fn replace_leading(slice: &mut [u8], old: u8, new: u8) {
let mut start = 0;
while start < slice.len() && slice[start] == old { slice[start] = new; start += 1; }
}
/* trim leading */
/// Returns a subslice without the given leading `byte`s.
///
/// # Example
/// ```
/// # use devela::Slice;
/// assert_eq!(Slice::trim_leading(b"000123", b'0'), b"123");
/// ```
#[must_use]
pub const fn trim_leading(slice: &[u8], byte: u8) -> &[u8] {
let mut first = 0;
while first < slice.len() && slice[first] == byte { first += 1; }
Slice::range_from(slice, first)
}
/// Returns a subslice without the given leading `byte`s, except of `keep` number of them.
///
/// # Example
/// ```
/// # use devela::Slice;
/// assert_eq!(Slice::trim_leading_keep(b"000123", b'0', 0), b"123");
/// assert_eq!(Slice::trim_leading_keep(b"000123", b'0', 2), b"00123");
/// assert_eq!(Slice::trim_leading_keep(b"000123", b'0', 10), b"000123");
/// ```
#[must_use]
pub const fn trim_leading_keep(slice: &[u8], byte: u8, keep: usize) -> &[u8] {
let mut first = 0;
while first < slice.len() && slice[first] == byte { first += 1; }
let first = first.saturating_sub(keep);
Slice::range_from(slice, first)
}
/// Returns a subslice without the given leading `byte`s,
/// ensuring the result has at least `min_len` bytes (if possible).
///
/// # Example
/// ```
/// # use devela::Slice;
/// assert_eq!(Slice::trim_leading_min_len(b"000000", b'0', 1), b"0");
/// assert_eq!(Slice::trim_leading_min_len(b"000123", b'0', 0), b"123");
/// assert_eq!(Slice::trim_leading_min_len(b"000123", b'0', 4), b"0123");
/// assert_eq!(Slice::trim_leading_min_len(b"000123", b'0', 10), b"000123");
/// ```
pub const fn trim_leading_min_len(slice: &[u8], byte: u8, min_len: usize) -> &[u8] {
let mut first = 0;
while first < slice.len() && slice[first] == byte { first += 1; }
let first = Cmp(first).min(slice.len().saturating_sub(min_len));
Slice::range_from(slice, first)
}
/* trim trailing*/
/// Returns a subslice without the given trailing `byte`s.
///
/// # Example
/// ```
/// # use devela::Slice;
/// assert_eq!(Slice::trim_trailing(b"123000", b'0'), b"123");
/// ```
#[must_use]
pub const fn trim_trailing(slice: &[u8], byte: u8) -> &[u8] {
let mut last = slice.len();
while last > 0 && slice[last - 1] == byte { last -= 1; }
Slice::range_to(slice, last)
}
/// Returns a subslice without the given trailing `byte`s, except of `keep` number of them.
///
/// # Example
/// ```
/// # use devela::Slice;
/// assert_eq!(Slice::trim_trailing_keep(b"123000", b'0', 0), b"123");
/// assert_eq!(Slice::trim_trailing_keep(b"123000", b'0', 2), b"12300");
/// assert_eq!(Slice::trim_trailing_keep(b"123000", b'0', 10), b"123000");
/// ```
#[must_use]
pub const fn trim_trailing_keep(slice: &[u8], byte: u8, keep: usize) -> &[u8] {
let mut last = slice.len();
while last > 0 && slice[last - 1] == byte { last -= 1; }
let last = slice.len() - (slice.len() - last).saturating_sub(keep);
Slice::range_to(slice, last)
}
/// Returns a subslice without the given trailing `byte`s,
/// ensuring the result has at least `min_len` bytes (if possible).
///
/// # Example
/// ```
/// # use devela::Slice;
/// assert_eq!(Slice::trim_trailing_min_len(b"000000", b'0', 1), b"0");
/// assert_eq!(Slice::trim_trailing_min_len(b"123000", b'0', 0), b"123");
/// assert_eq!(Slice::trim_trailing_min_len(b"123000", b'0', 4), b"1230");
/// assert_eq!(Slice::trim_trailing_min_len(b"123000", b'0', 10), b"123000");
/// ```
pub const fn trim_trailing_min_len(slice: &[u8], byte: u8, min_len: usize) -> &[u8] {
let mut last = slice.len();
while last > 0 && slice[last - 1] == byte { last -= 1; }
let last = slice.len() - Cmp(slice.len() - last).min(slice.len().saturating_sub(min_len));
Slice::range_to(slice, last)
}
/* trim edges */
/// Returns a subslice without the given leading and trailing `byte`s.
///
/// # Example
/// ```
/// # use devela::Slice;
/// assert_eq!(Slice::trim_edges(b"000123000", b'0'), b"123");
/// ```
#[must_use] #[inline(always)]
pub const fn trim_edges(slice: &[u8], byte: u8) -> &[u8] {
Slice::trim_trailing(Slice::trim_leading(slice, byte), byte)
}
/// Returns a subslice without the given leading and trailing `byte`s,
/// except for `keep` number of them on each side.
///
/// # Example
/// ```
/// # use devela::Slice;
/// assert_eq!(Slice::trim_edges_keep(b"000123000", b'0', 0), b"123");
/// assert_eq!(Slice::trim_edges_keep(b"000123000", b'0', 1), b"01230");
/// assert_eq!(Slice::trim_edges_keep(b"000123000", b'0', 2), b"0012300");
/// assert_eq!(Slice::trim_edges_keep(b"000123000", b'0', 10), b"000123000");
/// ```
#[must_use] #[inline(always)]
pub const fn trim_edges_keep(slice: &[u8], byte: u8, keep: usize) -> &[u8] {
Slice::trim_trailing_keep(Slice::trim_leading_keep(slice, byte, keep), byte, keep)
}
/// Returns a subslice without the given leading and edges `byte`s,
/// ensuring the result has at least `min_len` bytes (if possible), with a left bias.
///
/// # Example
/// ```
/// # use devela::Slice;
/// assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 0), b"123");
/// assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 4), b"0123");
/// assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 5), b"01230");
/// assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 6), b"001230");
/// assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 7), b"0012300");
/// assert_eq!(Slice::trim_edges_min_len_left(b"000123000", b'0', 20), b"000123000");
/// ```
pub const fn trim_edges_min_len_left(slice: &[u8], byte: u8, min_len: usize) -> &[u8] {
let leading_trimmed = Slice::trim_leading(slice, byte);
let leading_kept = slice.len() - leading_trimmed.len();
let trailing_trimmed = Slice::trim_trailing(leading_trimmed, byte);
let trailing_kept = leading_trimmed.len() - trailing_trimmed.len();
if trailing_trimmed.len() >= min_len { return trailing_trimmed; }
let bytes_needed = min_len - trailing_trimmed.len();
// Left bias: give the extra byte to the left side
let restore_leading = Cmp(leading_kept).min(bytes_needed / 2 + bytes_needed % 2);
let restore_trailing = Cmp(trailing_kept).min(bytes_needed - restore_leading);
let start = leading_kept.saturating_sub(restore_leading);
let end = slice.len() - trailing_kept + restore_trailing;
Slice::range(slice, start, end)
}
/// Returns a subslice without the given leading and edges `byte`s,
/// ensuring the result has at least `min_len` bytes (if possible), with a right bias.
///
/// # Example
/// ```
/// # use devela::Slice;
/// assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 0), b"123");
/// assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 4), b"1230");
/// assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 5), b"01230");
/// assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 6), b"012300");
/// assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 7), b"0012300");
/// assert_eq!(Slice::trim_edges_min_len_right(b"000123000", b'0', 20), b"000123000");
/// ```
pub const fn trim_edges_min_len_right(slice: &[u8], byte: u8, min_len: usize) -> &[u8] {
let leading_trimmed = Slice::trim_leading(slice, byte);
let leading_kept = slice.len() - leading_trimmed.len();
let trailing_trimmed = Slice::trim_trailing(leading_trimmed, byte);
let trailing_kept = leading_trimmed.len() - trailing_trimmed.len();
if trailing_trimmed.len() >= min_len { return trailing_trimmed; }
let bytes_needed = min_len - trailing_trimmed.len();
// Right bias: give the extra byte to the right side
let restore_trailing = Cmp(trailing_kept).min(bytes_needed / 2 + bytes_needed % 2);
let restore_leading = Cmp(leading_kept).min(bytes_needed - restore_trailing);
let start = leading_kept.saturating_sub(restore_leading);
let end = slice.len() - trailing_kept + restore_trailing;
Slice::range(slice, start, end)
}
}