1use core::ops::{
2 Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
3};
4
5use crate::cesu8::Cesu8Str;
6use crate::java::JavaStr;
7
8#[cfg(feature = "alloc")]
9use crate::cesu8::Cesu8String;
10#[cfg(feature = "alloc")]
11use crate::java::JavaString;
12
13macro_rules! impl_index {
14 ($kind:ty, $str:ty, $string:ty) => {
15 impl Index<$kind> for $str {
16 type Output = $str;
17
18 #[inline]
19 fn index(&self, index: $kind) -> &Self::Output {
20 let internal = self.internal.index(index);
21 unsafe { <$str>::from_internal_unchecked(internal) }
22 }
23 }
24
25 impl Index<$kind> for &$str {
26 type Output = $str;
27
28 #[inline]
29 fn index(&self, index: $kind) -> &Self::Output {
30 let internal = self.internal.index(index);
31 unsafe { <$str>::from_internal_unchecked(internal) }
32 }
33 }
34
35 impl Index<$kind> for &mut $str {
36 type Output = $str;
37
38 #[inline]
39 fn index(&self, index: $kind) -> &Self::Output {
40 let internal = self.internal.index(index);
41 unsafe { <$str>::from_internal_unchecked(internal) }
42 }
43 }
44
45 impl IndexMut<$kind> for $str {
46 #[inline]
47 fn index_mut(&mut self, index: $kind) -> &mut Self::Output {
48 let internal = self.internal.index_mut(index);
49 unsafe { <$str>::from_internal_unchecked_mut(internal) }
50 }
51 }
52
53 impl IndexMut<$kind> for &mut $str {
54 #[inline]
55 fn index_mut(&mut self, index: $kind) -> &mut Self::Output {
56 let internal = self.internal.index_mut(index);
57 unsafe { <$str>::from_internal_unchecked_mut(internal) }
58 }
59 }
60
61 #[cfg(feature = "alloc")]
62 impl Index<$kind> for $string {
63 type Output = $str;
64
65 #[inline]
66 fn index(&self, index: $kind) -> &Self::Output {
67 self.as_str().index(index)
68 }
69 }
70
71 #[cfg(feature = "alloc")]
72 impl Index<$kind> for &$string {
73 type Output = $str;
74
75 #[inline]
76 fn index(&self, index: $kind) -> &Self::Output {
77 self.as_str().index(index)
78 }
79 }
80
81 #[cfg(feature = "alloc")]
82 impl Index<$kind> for &mut $string {
83 type Output = $str;
84
85 #[inline]
86 fn index(&self, index: $kind) -> &Self::Output {
87 self.as_str().index(index)
88 }
89 }
90
91 #[cfg(feature = "alloc")]
92 impl IndexMut<$kind> for $string {
93 #[inline]
94 fn index_mut(&mut self, index: $kind) -> &mut Self::Output {
95 self.as_mut_str().index_mut(index)
96 }
97 }
98
99 #[cfg(feature = "alloc")]
100 impl IndexMut<$kind> for &mut $string {
101 #[inline]
102 fn index_mut(&mut self, index: $kind) -> &mut Self::Output {
103 self.as_mut_str().index_mut(index)
104 }
105 }
106 };
107}
108
109impl_index!(Range<usize>, Cesu8Str, Cesu8String);
110impl_index!(RangeFrom<usize>, Cesu8Str, Cesu8String);
111impl_index!(RangeFull, Cesu8Str, Cesu8String);
112impl_index!(RangeInclusive<usize>, Cesu8Str, Cesu8String);
113impl_index!(RangeTo<usize>, Cesu8Str, Cesu8String);
114impl_index!(RangeToInclusive<usize>, Cesu8Str, Cesu8String);
115
116impl_index!(Range<usize>, JavaStr, JavaString);
117impl_index!(RangeFrom<usize>, JavaStr, JavaString);
118impl_index!(RangeFull, JavaStr, JavaString);
119impl_index!(RangeInclusive<usize>, JavaStr, JavaString);
120impl_index!(RangeTo<usize>, JavaStr, JavaString);
121impl_index!(RangeToInclusive<usize>, JavaStr, JavaString);