const_format/__str_methods/
str_splice.rs

1use super::str_indexing::{pass_range_types, IndexValidity, StrIndexArgs, StrIndexArgsConv};
2
3pub struct StrSplceArgsConv<T> {
4    pub arg: T,
5    pub str: &'static str,
6    pub insert: &'static str,
7}
8
9#[allow(non_snake_case)]
10pub const fn StrSplceArgsConv<T>(
11    str: &'static str,
12    arg: T,
13    insert: &'static str,
14) -> StrSplceArgsConv<T> {
15    StrSplceArgsConv { str, arg, insert }
16}
17
18pub struct StrSpliceArgs {
19    pub str: &'static str,
20    pub insert: &'static str,
21    pub index_validity: IndexValidity,
22    pub used_rstart: usize,
23    pub used_rlen: usize,
24    pub insert_len: usize,
25    pub suffix_len: usize,
26    pub out_len: usize,
27}
28
29/// The return value of [`str_splice`](./macro.str_splice.html)
30#[derive(Debug, Copy, Clone, PartialEq, Eq)]
31pub struct SplicedStr {
32    /// A string that had `removed` replaced with some other string.
33    pub output: &'static str,
34    /// The part of the string that was removed.
35    pub removed: &'static str,
36}
37
38#[repr(C, packed)]
39pub struct DecomposedString<P, M, S> {
40    pub prefix: P,
41    pub middle: M,
42    pub suffix: S,
43}
44
45macro_rules! define_conversions {
46    (
47        $( fn($self:ident, $ty:ty) $block:block )*
48    ) => {
49        $(
50            impl StrSplceArgsConv<$ty> {
51                pub const fn conv(self) -> StrSpliceArgs {
52                    let StrIndexArgs{
53                        str,
54                        index_validity,
55                        used_rstart,
56                        used_rend,
57                        used_rlen,
58                    } = StrIndexArgsConv{
59                        arg: self.arg,
60                        str: self.str,
61                    }.conv();
62
63                    StrSpliceArgs{
64                        str,
65                        index_validity,
66                        used_rstart,
67                        used_rlen,
68                        insert: self.insert,
69                        insert_len: self.insert.len(),
70                        suffix_len: str.len() - used_rend,
71                        out_len: str.len() - used_rlen + self.insert.len(),
72                    }
73                }
74            }
75        )*
76    };
77}
78
79pass_range_types! {define_conversions}