Skip to content

Commit

Permalink
Apply clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone authored and progval committed Mar 3, 2023
1 parent 8b4bcc5 commit 5cd226f
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 64 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
generator::generate(Some(&generated_path), None);
}
{
let mut generated_phf_path = out_dir.clone();
let mut generated_phf_path = out_dir;
generated_phf_path.push("generated_phf.rs");
generator::generate_phf(Some(&generated_phf_path), None, 3, 2);
}
Expand Down
29 changes: 14 additions & 15 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn create_lexicon_and_offsets(
let mut substring_overlaps = 0;
let mut substring_o_bytes = 0;

for &(_, ref name) in codepoint_names.iter() {
for &(_, name) in codepoint_names.iter() {
for n in util::split(name, SPLITTERS) {
if n.len() == 1 && SPLITTERS.contains(&n.as_bytes()[0]) {
continue;
Expand Down Expand Up @@ -229,7 +229,7 @@ fn write_codepoint_maps(ctxt: &mut Context, codepoint_names: Vec<(char, &str)>)
// and then sort the rest into groups of equal length, to allow us
// to avoid storing the full length table; just the indices. The
// ordering is irrelevant here; just that they are in groups.
lexicon_words[short..].sort_by(|&(_, ref a, _), &(_, ref b, _)| a.len().cmp(&b.len()));
lexicon_words[short..].sort_by(|(_, a, _), (_, b, _)| a.len().cmp(&b.len()));

// the encoding for each word, to avoid having to recompute it
// each time, we can just blit it out of here.
Expand Down Expand Up @@ -285,7 +285,7 @@ fn write_codepoint_maps(ctxt: &mut Context, codepoint_names: Vec<(char, &str)>)
// using the binning, below.
let mut phrasebook_offsets = repeat(0).take(0x10FFFF + 1).collect::<Vec<_>>();
let mut longest_name = 0;
for &(cp, ref name) in codepoint_names.iter() {
for &(cp, name) in codepoint_names.iter() {
longest_name = cmp::max(name.len(), longest_name);

let start = phrasebook.len() as u32;
Expand Down Expand Up @@ -348,27 +348,28 @@ fn make_context(path: Option<&Path>) -> Context {
let mut ctxt = Context {
out: match path {
Some(p) => Box::new(BufWriter::new(
File::create(&p.with_extension("tmp")).unwrap(),
File::create(p.with_extension("tmp")).unwrap(),
)) as Box<dyn Write>,
None => Box::new(io::sink()) as Box<dyn Write>,
},
};
ctxt.out
let _ = ctxt
.out
.write(b"// autogenerated by generator.rs\n")
.unwrap();
ctxt
}

#[allow(clippy::type_complexity)]
fn get_truncated_table_data(
truncate: Option<usize>,
) -> (Vec<(char, &'static str)>, Vec<(char, char)>) {
let TableData {
mut codepoint_names,
cjk_ideograph_ranges: cjk,
} = get_table_data();
match truncate {
Some(n) => codepoint_names.truncate(n),
None => {}
if let Some(n) = truncate {
codepoint_names.truncate(n)
}
(codepoint_names, cjk)
}
Expand All @@ -384,9 +385,8 @@ pub fn generate_phf(path: Option<&Path>, truncate: Option<usize>, lambda: usize,

ctxt.write_debugs("NAME2CODE_CODE", "char", &data);

match path {
Some(f) => fs::rename(&f.with_extension("tmp"), &f).unwrap(),
None => {}
if let Some(path) = path {
fs::rename(path.with_extension("tmp"), path).unwrap()
}
}

Expand All @@ -395,11 +395,10 @@ pub fn generate(path: Option<&Path>, truncate: Option<usize>) {
let mut ctxt = make_context(path);

write_cjk_ideograph_ranges(&mut ctxt, &cjk);
ctxt.out.write(b"\n").unwrap();
let _ = ctxt.out.write(b"\n").unwrap();
write_codepoint_maps(&mut ctxt, codepoint_names);

match path {
Some(f) => fs::rename(&f.with_extension("tmp"), &f).unwrap(),
None => {}
if let Some(path) = path {
fs::rename(path.with_extension("tmp"), path).unwrap()
}
}
10 changes: 3 additions & 7 deletions generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,13 @@ fn main() {

let truncate = matches
.opt_str("truncate")
.map(|s| s.parse().ok().expect("truncate should be an integer"));
.map(|s| s.parse().expect("truncate should be an integer"));

let lambda = matches.opt_str("phf-lambda");
let tries = matches.opt_str("phf-tries");
if do_phf {
let lambda = lambda
.map(|s| s.parse().ok().expect("invalid -l"))
.unwrap_or(3);
let tries = tries
.map(|s| s.parse().ok().expect("invalid -t"))
.unwrap_or(2);
let lambda = lambda.map(|s| s.parse().expect("invalid -l")).unwrap_or(3);
let tries = tries.map(|s| s.parse().expect("invalid -t")).unwrap_or(2);
unicode_names2_generator::generate_phf(path, truncate, lambda, tries);
} else {
if lambda.is_some() {
Expand Down
24 changes: 11 additions & 13 deletions generator/src/phf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct Hash {
f2: u32,
}

#[allow(clippy::type_complexity)]
fn try_phf_table(
values: &[(char, &str)],
lambda: usize,
Expand All @@ -46,7 +47,7 @@ fn try_phf_table(
) -> Option<(Vec<(u32, u32)>, Vec<char>)> {
let hashes: Vec<_> = values
.iter()
.map(|&(n, ref s)| (split(hash(s, seed)), n))
.map(|&(n, s)| (split(hash(s, seed)), n))
.collect();

let table_len = hashes.len();
Expand All @@ -60,7 +61,7 @@ fn try_phf_table(
}

// place the large buckets first.
buckets.sort_by(|&(_, ref a), &(_, ref b)| b.len().cmp(&a.len()));
buckets.sort_by(|(_, a), (_, b)| b.len().cmp(&a.len()));

// this stores the final computed backing vector, i.e. getting the
// value for `foo` is "just" `map[displace(hash(foo))]`, where
Expand Down Expand Up @@ -151,17 +152,14 @@ pub fn create_phf(
println!("PHF #{}: starting {:.2}", i, my_start - start);

let seed = rng.gen();
match try_phf_table(data, lambda, seed, &mut rng) {
Some((disp, map)) => {
let end = time::precise_time_s();
println!(
"PHF took: total {:.2} s, successive {:.2} s",
end - start,
end - my_start
);
return (seed, disp, map);
}
None => {}
if let Some((disp, map)) = try_phf_table(data, lambda, seed, &mut rng) {
let end = time::precise_time_s();
println!(
"PHF took: total {:.2} s, successive {:.2} s",
end - start,
end - my_start
);
return (seed, disp, map);
}
}
panic!(
Expand Down
13 changes: 5 additions & 8 deletions generator/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,12 @@ impl<'a> Iterator for Items<'a> {
type Item = (usize, Vec<u8>, Option<usize>);
fn next(&mut self) -> Option<(usize, Vec<u8>, Option<usize>)> {
'outer: loop {
match self.current {
Some(t) => {
self.current = None;
self.stack.push(t.children.iter());
if t.count > 0 {
return Some((t.count, self.parents.clone(), t.offset));
}
if let Some(t) = self.current {
self.current = None;
self.stack.push(t.children.iter());
if t.count > 0 {
return Some((t.count, self.parents.clone(), t.offset));
}
None => {}
}

loop {
Expand Down
2 changes: 1 addition & 1 deletion src/iter_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Iterator for IterStr {
let offset = LEXICON_OFFSETS[idx] as usize;
&LEXICON[offset..offset + length]
};
self.phrasebook = if is_end { (&[]).iter() } else { tmp };
self.phrasebook = if is_end { ([]).iter() } else { tmp };
ret
})
}
Expand Down
18 changes: 9 additions & 9 deletions src/jamo.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//! Algorithmic mapping for hangul syllables.
// derived from Jamo.txt
pub static CHOSEONG: &'static [&'static str] = &[
pub static CHOSEONG: &[&str] = &[
"G", "GG", "N", "D", "DD", "R", "M", "B", "BB", "S", "SS", "", "J", "JJ", "C", "K", "T", "P",
"H",
];
pub static JUNGSEONG: &'static [&'static str] = &[
pub static JUNGSEONG: &[&str] = &[
"A", "AE", "YA", "YAE", "EO", "E", "YEO", "YE", "O", "WA", "WAE", "OE", "YO", "U", "WEO", "WE",
"WI", "YU", "EU", "YI", "I",
];
pub static JONGSEONG: &'static [&'static str] = &[
pub static JONGSEONG: &[&str] = &[
"", "G", "GG", "GS", "N", "NJ", "NH", "D", "L", "LG", "LM", "LB", "LS", "LT", "LP", "LH", "M",
"B", "BS", "S", "SS", "NG", "J", "C", "K", "T", "P", "H",
];

pub fn is_hangul_syllable(c: char) -> bool {
'\u{AC00}' <= c && c <= '\u{D7A3}'
('\u{AC00}'..='\u{D7A3}').contains(&c)
}

pub fn syllable_decomposition(c: char) -> Option<(u8, u8, u8)> {
Expand All @@ -32,15 +32,15 @@ pub fn syllable_decomposition(c: char) -> Option<(u8, u8, u8)> {
Some((choseong as u8, jungseong as u8, jongseong as u8))
}

fn slice_shift_byte<'a>(a: &'a [u8]) -> (Option<u8>, &'a [u8]) {
if a.len() >= 1 {
fn slice_shift_byte(a: &[u8]) -> (Option<u8>, &[u8]) {
if !a.is_empty() {
(Some(a[0]), &a[1..])
} else {
(None, a)
}
}

pub fn slice_shift_choseong<'a>(name: &'a [u8]) -> (Option<u32>, &'a [u8]) {
pub fn slice_shift_choseong(name: &[u8]) -> (Option<u32>, &[u8]) {
match slice_shift_byte(name) {
(Some(b'G'), name) => match slice_shift_byte(name) {
(Some(b'G'), name) => (Some(1), name),
Expand Down Expand Up @@ -74,7 +74,7 @@ pub fn slice_shift_choseong<'a>(name: &'a [u8]) -> (Option<u32>, &'a [u8]) {
}
}

pub fn slice_shift_jungseong<'a>(name: &'a [u8]) -> (Option<u32>, &'a [u8]) {
pub fn slice_shift_jungseong(name: &[u8]) -> (Option<u32>, &[u8]) {
match slice_shift_byte(name) {
(Some(b'A'), name) => match slice_shift_byte(name) {
(Some(b'E'), name) => (Some(1), name),
Expand Down Expand Up @@ -121,7 +121,7 @@ pub fn slice_shift_jungseong<'a>(name: &'a [u8]) -> (Option<u32>, &'a [u8]) {
}
}

pub fn slice_shift_jongseong<'a>(name: &'a [u8]) -> (Option<u32>, &'a [u8]) {
pub fn slice_shift_jongseong(name: &[u8]) -> (Option<u32>, &[u8]) {
match slice_shift_byte(name) {
(Some(b'G'), name) => match slice_shift_byte(name) {
(Some(b'G'), name) => (Some(2), name),
Expand Down
21 changes: 13 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
//! name)`).
//!
//! ```rust
//! fn main() {
//! println!("☃ is called {:?}", unicode_names2::name('☃')); // SNOWMAN
//! println!("{:?} is happy", unicode_names2::character("white smiling face")); // ☺
//! // (NB. case insensitivity)
//! }
//! ```
//!
//! [**Source**](https://github.com/ProgVal/unicode_names2).
Expand Down Expand Up @@ -75,10 +73,14 @@ use generated::{
use generated_phf as phf;

#[allow(dead_code)]
#[rustfmt::skip]
#[allow(clippy::all)]
mod generated {
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
}
#[allow(dead_code)]
#[rustfmt::skip]
#[allow(clippy::all)]
mod generated_phf {
include!(concat!(env!("OUT_DIR"), "/generated_phf.rs"));
}
Expand All @@ -87,8 +89,8 @@ mod jamo;

mod iter_str;

static HANGUL_SYLLABLE_PREFIX: &'static str = "HANGUL SYLLABLE ";
static CJK_UNIFIED_IDEOGRAPH_PREFIX: &'static str = "CJK UNIFIED IDEOGRAPH-";
static HANGUL_SYLLABLE_PREFIX: &str = "HANGUL SYLLABLE ";
static CJK_UNIFIED_IDEOGRAPH_PREFIX: &str = "CJK UNIFIED IDEOGRAPH-";

fn is_cjk_unified_ideograph(ch: char) -> bool {
generated::CJK_IDEOGRAPH_RANGES
Expand All @@ -105,13 +107,15 @@ fn is_cjk_unified_ideograph(ch: char) -> bool {
pub struct Name {
data: Name_,
}
#[allow(clippy::upper_case_acronyms)]
#[derive(Clone)]
enum Name_ {
Plain(iter_str::IterStr),
CJK(CJK),
Hangul(Hangul),
}

#[allow(clippy::upper_case_acronyms)]
#[derive(Copy)]
struct CJK {
emit_prefix: bool,
Expand All @@ -138,6 +142,7 @@ impl Clone for Hangul {
}
}

#[allow(clippy::len_without_is_empty)]
impl Name {
/// The number of bytes in the name.
///
Expand Down Expand Up @@ -170,7 +175,7 @@ impl Iterator for Name {
.map(|digit| *digit as usize)
.map(|d| {
state.idx += 1;
static DIGITS: &'static str = "0123456789ABCDEF";
static DIGITS: &str = "0123456789ABCDEF";
&DIGITS[d..d + 1]
})
}
Expand Down Expand Up @@ -241,7 +246,7 @@ pub fn name(c: char) -> Option<Name> {
(PHRASEBOOK_OFFSETS1[cc >> PHRASEBOOK_OFFSET_SHIFT] as usize) << PHRASEBOOK_OFFSET_SHIFT;

let mask = (1 << PHRASEBOOK_OFFSET_SHIFT) - 1;
let offset = PHRASEBOOK_OFFSETS2[offset + (cc & mask) as usize];
let offset = PHRASEBOOK_OFFSETS2[offset + (cc & mask)];
if offset == 0 {
if is_cjk_unified_ideograph(c) {
// write the hex number out right aligned in this array.
Expand All @@ -262,7 +267,7 @@ pub fn name(c: char) -> Option<Name> {
data: Name_::CJK(CJK {
emit_prefix: true,
idx: data_start,
data: data,
data,
}),
})
} else {
Expand Down Expand Up @@ -378,7 +383,7 @@ pub fn character(search_name: &str) -> Option<char> {
}

// get the parts of the hash...
let (g, f1, f2) = split(fnv_hash(search_name.iter().map(|x| *x)));
let (g, f1, f2) = split(fnv_hash(search_name.iter().copied()));
// ...and the appropriate displacements...
let (d1, d2) = phf::NAME2CODE_DISP[g as usize % phf::NAME2CODE_DISP.len()];

Expand Down
4 changes: 2 additions & 2 deletions unicode_names2_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl syn::parse::Parse for StringWithCharNames {
let mut errors = Vec::new();
let new = names_re.replace_all(&string.value(), |c: &regex::Captures| {
let full = c.at(0).unwrap();
if !full.ends_with("}") {
if !full.ends_with('}') {
errors.push(format!("unclosed escape in `named!`: {}", full));
} else {
let name = c.at(1).unwrap();
Expand All @@ -54,7 +54,7 @@ impl syn::parse::Parse for StringWithCharNames {
// failed :(
String::new()
});
if errors.len() > 0 {
if !errors.is_empty() {
// TODO: show all errors at once?
Err(syn::Error::new(string.span(), errors.get(0).unwrap()))
} else {
Expand Down

0 comments on commit 5cd226f

Please sign in to comment.