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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
use crate::internal::{InternalCharIndices, InternalChars};
use core::fmt::Write;
use core::ops::{
Index, IndexMut, Range, RangeBounds, RangeFrom, RangeFull, RangeInclusive, RangeTo,
RangeToInclusive,
};
#[cfg(feature = "alloc")]
use super::InternalString;
#[cfg(feature = "alloc")]
use alloc::borrow::ToOwned;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
/// A CESU-8 encoded string slice.
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct InternalStr {
bytes: [u8],
}
impl InternalStr {
/// Converts a slice of bytes to a `InternalStr` without checking that the
/// string contains valid CESU-8.
///
/// An `InternalStr` does not validate its contents. It defines operations
/// that can be made on any string following the CESU-8 format. Java's
/// variant makes one change, nul bytes are encoded in two bytes. This
/// library does not check that the characters are valid, merely that the
/// format of the string is valid.
///
/// # Safety
///
/// The bytes passed in must be valid CESU-8.
#[inline]
#[must_use]
pub(crate) const unsafe fn from_unchecked(v: &[u8]) -> &Self {
unsafe { &*(v as *const [u8] as *const Self) }
}
/// Converts a mutable slice of bytes to a mutable `InternalStr` without
/// checking that the string contains valid CESU-8.
///
/// # Safety
///
/// The bytes passed in must be valid CESU-8.
#[inline]
#[must_use]
pub(crate) unsafe fn from_unchecked_mut(v: &mut [u8]) -> &mut Self {
unsafe { &mut *(v as *mut [u8] as *mut Self) }
}
/// Converts a boxed slice of bytes to a boxed string slice without checking
/// that the string contains valid CESU-8.
///
/// # Safety
///
/// The bytes passed in must be valid.
#[cfg(feature = "alloc")]
#[inline]
#[must_use]
pub(crate) unsafe fn from_boxed_unchecked(v: Box<[u8]>) -> Box<Self> {
unsafe { Box::from_raw(Box::into_raw(v) as *mut Self) }
}
/// Returns the length of `self`.
///
/// This length is in bytes, not [`char`]s or graphemes. In other words, it
/// might not be what a human considers the length of the string.
#[inline]
#[must_use]
pub(crate) const fn len(&self) -> usize {
self.bytes.len()
}
/// Returns `true` if `self` has a length of zero bytes.
#[inline]
#[must_use]
pub(crate) const fn is_empty(&self) -> bool {
self.bytes.is_empty()
}
/// Checks that the `index`-th byte is the first byte in a CESU-8 code point
/// sequence or the end of the string.
///
/// The start and end of the string (when `index == self.len()`) are
/// considered to be boundaries.
///
/// Returns `false` if `index is greater than self.len()`.
#[inline]
#[must_use]
pub(crate) fn is_char_boundary(&self, index: usize) -> bool {
// 0 is always ok. This is a fast path so that it can optimze out the check
// easily and skip reading string data for that case.
if index == 0 {
return true;
}
match self.bytes.get(index) {
None => index == self.len(),
Some(&b) => {
if b < 128 || b & 0b111_00000 == 0b110_00000 {
// 1 or 2-byte characters
return true;
} else if b & 0b1111_0000 == 0b1110_0000 {
// 3 or 6-byte characters
// SAFETY: Since this is a valid string, we know that there
// must be at least two more bytes.
let next = unsafe { self.bytes.get(index + 1).unwrap_unchecked() };
if b == 0b1110_1101 && next & 0b1111_0000 == 0b1011_0000 {
// Second code-point in a surrogate pair
return false;
}
return true;
}
false
}
}
}
/// Converts a string slice to a byte slice.
#[inline]
#[must_use]
pub(crate) const fn as_bytes(&self) -> &[u8] {
unsafe { core::mem::transmute(self) }
}
/// Converts a mutable string slice to a mutable byte slice.
///
/// # Safety
///
/// The caller must ensure that the content of the slice is valid CESU-8
/// before the borrow ends and the underlying `InternalStr` is used.
///
/// Use of a `InternalStr` whose contents are not valid CESU-8 is undefined
/// behavior.
#[inline]
#[must_use]
pub(crate) unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
unsafe { &mut *(self as *mut InternalStr as *mut [u8]) }
}
/// Converts a string slice to a raw pointer.
///
/// As string slices are a slice of bytes, the raw pointer points to a
/// [`u8`]. This pointer will be pointing to the first bytes of the string
/// slice.
///
/// The caller must ensure that the returned pointer is never written to. If
/// you need to mutate the contents of the string slice, use [`as_mut_ptr`].
///
/// [`as_mut_ptr`]: Self::as_mut_ptr
#[inline]
#[must_use]
pub(crate) const fn as_ptr(&self) -> *const u8 {
self as *const InternalStr as *mut u8
}
/// Converts a mutable string slice to a raw pointer.
///
/// As string slices are a slice of bytes, the raw pointer points to a
/// [`u8`]. This pointer will be pointing to the first byte of the string
/// slice.
///
/// It is your responsibility to make sure that the string slice only gets
/// modified in a way that it remains valid CESU-8.
#[inline]
#[must_use]
pub(crate) fn as_mut_ptr(&mut self) -> *mut u8 {
self as *mut InternalStr as *mut u8
}
/// Calculate the bounds for a given range.
#[inline]
#[must_use]
fn get_bounds<I: RangeBounds<usize>>(&self, index: I) -> (usize, usize) {
let start = match index.start_bound() {
core::ops::Bound::Excluded(&x) => x + 1,
core::ops::Bound::Included(&x) => x,
core::ops::Bound::Unbounded => 0,
};
let end = match index.end_bound() {
core::ops::Bound::Excluded(&x) => x,
core::ops::Bound::Included(&x) => x + 1,
core::ops::Bound::Unbounded => self.len(),
};
(start, end)
}
/// Calculate the bounds for a given range and check that they result in
/// valid start and end indices.
#[inline]
#[must_use]
fn get_checked_bounds<I: RangeBounds<usize>>(&self, index: I) -> Option<(usize, usize)> {
let (start, end) = self.get_bounds(index);
if end > start || end > self.len() {
return None;
}
if !self.is_char_boundary(start) || !self.is_char_boundary(end) {
return None;
}
Some((start, end))
}
/// Returns a subslice of `InternalStr`.
///
/// This is the non-panicking alternative to indexing the `str`. Returns
/// [`None`] whenever equivalent indexing operations would panic.
#[inline]
#[must_use]
pub(crate) fn get<I: RangeBounds<usize>>(&self, index: I) -> Option<&Self> {
let (start, end) = self.get_checked_bounds(index)?;
Some(unsafe { Self::from_unchecked(&self.bytes[start..end]) })
}
/// Returns a mutable subslice of `InternalStr`.
///
/// This is the non-panicking alternative to indexing the `InternalStr`.
/// Returns [`None`] whenver equivalent indexing operations would panic.
#[inline]
#[must_use]
pub(crate) fn get_mut<I: RangeBounds<usize>>(&mut self, index: I) -> Option<&mut Self> {
let (start, end) = self.get_checked_bounds(index)?;
Some(unsafe { Self::from_unchecked_mut(&mut self.bytes[start..end]) })
}
/// Returns an unchecked subslice of `InternalStr`.
///
/// This is the unchecked alternative to indexing the `InternalStr`.
///
/// # Safety
///
/// Callers of this function are responsible for ensuring that:
/// * The starting index does not exceed the ending index;
/// * The indices are within the bounds of the original slice;
/// * The indices fall on CESU-8 sequence boundaries.
///
/// Failing that, the returned string slice may reference invalid memory or
/// violate the invariants communicated by the `InternalStr` type.
#[inline]
#[must_use]
pub(crate) unsafe fn get_unchecked<I: RangeBounds<usize>>(&self, index: I) -> &Self {
let (start, end) = self.get_bounds(index);
unsafe { Self::from_unchecked(&self.bytes[start..end]) }
}
/// Returns a mutable, unchecked subslice of `InternalStr`.
///
/// This the unchecked alternative to indexing the `InternalStr`.
///
/// # Safety
///
/// Callers of this function are responsible for ensuring that:
/// * The starting index does not exceed the ending index;
/// * The indices are within the bounds of the original slice;
/// * The indices fall on CESU-8 sequence boundaries.
///
/// Failing that, the returned string slice may reference invalid memory or
/// violate the invariants communicated by the `InternalStr` type.
#[inline]
#[must_use]
pub(crate) unsafe fn get_unchecked_mut<I: RangeBounds<usize>>(
&mut self,
index: I,
) -> &mut Self {
let (start, end) = self.get_bounds(index);
unsafe { Self::from_unchecked_mut(&mut self.bytes[start..end]) }
}
/// Divide one string slice into two at an index.
///
/// The argument, `mid`, should be a byte offset from the start of the
/// string. It must also be on the boundary of a CESU-8 character.
///
/// The two slices returned go from the string of the string slice to `mid`,
/// and from `mid` to the end of the string slice.
///
/// To get mutable string slices instead, see the [`split_at_mut`] method.
///
/// [`split_at_mut`]: Self::split_at_mut
#[inline]
#[must_use]
pub(crate) fn split_at(&self, mid: usize) -> (&Self, &Self) {
assert!(
mid <= self.len(),
"byte index {mid} is out of bounds of str"
);
assert!(
self.is_char_boundary(mid),
"byte index {mid} is not a char boundary"
);
unsafe { self.split_at_unchecked(mid) }
}
/// Divide one mutable string slice into two at an index.
///
/// The argument, `mid`, should be a byte offset from the start of the
/// string. It must also be on the boundary of a CESU-8 character.
///
/// The two slices returned go from the string of the string slice to `mid`,
/// and from `mid` to the end of the string slice.
///
/// To get immutable string slices instead, see the [`split_at`] method.
///
/// [`split_at`]: Self::split_at
#[inline]
#[must_use]
pub(crate) fn split_at_mut(&mut self, mid: usize) -> (&mut Self, &mut Self) {
assert!(
mid <= self.len(),
"byte index {mid} is out of bounds of str"
);
assert!(
self.is_char_boundary(mid),
"byte index {mid} is not a char boundary"
);
unsafe { self.split_at_mut_unchecked(mid) }
}
/// Divide one string slice into two at an index.
///
/// The argument, `mid`, should be a valid byte offset from the start of the
/// string. It must also be on the boundary of a CESU-8 code point. The
/// method returns `None` if that's not the case.
///
/// The two slices returned go from the start of the string slice to `mid`,
/// and from `mid` to the end of the string slice.
///
/// To get mutable string slices instead, see the [`split_at_mut_checked`]
/// method.
///
/// [`split_at_mut_checked`]: Self::split_at_mut_checked
#[inline]
#[must_use]
pub(crate) fn split_at_checked(&self, mid: usize) -> Option<(&Self, &Self)> {
// is_char_boundary checks whether the index is in [0, len] and on a char
// boundary.
if self.is_char_boundary(mid) {
// SAFETY: We know that `mid` is on a char boundary.
Some(unsafe {
(
self.get_unchecked(0..mid),
self.get_unchecked(mid..self.len()),
)
})
} else {
None
}
}
/// Divide one mutable string slice into two at an index.
///
/// The argument, `mid`, should be a valid byte offset from the start of the
/// string. It must also be on the boundary of a CESU-8 code point. The
/// method returns `None` if that's not the case.
///
/// The two slices returned go from the start of the string slice to `mid`,
/// and from `mid` to the end of the string slice.
///
/// To get immutable string slices instead, see the [`split_at_checked`]
/// method.
///
/// [`split_at_checked`]: Self::split_at_checked
#[inline]
#[must_use]
pub(crate) fn split_at_mut_checked(&mut self, mid: usize) -> Option<(&mut Self, &mut Self)> {
// is_char_boundary checks whether the index is in [0, len] and on a char
// boundary.
if self.is_char_boundary(mid) {
// SAFETY: We know that `mid` is on a char boundary.
Some(unsafe { self.split_at_mut_unchecked(mid) })
} else {
None
}
}
/// Divide a string into two at an index.
///
/// The two slices returned go from the start of the string slice to `mid`,
/// and from `mid` to the end of the string slice.
///
/// To get mutable string slices instead, see the [`split_at_mut_unchecked`]
/// method.
///
/// [`split_at_mut_unchecked`]: Self::split_at_mut_unchecked
///
/// # Safety
///
/// The caller must ensure that `mid` is a valid byte offset from the start
/// of the string and falls on the boundary of a CESU-8 character.
#[inline]
#[must_use]
pub(crate) unsafe fn split_at_unchecked(&self, mid: usize) -> (&Self, &Self) {
let len = self.len();
let ptr = self.as_ptr();
// SAFETY: The caller guarantees `mid` is in bounds and on a char boundary.
unsafe {
(
Self::from_unchecked(core::slice::from_raw_parts(ptr, mid)),
Self::from_unchecked(core::slice::from_raw_parts(ptr.add(mid), len - mid)),
)
}
}
/// Divide a mutable string into two at an index.
///
/// The two slices returned go from the start of the string slice to `mid`,
/// and from `mid` to the end of the string slice.
///
/// To get immutable string slices instead, see the [`split_at_unchecked`]
/// method.
///
/// [`split_at_unchecked`]: Self::split_at_unchecked
///
/// # Safety
///
/// The caller must ensure that `mid` is a valid byte offset from the start
/// of the string and falls on the boundary of a CESU-8 character.
#[inline]
#[must_use]
pub(crate) unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut Self, &mut Self) {
let len = self.len();
let ptr = self.as_mut_ptr();
// SAFETY: The caller guarantees `mid` is in bounds and on a char boundary.
unsafe {
(
Self::from_unchecked_mut(core::slice::from_raw_parts_mut(ptr, mid)),
Self::from_unchecked_mut(core::slice::from_raw_parts_mut(ptr.add(mid), len - mid)),
)
}
}
/// Returns an iterator over the [`char`]s of a string slice.
///
/// As an `InternalStr` consists of valid CESU-8, we can iterate through a
/// string by [`char`]. This method returns such an iterator.
///
/// It's important to remember that [`char`] represents a Unicode Scalar
/// Value, and might not match your idea of what a 'character' is. Iteration
/// over grapheme clusters may be what you actually want. This functionality
/// is not provided by this crate.
#[inline]
pub(crate) fn chars(&self) -> InternalChars {
InternalChars {
iter: self.bytes.iter(),
}
}
/// Returns an iterator over the [`char`]s of a string slice, and their
/// positions.
///
/// As an `InternalStr` consists of valid CESU-8, we can iterate through a
/// string by [`char`]. This method returns an iterator of both these
/// [`char`]s, as well as their byte positions.
///
/// The iterator yields tuples. The position is first,
/// the [`char`] is second.
#[inline]
pub(crate) fn char_indices(&self) -> InternalCharIndices {
InternalCharIndices {
front_offset: 0,
iter: self.chars(),
}
}
/// Checks if all characters in this string are within the ASCII range.
#[inline]
#[must_use]
pub(crate) const fn is_ascii(&self) -> bool {
self.as_bytes().is_ascii()
}
/// Panics if the range is invalid.
///
/// # Panics
///
/// Panics when:
/// * `start` or `end` are out of bounds
/// * `start` > `end`
/// * `start` or `end` are not on character boundaries
#[inline]
#[track_caller]
fn check_index_internal(&self, start: usize, end: usize) {
// Slice
assert!(
start <= end,
"slice index starts at {start} but ends at {end}"
);
assert!(
start <= self.len(),
"start index {start} out of range for slice of length {}",
self.len()
);
assert!(
end <= self.len(),
"end index {end} out of range for slice of length {}",
self.len()
);
// str-specific
assert!(
self.is_char_boundary(start),
"byte index {start} is not a char boundary"
);
assert!(
self.is_char_boundary(end),
"byte index {end} is not a char boundary"
);
}
/// Returns an immutable string. Panics if the range is invalid.
///
/// # Panics
///
/// Panics when:
/// * `start` or `end` are out of bounds
/// * `start` > `end`
/// * `start` or `end` are not on character boundaries
#[inline]
#[must_use]
#[track_caller]
fn index_internal(&self, start: usize, end: usize) -> &InternalStr {
self.check_index_internal(start, end);
unsafe { Self::from_unchecked(&self.bytes[start..end]) }
}
/// Returns a mutable string. Panics if the range is invalid.
///
/// # Panics
///
/// Panics when:
/// * `start` or `end` are out of bounds
/// * `start` > `end`
/// * `start` or `end` are not on character boundaries
#[inline]
#[must_use]
#[track_caller]
fn index_mut_internal(&mut self, start: usize, end: usize) -> &mut InternalStr {
self.check_index_internal(start, end);
unsafe { Self::from_unchecked_mut(&mut self.bytes[start..end]) }
}
}
macro_rules! impl_index_internal {
($kind:ty) => {
impl Index<$kind> for InternalStr {
type Output = InternalStr;
#[inline]
fn index(&self, index: $kind) -> &Self::Output {
let (start, end) = self.get_bounds(index);
self.index_internal(start, end)
}
}
impl Index<$kind> for &InternalStr {
type Output = InternalStr;
#[inline]
fn index(&self, index: $kind) -> &Self::Output {
let (start, end) = self.get_bounds(index);
self.index_internal(start, end)
}
}
impl Index<$kind> for &mut InternalStr {
type Output = InternalStr;
#[inline]
fn index(&self, index: $kind) -> &Self::Output {
let (start, end) = self.get_bounds(index);
self.index_internal(start, end)
}
}
impl IndexMut<$kind> for InternalStr {
#[inline]
fn index_mut(&mut self, index: $kind) -> &mut Self::Output {
let (start, end) = self.get_bounds(index);
self.index_mut_internal(start, end)
}
}
impl IndexMut<$kind> for &mut InternalStr {
#[inline]
fn index_mut(&mut self, index: $kind) -> &mut Self::Output {
let (start, end) = self.get_bounds(index);
self.index_mut_internal(start, end)
}
}
};
}
impl_index_internal!(Range<usize>);
impl_index_internal!(RangeFrom<usize>);
impl_index_internal!(RangeFull);
impl_index_internal!(RangeInclusive<usize>);
impl_index_internal!(RangeTo<usize>);
impl_index_internal!(RangeToInclusive<usize>);
#[cfg(feature = "alloc")]
impl ToOwned for InternalStr {
type Owned = InternalString;
#[inline]
fn to_owned(&self) -> Self::Owned {
let vec = self.bytes.to_owned();
unsafe { InternalString::from_unchecked(vec) }
}
}
impl AsRef<[u8]> for InternalStr {
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl core::fmt::Debug for InternalStr {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_char('"')?;
for c in self.chars() {
for c in c.escape_debug() {
f.write_char(c)?;
}
}
f.write_char('"')
}
}
impl core::fmt::Display for InternalStr {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for c in self.chars() {
f.write_char(c)?;
}
Ok(())
}
}