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
extern crate flate2;
extern crate libc;
extern crate regex;
use std::fs::File;
use std::io::{Read, BufReader, BufRead, Stdin, stdin};
use std::io;
use self::flate2::read::GzDecoder;
use self::regex::Regex;
use std::iter::Skip;
use std::slice::Iter;
use vectors::copy_memory;
pub struct GzipData {
v: Vec<u8>,
idx: usize
}
impl <'b> GzipData {
pub fn from_file(fname: &str) -> Result<GzipData, &'static str> {
let mut r: Vec<u8> = Vec::new();
try!(
try!(GzDecoder::new(
try!(File::open(fname)
.map_err(|_| "Could not open file")
)
)
.map_err(|_| "Invalid gzip header.")
)
.read_to_end(&mut r)
.map_err(|_| "Could not unzip data.")
);
Ok(GzipData {
v: r,
idx: 0
})
}
pub fn from_buf(v: Vec<u8>) -> GzipData {
GzipData {
v: v,
idx: 0
}
}
pub fn into_bytes(&self) -> Vec<u8> { self.v.clone() }
pub fn len(&self) -> usize { self.v.len() }
pub fn iter(&self) -> Skip<Iter<u8>> { self.v.iter().skip(self.idx) }
pub fn buf(&'b self) -> &'b [u8] { &self.v.split_at(self.idx).1 }
}
impl Read for GzipData {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if self.idx >= self.v.len() {
return Ok(0);
}
let n = buf.len();
let c = copy_memory(
buf,
self.v.split_at(self.idx).1,
n
);
self.idx += c;
Ok(c)
}
}
pub struct MatchLines<R: Read> {
reader: BufReader<R>,
r: Regex,
}
impl <R: Read> Iterator for MatchLines<R> {
type Item = io::Result<Vec<String>>;
fn next(&mut self) -> Option<Self::Item> {
loop {
let mut buf = String::new();
match self.reader.read_line(&mut buf) {
Ok(0) => {
return None
},
Err(e) => {
return Some(Err(e))
},
Ok(_n) => {
if buf.ends_with("\n") {
buf.pop();
}
match self.r.captures(&buf) {
Some(cap) => {
return Some(Ok(cap.iter().map(|s| s.unwrap().to_string()).collect()));
},
_ => ()
}
}
}
}
}
}
pub fn match_lines_stdin(r: Regex) -> MatchLines<Stdin> {
MatchLines {
reader: BufReader::new(stdin()),
r: r,
}
}
pub fn match_lines<R: Read>(reader: R, r: Regex) -> MatchLines<R> {
MatchLines {
reader: BufReader::new(reader),
r: r
}
}
#[cfg(test)]
mod tests {
extern crate regex;
use super::*;
use std::io::Read;
use self::regex::Regex;
use std::fs::File;
use std::io::BufReader;
#[test]
fn test_read_gzip() {
assert_eq!(
String::from_utf8(
GzipData::from_file("datasets/testing/hello_world.gz").unwrap().into_bytes()).unwrap(),
"hello world".to_string()
);
assert_eq!(GzipData::from_file("datasets/testing/hello_world.gz").unwrap().len(), 11);
assert!(GzipData::from_file("datasets/testing/random.data").is_err());
let mut data = GzipData::from_file("datasets/testing/hello_world.gz").unwrap();
let mut v: Vec<u8> = Vec::new();
assert!(data.read_to_end(&mut v).is_ok());
assert_eq!(String::from_utf8(v).unwrap(), "hello world".to_string());
}
#[test]
fn test_match_lines() {
let f = File::open("datasets/testing/lines.txt").unwrap();
let r = BufReader::new(f);
let mut v = Vec::new();
for line in match_lines(r, Regex::new(r"^[a-z]+ (\d+)$").unwrap()) {
let captures = line.unwrap();
v.push(captures[1].parse::<usize>().unwrap());
}
assert_eq!(v, vec![1, 2, 3]);
}
}