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
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::thread;

mod config;
pub use config::Config;

mod line;
use line::Line;

/// Reads file content. This function returns only unique lines.
pub fn read_file(file: &str) -> io::Result<String> {
    let path = Path::new(file);
    if !path.exists() {
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            "file does not exist",
        ));
    }
    let mut f = File::open(path)?;

    let mut buffer = String::new();
    f.read_to_string(&mut buffer)?;

    // only unique lines
    let mut content: Vec<&str> = vec![];
    for buf in buffer.lines() {
        if !content.contains(&buf) {
            content.push(buf);
        }
    }

    let mut text = content.join("\n");
    text.push_str("\n");
    Ok(text)
}

/// Reads files using read_file in multiple threads and returns its text.
pub fn read_files(files: Vec<String>) -> String {
    let contents = Arc::new(Mutex::new("".to_string()));
    let mut handles = vec![];

    for file in files {
        let contents = Arc::clone(&contents);
        let handle = thread::spawn(move || {
            let mut data = contents.lock().unwrap();
            let mut s = data.to_string();

            s.push_str(&read_file(&file).unwrap());
            *data = s;
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }

    let result = (*contents).lock().unwrap();
    result.to_string()
}

/// Returns overlap lines.
pub fn overlap(text: String, c: &Config) -> String {
    let mut lines: Vec<Line> = vec![];

    // counting up
    for s in text.lines() {
        let line = Line::new(s);

        // this sorting is needed for searching
        lines.sort();

        match lines.binary_search_by(|a| a.cmp(&line)) {
            Ok(i) => {
                let l = &mut lines[i];
                l.up();
            },
            Err(_) => {
                lines.push(line);
            },
        };
    }

    let mut result: Vec<String> = vec![];
    for line in lines {
        if line.count() > 1 && !line.text().is_empty() {
            let mut s = String::new();
            s.push_str(&line.text());
            if c.with_count {
                s.push_str(&format!(" {}", line.count()));
            }
            result.push(s);
        }
    }
    result.join("\n")
}

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

    #[test]
    fn test_read_file_returns_file_content() {
        let s = read_file("./Cargo.toml").unwrap();
        assert!(s.contains("overlap"));
    }

    #[test]
    fn test_overlap_returns_only_overlap_texts() {
        let c = Config::new(false);

        let text = "Hoi\nZäme!\nHoi\n".to_string();
        assert_eq!("Hoi", overlap(text, &c));

        let text = "Hoi\nZäme!\n".to_string();
        assert_eq!("", overlap(text, &c));
    }

    #[test]
    fn test_overlap_returns_with_count() {
        let text = "Hoi\nZäme!\nHoi\n".to_string();
        let c = Config::new(true);
        assert_eq!("Hoi 2", overlap(text, &c))
    }
}