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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! # String reader
//!
//! Zero-allocation string reader. The string reader can be used to parse
//! all kinds of values from strings. It can be used for construction of
//! traditional lexical analyzers for example. It is useful in situation when
//! you need to parse simple formatted strings but regular expressions are too
//! heavy-weight.
//!
//! # Example
//!
//! Parsing HTTP response header:
//!
//! ```
//! use std::num::ParseIntError;
//!
//! use str_reader::{ParseError, StringReader};
//!
//! /// Parse the first line of an HTTP response header.
//! fn parse_http_response_line(line: &str) -> Result<(u16, &str), HttpParseError> {
//!     let mut reader = StringReader::new(line);
//!
//!     reader.match_str("HTTP/")?;
//!
//!     match reader.read_word() {
//!         "1.0" => (),
//!         "1.1" => (),
//!         _ => return Err(HttpParseError),
//!     }
//!
//!     let status_code = reader.read_u16()?;
//!
//!     Ok((status_code, reader.as_str().trim()))
//! }
//!
//! #[derive(Debug)]
//! struct HttpParseError;
//!
//! impl From<ParseError> for HttpParseError {
//!     fn from(_: ParseError) -> Self {
//!         Self
//!     }
//! }
//!
//! impl From<ParseIntError> for HttpParseError {
//!     fn from(_: ParseIntError) -> Self {
//!         Self
//!     }
//! }
//!
//! let (status_code, status_msg) = parse_http_response_line("HTTP/1.1 404 Not Found").unwrap();
//!
//! assert_eq!(status_code, 404);
//! assert_eq!(status_msg, "Not Found");
//! ```

use std::{
    error::Error,
    fmt::{self, Display, Formatter},
    num::{ParseFloatError, ParseIntError},
    result,
    str::{Chars, FromStr},
};

/// String reader error.
#[derive(Debug, Copy, Clone)]
pub enum ParseError {
    EmptyInput,
    NoMatch,
}

impl Display for ParseError {
    fn fmt(&self, f: &mut Formatter) -> result::Result<(), fmt::Error> {
        let msg = match *self {
            Self::EmptyInput => "input is empty",
            Self::NoMatch => "the input does not match",
        };

        f.write_str(msg)
    }
}

impl Error for ParseError {}

/// String reader.
pub struct StringReader<'a> {
    input: Chars<'a>,
    current: Option<char>,
}

impl<'a> StringReader<'a> {
    /// Create a new reader for a given input.
    ///
    /// # Arguments
    ///
    /// * `input` - input string or an object that can be referenced as a
    ///   string
    pub fn new<T>(input: &'a T) -> Self
    where
        T: AsRef<str> + ?Sized,
    {
        let input = input.as_ref().chars();

        // We do not want to advance the input just yet. If we did that the
        // string matching methods would not work.
        let current = input.clone().next();

        Self { input, current }
    }

    /// Get the current character (if any) without advancing the input.
    #[inline]
    pub fn current_char(&self) -> Option<char> {
        self.current
    }

    /// Get the next character or return an error if the input is empty.
    pub fn read_char(&mut self) -> Result<char, ParseError> {
        let res = self.input.next().ok_or(ParseError::EmptyInput)?;

        // Peek for the next character without advancing the input.
        self.current = self.input.clone().next();

        Ok(res)
    }

    /// Match a given character to the input and, if successful, advance the
    /// input by exactly one character. An error is returned if the input
    /// character does not match with the given one or if the input is empty.
    ///
    /// # Arguments
    ///
    /// * `expected` - expected character
    pub fn match_char(&mut self, expected: char) -> Result<(), ParseError> {
        let c = self.current_char().ok_or(ParseError::EmptyInput)?;

        if c != expected {
            return Err(ParseError::NoMatch);
        }

        self.skip_char();

        Ok(())
    }

    /// Skip one character.
    pub fn skip_char(&mut self) {
        // Remove the current character.
        self.input.next();

        // Peek for the next character without advancing the input.
        self.current = self.input.clone().next();
    }

    /// Skip all whitespace characters.
    pub fn skip_whitespace(&mut self) {
        let rest = self.input.as_str().trim_start();

        self.input = rest.chars();

        // Peek for the next character without advancing the input.
        self.current = self.input.clone().next();
    }

    /// Match a given string to the input and, if successful, advance the input
    /// by the length of the given string. An error is returned if the input
    /// does not start with the given string.
    ///
    /// # Arguments
    ///
    /// * `val` - expected string
    pub fn match_str(&mut self, val: &str) -> Result<(), ParseError> {
        let input = self.input.as_str();

        if input.starts_with(val) {
            let (_, rest) = input.split_at(val.len());

            self.input = rest.chars();

            // Peek for the next character without advancing the input.
            self.current = self.input.clone().next();

            Ok(())
        } else {
            Err(ParseError::NoMatch)
        }
    }

    /// Read until a given condition is true or until the end of the input and
    /// return the string.
    ///
    /// # Arguments
    ///
    /// * `cnd` - a closure that takes a single character and returns
    /// true/false
    pub fn read_until<F>(&mut self, cnd: F) -> &'a str
    where
        F: FnMut(char) -> bool,
    {
        let rest = self.input.as_str();

        let index = rest.find(cnd).unwrap_or_else(|| rest.len());

        self.split_to(index)
    }

    /// Read one word from the input and return it. A word ends with the first
    /// whitespace character or with the end of the input. The method skips all
    /// initial whitespace characters (if any).
    #[inline]
    pub fn read_word(&mut self) -> &'a str {
        self.skip_whitespace();
        self.read_until(char::is_whitespace)
    }

    /// Read the next word and parse it. The input won't be advanced if the
    /// word cannot be parsed.
    pub fn parse_word<T>(&mut self) -> Result<T, T::Err>
    where
        T: FromStr,
    {
        let (word, rest) = self.first_word();

        let parsed = word.parse()?;

        self.input = rest.chars();

        // Peek for the next character without advancing the input.
        self.current = self.input.clone().next();

        Ok(parsed)
    }

    /// Read a decimal integer as i8.
    #[inline]
    pub fn read_i8(&mut self) -> Result<i8, ParseIntError> {
        self.parse_word()
    }

    /// Read a decimal integer as u8.
    #[inline]
    pub fn read_u8(&mut self) -> Result<u8, ParseIntError> {
        self.parse_word()
    }

    /// Read a decimal integer as i16.
    #[inline]
    pub fn read_i16(&mut self) -> Result<i16, ParseIntError> {
        self.parse_word()
    }

    /// Read a decimal integer as u16.
    #[inline]
    pub fn read_u16(&mut self) -> Result<u16, ParseIntError> {
        self.parse_word()
    }

    /// Read a decimal integer as i32.
    #[inline]
    pub fn read_i32(&mut self) -> Result<i32, ParseIntError> {
        self.parse_word()
    }

    /// Read a decimal integer as u32.
    #[inline]
    pub fn read_u32(&mut self) -> Result<u32, ParseIntError> {
        self.parse_word()
    }

    /// Read a decimal integer as i64.
    #[inline]
    pub fn read_i64(&mut self) -> Result<i64, ParseIntError> {
        self.parse_word()
    }

    /// Read a decimal integer as u64.
    #[inline]
    pub fn read_u64(&mut self) -> Result<u64, ParseIntError> {
        self.parse_word()
    }

    /// Read a decimal integer as i128.
    #[inline]
    pub fn read_i128(&mut self) -> Result<i128, ParseIntError> {
        self.parse_word()
    }

    /// Read a decimal integer as u128.
    #[inline]
    pub fn read_u128(&mut self) -> Result<u128, ParseIntError> {
        self.parse_word()
    }

    /// Read a decimal integer as isize.
    #[inline]
    pub fn read_isize(&mut self) -> Result<isize, ParseIntError> {
        self.parse_word()
    }

    /// Read a decimal integer as usize.
    #[inline]
    pub fn read_usize(&mut self) -> Result<usize, ParseIntError> {
        self.parse_word()
    }

    /// Read a floating point number as f32.
    #[inline]
    pub fn read_f32(&mut self) -> Result<f32, ParseFloatError> {
        self.parse_word()
    }

    /// Read a floating point number as f64.
    #[inline]
    pub fn read_f64(&mut self) -> Result<f64, ParseFloatError> {
        self.parse_word()
    }

    /// Check if the reader is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.current_char().is_none()
    }

    /// Get the rest of the input.
    #[inline]
    pub fn as_str(&self) -> &'a str {
        self.input.as_str()
    }

    /// Get the first word and the remainder of the input without advancing the
    /// input.
    fn first_word(&self) -> (&'a str, &'a str) {
        let input = self.input.as_str().trim_start();

        let index = input
            .find(char::is_whitespace)
            .unwrap_or_else(|| input.len());

        input.split_at(index)
    }

    /// Split the input at a given index and return the first part.
    ///
    /// The input will contain the remaining part after this operation.
    fn split_to(&mut self, index: usize) -> &'a str {
        let rest = self.input.as_str();

        let (word, rest) = rest.split_at(index);

        self.input = rest.chars();

        // Peek for the next character without advancing the input.
        self.current = self.input.clone().next();

        word
    }
}

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

    #[test]
    fn test_reader() {
        let input = "Hello, World!!!   1234\n\tfoo-bar";

        let mut reader = StringReader::new(input);

        assert!(!reader.is_empty());
        assert_eq!(reader.current_char(), Some('H'));
        assert_eq!(reader.as_str(), input);

        let word = reader.read_word();

        assert_eq!(word, "Hello,");
        assert_eq!(reader.as_str(), " World!!!   1234\n\tfoo-bar");

        reader.skip_whitespace();

        assert_eq!(reader.as_str(), "World!!!   1234\n\tfoo-bar");

        let c = reader.read_char();

        assert_eq!(c.ok(), Some('W'));

        let res = reader.match_char('o');

        assert!(res.is_ok());

        let res = reader.match_char('R');

        assert!(res.is_err());

        let res = reader.match_str("RLD!!!");

        assert!(res.is_err());

        let res = reader.match_str("rld!!!");

        assert!(res.is_ok());
        assert_eq!(reader.as_str(), "   1234\n\tfoo-bar");

        let n = reader.read_u32();

        assert_eq!(n.ok(), Some(1234));
        assert_eq!(reader.as_str(), "\n\tfoo-bar");

        let n = reader.read_u32();

        assert!(n.is_err());
        assert_eq!(reader.as_str(), "\n\tfoo-bar");

        let word = reader.read_word();

        assert_eq!(word, "foo-bar");
        assert_eq!(reader.as_str(), "");
        assert!(reader.is_empty());

        let word = reader.read_word();

        assert_eq!(word, "");

        let c = reader.read_char();

        assert!(c.is_err());
        assert!(reader.is_empty());
        assert_eq!(reader.as_str(), "");
    }
}