-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
71 lines (58 loc) · 1.72 KB
/
build.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
use std::fs;
use std::io::{Read, Write};
fn main() {
let mut word_width = 0;
let mut max_width_word = "";
let mut fh_chars = fs::File::open("./dict/chars.csv").unwrap();
let mut chars_buf = String::new();
fh_chars.read_to_string(&mut chars_buf).unwrap();
let mut fh_words = fs::File::open("./dict/words.csv").unwrap();
let mut words_buf = String::new();
fh_words.read_to_string(&mut words_buf).unwrap();
let mut output = "// Generated file
// 单词最大长度
pub const WORD_LEN: usize = {{4}};
// 字表
pub const CHARS: &'static [&'static str] = &[
"
.to_string();
let chars_list = chars_buf.split("\n");
for line in chars_list {
let l = line.trim();
if l == "" {
continue;
}
output.push_str(format!("\t\"{}\",\n", l).as_str());
}
output.push_str(
"];
// 单词表
pub const WORDS: &'static [&'static str] = &[
",
);
let words_list = words_buf.split("\n");
for line in words_list {
let l = line.trim();
if l == "" {
continue;
}
let segs: Vec<&str> = l.split(",").collect();
if segs.len() > 0 {
let key = segs.get(0).unwrap();
let width = key.chars().count();
if width > word_width {
word_width = width;
max_width_word = *key
}
}
output.push_str(format!("\t\"{}\",\n", l).as_str());
}
println!("{}-{}", word_width, max_width_word);
output.push_str(
"
];",
);
output = output.replace("{{4}}", format!("{}", word_width).as_str());
let mut outfile = fs::File::create("./src/vars.rs").unwrap();
outfile.write_all(output.as_bytes()).unwrap();
}