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
use crate::{Iterable, Consumer};

impl Iterable for String {
    type C = Self;
    type CC<U> = Vec<U>;
    type CR<'a> = String;
}

impl Consumer for String {
    type Item = char;
    type IntoIter = Chars;

    fn into_iter(self) -> Self::IntoIter {
        Chars {
            // TODO: use String.into_bytes to avoid alloc
            bytes: self.chars().collect(),
            idx: 0
        }
    }
}

impl<'a> Consumer for &'a String {
    type Item = char;
    type IntoIter = std::str::Chars<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.chars()
    }
}

pub struct Chars {
    // TODO: use Vec<u8> to avoid alloc
    bytes: Vec<char>,
    idx: usize,
}

impl Iterator for Chars {
    type Item = char;
    fn next(&mut self) -> Option<Self::Item> {
        if self.idx == self.bytes.len() {
            None
        } else {
            let ret = self.bytes[self.idx];
            self.idx += 1;
            Some(ret)
        }
    }
}

impl DoubleEndedIterator for Chars {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.bytes.pop()
    }
}

delegate_from_iterator!(String, char, impl);
delegate_extend!(String, char, impl);
delegate_from_iterator!(String, &'a char, impl <'a>);
delegate_extend!(String, &'a char, impl <'a>);

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

    #[test]
    fn test_c() {
        let v = "123你我".to_string();
        let res = v.filter(|c| *c != '你');
        assert_eq!(res, "123我".to_string());
    }

    #[test]
    fn test_f() {
        let v = "123你我".to_string();
        let res = v.rev();
        assert_eq!(res, "我你321".to_string());
    }

    #[test]
    fn test_cc() {
        let v = "123你我".to_string();
        let res = v.map(|_| 1u8);
        assert_eq!(res, vec![1, 1, 1, 1, 1]);
    }

    #[test]
    fn test_c_r() {
        let v = "123你我".to_string();
        let res = (&v).filter(|c| *c != '你');
        assert_eq!(res, "123我".to_string());
    }

    #[test]
    fn test_f_r() {
        let v = "123你我".to_string();
        let res = (&v).rev();
        assert_eq!(res, "我你321".to_string());
    }

    #[test]
    fn test_cc_r() {
        let v = "123你我".to_string();
        let res = (&v).map(|_| 1u8);
        assert_eq!(res, vec![1, 1, 1, 1, 1]);
    }

    #[test]
    fn test_chars_iterator() {
        let mut chars = Chars {
            bytes: vec!['1'],
            idx: 0,
        };
        let a = chars.next();
        assert_eq!(a, Some('1'));
        let b = chars.next();
        assert_eq!(b, None);
        let c = chars.next();
        assert_eq!(c, None);
    }
}