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
pub struct StrIndexArgsConv<T> {
    pub str: &'static str,
    pub arg: T,
}

#[allow(non_snake_case)]
pub const fn StrIndexArgsConv<T>(str: &'static str, arg: T) -> StrIndexArgsConv<T> {
    StrIndexArgsConv { str, arg }
}

pub struct StrIndexArgs {
    pub str: &'static str,
    pub index_validity: IndexValidity,
    pub used_rstart: usize,
    pub used_rlen: usize,
    pub used_rend: usize,
}

#[derive(Copy, Clone)]
#[cfg_attr(test, derive(Debug, PartialEq))]
pub enum IndexValidity {
    Valid,
    StartOob(usize),
    StartInsideChar(usize),
    EndOob(usize),
    EndInsideChar(usize),
}

impl IndexValidity {
    pub const fn is_valid(self) -> bool {
        matches!(self, Self::Valid)
    }

    pub const fn assert_valid(self) {
        match self {
            Self::Valid => (),
            Self::StartOob(index) => [/*start index is out of bounds*/][index],
            Self::StartInsideChar(index) => [/*start index is not on a char boundary*/][index],
            Self::EndOob(index) => [/*end index is out of bounds*/][index],
            Self::EndInsideChar(index) => [/*end index is not on a char boundary*/][index],
        }
    }
}

macro_rules! pass_range_types {
    ($macro:ident) => {
        const _: () = {
            use core::ops;

            #[allow(unused_imports)]
            use crate::__hidden_utils::{is_char_boundary_no_len_check, max_usize, saturating_add};

            $macro! {
                fn(self, usize) {
                    let mut end = saturating_add(self.arg, 1);
                    let bytes = self.str.as_bytes();

                    if end < self.str.len() {
                        while !is_char_boundary_no_len_check(bytes, end) {
                            end = saturating_add(end, 1);
                        }
                    }

                    self.arg .. end
                }

                fn(self, ops::Range<usize>) {
                    let ops::Range{start, end} = self.arg;
                    start .. max_usize(start, end)
                }

                fn(self, ops::RangeTo<usize>) {
                    0..self.arg.end
                }

                fn(self, ops::RangeFrom<usize>) {
                    self.arg.start..self.str.len()
                }

                fn(self, ops::RangeInclusive<usize>) {
                    let start = *self.arg.start();
                    start .. max_usize(saturating_add(*self.arg.end(), 1), start)
                }

                fn(self, ops::RangeToInclusive<usize>) {
                    0 .. saturating_add(self.arg.end, 1)
                }

                fn(self, ops::RangeFull) {
                    0 .. self.str.len()
                }
            }
        };
    };
}
pub(super) use pass_range_types;

macro_rules! define_conversions {
    (
        $( fn($self:ident, $ty:ty) $block:block )*
    ) => {

        $(
            impl StrIndexArgsConv<$ty> {
                pub const fn conv($self) -> StrIndexArgs {
                    use crate::__hidden_utils::is_char_boundary_no_len_check;

                    let range = $block;

                    let str_len = $self.str.len();

                    let mut used_rstart = 0;
                    let mut used_rend = str_len;

                    let mut index_validity = IndexValidity::Valid;
                    let bytes = $self.str.as_bytes();

                    if range.end > str_len {
                        index_validity = IndexValidity::EndOob(range.end);
                    } else if is_char_boundary_no_len_check(bytes, range.end) {
                        used_rend = range.end;
                    } else {
                        index_validity = IndexValidity::EndInsideChar(range.end);
                    };

                    if range.start > str_len {
                        index_validity = IndexValidity::StartOob(range.start);
                    } else if is_char_boundary_no_len_check(bytes, range.start) {
                        used_rstart = range.start;
                    } else {
                        index_validity = IndexValidity::StartInsideChar(range.start);
                    };

                    StrIndexArgs {
                        str: $self.str,
                        index_validity,
                        used_rstart,
                        used_rend,
                        used_rlen: used_rend - used_rstart,
                    }
                }
            }
        )*
    };
}

pass_range_types! {define_conversions}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn index_validity_test() {
        macro_rules! miv {
            ($str:expr, $range:expr) => {
                StrIndexArgsConv($str, $range).conv().index_validity
            };
        }

        assert_eq!(miv!("効率的", 3), IndexValidity::Valid);
        assert_eq!(miv!("効率的", 6), IndexValidity::Valid);
        assert_eq!(miv!("効率的", 3..6), IndexValidity::Valid);

        assert_eq!(miv!("効率的", 4..6), IndexValidity::StartInsideChar(4));
        assert_eq!(miv!("効率的", 3..5), IndexValidity::EndInsideChar(5));
        assert_eq!(miv!("効率的", 7..9), IndexValidity::StartInsideChar(7));

        assert_eq!(miv!("効率的", 100..9), IndexValidity::StartOob(100));
        assert_eq!(miv!("効率的", 3..10), IndexValidity::EndOob(10));
        assert_eq!(miv!("効率的", 9), IndexValidity::EndOob(10));
        assert_eq!(miv!("効率的", 10), IndexValidity::StartOob(10));
        assert_eq!(miv!("効率的", 100..900), IndexValidity::StartOob(100));
    }
}