This repository has been archived by the owner on Nov 14, 2021. It is now read-only.
forked from dobkeratops/rustfind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.rs
85 lines (75 loc) · 2.29 KB
/
util.rs
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
use std::vec::Vec;
pub fn text_line_pos_to_offset(text: &[u8], (line, ofs_in_line): (u32, u32))->Option<u32> {
// line as reported by grep & text editors,counted from '1' not '0'
let mut pos = 0;
let tlen=text.len() as u32;
let mut tline=0;
let mut line_start_pos = 0;
while pos<tlen{
match text[pos as uint] as char{
'\n' => {tline+=1; line_start_pos=pos;},
// "\a" => {tpos=0;line_pos=pos;},
_ => {}
}
// todo - clamp line end
if tline==(line-1){
return Some(line_start_pos + ofs_in_line);
}
pos+=1;
}
return None;
}
pub fn get_filename_only(fnm:&str)->~str {
let toks:~[&str]=fnm.split(':').collect();
return toks[0].to_str();
}
pub fn text_offset_to_line_pos(text:&[u8], src_ofs: u32)-> Option<(u32, u32)> {
// line as reported by grep & text editors,counted from '1' not '0'
let mut pos = 0;
let tlen = text.len() as u32;
let mut tline=0;
let mut line_start_pos=0;
while pos<tlen{
match text[pos as uint] as char{
'\n' => {
if src_ofs<=pos && src_ofs>line_start_pos {
return Some((tline+1,src_ofs-line_start_pos));
}
tline+=1; line_start_pos=pos;
},
// "\a" => {tpos=0;line_pos=pos;},
_ => {}
}
// todo - clamp line end
pos+=1;
}
return None;
}
pub fn flatten_to_str<T,U:ToStr>(xs:&[T],f: |&T| -> U, sep:&str)->~str {
let mut acc=~"";
let mut i=0; // TODO - functional way.
while i<xs.len() {
if i>0 {acc.push_str(sep);}
acc.push_str( f(&xs[i]).to_str() );
i+=1;
}
acc
}
pub fn flatten_to_str_ng<T, U:ToStr>(xs: &Vec<T>, f: |&T| -> U, sep: &str) -> ~str {
use std::str::with_capacity;
if xs.is_empty() { return ~""; }
// this calculation is not right, but not sure what the best approximation is
// code taken originally from StrVector code for collect()
let len = sep.len() * (xs.len() * 2 - 1);
let mut result = with_capacity(len);
let mut first = true;
for s in xs.iter() {
if first {
first = false;
} else {
result.push_str(sep);
}
result.push_str(f(s).to_str());
}
result
}