Skip to content

Instantly share code, notes, and snippets.

@squiidz
Created July 9, 2016 00:13
Show Gist options
  • Save squiidz/0adbb221188245d77c0d21e4d9f28dcd to your computer and use it in GitHub Desktop.
Save squiidz/0adbb221188245d77c0d21e4d9f28dcd to your computer and use it in GitHub Desktop.
rust_parse_string
use std::io::{self, Read};
use std::env;
#[derive(Debug)]
struct Entity {
data: String,
iter: Iterat,
}
#[derive(Debug)]
struct Iterat {
index: usize,
found: i32,
map: Vec<i32>,
}
impl Entity {
fn find_occur(&mut self, c: &str) -> bool {
if self.iter.index == self.data.len() {
return true;
}
else if self.data.chars().nth(self.iter.index).unwrap() == c.chars().nth(0).unwrap() {
if parse_token(&self.data, c, self.iter.index) {
self.iter.found += 1;
self.iter.map.push(self.iter.index as i32);
}
}
self.iter.index += 1;
self.find_occur(c)
}
}
fn parse_token(data: &str, token: &str, index: usize) -> bool {
for i in 0..token.len() {
if data.chars().nth(i + index).unwrap() != token.chars().nth(i).unwrap() {
return false;
}
}
return true;
}
fn new_entity(data: String) -> Entity {
return Entity {data: data,
iter: Iterat{index: 0, found: 0, map: Vec::new()}}
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() <= 1 {
return
}
let c = args[1].clone();
let mut buffer = String::new();
let _ = io::stdin().read_to_string(&mut buffer);
let mut ent = new_entity(buffer.to_owned());
if ent.find_occur(&c) {
println!("Caractere [{}] found {} times !", c, ent.iter.found);
println!("Position {:?}", ent.iter.map)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment