Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate memmap;
- use std::{str, process};
- use memmap::{Mmap, Protection};
- #[derive(Debug, Default, Clone, Copy)]
- struct LexerT<'a> {
- pos: usize,
- text: &'a str,
- token: &'a str,
- }
- fn next_token(state: LexerT) -> Option<LexerT> {
- let mut start_pos = 0;
- let mut end_pos = start_pos;
- let ref slice = state.text[state.pos .. state.text.len()];
- for ch in slice.chars() {
- if (ch < 'A' || ch > 'Z')
- && (ch < 'a' || ch > 'z') {
- if start_pos == end_pos {
- if ch != ' ' {
- end_pos += 1;
- break;
- } else {
- start_pos += 1;
- end_pos += 1;
- continue;
- }
- }
- break;
- } else {
- end_pos += 1;
- }
- }
- if start_pos != end_pos {
- Some(LexerT {pos: end_pos, text: slice, token: &slice[start_pos..end_pos]})
- } else {
- None
- }
- }
- fn main() {
- let map = Mmap::open_path("data", Protection::Read).unwrap();
- let memory = unsafe { map.as_slice() };
- let string = str::from_utf8(memory).unwrap();
- let mut lexer = LexerT {pos: 0, text: string, token: ""};
- let mut cnt_word = 0;
- let mut cnt_point = 0;
- while let Some(lexer_now) = next_token(lexer) {
- lexer = lexer_now;
- if lexer.token.chars().nth(0).unwrap() == ',' {
- cnt_point = cnt_point + 1;
- } else {
- cnt_word = cnt_word + 1;
- }
- }
- println!("Итого: {} слов, и {} запятых",cnt_word,cnt_point);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement