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
mod bufferize;

use std::fs::File;
use std::io::{Result, BufReader, stdin, Stdin};
use crate::bufferize::Bufferize;

pub struct Charwise<S: Bufferize> {
    source: S,
    buffer: Vec<char>,
    position: usize,
    position_in_buffer: usize
}

const CLEANUP_THRESHOLD: usize = 1024;

impl Charwise<BufReader<File>> {

    pub fn from_file(file: File) -> Self {
        Self {
            source: BufReader::new(file),
            buffer: vec![],
            position: 0,
            position_in_buffer: 0
        }
    }

}

impl Charwise<Stdin> {

    pub fn from_stdin() -> Self {
        Self {
            source: stdin(),
            buffer: vec![],
            position: 0,
            position_in_buffer: 0
        }
    }

}

impl<S: Bufferize> Charwise<S> {

    /// Returns the position of the next character to be read, or,
    /// in other words, the amount of characters read so far
    pub fn reading_position(&self) -> usize {
        self.position + self.position_in_buffer
    }

    /// Reads the next character without changing the current position
    pub fn peek(&mut self) -> Option<Result<char>> {
        self.peek_nth(0)
    }

    /// Reads the n-th character ahead of the reader without altering the current position,
    /// calling `peek_nth(0)` is equivalent to reading the next character similar to `next()`
    pub fn peek_nth(&mut self, n: usize) -> Option<Result<char>> {

        loop {

            self.cleanup_buffer();

            if self.position_in_buffer + n < self.buffer.len() {
                return Some(Ok(self.buffer[self.position_in_buffer + n]));
            }

            let mut temp_buffer = String::new();

            match self.source.read_to_string(&mut temp_buffer) {
                Ok(bytes_read) => {

                    if bytes_read == 0 {
                        // eof reached
                        return None
                    }

                    let temp_buffer: &mut Vec<char> = &mut temp_buffer.chars().collect();

                    debug_assert!(temp_buffer.len() >= 1);

                    self.buffer.append(temp_buffer);

                    debug_assert!(self.buffer.len() >= 1);

                }
                Err(e) => {
                    return Some(Err(e));
                }
            }
        }
    }

    /// Assuming the character has been peeked, advance the stream without looking at it
    /// Call this function only in case you just want to skip the current character because
    /// you already know it after calling `peek`.
    pub fn skip_peeked(&mut self) {

        debug_assert!(self.position_in_buffer < self.buffer.len());

        self.position_in_buffer += 1;
        self.cleanup_buffer();

    }

    /// Similar to `skip_peeked`, this function should be called only after calling
    /// `peek(k)` for `k >= n`. In other words, the function expects that at least n
    /// characters are already buffered and assumes that without further checks.
    pub fn skip_n_peeked(&mut self, n: usize) {

        debug_assert!(self.position_in_buffer + n < self.buffer.len());

        self.position_in_buffer += n + 1;
        self.cleanup_buffer();

    }

    fn cleanup_buffer(&mut self) {
        if self.position_in_buffer >= CLEANUP_THRESHOLD {
            self.buffer.drain(..self.position_in_buffer);
            self.position += self.position_in_buffer;
            self.position_in_buffer = 0;
        }
    }

}

impl<S: Bufferize> Iterator for Charwise<S> {

    type Item = Result<char>;

    fn next(&mut self) -> Option<Self::Item> {

        if self.position_in_buffer < self.buffer.len() {
            let c = self.buffer[self.position_in_buffer];
            self.position_in_buffer += 1;
            self.cleanup_buffer();
            return Some(Ok(c));
        }

        let mut temp_buffer = String::new();

        match self.source.read_to_string(&mut temp_buffer) {
            Ok(bytes_read) => {

                if bytes_read == 0 {
                    // eof reached
                    return None
                }

                self.buffer = temp_buffer.chars().collect();

                debug_assert!(self.buffer.len() >= 1);

                self.position += self.position_in_buffer;

                self.position_in_buffer = 1;

                Some(Ok(self.buffer[0]))
            }
            Err(e) => {
                Some(Err(e))
            }
        }
    }

}

#[cfg(test)]
mod tests;